Skip to content
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

Add display of lifetime of Ref in fn fmt() for TypeError #107475

Closed
wants to merge 1 commit into from

Conversation

yanchen4791
Copy link
Contributor

Fix for #106517.

Problem:
When explaining the source of a type error, the lifetime portion of references is sometimes omitted in order to shorten the message. This can lead to confusion as the message can disagree with other spans which use the long form with the lifetime portion. For example, for the same type error, one span may print expected '&str', found enum 'option' while another span may print expected '&'static str' because of return type and note: expected reference '&'static str' found enum 'option<&str>'.

Solution:
To add the lifetime portion of the references when generating the message. This will ensure consistency between the messages and avoid confusion, especially when the message is already short and the omitted information does not have to be removed.

@rustbot
Copy link
Collaborator

rustbot commented Jan 30, 2023

r? @WaffleLapkin

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 30, 2023
@yanchen4791
Copy link
Contributor Author

r? nagisa

@rustbot rustbot assigned nagisa and unassigned WaffleLapkin Jan 30, 2023
@nagisa
Copy link
Member

nagisa commented Jan 30, 2023

Sorry for the lack of earlier feedback on the issue. The bug that I was reporting here was specifically the mismatch between:

help: consider introducing a named lifetime parameter
1 | fn parse_type<'a>(iter: Box<dyn Iterator<Item=&'a str>+'static>) -> &'a str { iter.next() }

and the

expected `&'static str` because of return type

Rather than between these:

  |                                                              ----   ^^^^^^^^^^^ expected `&str`, found enum `Option`
  |                                                              |
  |                                                              expected `&'static str` because of return type

As thus, I don’t believe this PR to be a fix for the referenced issue. I’m not particularly familiar with the code, since I haven’t had an opportunity to look at this, but I’m surprised that ReStatic is the lifetime the compiler comes up with to pass onto later passes. It might make sense to introduce (if there isn’t already) a ReError or something to indicate that a region-related error has already been output, and to signal to downstream diagnostics to not print any lifetime for that type, or to potentially not attempt any further diagnostics at all, regardless of the context. Alternatively, I think having these be TyError might also make sense

I also don’t think this PR is the right thing to do in general. Adding lifetimes to all notes that are likely pointing out larger structural differences (like reference vs enum) won’t make the diagnostic much clearer – in fact it will likely contribute to clutter. Adding lifetimes probably only makes sense if the lifetime mismatch is between two reference types of otherwise equivalent structure.

cc @estebank

@estebank
Copy link
Contributor

estebank commented Jan 30, 2023

I’m surprised that ReStatic is the lifetime the compiler comes up with to pass onto later passes. It might make sense to introduce (if there isn’t already) a ReError or something to indicate that a region-related error has already been output

There isn't, and ReStatic is chosen because it is the closest "safe" lifetime to use, but I'm in favor of introducing ReError.

Edit: another case where ReError would have helped #69314

Edit 2: #107652

@bors
Copy link
Contributor

bors commented Jan 31, 2023

☔ The latest upstream changes (presumably #106399) made this pull request unmergeable. Please resolve the merge conflicts.

@nagisa
Copy link
Member

nagisa commented Feb 26, 2023

Given that there's a broader agreement that this is not an approach that will make the situation better, and given the introduction of ReError, I’ll close this.

Thanks for the contribution @yanchen4791, and sorry for not making myself clearer in the issue description!

@nagisa nagisa closed this Feb 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants