Fix mismatched_lifetime_syntaxes suggestions for hidden path lifetimes#154592
Fix mismatched_lifetime_syntaxes suggestions for hidden path lifetimes#154592rust-bors[bot] merged 3 commits intorust-lang:mainfrom
mismatched_lifetime_syntaxes suggestions for hidden path lifetimes#154592Conversation
| | ^^^^ ^^^^^ | ||
| | | | | ||
| | | the same lifetime is hidden here | ||
| | | the same lifetime is hidden here |
There was a problem hiding this comment.
it's better to keep only one the same lifetime is hidden here here?
There was a problem hiding this comment.
Ah okay, I missed that comment—thanks.
There was a problem hiding this comment.
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<'_, '_>) {
| ++++++++ ++++++++
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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))
}There was a problem hiding this comment.
Changed Span to HirId!
53815bb to
8cef68a
Compare
| 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) |
There was a problem hiding this comment.
To keep it similar to the other code above, I made it output the same lifetimes are \ the same lifetime is.
|
Thanks! |
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`)
Rollup merge of #154592 - reddevilmidzy:lifetime, r=chenyukang Fix `mismatched_lifetime_syntaxes` suggestions for hidden path lifetimes close: #154493 r? shepmaster cc: @chenyukang

close: #154493
r? shepmaster
cc: @chenyukang