Skip to content

Commit d618ad7

Browse files
committed
elided_named_lifetimes: allow elided 'static in const/static type annotations
1 parent 3819154 commit d618ad7

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

compiler/rustc_resolve/src/late.rs

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

709716
/// Walks the whole crate in DFS order, visiting each item, resolving names as it goes.
@@ -1349,6 +1356,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
13491356
in_func_body: false,
13501357
lifetime_uses: Default::default(),
13511358
crate_node_id: krate.id,
1359+
allow_elided_static: false,
13521360
}
13531361
}
13541362

@@ -1559,6 +1567,13 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
15591567
ret
15601568
}
15611569

1570+
#[instrument(level = "debug", skip(self))]
1571+
fn visit_ty_allow_elided_static(&mut self, ty: &'ast Ty) {
1572+
self.allow_elided_static = true;
1573+
self.visit_ty(ty);
1574+
self.allow_elided_static = false;
1575+
}
1576+
15621577
#[instrument(level = "debug", skip(self))]
15631578
fn resolve_lifetime(&mut self, lifetime: &'ast Lifetime, use_ctxt: visit::LifetimeCtxt) {
15641579
let ident = lifetime.ident;
@@ -2054,12 +2069,14 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
20542069
debug_assert_eq!(id, missing.id);
20552070
match res {
20562071
LifetimeRes::Static => {
2057-
self.r.lint_buffer.buffer_lint(
2058-
lint::builtin::ELIDED_NAMED_LIFETIMES,
2059-
missing.id_if_not_fake_or(self.crate_node_id),
2060-
missing.span,
2061-
BuiltinLintDiag::ElidedIsStatic { elided: missing.span },
2062-
);
2072+
if !self.allow_elided_static {
2073+
self.r.lint_buffer.buffer_lint(
2074+
lint::builtin::ELIDED_NAMED_LIFETIMES,
2075+
missing.id_if_not_fake_or(self.crate_node_id),
2076+
missing.span,
2077+
BuiltinLintDiag::ElidedIsStatic { elided: missing.span },
2078+
);
2079+
}
20632080
}
20642081
LifetimeRes::Param { param, binder } => {
20652082
self.r.lint_buffer.buffer_lint(
@@ -2606,7 +2623,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
26062623
ItemKind::Static(box ast::StaticItem { ref ty, ref expr, .. }) => {
26072624
self.with_static_rib(def_kind, |this| {
26082625
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| {
2609-
this.visit_ty(ty);
2626+
this.visit_ty_allow_elided_static(ty);
26102627
});
26112628
if let Some(expr) = expr {
26122629
// We already forbid generic params because of the above item rib,
@@ -2637,7 +2654,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
26372654

26382655
this.with_lifetime_rib(
26392656
LifetimeRibKind::Elided(LifetimeRes::Static),
2640-
|this| this.visit_ty(ty),
2657+
|this| this.visit_ty_allow_elided_static(ty),
26412658
);
26422659

26432660
if let Some(expr) = expr {

0 commit comments

Comments
 (0)