Skip to content

Commit e1b1b2f

Browse files
committed
suggest correct anonymous constant in where bounds in NonConstEvaluatable error message
1 parent 3c9701b commit e1b1b2f

File tree

1 file changed

+31
-4
lines changed
  • compiler/rustc_trait_selection/src/traits/error_reporting

1 file changed

+31
-4
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
804804
)
805805
}
806806
SelectionError::NotConstEvaluatable(NotConstEvaluatable::MentionsParam) => {
807+
debug!("NotConstEvaluatable::MentionsParam error");
808+
807809
if !self.tcx.features().generic_const_exprs {
808810
let mut err = self.tcx.sess.struct_span_err(
809811
span,
@@ -822,16 +824,41 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
822824

823825
match obligation.predicate.kind().skip_binder() {
824826
ty::PredicateKind::ConstEvaluatable(uv) => {
827+
debug!(?uv);
828+
825829
let mut err =
826830
self.tcx.sess.struct_span_err(span, "unconstrained generic constant");
827-
let const_span = self.tcx.def_span(uv.def.did);
828-
match self.tcx.sess.source_map().span_to_snippet(const_span) {
829-
Ok(snippet) => err.help(&format!(
831+
832+
let anon_const_sugg = match AbstractConst::new(self.tcx, uv) {
833+
Ok(Some(a)) => a.try_print_with_replacing_substs(self.tcx).map_or(
834+
{
835+
let const_span = self.tcx.def_span(uv.def.did);
836+
self.tcx
837+
.sess
838+
.source_map()
839+
.span_to_snippet(const_span)
840+
.map_or(None, Some)
841+
},
842+
|s| Some(format!("{{ {} }}", s)),
843+
),
844+
_ => {
845+
let const_span = self.tcx.def_span(uv.def.did);
846+
self.tcx
847+
.sess
848+
.source_map()
849+
.span_to_snippet(const_span)
850+
.map_or(None, Some)
851+
}
852+
};
853+
854+
match anon_const_sugg {
855+
Some(snippet) => err.help(&format!(
830856
"try adding a `where` bound using this expression: `where [(); {}]:`",
831857
snippet
832858
)),
833-
_ => err.help("consider adding a `where` bound using this expression"),
859+
None => err.help("consider adding a `where` bound using this expression"),
834860
};
861+
835862
err
836863
}
837864
_ => {

0 commit comments

Comments
 (0)