diff options
| author | bors <bors@rust-lang.org> | 2021-07-30 14:38:00 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2021-07-30 14:38:00 +0000 |
| commit | f3f8e758f2b2abd84b76bcb4ec0b6ae263e1e7b9 (patch) | |
| tree | 4c37a298b6af37a71803febc8bc5f00dd848c4d9 | |
| parent | 87dc8242484110c75596a91ebd2043a476c09839 (diff) | |
| parent | 74d1bd21c78765712612afdd5eab3efa00757064 (diff) | |
| download | rust-f3f8e758f2b2abd84b76bcb4ec0b6ae263e1e7b9.tar.gz rust-f3f8e758f2b2abd84b76bcb4ec0b6ae263e1e7b9.zip | |
Auto merge of #85971 - FabianWolff:issue-85586, r=davidtwco
Use more precise span for E0282 in cast expressions
This pull request fixes #85586. The example code given there:
```rust
fn main() {
let a = [1, 2, 3].iter().sum();
let b = (a + 1) as usize;
}
```
currently produces
```
error[E0282]: type annotations needed
--> issue-85586.rs:3:13
|
3 | let b = (a + 1) as usize;
| ^^^^^^^^^^^^^^^^ cannot infer type
|
= note: type must be known at this point
error: aborting due to previous error
```
even though the type of the entire cast expression quite clearly should be `usize`. The error is in the cast's left-hand side, which is made explicit by the changes in this PR:
```
error[E0282]: type annotations needed
--> issue-85586.rs:3:13
|
3 | let b = (a + 1) as usize;
| ^^^^^^^ cannot infer type
|
= note: type must be known at this point
error: aborting due to previous error
```
| -rw-r--r-- | compiler/rustc_typeck/src/check/cast.rs | 4 | ||||
| -rw-r--r-- | src/test/ui/cast/issue-85586.rs | 10 | ||||
| -rw-r--r-- | src/test/ui/cast/issue-85586.stderr | 11 |
3 files changed, 23 insertions, 2 deletions
diff --git a/compiler/rustc_typeck/src/check/cast.rs b/compiler/rustc_typeck/src/check/cast.rs index b3808eae1ad..181972e3e7b 100644 --- a/compiler/rustc_typeck/src/check/cast.rs +++ b/compiler/rustc_typeck/src/check/cast.rs @@ -604,8 +604,8 @@ impl<'a, 'tcx> CastCheck<'tcx> { } pub fn check(mut self, fcx: &FnCtxt<'a, 'tcx>) { - self.expr_ty = fcx.structurally_resolved_type(self.span, self.expr_ty); - self.cast_ty = fcx.structurally_resolved_type(self.span, self.cast_ty); + self.expr_ty = fcx.structurally_resolved_type(self.expr.span, self.expr_ty); + self.cast_ty = fcx.structurally_resolved_type(self.cast_span, self.cast_ty); debug!("check_cast({}, {:?} as {:?})", self.expr.hir_id, self.expr_ty, self.cast_ty); diff --git a/src/test/ui/cast/issue-85586.rs b/src/test/ui/cast/issue-85586.rs new file mode 100644 index 00000000000..78816582b57 --- /dev/null +++ b/src/test/ui/cast/issue-85586.rs @@ -0,0 +1,10 @@ +// Check that errors for unresolved types in cast expressions are reported +// for the offending subexpression, not the whole cast expression. + +#![allow(unused_variables)] + +fn main() { + let a = [1, 2, 3].iter().sum(); + let b = (a + 1) as usize; + //~^ ERROR: type annotations needed [E0282] +} diff --git a/src/test/ui/cast/issue-85586.stderr b/src/test/ui/cast/issue-85586.stderr new file mode 100644 index 00000000000..271885a133a --- /dev/null +++ b/src/test/ui/cast/issue-85586.stderr @@ -0,0 +1,11 @@ +error[E0282]: type annotations needed + --> $DIR/issue-85586.rs:8:13 + | +LL | let b = (a + 1) as usize; + | ^^^^^^^ cannot infer type + | + = note: type must be known at this point + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0282`. |
