@@ -172,8 +172,19 @@ fn closure_args(fn_sig: &ty::PolyFnSig<'_>) -> String {
172172}
173173
174174pub enum TypeAnnotationNeeded {
175+ /// ```compile_fail,E0282
176+ /// let x = "hello".chars().rev().collect();
177+ /// ```
175178 E0282 ,
179+ /// An implementation cannot be chosen unambiguously because of lack of information.
180+ /// ```compile_fail,E0283
181+ /// let _ = Default::default();
182+ /// ```
176183 E0283 ,
184+ /// ```compile_fail,E0284
185+ /// let mut d: u64 = 2;
186+ /// d = d % 1u32.into();
187+ /// ```
177188 E0284 ,
178189}
179190
@@ -261,7 +272,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
261272 printer. name_resolver = Some ( Box :: new ( & getter) ) ;
262273 let _ = if let ty:: FnDef ( ..) = ty. kind {
263274 // We don't want the regular output for `fn`s because it includes its path in
264- // invalid pseduo -syntax, we want the `fn`-pointer output instead.
275+ // invalid pseudo -syntax, we want the `fn`-pointer output instead.
265276 ty. fn_sig ( self . tcx ) . print ( printer)
266277 } else {
267278 ty. print ( printer)
@@ -518,6 +529,36 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
518529 err
519530 }
520531
532+ // FIXME(const_generics): We should either try and merge this with `need_type_info_err`
533+ // or improve the errors created here.
534+ //
535+ // Unlike for type inference variables, we don't yet store the origin of const inference variables.
536+ // This is needed for to get a more relevant error span.
537+ pub fn need_type_info_err_const (
538+ & self ,
539+ body_id : Option < hir:: BodyId > ,
540+ span : Span ,
541+ ct : & ' tcx ty:: Const < ' tcx > ,
542+ error_code : TypeAnnotationNeeded ,
543+ ) -> DiagnosticBuilder < ' tcx > {
544+ let mut local_visitor = FindHirNodeVisitor :: new ( & self , ct. into ( ) , span) ;
545+ if let Some ( body_id) = body_id {
546+ let expr = self . tcx . hir ( ) . expect_expr ( body_id. hir_id ) ;
547+ local_visitor. visit_expr ( expr) ;
548+ }
549+
550+ let error_code = error_code. into ( ) ;
551+ let mut err = self . tcx . sess . struct_span_err_with_code (
552+ local_visitor. target_span ,
553+ & format ! ( "type annotations needed" ) ,
554+ error_code,
555+ ) ;
556+
557+ err. note ( "unable to infer the value of a const parameter" ) ;
558+
559+ err
560+ }
561+
521562 /// If the `FnSig` for the method call can be found and type arguments are identified as
522563 /// needed, suggest annotating the call, otherwise point out the resulting type of the call.
523564 fn annotate_method_call (
0 commit comments