The Wayback Machine - https://web.archive.org/web/20210720234733/https://github.com/rust-lang/rust/pull/87244
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

Better diagnostics with mismatched types due to implicit static lifetime #87244

Merged
merged 2 commits into from Jul 20, 2021

Conversation

@jackh726
Copy link
Contributor

@jackh726 jackh726 commented Jul 18, 2021

Fixes #78113

I think this is my first diagnostics PR...definitely happy to hear thoughts on the direction/implementation here.

I was originally just trying to solve the error above, where the lifetime on a GAT was causing a cryptic "mismatched types" error. But as I was writing this, I realized that this (unintentionally) also applied to a different case: wf-in-foreign-fn-decls-issue-80468.rs. I'm not sure if this diagnostic should get a new error code, or even reuse an existing one. And, there might be some ways to make this even more generalized. Also, the error is a bit more lengthy and verbose than probably needed. So thoughts there are welcome too.

This PR essentially ended up adding a new nice region error pass that triggers if a type doesn't match the self type of an impl which is selected because of a predicate because of an implicit static bound on that self type.

r? @estebank

@rust-log-analyzer

This comment has been hidden.

@jackh726 jackh726 force-pushed the jackh726:issue-71883 branch 2 times, most recently from 0542c18 to 69531a4 Jul 18, 2021
@rust-log-analyzer

This comment has been hidden.

@jackh726 jackh726 force-pushed the jackh726:issue-71883 branch from 69531a4 to aa0c0c9 Jul 18, 2021
Copy link
Contributor

@estebank estebank left a comment

The new output is great! I have some nitpicks that I'd like to address before we land this.
Particularly I'd like to tweak the resulting output to be as short and clear as possible to people that might not know what a "requirement" or a "bound" is, or why a 'static lifetime requirement is involved.

I'm not sure if this diagnostic should get a new error code, or even reuse an existing one.

If we can add a new error code that'd be great, but it is perfectly fine to land things like this without an error code, at least at the beginning, as long as the error itself contains as much information as an error code index entry would.

LL | pub struct Wrapper<T: Trait>(T);
| ^^^^^
Comment on lines 16 to 17

This comment has been minimized.

@estebank

estebank Jul 19, 2021
Contributor

In this case the 'static lifetime obligation is introduced by the type parameter (T: Trait is implicitly 'sized). It'd be nice to proactively handle this, detect this case and mention that.

This comment has been minimized.

@jackh726

jackh726 Jul 19, 2021
Author Contributor

Actually, this is because the lifetime on Ref in impl Trait for Ref {} falls back to 'static in resolution.

The lifetime for Ref in pub fn repro(_: Wrapper<Ref>) on the other hand defaults to an anonymous lifetimes.

A diagnostic here still might be useful to say something like "this lifetime is being assumed as 'static'"

This comment has been minimized.

@jackh726

jackh726 Jul 19, 2021
Author Contributor

I think an extra diagnostic here might make sense for another PR?

@jackh726 jackh726 force-pushed the jackh726:issue-71883 branch from aa0c0c9 to 7be9708 Jul 19, 2021
@jackh726 jackh726 force-pushed the jackh726:issue-71883 branch from 7be9708 to 3cd5ad5 Jul 19, 2021
Copy link
Contributor

@estebank estebank left a comment

I think an extra diagnostic here might make sense for another PR?

I would prefer if it was in the same PR, but I'm ready to merge this after addressing my nitpicks if you don't have the time to try and handle that case.

@jackh726 jackh726 force-pushed the jackh726:issue-71883 branch from e687b4d to ae02491 Jul 20, 2021
@jackh726
Copy link
Contributor Author

@jackh726 jackh726 commented Jul 20, 2021

Ok @estebank I modified the output just a bit, in order to make it a bit better in cases where there is an explicit static, multiple implicit statics, or when the impl isn't local.

I also added a small note to the assumed static in cases of missing lifetimes. This isn't in the implicit static error message, but should maybe be enough for someone to figure out what's going on. I'd rather land this and maybe work more on that another time.

@@ -336,6 +336,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
insertion_sp,
suggestion,
);
err.note("assuming a `'static` lifetime...");

This comment has been minimized.

@estebank

estebank Jul 20, 2021
Contributor

Not a fan of this new note, but I see your point.

@estebank
Copy link
Contributor

@estebank estebank commented Jul 20, 2021

@bors r+

@bors
Copy link
Contributor

@bors bors commented Jul 20, 2021

📌 Commit ae02491 has been approved by estebank

@bors
Copy link
Contributor

@bors bors commented Jul 20, 2021

Testing commit ae02491 with merge da7d405...

@bors
Copy link
Contributor

@bors bors commented Jul 20, 2021

☀️ Test successful - checks-actions
Approved by: estebank
Pushing da7d405 to master...

@bors bors merged commit da7d405 into rust-lang:master Jul 20, 2021
11 checks passed
11 checks passed
@github-actions
PR (mingw-check, ubuntu-latest-xl)
Details
@github-actions
PR (x86_64-gnu-llvm-10, ubuntu-latest-xl)
Details
@github-actions
PR (x86_64-gnu-tools, 1, ubuntu-latest-xl)
Details
@github-actions
auto
Details
@github-actions
master
Details
@github-actions
bors build finished
Details
@github-actions
bors build finished
Details
@github-actions
bors build finished
Details
@github-actions
bors build finished
Details
@bors
homu Test successful
Details
@rustbot rustbot added this to the 1.55.0 milestone Jul 20, 2021
@jackh726 jackh726 deleted the jackh726:issue-71883 branch Jul 20, 2021
@pnkfelix
Copy link
Member

@pnkfelix pnkfelix commented Jul 20, 2021

This was flagged as causing a moderate regression with respect to instruction counts in the compiler: perf.rlo link

Most of the regressions are under the 1% threshold, but there's a lot of them that exceed the 0.2% threshold we use for non-noisy benchmarks.

Was this PR, which I understand adds an extra region error pass, expected to cause that much additional overhead? Skimming, I see a place where a Vec was replaced with a HashMap, maybe it is from that change?

@jackh726
Copy link
Contributor Author

@jackh726 jackh726 commented Jul 20, 2021

@pnkfelix nope that's unexpected; the pass should should only run after a region error has been generated. The only thing that could have possibly had an effect, I think, is https://github.com/rust-lang/rust/pull/87244/files#diff-34d5893dd95fe09f9a0fd3341efacd1c21853bb34ba29d9d79bb9af26bb8a0a0R1906-R1911. Which is...not much of a change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

7 participants