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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

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.

Edit(fmease): Fixes #33624.

@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
Copy link
Collaborator

The job x86_64-gnu-llvm-19 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
#18 exporting to docker image format
#18 sending tarball 19.8s done
#18 DONE 26.2s
##[endgroup]
Setting extra environment values for docker:  --env ENABLE_GCC_CODEGEN=1 --env GCC_EXEC_PREFIX=/usr/lib/gcc/
[CI_JOB_NAME=x86_64-gnu-llvm-19]
[CI_JOB_NAME=x86_64-gnu-llvm-19]
debug: `DISABLE_CI_RUSTC_IF_INCOMPATIBLE` configured.
---
sccache: Listening on address 127.0.0.1:4226
##[group]Configure the build
configure: processing command line
configure: 
configure: build.configure-args := ['--build=x86_64-unknown-linux-gnu', '--llvm-root=/usr/lib/llvm-19', '--enable-llvm-link-shared', '--set', 'rust.randomize-layout=true', '--set', 'rust.thin-lto-import-instr-limit=10', '--set', 'build.print-step-timings', '--enable-verbose-tests', '--set', 'build.metrics', '--enable-verbose-configure', '--enable-sccache', '--disable-manage-submodules', '--enable-locked-deps', '--enable-cargo-native-static', '--set', 'rust.codegen-units-std=1', '--set', 'dist.compression-profile=balanced', '--dist-compression-formats=xz', '--set', 'rust.lld=false', '--disable-dist-src', '--release-channel=nightly', '--enable-debug-assertions', '--enable-overflow-checks', '--enable-llvm-assertions', '--set', 'rust.verify-llvm-ir', '--set', 'rust.codegen-backends=llvm,cranelift,gcc', '--set', 'llvm.static-libstdcpp', '--set', 'gcc.download-ci-gcc=true', '--enable-new-symbol-mangling']
configure: build.build          := x86_64-unknown-linux-gnu
configure: target.x86_64-unknown-linux-gnu.llvm-config := /usr/lib/llvm-19/bin/llvm-config
configure: llvm.link-shared     := True
configure: rust.randomize-layout := True
configure: rust.thin-lto-import-instr-limit := 10
---
failures:

---- [ui] tests/ui/error-codes/E0195.rs stdout ----

error: /checkout/tests/ui/error-codes/E0195.rs:16: unexpected NOTE: '16:12: 16:14: `'a` differs between the trait and impl'

error: /checkout/tests/ui/error-codes/E0195.rs:16: unexpected NOTE: '16:15: 16:17: `'b` differs between the trait and impl'

error: /checkout/tests/ui/error-codes/E0195.rs:16: expected NOTE not found: the late-boundedness of `'a` differs

error: /checkout/tests/ui/error-codes/E0195.rs:16: expected NOTE not found: the late-boundedness of `'b` differs

error: 2 unexpected errors found, 2 expected errors not found
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/error-codes/E0195.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/error-codes/E0195" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers"

--- unexpected errors (from JSON output) ---
NOTEline  16: 16:12: 16:14: `'a` differs between the trait and impl
NOTEline  16: 16:15: 16:17: `'b` differs between the trait and impl
---
--- not found errors (from test file) ---
NOTEline  16: the late-boundedness of `'a` differs
NOTEline  16: the late-boundedness of `'b` differs
---


thread '[ui] tests/ui/error-codes/E0195.rs' panicked at src/tools/compiletest/src/runtest.rs:789:13:
errors differ from expected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

method has an incompatible type for trait due to lifetimes has too wide a span
6 participants