Skip to content

Commit 4fe52e4

Browse files
committed
Eagerly evaluate lifetime syntax category
1 parent 3afcc00 commit 4fe52e4

File tree

2 files changed

+11
-32
lines changed

2 files changed

+11
-32
lines changed

compiler/rustc_lint/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
2222
// tidy-alphabetical-start
2323
#![allow(internal_features)]
24+
#![cfg_attr(bootstrap, feature(iter_chain))]
2425
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
2526
#![doc(rust_logo)]
2627
#![feature(array_windows)]

compiler/rustc_lint/src/lifetime_syntax.rs

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,11 @@ enum LifetimeSyntaxCategory {
148148
}
149149

150150
impl LifetimeSyntaxCategory {
151-
fn new(syntax_source: (hir::LifetimeSyntax, LifetimeSource)) -> Option<Self> {
151+
fn new(lifetime: &hir::Lifetime) -> Option<Self> {
152152
use LifetimeSource::*;
153153
use hir::LifetimeSyntax::*;
154154

155-
match syntax_source {
155+
match (lifetime.syntax, lifetime.source) {
156156
// E.g. `&T`.
157157
(Implicit, Reference) |
158158
// E.g. `&'_ T`.
@@ -233,22 +233,8 @@ impl std::ops::Add for LifetimeSyntaxCategories<usize> {
233233
}
234234

235235
fn lifetimes_use_matched_syntax(input_info: &[Info<'_>], output_info: &[Info<'_>]) -> bool {
236-
let mut syntax_counts = LifetimeSyntaxCategories::<usize>::default();
237-
238-
for info in input_info.iter().chain(output_info) {
239-
if let Some(category) = info.lifetime_syntax_category() {
240-
*syntax_counts.select(category) += 1;
241-
}
242-
}
243-
244-
tracing::debug!(?syntax_counts);
245-
246-
matches!(
247-
syntax_counts,
248-
LifetimeSyntaxCategories { hidden: _, elided: 0, named: 0 }
249-
| LifetimeSyntaxCategories { hidden: 0, elided: _, named: 0 }
250-
| LifetimeSyntaxCategories { hidden: 0, elided: 0, named: _ }
251-
)
236+
let (first, inputs) = input_info.split_first().unwrap();
237+
std::iter::chain(inputs, output_info).all(|info| info.syntax_category == first.syntax_category)
252238
}
253239

254240
fn emit_mismatch_diagnostic<'tcx>(
@@ -312,11 +298,6 @@ fn emit_mismatch_diagnostic<'tcx>(
312298

313299
let syntax_source = info.syntax_source();
314300

315-
if let (_, Other) = syntax_source {
316-
// Ignore any other kind of lifetime.
317-
continue;
318-
}
319-
320301
if let (ExplicitBound, _) = syntax_source {
321302
bound_lifetime = Some(info);
322303
}
@@ -393,9 +374,7 @@ fn emit_mismatch_diagnostic<'tcx>(
393374
let categorize = |infos: &[Info<'_>]| {
394375
let mut categories = LifetimeSyntaxCategories::<Vec<_>>::default();
395376
for info in infos {
396-
if let Some(category) = info.lifetime_syntax_category() {
397-
categories.select(category).push(info.reporting_span());
398-
}
377+
categories.select(info.syntax_category).push(info.reporting_span());
399378
}
400379
categories
401380
};
@@ -518,6 +497,7 @@ fn build_mismatch_suggestion(
518497
#[derive(Debug)]
519498
struct Info<'tcx> {
520499
lifetime: &'tcx hir::Lifetime,
500+
syntax_category: LifetimeSyntaxCategory,
521501
ty: &'tcx hir::Ty<'tcx>,
522502
}
523503

@@ -526,10 +506,6 @@ impl<'tcx> Info<'tcx> {
526506
(self.lifetime.syntax, self.lifetime.source)
527507
}
528508

529-
fn lifetime_syntax_category(&self) -> Option<LifetimeSyntaxCategory> {
530-
LifetimeSyntaxCategory::new(self.syntax_source())
531-
}
532-
533509
fn lifetime_name(&self) -> &str {
534510
self.lifetime.ident.as_str()
535511
}
@@ -589,8 +565,10 @@ impl<'a, 'tcx> LifetimeInfoCollector<'a, 'tcx> {
589565
impl<'a, 'tcx> Visitor<'tcx> for LifetimeInfoCollector<'a, 'tcx> {
590566
#[instrument(skip(self))]
591567
fn visit_lifetime(&mut self, lifetime: &'tcx hir::Lifetime) {
592-
let info = Info { lifetime, ty: self.ty };
593-
self.map.entry(&lifetime.kind).or_default().push(info);
568+
if let Some(syntax_category) = LifetimeSyntaxCategory::new(lifetime) {
569+
let info = Info { lifetime, syntax_category, ty: self.ty };
570+
self.map.entry(&lifetime.kind).or_default().push(info);
571+
}
594572
}
595573

596574
#[instrument(skip(self))]

0 commit comments

Comments
 (0)