Skip to content

Commit 07abde9

Browse files
authored
Rollup merge of rust-lang#93845 - compiler-errors:in-band-lifetimes, r=cjgillot
Remove in band lifetimes As discussed in t-lang backlog bonanza, the `in_band_lifetimes` FCP closed in favor for the feature not being stabilized. This PR removes `#![feature(in_band_lifetimes)]` in its entirety. Let me know if this PR is too hasty, and if we should instead do something intermediate for deprecate the feature first. r? `@scottmcm` (or feel free to reassign, just saw your last comment on rust-lang#44524) Closes rust-lang#44524
2 parents 3d127e2 + 43dbd83 commit 07abde9

File tree

70 files changed

+98
-1537
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+98
-1537
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+12-45
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,9 @@ struct LoweringContext<'a, 'hir: 'a> {
142142
/// indicate whether or not we're in a place where new lifetimes will result
143143
/// in in-band lifetime definitions, such a function or an impl header,
144144
/// including implicit lifetimes from `impl_header_lifetime_elision`.
145-
is_collecting_in_band_lifetimes: bool,
145+
is_collecting_anonymous_lifetimes: bool,
146146

147147
/// Currently in-scope lifetimes defined in impl headers, fn headers, or HRTB.
148-
/// When `is_collecting_in_band_lifetimes` is true, each lifetime is checked
149-
/// against this list to see if it is already in-scope, or if a definition
150-
/// needs to be created for it.
151-
///
152148
/// We always store a `normalize_to_macros_2_0()` version of the param-name in this
153149
/// vector.
154150
in_scope_lifetimes: Vec<ParamName>,
@@ -377,7 +373,7 @@ pub fn lower_crate<'a, 'hir>(
377373
task_context: None,
378374
current_item: None,
379375
lifetimes_to_define: Vec::new(),
380-
is_collecting_in_band_lifetimes: false,
376+
is_collecting_anonymous_lifetimes: false,
381377
in_scope_lifetimes: Vec::new(),
382378
allow_try_trait: Some([sym::try_trait_v2][..].into()),
383379
allow_gen_future: Some([sym::gen_future][..].into()),
@@ -724,13 +720,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
724720
&mut self,
725721
f: impl FnOnce(&mut Self) -> T,
726722
) -> (Vec<(Span, ParamName)>, T) {
727-
let was_collecting = std::mem::replace(&mut self.is_collecting_in_band_lifetimes, true);
723+
let was_collecting = std::mem::replace(&mut self.is_collecting_anonymous_lifetimes, true);
728724
let len = self.lifetimes_to_define.len();
729725

730726
let res = f(self);
731727

732728
let lifetimes_to_define = self.lifetimes_to_define.split_off(len);
733-
self.is_collecting_in_band_lifetimes = was_collecting;
729+
self.is_collecting_anonymous_lifetimes = was_collecting;
734730
(lifetimes_to_define, res)
735731
}
736732

@@ -747,7 +743,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
747743
// that collisions are ok here and this shouldn't
748744
// really show up for end-user.
749745
let (str_name, kind) = match hir_name {
750-
ParamName::Plain(ident) => (ident.name, hir::LifetimeParamKind::InBand),
746+
ParamName::Plain(ident) => (ident.name, hir::LifetimeParamKind::Explicit),
751747
ParamName::Fresh(_) => (kw::UnderscoreLifetime, hir::LifetimeParamKind::Elided),
752748
ParamName::Error => (kw::UnderscoreLifetime, hir::LifetimeParamKind::Error),
753749
};
@@ -771,38 +767,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
771767
}
772768
}
773769

