Skip to content

Commit 8458eba

Browse files
committed
Point at method call on missing annotation error
Make it clearer where the type name that couldn't be infered comes from.
1 parent 7b0085a commit 8458eba

File tree

5 files changed

+51
-12
lines changed

5 files changed

+51
-12
lines changed

src/librustc/infer/error_reporting/need_type_info.rs

+32-7
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
150150
&self,
151151
ty: Ty<'tcx>,
152152
highlight: Option<ty::print::RegionHighlightMode>,
153-
) -> String {
153+
) -> (String, Option<Span>) {
154154
if let ty::Infer(ty::TyVar(ty_vid)) = ty.sty {
155155
let ty_vars = self.type_variables.borrow();
156-
if let TypeVariableOriginKind::TypeParameterDefinition(name) =
157-
ty_vars.var_origin(ty_vid).kind {
158-
return name.to_string();
156+
let var_origin = ty_vars.var_origin(ty_vid);
157+
if let TypeVariableOriginKind::TypeParameterDefinition(name) = var_origin.kind {
158+
return (name.to_string(), Some(var_origin.span));
159159
}
160160
}
161161

@@ -165,7 +165,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
165165
printer.region_highlight_mode = highlight;
166166
}
167167
let _ = ty.print(printer);
168-
s
168+
(s, None)
169169
}
170170

171171
pub fn need_type_info_err(
@@ -175,7 +175,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
175175
ty: Ty<'tcx>,
176176
) -> DiagnosticBuilder<'tcx> {
177177
let ty = self.resolve_vars_if_possible(&ty);
178-
let name = self.extract_type_name(&ty, None);
178+
let (name, name_sp) = self.extract_type_name(&ty, None);
179179

180180
let mut local_visitor = FindLocalByTypeVisitor::new(&self, ty, &self.tcx.hir());
181181
let ty_to_string = |ty: Ty<'tcx>| -> String {
@@ -200,6 +200,14 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
200200
}
201201
let err_span = if let Some(pattern) = local_visitor.found_arg_pattern {
202202
pattern.span
203+
} else if let Some(span) = name_sp {
204+
// `span` here lets us point at `sum` instead of the entire right hand side expr:
205+
// error[E0282]: type annotations needed
206+
// --> file2.rs:3:15
207+
// |
208+
// 3 | let _ = x.sum() as f64;
209+
// | ^^^ cannot infer type for `S`
210+
span
203211
} else {
204212
span
205213
};
@@ -325,6 +333,23 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
325333
};
326334
err.span_label(pattern.span, msg);
327335
}
336+
// Instead of the following:
337+
// error[E0282]: type annotations needed
338+
// --> file2.rs:3:15
339+
// |
340+
// 3 | let _ = x.sum() as f64;
341+
// | --^^^--------- cannot infer type for `S`
342+
// |
343+
// = note: type must be known at this point
344+
// We want:
345+
// error[E0282]: type annotations needed
346+
// --> file2.rs:3:15
347+
// |
348+
// 3 | let _ = x.sum() as f64;
349+
// | ^^^ cannot infer type for `S`
350+
// |
351+
// = note: type must be known at this point
352+
let span = name_sp.unwrap_or(span);
328353
if !err.span.span_labels().iter().any(|span_label| {
329354
span_label.label.is_some() && span_label.span == span
330355
}) && local_visitor.found_arg_pattern.is_none()
@@ -342,7 +367,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
342367
ty: Ty<'tcx>,
343368
) -> DiagnosticBuilder<'tcx> {
344369
let ty = self.resolve_vars_if_possible(&ty);
345-
let name = self.extract_type_name(&ty, None);
370+
let name = self.extract_type_name(&ty, None).0;
346371
let mut err = struct_span_err!(
347372
self.tcx.sess, span, E0698, "type inside {} must be known in this context", kind,
348373
);

src/librustc_mir/borrow_check/nll/region_infer/error_reporting/region_name.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
413413
) -> Option<RegionName> {
414414
let mut highlight = RegionHighlightMode::default();
415415
highlight.highlighting_region_vid(needle_fr, *counter);
416-
let type_name = infcx.extract_type_name(&argument_ty, Some(highlight));
416+
let type_name = infcx.extract_type_name(&argument_ty, Some(highlight)).0;
417417

418418
debug!(
419419
"give_name_if_we_cannot_match_hir_ty: type_name={:?} needle_fr={:?}",
@@ -695,7 +695,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
695695

696696
let mut highlight = RegionHighlightMode::default();
697697
highlight.highlighting_region_vid(fr, *counter);
698-
let type_name = infcx.extract_type_name(&return_ty, Some(highlight));
698+
let type_name = infcx.extract_type_name(&return_ty, Some(highlight)).0;
699699

700700
let mir_hir_id = tcx.hir().as_local_hir_id(mir_def_id).expect("non-local mir");
701701

@@ -758,7 +758,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
758758

759759
let mut highlight = RegionHighlightMode::default();
760760
highlight.highlighting_region_vid(fr, *counter);
761-
let type_name = infcx.extract_type_name(&yield_ty, Some(highlight));
761+
let type_name = infcx.extract_type_name(&yield_ty, Some(highlight)).0;
762762

763763
let mir_hir_id = tcx.hir().as_local_hir_id(mir_def_id).expect("non-local mir");
764764

src/test/ui/span/issue-42234-unknown-receiver-type.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error[E0282]: type annotations needed for `std::option::Option<_>`
2-
--> $DIR/issue-42234-unknown-receiver-type.rs:7:5
2+
--> $DIR/issue-42234-unknown-receiver-type.rs:7:7
33
|
44
LL | let x: Option<_> = None;
55
| - consider giving `x` the explicit type `std::option::Option<_>`, where the type parameter `T` is specified
66
LL | x.unwrap().method_that_could_exist_on_some_type();
7-
| ^^^^^^^^^^ cannot infer type for `T`
7+
| ^^^^^^ cannot infer type for `T`
88
|
99
= note: type must be known at this point
1010

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
let _ = (vec![1,2,3]).into_iter().sum() as f64; //~ ERROR E0282
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/type-annotations-needed-expr.rs:2:39
3+
|
4+
LL | let _ = (vec![1,2,3]).into_iter().sum() as f64;
5+
| ^^^ cannot infer type for `S`
6+
|
7+
= note: type must be known at this point
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)