Skip to content

Commit 865d0fe

Browse files
committed
Ban non-static lifetimes from AnonConst on AST.
The extra diagnostics come from the compiler no longer aborting before typeck.
1 parent b711723 commit 865d0fe

File tree

3 files changed

+37
-38
lines changed

3 files changed

+37
-38
lines changed

compiler/rustc_resolve/src/late.rs

+21-6
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ enum LifetimeRibKind {
204204
/// lifetimes in const generics. See issue #74052 for discussion.
205205
ConstGeneric,
206206

207+
/// Non-static lifetimes are prohibited in anonymous constants under `min_const_generics`.
208+
/// This function will emit an error if `generic_const_exprs` is not enabled, the body identified by
209+
/// `body_id` is an anonymous constant and `lifetime_ref` is non-static.
210+
AnonConst,
211+
207212
/// For **Modern** cases, create a new anonymous region parameter
208213
/// and reference that.
209214
///
@@ -532,7 +537,9 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
532537
}
533538
fn visit_anon_const(&mut self, constant: &'ast AnonConst) {
534539
// We deal with repeat expressions explicitly in `resolve_expr`.
535-
self.resolve_anon_const(constant, IsRepeatExpr::No);
540+
self.with_lifetime_rib(LifetimeRibKind::AnonConst, |this| {
541+
this.resolve_anon_const(constant, IsRepeatExpr::No);
542+
})
536543
}
537544
fn visit_expr(&mut self, expr: &'ast Expr) {
538545
self.resolve_expr(expr, None);
@@ -1117,7 +1124,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
11171124
this.ribs[TypeNS].push(forward_ty_ban_rib);
11181125
this.ribs[ValueNS].push(forward_const_ban_rib);
11191126
this.with_lifetime_rib(LifetimeRibKind::ConstGeneric, |this| {
1120-
this.visit_anon_const(expr)
1127+
this.resolve_anon_const(expr, IsRepeatExpr::No)
11211128
});
11221129
forward_const_ban_rib = this.ribs[ValueNS].pop().unwrap();
11231130
forward_ty_ban_rib = this.ribs[TypeNS].pop().unwrap();
@@ -1174,6 +1181,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
11741181
self.r.lifetimes_res_map.insert(lifetime.id, LifetimeRes::Error);
11751182
return;
11761183
}
1184+
LifetimeRibKind::AnonConst => {
1185+
self.maybe_emit_forbidden_non_static_lifetime_error(lifetime);
1186+
self.r.lifetimes_res_map.insert(lifetime.id, LifetimeRes::Error);
1187+
return;
1188+
}
11771189
_ => {}
11781190
}
11791191
}
@@ -3076,9 +3088,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
30763088
is_repeat,
30773089
constant.value.is_potential_trivial_const_param(),
30783090
None,
3079-
|this| {
3080-
visit::walk_anon_const(this, constant);
3081-
},
3091+
|this| visit::walk_anon_const(this, constant),
30823092
);
30833093
}
30843094

@@ -3229,7 +3239,12 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
32293239
}
32303240
ExprKind::Repeat(ref elem, ref ct) => {
32313241
self.visit_expr(elem);
3232-
self.resolve_anon_const(ct, IsRepeatExpr::Yes);
3242+
self.with_lifetime_rib(LifetimeRibKind::AnonConst, |this| {
3243+
this.resolve_anon_const(ct, IsRepeatExpr::Yes)
3244+
});
3245+
}
3246+
ExprKind::ConstBlock(ref ct) => {
3247+
self.resolve_anon_const(ct, IsRepeatExpr::No);
32333248
}
32343249
ExprKind::Index(ref elem, ref idx) => {
32353250
self.resolve_expr(elem, Some(expr));

compiler/rustc_resolve/src/late/diagnostics.rs

+16-28
Original file line numberDiff line numberDiff line change
@@ -1901,6 +1901,22 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
19011901
)
19021902
.emit();
19031903
}
1904+
1905+
/// Non-static lifetimes are prohibited in anonymous constants under `min_const_generics`.
1906+
/// This function will emit an error if `generic_const_exprs` is not enabled, the body identified by
1907+
/// `body_id` is an anonymous constant and `lifetime_ref` is non-static.
1908+
crate fn maybe_emit_forbidden_non_static_lifetime_error(&self, lifetime_ref: &ast::Lifetime) {
1909+
let feature_active = self.r.session.features_untracked().generic_const_exprs;
1910+
if !feature_active {
1911+
feature_err(
1912+
&self.r.session.parse_sess,
1913+
sym::generic_const_exprs,
1914+
lifetime_ref.ident.span,
1915+
"a non-static lifetime is not allowed in a `const`",
1916+
)
1917+
.emit();
1918+
}
1919+
}
19041920
}
19051921

19061922
impl<'tcx> LifetimeContext<'_, 'tcx> {
@@ -2398,32 +2414,4 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
23982414
_ => unreachable!(),
23992415
}
24002416
}
2401-
2402-
/// Non-static lifetimes are prohibited in anonymous constants under `min_const_generics`.
2403-
/// This function will emit an error if `generic_const_exprs` is not enabled, the body identified by
2404-
/// `body_id` is an anonymous constant and `lifetime_ref` is non-static.
2405-
crate fn maybe_emit_forbidden_non_static_lifetime_error(
2406-
&self,
2407-
body_id: hir::BodyId,
2408-
lifetime_ref: &'tcx hir::Lifetime,
2409-
) {
2410-
let is_anon_const = matches!(
2411-
self.tcx.def_kind(self.tcx.hir().body_owner_def_id(body_id)),
2412-
hir::def::DefKind::AnonConst
2413-
);
2414-
let is_allowed_lifetime = matches!(
2415-
lifetime_ref.name,
2416-
hir::LifetimeName::Implicit | hir::LifetimeName::Static | hir::LifetimeName::Underscore
2417-
);
2418-
2419-
if !self.tcx.lazy_normalization() && is_anon_const && !is_allowed_lifetime {
2420-
feature_err(
2421-
&self.tcx.sess.parse_sess,
2422-
sym::generic_const_exprs,
2423-
lifetime_ref.span,
2424-
"a non-static lifetime is not allowed in a `const`",
2425-
)
2426-
.emit();
2427-
}
2428-
}
24292417
}

compiler/rustc_resolve/src/late/lifetimes.rs

-4
Original file line numberDiff line numberDiff line change
@@ -2243,10 +2243,6 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
22432243
let result = loop {
22442244
match *scope {
22452245
Scope::Body { id, s } => {
2246-
// Non-static lifetimes are prohibited in anonymous constants without
2247-
// `generic_const_exprs`.
2248-
self.maybe_emit_forbidden_non_static_lifetime_error(id, lifetime_ref);
2249-
22502246
outermost_body = Some(id);
22512247
scope = s;
22522248
}

0 commit comments

Comments
 (0)