Skip to content

Use more precise span for E0282 in cast expressions #85971

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2021

Conversation

FabianWolff
Copy link
Contributor

This pull request fixes #85586. The example code given there:

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

@rust-highfive
Copy link
Contributor

r? @matthewjasper

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 3, 2021
@rust-log-analyzer

This comment has been minimized.

@@ -995,7 +995,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} else {
// Defer other checks until we're done type checking.
let mut deferred_cast_checks = self.deferred_cast_checks.borrow_mut();
match cast::CastCheck::new(self, e, t_expr, t_cast, t.span, expr.span) {
match cast::CastCheck::new(self, e, t_expr, e.span, t_cast, t.span, expr.span) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to remove the new Span parameter, and just use self.expr.span inside of CastCheck

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right; it seems that I got confused with e.span and expr.span. Thanks for pointing this out, I have fixed it now.

@crlf0710 crlf0710 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 26, 2021
@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 12, 2021
@camelid camelid added A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. labels Jul 29, 2021
@camelid
Copy link
Member

camelid commented Jul 29, 2021

r? @davidtwco

@davidtwco
Copy link
Member

@bors r+

@bors
Copy link
Collaborator

bors commented Jul 30, 2021

📌 Commit 74d1bd2 has been approved by davidtwco

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 30, 2021
@bors
Copy link
Collaborator

bors commented Jul 30, 2021

⌛ Testing commit 74d1bd2 with merge f3f8e75...

@bors
Copy link
Collaborator

bors commented Jul 30, 2021

☀️ Test successful - checks-actions
Approved by: davidtwco
Pushing f3f8e75 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jul 30, 2021
@bors bors merged commit f3f8e75 into rust-lang:master Jul 30, 2021
@rustbot rustbot added this to the 1.56.0 milestone Jul 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-confusing Diagnostics: Confusing error or lint that should be reworked. merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Confusing type inference error span