Skip to content

Reword mismatched-lifetime-syntaxes text based on feedback #143914

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 38 additions & 15 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -508,27 +508,50 @@ lint_metavariable_still_repeating = variable `{$name}` is still repeating at thi
lint_metavariable_wrong_operator = meta-variable repeats with different Kleene operator
lint_mismatched_lifetime_syntaxes =
lifetime flowing from input to output with different syntax can be confusing
.label_mismatched_lifetime_syntaxes_inputs =
{$n_inputs ->
[one] this lifetime flows
*[other] these lifetimes flow
} to the output
.label_mismatched_lifetime_syntaxes_outputs =
the {$n_outputs ->
[one] lifetime gets
*[other] lifetimes get
} resolved as `{$lifetime_name}`
lint_mismatched_lifetime_syntaxes_eliding_while_named =
eliding a lifetime that's named elsewhere is confusing
lint_mismatched_lifetime_syntaxes_help =
the same lifetime is referred to in inconsistent ways, making the signature confusing
lint_mismatched_lifetime_syntaxes_hiding_and_eliding_while_named =
hiding or eliding a lifetime that's named elsewhere is confusing
lint_mismatched_lifetime_syntaxes_hiding_while_elided =
hiding a lifetime that's elided elsewhere is confusing
lint_mismatched_lifetime_syntaxes_hiding_while_named =
hiding a lifetime that's named elsewhere is confusing
lint_mismatched_lifetime_syntaxes_input_elided =
the lifetime is elided here
lint_mismatched_lifetime_syntaxes_input_hidden =
the lifetime is hidden here
lint_mismatched_lifetime_syntaxes_input_named =
the lifetime is named here
lint_mismatched_lifetime_syntaxes_output_elided =
the same lifetime is elided here
lint_mismatched_lifetime_syntaxes_output_hidden =
the same lifetime is hidden here
lint_mismatched_lifetime_syntaxes_output_named =
the same lifetime is named here
lint_mismatched_lifetime_syntaxes_suggestion_explicit =
one option is to consistently use `{$lifetime_name}`
consistently use `{$lifetime_name}`
lint_mismatched_lifetime_syntaxes_suggestion_implicit =
one option is to consistently remove the lifetime
remove the lifetime name from references
lint_mismatched_lifetime_syntaxes_suggestion_mixed =
one option is to remove the lifetime for references and use the anonymous lifetime for paths
remove the lifetime name from references and use `'_` for type paths
lint_mismatched_lifetime_syntaxes_suggestion_mixed_only_paths =
use `'_` for type paths
lint_missing_unsafe_on_extern = extern blocks should be unsafe
.suggestion = needs `unsafe` before the extern keyword
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ mod invalid_from_utf8;
mod late;
mod let_underscore;
mod levels;
mod lifetime_syntax;
pub mod lifetime_syntax;
mod lints;
mod macro_expr_fragment_specifier_2024_migration;
mod map_unit_fn;
Expand Down
149 changes: 118 additions & 31 deletions compiler/rustc_lint/src/lifetime_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,43 +140,115 @@ fn report_mismatches<'tcx>(
}
}

