Skip to content

Commit 74d1bd2

Browse files
committed
Use more precise span for E0282 in cast expressions
1 parent c5fbcd3 commit 74d1bd2

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

compiler/rustc_typeck/src/check/cast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,8 @@ impl<'a, 'tcx> CastCheck<'tcx> {
609609
}
610610

611611
pub fn check(mut self, fcx: &FnCtxt<'a, 'tcx>) {
612-
self.expr_ty = fcx.structurally_resolved_type(self.span, self.expr_ty);
613-
self.cast_ty = fcx.structurally_resolved_type(self.span, self.cast_ty);
612+
self.expr_ty = fcx.structurally_resolved_type(self.expr.span, self.expr_ty);
613+
self.cast_ty = fcx.structurally_resolved_type(self.cast_span, self.cast_ty);
614614

615615
debug!("check_cast({}, {:?} as {:?})", self.expr.hir_id, self.expr_ty, self.cast_ty);
616616

src/test/ui/cast/issue-85586.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Check that errors for unresolved types in cast expressions are reported
2+
// for the offending subexpression, not the whole cast expression.
3+
4+
#![allow(unused_variables)]
5+
6+
fn main() {
7+
let a = [1, 2, 3].iter().sum();
8+
let b = (a + 1) as usize;
9+
//~^ ERROR: type annotations needed [E0282]
10+
}

src/test/ui/cast/issue-85586.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/issue-85586.rs:8:13
3+
|
4+
LL | let b = (a + 1) as usize;
5+
| ^^^^^^^ cannot infer type
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)