Skip to content

Commit ec4fc72

Browse files
authored
Rollup merge of #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 #44524) Closes #44524
2 parents 03c8ffa + 9386ea9 commit ec4fc72

File tree

71 files changed

+99
-1538
lines changed

Some content is hidden

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

71 files changed

+99
-1538
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>,
@@ -379,7 +375,7 @@ pub fn lower_crate<'a, 'hir>(
379375
task_context: None,
380376
current_item: None,
381377
lifetimes_to_define: Vec::new(),
382-
is_collecting_in_band_lifetimes: false,
378+
is_collecting_anonymous_lifetimes: false,
383379
in_scope_lifetimes: Vec::new(),
384380
allow_try_trait: Some([sym::try_trait_v2][..].into()),
385381
allow_gen_future: Some([sym::gen_future][..].into()),
@@ -726,13 +722,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
726722
&mut self,
727723
f: impl FnOnce(&mut Self) -> T,
728724
) -> (Vec<(Span, ParamName)>, T) {
729-
let was_collecting = std::mem::replace(&mut self.is_collecting_in_band_lifetimes, true);
725+
let was_collecting = std::mem::replace(&mut self.is_collecting_anonymous_lifetimes, true);
730726
let len = self.lifetimes_to_define.len();
731727

732728
let res = f(self);
733729

734730
let lifetimes_to_define = self.lifetimes_to_define.split_off(len);
735-
self.is_collecting_in_band_lifetimes = was_collecting;
731+
self.is_collecting_anonymous_lifetimes = was_collecting;
736732
(lifetimes_to_define, res)
737733
}
738734

@@ -749,7 +745,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
749745
// that collisions are ok here and this shouldn't
750746
// really show up for end-user.
751747
let (str_name, kind) = match hir_name {
752-
ParamName::Plain(ident) => (ident.name, hir::LifetimeParamKind::InBand),
748+
ParamName::Plain(ident) => (ident.name, hir::LifetimeParamKind::Explicit),
753749
ParamName::Fresh(_) => (kw::UnderscoreLifetime, hir::LifetimeParamKind::Elided),
754750
ParamName::Error => (kw::UnderscoreLifetime, hir::LifetimeParamKind::Error),
755751
};
@@ -773,38 +769,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
773769
}
774770
}
775771

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

@@ -1957,7 +1925,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19571925
AnonymousLifetimeMode::ReportError => self.new_error_lifetime(Some(l.id), span),
19581926
},
19591927
ident => {
1960-
self.maybe_collect_in_band_lifetime(ident);
19611928
let param_name = ParamName::Plain(self.lower_ident(ident));
19621929
self.new_named_lifetime(l.id, span, hir::LifetimeName::Param(param_name))
19631930
}
@@ -2001,8 +1968,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20011968

20021969
let (name, kind) = match param.kind {
20031970
GenericParamKind::Lifetime => {
2004-
let was_collecting_in_band = self.is_collecting_in_band_lifetimes;
2005-
self.is_collecting_in_band_lifetimes = false;
1971+
let was_collecting_in_band = self.is_collecting_anonymous_lifetimes;
1972+
self.is_collecting_anonymous_lifetimes = false;
20061973

20071974
let lt = self
20081975
.with_anonymous_lifetime_mode(AnonymousLifetimeMode::ReportError, |this| {
@@ -2025,7 +1992,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20251992
let kind =
20261993
hir::GenericParamKind::Lifetime { kind: hir::LifetimeParamKind::Explicit };
20271994

2028-
self.is_collecting_in_band_lifetimes = was_collecting_in_band;
1995+
self.is_collecting_anonymous_lifetimes = was_collecting_in_band;
20291996

20301997
(param_name, kind)
20311998
}
@@ -2384,7 +2351,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23842351
// Hence `impl Foo for &u32` becomes `impl<'f> Foo for &'f u32` for some fresh
23852352
// `'f`.
23862353
AnonymousLifetimeMode::CreateParameter => {
2387-
let fresh_name = self.collect_fresh_in_band_lifetime(span);
2354+
let fresh_name = self.collect_fresh_anonymous_lifetime(span);
23882355
hir::Lifetime {
23892356
hir_id: self.next_id(),
23902357
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
@@ -471,11 +471,6 @@ pub enum LifetimeParamKind {
471471
// `fn foo<'a>(x: &'a u8) -> &'a u8 { x }`).
472472
Explicit,
473473

474-
// Indicates that the lifetime definition was synthetically added
475-
// as a result of an in-band lifetime usage (e.g., in
476-
// `fn foo(x: &'a u8) -> &'a u8 { x }`).
477-
InBand,
478-
479474
// Indication that the lifetime was elided (e.g., in both cases in
480475
// `fn foo(x: &u8) -> &'_ u8 { x }`).
481476
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
@@ -1838,7 +1838,6 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
18381838
lifetime_ref
18391839
);
18401840
err.span_label(lifetime_ref.span, "undeclared lifetime");
1841-
let mut suggests_in_band = false;
18421841
let mut suggested_spans = vec![];
18431842
for missing in &self.missing_named_lifetime_spots {
18441843
match missing {
@@ -1854,7 +1853,6 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
18541853
}) {
18551854
(param.span.shrink_to_lo(), format!("{}, ", lifetime_ref))
18561855
} else {
1857-
suggests_in_band = true;
18581856
(generics.span, format!("<{}>", lifetime_ref))
18591857
};
18601858
if suggested_spans.contains(&span) {
@@ -1889,15 +1887,6 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
18891887
_ => {}
18901888
}
18911889
}
1892-
if self.tcx.sess.is_nightly_build()
1893-
&& !self.tcx.features().in_band_lifetimes
1894-
&& suggests_in_band
1895-
{
1896-
err.help(
1897-
"if you want to experiment with in-band lifetime bindings, \
1898-
add `#![feature(in_band_lifetimes)]` to the crate attributes",
1899-
);
1900-
}
19011890
err.emit();
19021891
}
19031892

0 commit comments

Comments
 (0)