Skip to content

Enhance type inference errors involving the ? operator #80517

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

Conversation

wabain
Copy link
Contributor

@wabain wabain commented Dec 30, 2020

This patch adds a special-cased note on type inference errors when the error span points to a ? return. It also makes the primary label for such errors "cannot infer type of ? error" in cases where before we would have only said "cannot infer type".

One beneficiary of this change is async blocks, where we can't explicitly annotate the return type and so may not generate any other help (#77880); this lets us at least print the error type we're converting from and anything we know about the type we can't fully infer. More generally, it signposts that an implicit conversion is happening that may have impeded type inference the user was expecting. We already do something similar for mismatched type errors.

The check for a relevant ? operator is built into the existing HIR traversal which looks for places that could be annotated to resolve the error. That means we could identify ? uses anywhere in the function that output the type we can't infer, but this patch just sticks to adding the note if the primary span given for the error has the operator; if there are other expressions where the type occurs and one of them is selected for the error instead, it's more likely that the ? operator's implicit conversion isn't the sole cause of the inference failure and that adding an additional diagnostic would just be noise. I added a ui test for one such case.

The data about the ? conversion is passed around in a UseDiagnostic enum that in theory could be used to add more of this kind of note in the future. It was also just easier to pass around than something with a more specific name. There are some follow-up refactoring commits for the code that generates the error label, which was already pretty involved and made a bit more complicated by this change.

@rust-highfive
Copy link
Contributor

r? @davidtwco

(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 Dec 30, 2020
@@ -448,6 +544,13 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
error_code,
);

let use_diag = local_visitor.found_use_diagnostic.as_ref();
if let Some(use_diag) = use_diag {
if use_diag.applies_to(err_span) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are various cases where we shrink the span used in the error message after the HIR traversal, so this check makes sure that the note still makes sense with the chosen span.

Copy link
Member

@davidtwco davidtwco left a comment

Choose a reason for hiding this comment

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

LGTM, left a few minor suggestions.

@davidtwco davidtwco added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 31, 2020
@bors

This comment has been minimized.

@wabain wabain force-pushed the issue-77880-infer-error-try-conversion-msg branch from e92e29a to d46c3e3 Compare January 11, 2021 01:48
@wabain
Copy link
Contributor Author

wabain commented Jan 11, 2021

Merge conflicts and suggested changes are resolved

@davidtwco
Copy link
Member

@bors r+

Thanks!

@bors
Copy link
Collaborator

bors commented Jan 11, 2021

📌 Commit d46c3e3 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 11, 2021
@bors
Copy link
Collaborator

bors commented Jan 12, 2021

⌛ Testing commit d46c3e3 with merge 497c9a2...

@bors
Copy link
Collaborator

bors commented Jan 12, 2021

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

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jan 12, 2021
@bors bors merged commit 497c9a2 into rust-lang:master Jan 12, 2021
@rustbot rustbot added this to the 1.51.0 milestone Jan 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

5 participants