Skip to content

Fix mismatched_lifetime_syntaxes suggestions for hidden path lifetimes#154592

Merged
rust-bors[bot] merged 3 commits intorust-lang:mainfrom
reddevilmidzy:lifetime
Apr 1, 2026
Merged

Fix mismatched_lifetime_syntaxes suggestions for hidden path lifetimes#154592
rust-bors[bot] merged 3 commits intorust-lang:mainfrom
reddevilmidzy:lifetime

Conversation

@reddevilmidzy
Copy link
Copy Markdown
Member

close: #154493

r? shepmaster

cc: @chenyukang

@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 Mar 30, 2026
| ^^^^ ^^^^^
| | |
| | the same lifetime is hidden here
| | the same lifetime is hidden here
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

it's better to keep only one the same lifetime is hidden here here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ah okay, I missed that comment—thanks.

Copy link
Copy Markdown
Member Author

@reddevilmidzy reddevilmidzy Mar 31, 2026

Choose a reason for hiding this comment

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

Would you prefer showing the same lifetime is hidden here only once even in cases like this?

struct Foo<'a, 'b> {
    ptr1: &'a str,
    ptr2: &'b str,
}

fn missing(_: &str) -> (Foo, Foo) {
    //~^ ERROR: hiding a lifetime that's elided elsewhere is confusing
    todo!()
}
   |
LL | fn missing(_: &str) -> (Foo, Foo) {
   |               ^^^^      ^^^  ^^^
   |               |         |
   |               |         the same lifetime is hidden here
   |               the lifetime is elided here
   |
   = help: the same lifetime is referred to in inconsistent ways, making the signature confusing
note: the lint level is defined here
  --> $DIR/hello.rs:2:9
   |
LL | #![deny(mismatched_lifetime_syntaxes)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: use `'_` for type paths
   |
LL | fn missing(_: &str) -> (Foo<'_, '_>, Foo<'_, '_>) {
   |                            ++++++++     ++++++++

Copy link
Copy Markdown
Member

@chenyukang chenyukang Mar 31, 2026

Choose a reason for hiding this comment

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

i'm neutral for this, since there are two struct type here.
it only seems weird for this:

Image

it's perfect if we show as something like the same 2 lifetimes are hidden here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ah, good. Then I will modify it to emit summarized message only within the same struct.

}

let mut suggestions = Vec::new();
let mut path_counts: FxIndexMap<(Span, PathSuggestionKind), usize> = FxIndexMap::default();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

it's not valid to use span as key here, repeated metavariable expansions can share the same source span, which makes this merge distinct hidden-path occurrences and produces suggestions like Pair<'a, 'a, 'a, 'a> instead of Pair<'a, 'a>.

You can test it with this test case:

#![deny(mismatched_lifetime_syntaxes)]

struct Pair<'a, 'b>(&'a u8, &'b u8);

macro_rules! repeated {
    ($($pair:ident),+ ; $middle:ty) => {
        ($($pair),+, $middle, $($pair),+)
        //~^ ERROR hiding or eliding a lifetime that's named elsewhere is confusing
    };
}

fn repeated_macro<'a>(x: &'a u8) -> repeated!(Pair, Pair; &'_ u8) {
    (Pair(x, x), Pair(x, x), x, Pair(x, x), Pair(x, x))
}

fn main() {}

right fix should be:

macro_rules! repeated {
    ($($pair:ident),+ ; $middle:ty) => {
        ($($pair<'a, 'a>),+, $middle, $($pair<'a, 'a>),+)
        //~^ ERROR hiding or eliding a lifetime that's named elsewhere is confusing
    };
}

fn repeated_macro<'a>(x: &'a u8) -> repeated!(Pair, Pair; &'a u8) {
    (Pair(x, x), Pair(x, x), x, Pair(x, x), Pair(x, x))
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Changed Span to HirId!

Comment on lines +3239 to +3249
for (span, count) in hidden_output_counts {
let label = msg!(
"the same {$count ->
[one] lifetime
*[other] lifetimes
} {$count ->
[one] is
*[other] are
} hidden here"
)
.arg("count", count)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

To keep it similar to the other code above, I made it output the same lifetimes are \ the same lifetime is.

@chenyukang
Copy link
Copy Markdown
Member

Thanks!
@bors r=chenyukang

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 1, 2026

📌 Commit 8cef68a has been approved by chenyukang

It is now in the queue for this repository.

@rust-bors rust-bors bot 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 Apr 1, 2026
rust-bors bot pushed a commit that referenced this pull request Apr 1, 2026
Rollup of 5 pull requests

Successful merges:

 - #152935 (c-variadic: error when we can't guarantee that the backend does the right thing)
 - #153207 (std::net: clamp linger timeout value to prevent silent truncation.)
 - #154592 (Fix `mismatched_lifetime_syntaxes` suggestions for hidden path lifetimes)
 - #154643 (fix pin docs)
 - #154648 (Clarify `ty::List`)
@rust-bors rust-bors bot merged commit 1f5dcf3 into rust-lang:main Apr 1, 2026
11 checks passed
@rustbot rustbot added this to the 1.96.0 milestone Apr 1, 2026
rust-timer added a commit that referenced this pull request Apr 1, 2026
Rollup merge of #154592 - reddevilmidzy:lifetime, r=chenyukang

Fix `mismatched_lifetime_syntaxes` suggestions for hidden path lifetimes

close: #154493

r? shepmaster

cc: @chenyukang
@reddevilmidzy reddevilmidzy deleted the lifetime branch April 1, 2026 09:33
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mismatched_lifetime_syntaxes lint is suggesting a wrong code

4 participants