Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit f0c9cec

Browse files
committed
Move the override flag for elided_named_lifetimes
from `LateResolutionVisitor` to `LifetimeRes::Static`
1 parent bacbf46 commit f0c9cec

File tree

5 files changed

+33
-36
lines changed

5 files changed

+33
-36
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
838838

839839
(hir::ParamName::Fresh, hir::LifetimeParamKind::Elided(kind))
840840
}
841-
LifetimeRes::Static | LifetimeRes::Error => return None,
841+
LifetimeRes::Static { .. } | LifetimeRes::Error => return None,
842842
res => panic!(
843843
"Unexpected lifetime resolution {:?} for {:?} at {:?}",
844844
res, ident, ident.span
@@ -1657,7 +1657,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16571657
}
16581658

16591659
// Opaques do not capture `'static`
1660-
LifetimeRes::Static | LifetimeRes::Error => {
1660+
LifetimeRes::Static { .. } | LifetimeRes::Error => {
16611661
continue;
16621662
}
16631663

@@ -2070,7 +2070,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20702070
hir::LifetimeName::Param(param)
20712071
}
20722072
LifetimeRes::Infer => hir::LifetimeName::Infer,
2073-
LifetimeRes::Static => hir::LifetimeName::Static,
2073+
LifetimeRes::Static { .. } => hir::LifetimeName::Static,
20742074
LifetimeRes::Error => hir::LifetimeName::Error,
20752075
res => panic!(
20762076
"Unexpected lifetime resolution {:?} for {:?} at {:?}",

compiler/rustc_ast_lowering/src/lifetime_collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl<'ast> LifetimeCollectVisitor<'ast> {
2727
self.collected_lifetimes.insert(lifetime);
2828
}
2929
}
30-
LifetimeRes::Static | LifetimeRes::Error => {
30+
LifetimeRes::Static { .. } | LifetimeRes::Error => {
3131
self.collected_lifetimes.insert(lifetime);
3232
}
3333
LifetimeRes::Infer => {}

compiler/rustc_hir/src/def.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -821,8 +821,13 @@ pub enum LifetimeRes {
821821
/// This variant is used for anonymous lifetimes that we did not resolve during
822822
/// late resolution. Those lifetimes will be inferred by typechecking.
823823
Infer,
824-
/// Explicit `'static` lifetime.
825-
Static,
824+
/// `'static` lifetime.
825+
Static {
826+
/// We do not want to emit `elided_named_lifetimes`
827+
/// when we are inside of a const item or a static,
828+
/// because it would get too annoying.
829+
suppress_elision_warning: bool,
830+
},
826831
/// Resolution failure.
827832
Error,
828833
/// HACK: This is used to recover the NodeId of an elided lifetime.

compiler/rustc_resolve/src/late.rs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -704,14 +704,6 @@ struct LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
704704
/// [`elided_named_lifetimes`](lint::builtin::ELIDED_NAMED_LIFETIMES).
705705
/// See comments in [`MissingLifetime::id_if_exists_in_source_or`].
706706
crate_node_id: NodeId,
707-
708-
/// Don't emit [`elided_named_lifetimes`](lint::builtin::ELIDED_NAMED_LIFETIMES)
709-
/// when we are in a type annotation for a `const` or `static`.
710-
/// ```rust
711-
/// const HELLO_WORLD: &str = "Hello, world!";
712-
/// static ZEROES: &[u8] = &[0, 0, 0];
713-
/// ```
714-
warn_elided_static: bool,
715707
}
716708

717709
/// Walks the whole crate in DFS order, visiting each item, resolving names as it goes.
@@ -1357,7 +1349,6 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
13571349
in_func_body: false,
13581350
lifetime_uses: Default::default(),
13591351
crate_node_id: krate.id,
1360-
warn_elided_static: true,
13611352
}
13621353
}
13631354

@@ -1568,21 +1559,14 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
15681559
ret
15691560
}
15701561

