Skip to content

Better error message for late/early lifetime param mismatch #140523

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 2 commits into from
May 8, 2025

Conversation

compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented Apr 30, 2025

Rework the way we report early-/late-bound lifetime param mismatches to equate the trait and impl signatures using region variables, so that we can detect when a late-bound param is present in the signature in place of an early-bound param, or vice versa.

The diagnostic is a bit more technical, but it's more obviously clear to see what the problem is, even if it's not great at explaining how to fix it. I think this could be improved further, but I still think it's much better than what exists today.

Note to reviewer(s): I'd appreciate if we didn't bikeshed too much about this verbiage, b/c I hope it's clear that the old message sucked a lot. I'm happy to file bugs for interested new contributors to improve the messaging further.

@rustbot
Copy link
Collaborator

rustbot commented Apr 30, 2025

r? @jackh726

rustbot has assigned @jackh726.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@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 Apr 30, 2025
@@ -17,11 +17,11 @@ pub trait Foo<'a, 't> {

impl<'a, 't> Foo<'a, 't> for &'a isize {
fn no_bound<'b:'a>(self, b: Inv<'b>) {
//~^ ERROR lifetime parameters or bounds on method `no_bound` do not match
//~^ ERROR lifetime parameters do not match the trait definition, since they differ in late-boundedness
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, brief thought as I skim this, but how often do we actually use the terminology "late-bound"/"early-bound" in error mesasges? In general, "late-boundedness" is kind of weird.

Copy link
Member Author

Choose a reason for hiding this comment

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

no idea, but also we do need a precise term here. hiding this detail feels like we're obscuring the whole fact that this error arises.

Copy link
Member

@fmease fmease Apr 30, 2025

Choose a reason for hiding this comment

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

AFAIK (I've once grepped for this) in diagnostics we only ever mention boundedness when a user tries to bind a late-bound var early:

fn main() { fn f<'a>() {} f::<'static>; }
//~^ ERROR cannot specify lifetime arguments explicitly if late bound lifetime parameters are present [E0794]
Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

@jackh726 jackh726 May 1, 2025

Choose a reason for hiding this comment

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

I kind of think that maybe just exclude the "since they differ in late-boundedness" part in the main error text.

We could also just kind of "lie" a bit and state that the reason there is an error is because of the mismatched bounds (which is true, but not precise).

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll move it down to a note, but I'd rather keep the early and late in the labels so that people can at least google it. Not really sure how to reword the main message, though, in that case.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, having "late-bound" and "early-bound" in the note is fine because of googleability, but something about the "lateboundedness" term rubs me the wrong way - this is mostly just bikeshedding, because I don't immediately have a better answer.

Copy link
Member Author

Choose a reason for hiding this comment

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

Reworded this to just say "lifetime parameters do not match the trait definition", and added a note saying "lifetime parameters differ in whether they are early- or late-bound".

@jackh726
Copy link
Member

jackh726 commented May 1, 2025

Thanks for putting up with my bikeshedding - I think this is clearly an improvement.

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented May 1, 2025

📌 Commit 7ccc65b has been approved by jackh726

It is now in the queue for this repository.

@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 May 1, 2025
@compiler-errors
Copy link
Member Author

Wait I didn't remove all instances of late-boundedness

@bors r-

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels May 1, 2025
@rust-log-analyzer

This comment has been minimized.

@compiler-errors
Copy link
Member Author

@bors r=jackh726

@bors
Copy link
Collaborator

bors commented May 7, 2025

📌 Commit f03d246 has been approved by jackh726

It is now in the queue for this repository.

@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 May 7, 2025
Zalathar added a commit to Zalathar/rust that referenced this pull request May 8, 2025
…h, r=jackh726

Better error message for late/early lifetime param mismatch

Rework the way we report early-/late-bound lifetime param mismatches to equate the trait and impl signatures using region variables, so that we can detect when a late-bound param is present in the signature in place of an early-bound param, or vice versa.

The diagnostic is a bit more technical, but it's more obviously clear to see what the problem is, even if it's not great at explaining how to fix it. I think this could be improved further, but I still think it's much better than what exists today.

Note to reviewer(s): I'd appreciate if we didn't bikeshed *too* much about this verbiage, b/c I hope it's clear that the old message sucked a lot. I'm happy to file bugs for interested new contributors to improve the messaging further.

Edit(fmease): Fixes rust-lang#33624.
bors added a commit to rust-lang-ci/rust that referenced this pull request May 8, 2025
Rollup of 15 pull requests

Successful merges:

 - rust-lang#138736 (Sanitizers target modificators)
 - rust-lang#140260 (Only prefer param-env candidates if they remain non-global after norm)
 - rust-lang#140523 (Better error message for late/early lifetime param mismatch)
 - rust-lang#140579 (Remove estebank from automated review assignment)
 - rust-lang#140641 (detect additional uses of opaques after writeback)
 - rust-lang#140711 (Do not discard constraints on overflow if there was candidate ambiguity)
 - rust-lang#140716 (Improve `-Zremap-path-scope` tests with dependency)
 - rust-lang#140755 ([win][arm64] Disable various DebugInfo tests that don't work on Arm64 Windows)
 - rust-lang#140756 ([arm64] Pointer auth test should link with C static library statically)
 - rust-lang#140758 ([win][arm64] Disable MSVC Linker 'Arm Hazard' warning)
 - rust-lang#140759 ([win][arm64] Disable std::fs tests that require symlinks)
 - rust-lang#140762 (rustdoc-json: Remove newlines from attributes)
 - rust-lang#140764 (style: Never break within a nullary function call `func()` or a unit literal `()`)
 - rust-lang#140769 (Add `DefPathData::OpaqueLifetime` to avoid conflicts for remapped opaque lifetimes)
 - rust-lang#140773 (triagebot: Better message for changes to `tests/rustdoc-json`)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request May 8, 2025
…iaskrgr

Rollup of 9 pull requests

Successful merges:

 - rust-lang#140260 (Only prefer param-env candidates if they remain non-global after norm)
 - rust-lang#140523 (Better error message for late/early lifetime param mismatch)
 - rust-lang#140579 (Remove estebank from automated review assignment)
 - rust-lang#140641 (detect additional uses of opaques after writeback)
 - rust-lang#140711 (Do not discard constraints on overflow if there was candidate ambiguity)
 - rust-lang#140762 (rustdoc-json: Remove newlines from attributes)
 - rust-lang#140764 (style: Never break within a nullary function call `func()` or a unit literal `()`)
 - rust-lang#140769 (Add `DefPathData::OpaqueLifetime` to avoid conflicts for remapped opaque lifetimes)
 - rust-lang#140773 (triagebot: Better message for changes to `tests/rustdoc-json`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit a810f8a into rust-lang:master May 8, 2025
6 checks passed
@rustbot rustbot added this to the 1.88.0 milestone May 8, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request May 8, 2025
Rollup merge of rust-lang#140523 - compiler-errors:late-early-mismatch, r=jackh726

Better error message for late/early lifetime param mismatch

Rework the way we report early-/late-bound lifetime param mismatches to equate the trait and impl signatures using region variables, so that we can detect when a late-bound param is present in the signature in place of an early-bound param, or vice versa.

The diagnostic is a bit more technical, but it's more obviously clear to see what the problem is, even if it's not great at explaining how to fix it. I think this could be improved further, but I still think it's much better than what exists today.

Note to reviewer(s): I'd appreciate if we didn't bikeshed *too* much about this verbiage, b/c I hope it's clear that the old message sucked a lot. I'm happy to file bugs for interested new contributors to improve the messaging further.

Edit(fmease): Fixes rust-lang#33624.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
6 participants