From 63028ac3a1cdad56e4a61b8fcb260456efc2b71e Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 29 Apr 2023 10:32:31 +0000 Subject: [PATCH] Do not force anonymous lifetimes in consts to be static. --- compiler/rustc_resolve/src/late.rs | 4 ++-- .../elided-lifetime-in-anon-const.rs | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 tests/ui/lifetimes/elided-lifetime-in-anon-const.rs diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index a97857e05e2e9..5da67dbc2aa17 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -656,7 +656,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast, fn visit_anon_const(&mut self, constant: &'ast AnonConst) { // We deal with repeat expressions explicitly in `resolve_expr`. self.with_lifetime_rib(LifetimeRibKind::AnonConst, |this| { - this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| { + this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Infer), |this| { this.resolve_anon_const(constant, IsRepeatExpr::No); }) }) @@ -4130,7 +4130,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { ExprKind::Repeat(ref elem, ref ct) => { self.visit_expr(elem); self.with_lifetime_rib(LifetimeRibKind::AnonConst, |this| { - this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| { + this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Infer), |this| { this.resolve_anon_const(ct, IsRepeatExpr::Yes) }) }); diff --git a/tests/ui/lifetimes/elided-lifetime-in-anon-const.rs b/tests/ui/lifetimes/elided-lifetime-in-anon-const.rs new file mode 100644 index 0000000000000..69a7b61bab418 --- /dev/null +++ b/tests/ui/lifetimes/elided-lifetime-in-anon-const.rs @@ -0,0 +1,20 @@ +// Verify that elided lifetimes inside anonymous constants are not forced to be `'static`. +// check-pass + +fn foo() -> [(); { + let a = 10_usize; + let b: &'_ usize = &a; + *b + }] { + [(); 10] +} + +fn bar() -> [(); 10] { + [(); { + let a = 10_usize; + let b: &'_ usize = &a; + *b + }] +} + +fn main() {}