@@ -172,8 +172,19 @@ fn closure_args(fn_sig: &ty::PolyFnSig<'_>) -> String {
172
172
}
173
173
174
174
pub enum TypeAnnotationNeeded {
175
+ /// ```compile_fail,E0282
176
+ /// let x = "hello".chars().rev().collect();
177
+ /// ```
175
178
E0282 ,
179
+ /// An implementation cannot be chosen unambiguously because of lack of information.
180
+ /// ```compile_fail,E0283
181
+ /// let _ = Default::default();
182
+ /// ```
176
183
E0283 ,
184
+ /// ```compile_fail,E0284
185
+ /// let mut d: u64 = 2;
186
+ /// d = d % 1u32.into();
187
+ /// ```
177
188
E0284 ,
178
189
}
179
190
@@ -261,7 +272,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
261
272
printer. name_resolver = Some ( Box :: new ( & getter) ) ;
262
273
let _ = if let ty:: FnDef ( ..) = ty. kind {
263
274
// 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.
265
276
ty. fn_sig ( self . tcx ) . print ( printer)
266
277
} else {
267
278
ty. print ( printer)
@@ -518,6 +529,36 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
518
529
err
519
530
}
520
531
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
+
521
562
/// If the `FnSig` for the method call can be found and type arguments are identified as
522
563
/// needed, suggest annotating the call, otherwise point out the resulting type of the call.
523
564
fn annotate_method_call (
0 commit comments