Skip to content

Commit a87ab48

Browse files
committed
Ban non-static in const generics in AST.
1 parent e85edd9 commit a87ab48

File tree

3 files changed

+34
-33
lines changed

3 files changed

+34
-33
lines changed

compiler/rustc_resolve/src/late.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ enum LifetimeRibKind {
199199
/// This rib declares generic parameters.
200200
Generics { parent: NodeId, span: Span, kind: LifetimeBinderKind },
201201

202+
/// FIXME(const_generics): This patches over an ICE caused by non-'static lifetimes in const
203+
/// generics. We are disallowing this until we can decide on how we want to handle non-'static
204+
/// lifetimes in const generics. See issue #74052 for discussion.
205+
ConstGeneric,
206+
202207
/// For **Modern** cases, create a new anonymous region parameter
203208
/// and reference that.
204209
///
@@ -1102,14 +1107,18 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
11021107

11031108
this.ribs[TypeNS].push(Rib::new(ConstParamTyRibKind));
11041109
this.ribs[ValueNS].push(Rib::new(ConstParamTyRibKind));
1105-
this.visit_ty(ty);
1110+
this.with_lifetime_rib(LifetimeRibKind::ConstGeneric, |this| {
1111+
this.visit_ty(ty)
1112+
});
11061113
this.ribs[TypeNS].pop().unwrap();
11071114
this.ribs[ValueNS].pop().unwrap();
11081115

11091116
if let Some(ref expr) = default {
11101117
this.ribs[TypeNS].push(forward_ty_ban_rib);
11111118
this.ribs[ValueNS].push(forward_const_ban_rib);
1112-
this.visit_anon_const(expr);
1119+
this.with_lifetime_rib(LifetimeRibKind::ConstGeneric, |this| {
1120+
this.visit_anon_const(expr)
1121+
});
11131122
forward_const_ban_rib = this.ribs[ValueNS].pop().unwrap();
11141123
forward_ty_ban_rib = this.ribs[TypeNS].pop().unwrap();
11151124
}
@@ -1158,8 +1167,14 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
11581167
return;
11591168
}
11601169

1161-
if let LifetimeRibKind::Item = rib.kind {
1162-
break;
1170+
match rib.kind {
1171+
LifetimeRibKind::Item => break,
1172+
LifetimeRibKind::ConstGeneric => {
1173+
self.emit_non_static_lt_in_const_generic_error(lifetime);
1174+
self.r.lifetimes_res_map.insert(lifetime.id, LifetimeRes::Error);
1175+
return;
1176+
}
1177+
_ => {}
11631178
}
11641179
}
11651180

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,21 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
18861886

18871887
err.emit();
18881888
}
1889+
1890+
crate fn emit_non_static_lt_in_const_generic_error(&self, lifetime_ref: &ast::Lifetime) {
1891+
struct_span_err!(
1892+
self.r.session,
1893+
lifetime_ref.ident.span,
1894+
E0771,
1895+
"use of non-static lifetime `{}` in const generic",
1896+
lifetime_ref.ident
1897+
)
1898+
.note(
1899+
"for more information, see issue #74052 \
1900+
<https://github.com/rust-lang/rust/issues/74052>",
1901+
)
1902+
.emit();
1903+
}
18891904
}
18901905

18911906
impl<'tcx> LifetimeContext<'_, 'tcx> {
@@ -1982,24 +1997,6 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
19821997
}
19831998
}
19841999

1985-
// FIXME(const_generics): This patches over an ICE caused by non-'static lifetimes in const
1986-
// generics. We are disallowing this until we can decide on how we want to handle non-'static
1987-
// lifetimes in const generics. See issue #74052 for discussion.
1988-
crate fn emit_non_static_lt_in_const_generic_error(&self, lifetime_ref: &hir::Lifetime) {
1989-
let mut err = struct_span_err!(
1990-
self.tcx.sess,
1991-
lifetime_ref.span,
1992-
E0771,
1993-
"use of non-static lifetime `{}` in const generic",
1994-
lifetime_ref
1995-
);
1996-
err.note(
1997-
"for more information, see issue #74052 \
1998-
<https://github.com/rust-lang/rust/issues/74052>",
1999-
);
2000-
err.emit();
2001-
}
2002-
20032000
crate fn is_trait_ref_fn_scope(&mut self, trait_ref: &'tcx hir::PolyTraitRef<'tcx>) -> bool {
20042001
if let def::Res::Def(_, did) = trait_ref.trait_ref.path.res {
20052002
if [

compiler/rustc_resolve/src/late/lifetimes.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,6 @@ crate struct LifetimeContext<'a, 'tcx> {
164164
map: &'a mut NamedRegionMap,
165165
scope: ScopeRef<'a>,
166166

167-
is_in_const_generic: bool,
168-
169167
/// Indicates that we only care about the definition of a trait. This should
170168
/// be false if the `Item` we are resolving lifetimes for is not a trait or
171169
/// we eventually need lifetimes resolve for trait items.
@@ -452,7 +450,6 @@ fn do_resolve(
452450
tcx,
453451
map: &mut named_region_map,
454452
scope: ROOT_SCOPE,
455-
is_in_const_generic: false,
456453
trait_definition_only,
457454
labels_in_fn: vec![],
458455
xcrate_object_lifetime_defaults: Default::default(),
@@ -1266,10 +1263,6 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
12661263
self.insert_lifetime(lifetime_ref, Region::Static);
12671264
return;
12681265
}
1269-
if self.is_in_const_generic && lifetime_ref.name != LifetimeName::Error {
1270-
self.emit_non_static_lt_in_const_generic_error(lifetime_ref);
1271-
return;
1272-
}
12731266
self.resolve_lifetime_ref(lifetime_ref);
12741267
}
12751268

@@ -1341,14 +1334,11 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
13411334
}
13421335
}
13431336
GenericParamKind::Const { ref ty, default } => {
1344-
let was_in_const_generic = this.is_in_const_generic;
1345-
this.is_in_const_generic = true;
13461337
walk_list!(this, visit_param_bound, param.bounds);
13471338
this.visit_ty(&ty);
13481339
if let Some(default) = default {
13491340
this.visit_body(this.tcx.hir().body(default.body));
13501341
}
1351-
this.is_in_const_generic = was_in_const_generic;
13521342
}
13531343
}
13541344
}
@@ -1798,7 +1788,6 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
17981788
tcx: *tcx,
17991789
map,
18001790
scope: &wrap_scope,
1801-
is_in_const_generic: self.is_in_const_generic,
18021791
trait_definition_only: self.trait_definition_only,
18031792
labels_in_fn,
18041793
xcrate_object_lifetime_defaults,

0 commit comments

Comments
 (0)