Skip to content

Commit dea3949

Browse files
committed
Ban non-static in const generics in AST.
1 parent c7a6643 commit dea3949

File tree

3 files changed

+31
-33
lines changed

3 files changed

+31
-33
lines changed

compiler/rustc_resolve/src/late.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,11 @@ enum LifetimeRibKind {
222222
/// We passed through a function definition. Disallow in-band definitions.
223223
FnBody,
224224

225+
/// FIXME(const_generics): This patches over an ICE caused by non-'static lifetimes in const
226+
/// generics. We are disallowing this until we can decide on how we want to handle non-'static
227+
/// lifetimes in const generics. See issue #74052 for discussion.
228+
ConstGeneric,
229+
225230
/// For **Modern** cases, create a new anonymous region parameter
226231
/// and reference that.
227232
///
@@ -1095,16 +1100,18 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
10951100

10961101
this.ribs[TypeNS].push(Rib::new(ConstParamTyRibKind));
10971102
this.ribs[ValueNS].push(Rib::new(ConstParamTyRibKind));
1098-
this.visit_ty(ty);
1103+
this.with_lifetime_rib(LifetimeRibKind::ConstGeneric, |this| {
1104+
this.visit_ty(ty)
1105+
});
10991106
this.ribs[TypeNS].pop().unwrap();
11001107
this.ribs[ValueNS].pop().unwrap();
11011108

11021109
if let Some(ref expr) = default {
11031110
this.ribs[TypeNS].push(forward_ty_ban_rib);
11041111
this.ribs[ValueNS].push(forward_const_ban_rib);
1105-
this.lifetime_ribs.push(forward_lifetime_ban_rib);
1106-
this.visit_anon_const(expr);
1107-
forward_lifetime_ban_rib = this.lifetime_ribs.pop().unwrap();
1112+
this.with_lifetime_rib(LifetimeRibKind::ConstGeneric, |this| {
1113+
this.visit_anon_const(expr)
1114+
});
11081115
forward_const_ban_rib = this.ribs[ValueNS].pop().unwrap();
11091116
forward_ty_ban_rib = this.ribs[TypeNS].pop().unwrap();
11101117
}
@@ -1180,6 +1187,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
11801187
// Hijack `in_band_rib_index` to ensure we do not crate an in-band definition.
11811188
in_band_rib_index.get_or_insert((i, ident.span));
11821189
}
1190+
LifetimeRibKind::ConstGeneric => {
1191+
self.emit_non_static_lt_in_const_generic_error(lifetime);
1192+
self.r.lifetimes_res_map.insert(lifetime.id, LifetimeRes::Error);
1193+
return;
1194+
}
11831195
_ => {}
11841196
}
11851197
}

compiler/rustc_resolve/src/late/diagnostics.rs

+15-18
Original file line numberDiff line numberDiff line change
@@ -1867,6 +1867,21 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
18671867
}
18681868
err.emit();
18691869
}
1870+
1871+
crate fn emit_non_static_lt_in_const_generic_error(&self, lifetime_ref: &ast::Lifetime) {
1872+
struct_span_err!(
1873+
self.r.session,
1874+
lifetime_ref.ident.span,
1875+
E0771,
1876+
"use of non-static lifetime `{}` in const generic",
1877+
lifetime_ref.ident
1878+
)
1879+
.note(
1880+
"for more information, see issue #74052 \
1881+
<https://github.com/rust-lang/rust/issues/74052>",
1882+
)
1883+
.emit();
1884+
}
18701885
}
18711886

18721887
#[derive(Copy, Clone, PartialEq)]
@@ -1964,24 +1979,6 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
19641979
)
19651980
}
19661981

1967-
// FIXME(const_generics): This patches over an ICE caused by non-'static lifetimes in const
1968-
// generics. We are disallowing this until we can decide on how we want to handle non-'static
1969-
// lifetimes in const generics. See issue #74052 for discussion.
1970-
crate fn emit_non_static_lt_in_const_generic_error(&self, lifetime_ref: &hir::Lifetime) {
1971-
let mut err = struct_span_err!(
1972-
self.tcx.sess,
1973-
lifetime_ref.span,
1974-
E0771,
1975-
"use of non-static lifetime `{}` in const generic",
1976-
lifetime_ref
1977-
);
1978-
err.note(
1979-
"for more information, see issue #74052 \
1980-
<https://github.com/rust-lang/rust/issues/74052>",
1981-
);
1982-
err.emit();
1983-
}
1984-
19851982
crate fn is_trait_ref_fn_scope(&mut self, trait_ref: &'tcx hir::PolyTraitRef<'tcx>) -> bool {
19861983
if let def::Res::Def(_, did) = trait_ref.trait_ref.path.res {
19871984
if [

compiler/rustc_resolve/src/late/lifetimes.rs

-11
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,6 @@ crate struct LifetimeContext<'a, 'tcx> {
171171
/// Used to disallow the use of in-band lifetimes in `fn` or `Fn` syntax.
172172
is_in_fn_syntax: bool,
173173

174-
is_in_const_generic: bool,
175-
176174
/// Indicates that we only care about the definition of a trait. This should
177175
/// be false if the `Item` we are resolving lifetimes for is not a trait or
178176
/// we eventually need lifetimes resolve for trait items.
@@ -454,7 +452,6 @@ fn do_resolve(
454452
map: &mut named_region_map,
455453
scope: ROOT_SCOPE,
456454
is_in_fn_syntax: false,
457-
is_in_const_generic: false,
458455
trait_definition_only,
459456
xcrate_object_lifetime_defaults: Default::default(),
460457
lifetime_uses: &mut Default::default(),
@@ -1277,10 +1274,6 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
12771274
self.insert_lifetime(lifetime_ref, Region::Static);
12781275
return;
12791276
}
1280-
if self.is_in_const_generic && lifetime_ref.name != LifetimeName::Error {
1281-
self.emit_non_static_lt_in_const_generic_error(lifetime_ref);
1282-
return;
1283-
}
12841277
self.resolve_lifetime_ref(lifetime_ref);
12851278
}
12861279

@@ -1355,11 +1348,8 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
13551348
}
13561349
}
13571350
GenericParamKind::Const { ref ty, .. } => {
1358-
let was_in_const_generic = this.is_in_const_generic;
1359-
this.is_in_const_generic = true;
13601351
walk_list!(this, visit_param_bound, param.bounds);
13611352
this.visit_ty(&ty);
1362-
this.is_in_const_generic = was_in_const_generic;
13631353
}
13641354
}
13651355
}
@@ -1696,7 +1686,6 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
16961686
map,
16971687
scope: &wrap_scope,
16981688
is_in_fn_syntax: self.is_in_fn_syntax,
1699-
is_in_const_generic: self.is_in_const_generic,
17001689
trait_definition_only: self.trait_definition_only,
17011690
xcrate_object_lifetime_defaults,
17021691
lifetime_uses,

0 commit comments

Comments
 (0)