fn lifetimes_use_matched_syntax(input_info: &[Info<'_>], output_info: &[Info<'_>]) -> bool {
// Categorize lifetimes into source/syntax buckets.
let mut n_hidden = 0;
let mut n_elided = 0;
let mut n_named = 0;
#[derive(Debug, Copy, Clone, PartialEq)]
enum LifetimeSyntaxCategory {
Hidden,
Elided,
Named,
}

for info in input_info.iter().chain(output_info) {
impl LifetimeSyntaxCategory {
fn new(syntax_source: (hir::LifetimeSyntax, LifetimeSource)) -> Option<Self> {
use LifetimeSource::*;
use hir::LifetimeSyntax::*;

let syntax_source = (info.lifetime.syntax, info.lifetime.source);

match syntax_source {
// Ignore any other kind of lifetime.
(_, Other) => continue,

// E.g. `&T`.
(Implicit, Reference | OutlivesBound | PreciseCapturing) |
(Implicit, Reference) |
// E.g. `&'_ T`.
(ExplicitAnonymous, Reference | OutlivesBound | PreciseCapturing) |
(ExplicitAnonymous, Reference) |
// E.g. `ContainsLifetime<'_>`.
(ExplicitAnonymous, Path { .. }) => n_elided += 1,
(ExplicitAnonymous, Path { .. }) |
// E.g. `+ '_`, `+ use<'_>`.
(ExplicitAnonymous, OutlivesBound | PreciseCapturing) => {
Some(Self::Elided)
}

// E.g. `ContainsLifetime`.
(Implicit, Path { .. }) => n_hidden += 1,
(Implicit, Path { .. }) => {
Some(Self::Hidden)
}

// E.g. `&'a T`.
(ExplicitBound, Reference | OutlivesBound | PreciseCapturing) |
(ExplicitBound, Reference) |
// E.g. `ContainsLifetime<'a>`.
(ExplicitBound, Path { .. }) => n_named += 1,
};
(ExplicitBound, Path { .. }) |
// E.g. `+ 'a`, `+ use<'a>`.
(ExplicitBound, OutlivesBound | PreciseCapturing) => {
Some(Self::Named)
}

(Implicit, OutlivesBound | PreciseCapturing) |
(_, Other) => {
None
}
}
}
}

#[derive(Debug, Default)]
pub struct LifetimeSyntaxCategories<T> {
pub hidden: T,
pub elided: T,
pub named: T,
}

impl<T> LifetimeSyntaxCategories<T> {
fn select(&mut self, category: LifetimeSyntaxCategory) -> &mut T {
use LifetimeSyntaxCategory::*;

match category {
Elided => &mut self.elided,
Hidden => &mut self.hidden,
Named => &mut self.named,
}
}
}

impl<T> LifetimeSyntaxCategories<Vec<T>> {
pub fn len(&self) -> LifetimeSyntaxCategories<usize> {
LifetimeSyntaxCategories {
hidden: self.hidden.len(),
elided: self.elided.len(),
named: self.named.len(),
}
}

pub fn flatten(&self) -> impl Iterator<Item = &T> {
let Self { hidden, elided, named } = self;
[hidden.iter(), elided.iter(), named.iter()].into_iter().flatten()
}
}

impl std::ops::Add for LifetimeSyntaxCategories<usize> {
type Output = Self;

fn add(self, rhs: Self) -> Self::Output {
Self {
hidden: self.hidden + rhs.hidden,
elided: self.elided + rhs.elided,
named: self.named + rhs.named,
}
}
}

fn lifetimes_use_matched_syntax(input_info: &[Info<'_>], output_info: &[Info<'_>]) -> bool {
let mut syntax_counts = LifetimeSyntaxCategories::<usize>::default();

for info in input_info.iter().chain(output_info) {
if let Some(category) = info.lifetime_syntax_category() {
*syntax_counts.select(category) += 1;
}
}

let syntax_counts = (n_hidden, n_elided, n_named);
tracing::debug!(?syntax_counts);

matches!(syntax_counts, (_, 0, 0) | (0, _, 0) | (0, 0, _))
matches!(
syntax_counts,
LifetimeSyntaxCategories { hidden: _, elided: 0, named: 0 }
| LifetimeSyntaxCategories { hidden: 0, elided: _, named: 0 }
| LifetimeSyntaxCategories { hidden: 0, elided: 0, named: _ }
)
}

fn emit_mismatch_diagnostic<'tcx>(
Expand Down Expand Up @@ -238,7 +310,7 @@ fn emit_mismatch_diagnostic<'tcx>(
use LifetimeSource::*;
use hir::LifetimeSyntax::*;

let syntax_source = (info.lifetime.syntax, info.lifetime.source);
let syntax_source = info.syntax_source();

if let (_, Other) = syntax_source {
// Ignore any other kind of lifetime.
Expand All @@ -259,7 +331,6 @@ fn emit_mismatch_diagnostic<'tcx>(
// E.g. `&'_ T`.
(ExplicitAnonymous, Reference) => {
suggest_change_to_implicit.push(info);
suggest_change_to_mixed_implicit.push(info);
suggest_change_to_explicit_bound.push(info);
}

Expand Down Expand Up @@ -319,12 +390,22 @@ fn emit_mismatch_diagnostic<'tcx>(
}
}

let categorize = |infos: &[Info<'_>]| {
let mut categories = LifetimeSyntaxCategories::<Vec<_>>::default();
for info in infos {
if let Some(category) = info.lifetime_syntax_category() {
categories.select(category).push(info.reporting_span());
}
}
categories
};

let inputs = categorize(input_info);
let outputs = categorize(output_info);

let make_implicit_suggestions =
|infos: &[&Info<'_>]| infos.iter().map(|i| i.removing_span()).collect::<Vec<_>>();

let inputs = input_info.iter().map(|info| info.reporting_span()).collect();
let outputs = output_info.iter().map(|info| info.reporting_span()).collect();

let explicit_bound_suggestion = bound_lifetime.map(|info| {
build_mismatch_suggestion(info.lifetime_name(), &suggest_change_to_explicit_bound)
});
Expand Down Expand Up @@ -399,8 +480,6 @@ fn emit_mismatch_diagnostic<'tcx>(
?explicit_anonymous_suggestion,
);

let lifetime_name = bound_lifetime.map(|info| info.lifetime_name()).unwrap_or("'_").to_owned();

// We can produce a number of suggestions which may overwhelm
// the user. Instead, we order the suggestions based on Rust
// idioms. The "best" choice is shown to the user and the
Expand All @@ -413,21 +492,21 @@ fn emit_mismatch_diagnostic<'tcx>(

cx.emit_span_lint(
MISMATCHED_LIFETIME_SYNTAXES,
Vec::clone(&inputs),
lints::MismatchedLifetimeSyntaxes { lifetime_name, inputs, outputs, suggestions },
inputs.flatten().copied().collect::<Vec<_>>(),
lints::MismatchedLifetimeSyntaxes { inputs, outputs, suggestions },
);
}

fn build_mismatch_suggestion(
lifetime_name: &str,
infos: &[&Info<'_>],
) -> lints::MismatchedLifetimeSyntaxesSuggestion {
let lifetime_name_sugg = lifetime_name.to_owned();
let lifetime_name = lifetime_name.to_owned();

let suggestions = infos.iter().map(|info| info.suggestion(&lifetime_name)).collect();

lints::MismatchedLifetimeSyntaxesSuggestion::Explicit {
lifetime_name_sugg,
lifetime_name,
suggestions,
tool_only: false,
}
Expand All @@ -441,6 +520,14 @@ struct Info<'tcx> {
}

impl<'tcx> Info<'tcx> {
fn syntax_source(&self) -> (hir::LifetimeSyntax, LifetimeSource) {
(self.lifetime.syntax, self.lifetime.source)
}

fn lifetime_syntax_category(&self) -> Option<LifetimeSyntaxCategory> {
LifetimeSyntaxCategory::new(self.syntax_source())
}

fn lifetime_name(&self) -> &str {
self.lifetime.ident.as_str()
}
Expand Down
Loading
Loading