774-
/// When there is a reference to some lifetime `'a`, and in-band
775-
/// lifetimes are enabled, then we want to push that lifetime into
776-
/// the vector of names to define later. In that case, it will get
777-
/// added to the appropriate generics.
778-
fn maybe_collect_in_band_lifetime(&mut self, ident: Ident) {
779-
if !self.is_collecting_in_band_lifetimes {
780-
return;
781-
}
782-
783-
if !self.sess.features_untracked().in_band_lifetimes {
784-
return;
785-
}
786-
787-
if self.in_scope_lifetimes.contains(&ParamName::Plain(ident.normalize_to_macros_2_0())) {
788-
return;
789-
}
790-
791-
let hir_name = ParamName::Plain(ident);
792-
793-
if self.lifetimes_to_define.iter().any(|(_, lt_name)| {
794-
lt_name.normalize_to_macros_2_0() == hir_name.normalize_to_macros_2_0()
795-
}) {
796-
return;
797-
}
798-
799-
self.lifetimes_to_define.push((ident.span, hir_name));
800-
}
801-
802770
/// When we have either an elided or `'_` lifetime in an impl
803771
/// header, we convert it to an in-band lifetime.
804-
fn collect_fresh_in_band_lifetime(&mut self, span: Span) -> ParamName {
805-
assert!(self.is_collecting_in_band_lifetimes);
772+
fn collect_fresh_anonymous_lifetime(&mut self, span: Span) -> ParamName {
773+
assert!(self.is_collecting_anonymous_lifetimes);
806774
let index = self.lifetimes_to_define.len() + self.in_scope_lifetimes.len();
807775
let hir_name = ParamName::Fresh(index);
808776
self.lifetimes_to_define.push((span, hir_name));
@@ -1944,7 +1912,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19441912
}
19451913
ident if ident.name == kw::UnderscoreLifetime => match self.anonymous_lifetime_mode {
19461914
AnonymousLifetimeMode::CreateParameter => {
1947-
let fresh_name = self.collect_fresh_in_band_lifetime(span);
1915+
let fresh_name = self.collect_fresh_anonymous_lifetime(span);
19481916
self.new_named_lifetime(l.id, span, hir::LifetimeName::Param(fresh_name))
19491917
}
19501918

@@ -1955,7 +1923,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19551923
AnonymousLifetimeMode::ReportError => self.new_error_lifetime(Some(l.id), span),
19561924
},
19571925
ident => {
1958-
self.maybe_collect_in_band_lifetime(ident);
19591926
let param_name = ParamName::Plain(self.lower_ident(ident));
19601927
self.new_named_lifetime(l.id, span, hir::LifetimeName::Param(param_name))
19611928
}
@@ -1999,8 +1966,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19991966

20001967
let (name, kind) = match param.kind {
20011968
GenericParamKind::Lifetime => {
2002-
let was_collecting_in_band = self.is_collecting_in_band_lifetimes;
2003-
self.is_collecting_in_band_lifetimes = false;
1969+
let was_collecting_in_band = self.is_collecting_anonymous_lifetimes;
1970+
self.is_collecting_anonymous_lifetimes = false;
20041971

20051972
let lt = self
20061973
.with_anonymous_lifetime_mode(AnonymousLifetimeMode::ReportError, |this| {
@@ -2023,7 +1990,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20231990
let kind =
20241991
hir::GenericParamKind::Lifetime { kind: hir::LifetimeParamKind::Explicit };
20251992

2026-
self.is_collecting_in_band_lifetimes = was_collecting_in_band;
1993+
self.is_collecting_anonymous_lifetimes = was_collecting_in_band;
20271994

20281995
(param_name, kind)
20291996
}
@@ -2382,7 +2349,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23822349
// Hence `impl Foo for &u32` becomes `impl<'f> Foo for &'f u32` for some fresh
23832350
// `'f`.
23842351
AnonymousLifetimeMode::CreateParameter => {
2385-
let fresh_name = self.collect_fresh_in_band_lifetime(span);
2352+
let fresh_name = self.collect_fresh_anonymous_lifetime(span);
23862353
hir::Lifetime {
23872354
hir_id: self.next_id(),
23882355
span: self.lower_span(span),

compiler/rustc_error_codes/src/error_codes/E0687.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
#### Note: this error code is no longer emitted by the compiler.
2+
13
In-band lifetimes cannot be used in `fn`/`Fn` syntax.
24

35
Erroneous code examples:
46

5-
```compile_fail,E0687
7+
```ignore (feature got removed)
68
#![feature(in_band_lifetimes)]
79
810
fn foo(x: fn(&'a u32)) {} // error!

compiler/rustc_error_codes/src/error_codes/E0688.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
#### Note: this error code is no longer emitted by the compiler.
2+
13
In-band lifetimes were mixed with explicit lifetime binders.
24

35
Erroneous code example:
46

5-
```compile_fail,E0688
7+
```ignore (feature got removed)
68
#![feature(in_band_lifetimes)]
79
810
fn foo<'a>(x: &'a u32, y: &'b u32) {} // error!

compiler/rustc_feature/src/active.rs

-2
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,6 @@ declare_features! (
400400
(active, if_let_guard, "1.47.0", Some(51114), None),
401401
/// Allows using imported `main` function
402402
(active, imported_main, "1.53.0", Some(28937), None),
403-
/// Allows in-band quantification of lifetime bindings (e.g., `fn foo(x: &'a u8) -> &'a u8`).
404-
(active, in_band_lifetimes, "1.23.0", Some(44524), None),
405403
/// Allows inferring `'static` outlives requirements (RFC 2093).
406404
(active, infer_static_outlives_requirements, "1.26.0", Some(54185), None),
407405
/// Allows associated types in inherent impls.

compiler/rustc_feature/src/removed.rs

+3
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ declare_features! (
104104
(removed, impl_trait_in_bindings, "1.55.0", Some(63065), None,
105105
Some("the implementation was not maintainable, the feature may get reintroduced once the current refactorings are done")),
106106
(removed, import_shadowing, "1.0.0", None, None, None),
107+
/// Allows in-band quantification of lifetime bindings (e.g., `fn foo(x: &'a u8) -> &'a u8`).
108+
(removed, in_band_lifetimes, "1.23.0", Some(44524), None,
109+
Some("removed due to unsolved ergonomic questions and added lifetime resolution complexity")),
107110
/// Lazily evaluate constants. This allows constants to depend on type parameters.
108111
(removed, lazy_normalization_consts, "1.46.0", Some(72219), None, Some("superseded by `generic_const_exprs`")),
109112
/// Allows using the `#[link_args]` attribute.

compiler/rustc_hir/src/hir.rs

-5
Original file line numberDiff line numberDiff line change
@@ -470,11 +470,6 @@ pub enum LifetimeParamKind {
470470
// `fn foo<'a>(x: &'a u8) -> &'a u8 { x }`).
471471
Explicit,
472472

473-
// Indicates that the lifetime definition was synthetically added
474-
// as a result of an in-band lifetime usage (e.g., in
475-
// `fn foo(x: &'a u8) -> &'a u8 { x }`).
476-
InBand,
477-
478473
// Indication that the lifetime was elided (e.g., in both cases in
479474
// `fn foo(x: &u8) -> &'_ u8 { x }`).
480475
Elided,

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/find_anon_type.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl<'tcx> Visitor<'tcx> for FindNestedTypeVisitor<'tcx> {
125125
// Find the index of the named region that was part of the
126126
// error. We will then search the function parameters for a bound
127127
// region at the right depth with the same index
128-
(Some(rl::Region::EarlyBound(_, id, _)), ty::BrNamed(def_id, _)) => {
128+
(Some(rl::Region::EarlyBound(_, id)), ty::BrNamed(def_id, _)) => {
129129
debug!("EarlyBound id={:?} def_id={:?}", id, def_id);
130130
if id == def_id {
131131
self.found_type = Some(arg);
@@ -137,7 +137,7 @@ impl<'tcx> Visitor<'tcx> for FindNestedTypeVisitor<'tcx> {
137137
// error. We will then search the function parameters for a bound
138138
// region at the right depth with the same index
139139
(
140-
Some(rl::Region::LateBound(debruijn_index, _, id, _)),
140+
Some(rl::Region::LateBound(debruijn_index, _, id)),
141141
ty::BrNamed(def_id, _),
142142
) => {
143143
debug!(
@@ -155,8 +155,8 @@ impl<'tcx> Visitor<'tcx> for FindNestedTypeVisitor<'tcx> {
155155
Some(
156156
rl::Region::Static
157157
| rl::Region::Free(_, _)
158-
| rl::Region::EarlyBound(_, _, _)
159-
| rl::Region::LateBound(_, _, _, _)
158+
| rl::Region::EarlyBound(_, _)
159+
| rl::Region::LateBound(_, _, _)
160160
| rl::Region::LateBoundAnon(_, _, _),
161161
)
162162
| None,
@@ -221,15 +221,15 @@ impl<'tcx> Visitor<'tcx> for TyPathVisitor<'tcx> {
221221
}
222222
}
223223

224-
(Some(rl::Region::EarlyBound(_, id, _)), ty::BrNamed(def_id, _)) => {
224+
(Some(rl::Region::EarlyBound(_, id)), ty::BrNamed(def_id, _)) => {
225225
debug!("EarlyBound id={:?} def_id={:?}", id, def_id);
226226
if id == def_id {
227227
self.found_it = true;
228228
return; // we can stop visiting now
229229
}
230230
}
231231

232-
(Some(rl::Region::LateBound(debruijn_index, _, id, _)), ty::BrNamed(def_id, _)) => {
232+
(Some(rl::Region::LateBound(debruijn_index, _, id)), ty::BrNamed(def_id, _)) => {
233233
debug!("FindNestedTypeVisitor::visit_ty: LateBound depth = {:?}", debruijn_index,);
234234
debug!("id={:?}", id);
235235
debug!("def_id={:?}", def_id);
@@ -242,8 +242,8 @@ impl<'tcx> Visitor<'tcx> for TyPathVisitor<'tcx> {
242242
(
243243
Some(
244244
rl::Region::Static
245-
| rl::Region::EarlyBound(_, _, _)
246-
| rl::Region::LateBound(_, _, _, _)
245+
| rl::Region::EarlyBound(_, _)
246+
| rl::Region::LateBound(_, _, _)
247247
| rl::Region::LateBoundAnon(_, _, _)
248248
| rl::Region::Free(_, _),
249249
)

compiler/rustc_middle/src/middle/resolve_lifetime.rs

+3-36
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,14 @@ use crate::ty;
44

55
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
66
use rustc_hir::def_id::{DefId, LocalDefId};
7-
use rustc_hir::{GenericParam, ItemLocalId};
8-
use rustc_hir::{GenericParamKind, LifetimeParamKind};
7+
use rustc_hir::ItemLocalId;
98
use rustc_macros::HashStable;
109

11-
/// The origin of a named lifetime definition.
12-
///
13-
/// This is used to prevent the usage of in-band lifetimes in `Fn`/`fn` syntax.
14-
#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Debug, HashStable)]
15-
pub enum LifetimeDefOrigin {
16-
// Explicit binders like `fn foo<'a>(x: &'a u8)` or elided like `impl Foo<&u32>`
17-
ExplicitOrElided,
18-
// In-band declarations like `fn foo(x: &'a u8)`
19-
InBand,
20-
// Some kind of erroneous origin
21-
Error,
22-
}
23-
24-
impl LifetimeDefOrigin {
25-
pub fn from_param(param: &GenericParam<'_>) -> Self {
26-
match param.kind {
27-
GenericParamKind::Lifetime { kind } => match kind {
28-
LifetimeParamKind::InBand => LifetimeDefOrigin::InBand,
29-
LifetimeParamKind::Explicit => LifetimeDefOrigin::ExplicitOrElided,
30-
LifetimeParamKind::Elided => LifetimeDefOrigin::ExplicitOrElided,
31-
LifetimeParamKind::Error => LifetimeDefOrigin::Error,
32-
},
33-
_ => bug!("expected a lifetime param"),
34-
}
35-
}
36-
}
37-
3810
#[derive(Clone, Copy, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Debug, HashStable)]
3911
pub enum Region {
4012
Static,
41-
EarlyBound(/* index */ u32, /* lifetime decl */ DefId, LifetimeDefOrigin),
42-
LateBound(
43-
ty::DebruijnIndex,
44-
/* late-bound index */ u32,
45-
/* lifetime decl */ DefId,
46-
LifetimeDefOrigin,
47-
),
13+
EarlyBound(/* index */ u32, /* lifetime decl */ DefId),
14+
LateBound(ty::DebruijnIndex, /* late-bound index */ u32, /* lifetime decl */ DefId),
4815
LateBoundAnon(ty::DebruijnIndex, /* late-bound index */ u32, /* anon index */ u32),
4916
Free(DefId, /* lifetime decl */ DefId),
5017
}

compiler/rustc_resolve/src/late/diagnostics.rs

-11
Original file line numberDiff line numberDiff line change
@@ -1844,7 +1844,6 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
18441844
lifetime_ref
18451845
);
18461846
err.span_label(lifetime_ref.span, "undeclared lifetime");
1847-
let mut suggests_in_band = false;
18481847
let mut suggested_spans = vec![];
18491848
for missing in &self.missing_named_lifetime_spots {
18501849
match missing {
@@ -1860,7 +1859,6 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
18601859
}) {
18611860
(param.span.shrink_to_lo(), format!("{}, ", lifetime_ref))
18621861
} else {
1863-
suggests_in_band = true;
18641862
(generics.span, format!("<{}>", lifetime_ref))
18651863
};
18661864
if suggested_spans.contains(&span) {
@@ -1895,15 +1893,6 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
18951893
_ => {}
18961894
}
18971895
}
1898-
if self.tcx.sess.is_nightly_build()
1899-
&& !self.tcx.features().in_band_lifetimes
1900-
&& suggests_in_band
1901-
{
1902-
err.help(
1903-
"if you want to experiment with in-band lifetime bindings, \
1904-
add `#![feature(in_band_lifetimes)]` to the crate attributes",
1905-
);
1906-
}
19071896
err.emit();
19081897
}
19091898

0 commit comments

Comments
 (0)