1571-
#[instrument(level = "debug", skip(self))]
1572-
fn visit_ty_do_not_warn_elided_static(&mut self, ty: &'ast Ty) {
1573-
self.warn_elided_static = false;
1574-
self.visit_ty(ty);
1575-
self.warn_elided_static = true;
1576-
}
1577-
15781562
#[instrument(level = "debug", skip(self))]
15791563
fn resolve_lifetime(&mut self, lifetime: &'ast Lifetime, use_ctxt: visit::LifetimeCtxt) {
15801564
let ident = lifetime.ident;
15811565

15821566
if ident.name == kw::StaticLifetime {
15831567
self.record_lifetime_res(
15841568
lifetime.id,
1585-
LifetimeRes::Static,
1569+
LifetimeRes::Static { suppress_elision_warning: false },
15861570
LifetimeElisionCandidate::Named,
15871571
);
15881572
return;
@@ -1722,7 +1706,8 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
17221706
if lifetimes_in_scope.is_empty() {
17231707
self.record_lifetime_res(
17241708
lifetime.id,
1725-
LifetimeRes::Static,
1709+
// We are inside a const item, so do not warn.
1710+
LifetimeRes::Static { suppress_elision_warning: true },
17261711
elision_candidate,
17271712
);
17281713
return;
@@ -2069,8 +2054,8 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
20692054
LifetimeElisionCandidate::Missing(missing @ MissingLifetime { span: elided, .. }) => {
20702055
debug_assert_eq!(id, missing.id);
20712056
match res {
2072-
LifetimeRes::Static => {
2073-
if self.warn_elided_static {
2057+
LifetimeRes::Static { suppress_elision_warning } => {
2058+
if !suppress_elision_warning {
20742059
self.r.lint_buffer.buffer_lint(
20752060
lint::builtin::ELIDED_NAMED_LIFETIMES,
20762061
missing.id_if_exists_in_source_or(self.crate_node_id),
@@ -2106,7 +2091,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
21062091
}
21072092

21082093
match res {
2109-
LifetimeRes::Param { .. } | LifetimeRes::Fresh { .. } | LifetimeRes::Static => {
2094+
LifetimeRes::Param { .. } | LifetimeRes::Fresh { .. } | LifetimeRes::Static { .. } => {
21102095
if let Some(ref mut candidates) = self.lifetime_elision_candidates {
21112096
candidates.push((res, candidate));
21122097
}
@@ -2624,9 +2609,14 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
26242609

26252610
ItemKind::Static(box ast::StaticItem { ref ty, ref expr, .. }) => {
26262611
self.with_static_rib(def_kind, |this| {
2627-
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| {
2628-
this.visit_ty_do_not_warn_elided_static(ty);
2629-
});
2612+
this.with_lifetime_rib(
2613+
LifetimeRibKind::Elided(LifetimeRes::Static {
2614+
suppress_elision_warning: true,
2615+
}),
2616+
|this| {
2617+
this.visit_ty(ty);
2618+
},
2619+
);
26302620
if let Some(expr) = expr {
26312621
// We already forbid generic params because of the above item rib,
26322622
// so it doesn't matter whether this is a trivial constant.
@@ -2655,8 +2645,10 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
26552645
this.visit_generics(generics);
26562646

26572647
this.with_lifetime_rib(
2658-
LifetimeRibKind::Elided(LifetimeRes::Static),
2659-
|this| this.visit_ty_do_not_warn_elided_static(ty),
2648+
LifetimeRibKind::Elided(LifetimeRes::Static {
2649+
suppress_elision_warning: true,
2650+
}),
2651+
|this| this.visit_ty(ty),
26602652
);
26612653

26622654
if let Some(expr) = expr {
@@ -2983,7 +2975,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
29832975
},
29842976
|this| {
29852977
this.visit_generics(generics);
2986-
this.visit_ty_do_not_warn_elided_static(ty);
2978+
this.visit_ty(ty);
29872979

29882980
// Only impose the restrictions of `ConstRibKind` for an
29892981
// actual constant expression in a provided default.
@@ -3196,7 +3188,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
31963188
);
31973189

31983190
this.visit_generics(generics);
3199-
this.visit_ty_do_not_warn_elided_static(ty);
3191+
this.visit_ty(ty);
32003192
if let Some(expr) = expr {
32013193
// We allow arbitrary const expressions inside of associated consts,
32023194
// even if they are potentially not const evaluatable.

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3045,7 +3045,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
30453045
maybe_static = true;
30463046
in_scope_lifetimes = vec![(
30473047
Ident::with_dummy_span(kw::StaticLifetime),
3048-
(DUMMY_NODE_ID, LifetimeRes::Static),
3048+
(DUMMY_NODE_ID, LifetimeRes::Static { suppress_elision_warning: false }),
30493049
)];
30503050
}
30513051
} else if elided_len == 0 {
@@ -3057,7 +3057,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
30573057
maybe_static = true;
30583058
in_scope_lifetimes = vec![(
30593059
Ident::with_dummy_span(kw::StaticLifetime),
3060-
(DUMMY_NODE_ID, LifetimeRes::Static),
3060+
(DUMMY_NODE_ID, LifetimeRes::Static { suppress_elision_warning: false }),
30613061
)];
30623062
}
30633063
} else if num_params == 1 {

0 commit comments

Comments
 (0)