Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 9 pull requests #94331

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c00f635
Remove in-band lifetimes
compiler-errors Feb 10, 2022
43dbd83
Remove LifetimeDefOrigin
compiler-errors Feb 20, 2022
8ca47d7
Stop manually SIMDing in swap_nonoverlapping
scottmcm Feb 21, 2022
da896d3
Improve CheckCfg internal representation
Urgau Feb 19, 2022
fbe1c15
Add test for well known names defined by rustc
Urgau Feb 19, 2022
3d23477
Improve diagnostic of the unexpected_cfgs lint
Urgau Feb 19, 2022
8d3de56
Continue improvements on the --check-cfg implementation
Urgau Feb 20, 2022
a556a2a
Add compiler flag `--check-cfg` to the unstable book
Urgau Feb 21, 2022
c73a2f8
properly handle fat pointers to uninhabitable types
compiler-errors Feb 23, 2022
f047af2
Normalize main return type during mono item collection & codegen
tmiasko Feb 23, 2022
37d9ea7
Improve `scan_escape`.
nnethercote Feb 24, 2022
44308dc
Inline a hot closure in `from_lit_token`.
nnethercote Feb 24, 2022
70018c1
update auto trait lint
lcnr Feb 24, 2022
34319ff
Avoid emitting full macro body into JSON
Mark-Simulacrum Feb 24, 2022
8ba7436
better ObligationCause for normalization errors in can_type_implement…
compiler-errors Feb 6, 2022
ee98dc8
restore spans for issue-50480
compiler-errors Feb 6, 2022
44de8c0
Rollup merge of #93714 - compiler-errors:can-type-impl-copy-error-spa…
Dylan-DPC Feb 24, 2022
b33098f
Rollup merge of #93845 - compiler-errors:in-band-lifetimes, r=cjgillot
Dylan-DPC Feb 24, 2022
92b5d1d
Rollup merge of #94175 - Urgau:check-cfg-improvements, r=petrochenkov
Dylan-DPC Feb 24, 2022
87f826d
Rollup merge of #94212 - scottmcm:swapper, r=dtolnay
Dylan-DPC Feb 24, 2022
2da2737
Rollup merge of #94242 - compiler-errors:fat-uninhabitable-pointer, r…
Dylan-DPC Feb 24, 2022
c20ed90
Rollup merge of #94308 - tmiasko:normalize-main-ret-ty, r=oli-obk
Dylan-DPC Feb 24, 2022
d4db2be
Rollup merge of #94315 - lcnr:auto-trait-lint-update, r=oli-obk
Dylan-DPC Feb 24, 2022
25cc094
Rollup merge of #94316 - nnethercote:improve-string-literal-unescapin…
Dylan-DPC Feb 24, 2022
39d8195
Rollup merge of #94327 - Mark-Simulacrum:avoid-macro-sp, r=petrochenkov
Dylan-DPC Feb 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Remove in-band lifetimes
  • Loading branch information
compiler-errors committed Feb 20, 2022
commit c00f63541e42fb6c4ac4efccbf70a4566f3e209b
57 changes: 12 additions & 45 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,9 @@ struct LoweringContext<'a, 'hir: 'a> {
/// indicate whether or not we're in a place where new lifetimes will result
/// in in-band lifetime definitions, such a function or an impl header,
/// including implicit lifetimes from `impl_header_lifetime_elision`.
is_collecting_in_band_lifetimes: bool,
is_collecting_anonymous_lifetimes: bool,

/// Currently in-scope lifetimes defined in impl headers, fn headers, or HRTB.
/// When `is_collecting_in_band_lifetimes` is true, each lifetime is checked
/// against this list to see if it is already in-scope, or if a definition
/// needs to be created for it.
///
/// We always store a `normalize_to_macros_2_0()` version of the param-name in this
/// vector.
in_scope_lifetimes: Vec<ParamName>,
Expand Down Expand Up @@ -374,7 +370,7 @@ pub fn lower_crate<'a, 'hir>(
task_context: None,
current_item: None,
lifetimes_to_define: Vec::new(),
is_collecting_in_band_lifetimes: false,
is_collecting_anonymous_lifetimes: false,
in_scope_lifetimes: Vec::new(),
allow_try_trait: Some([sym::try_trait_v2][..].into()),
allow_gen_future: Some([sym::gen_future][..].into()),
Expand Down Expand Up @@ -718,13 +714,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
&mut self,
f: impl FnOnce(&mut Self) -> T,
) -> (Vec<(Span, ParamName)>, T) {
let was_collecting = std::mem::replace(&mut self.is_collecting_in_band_lifetimes, true);
let was_collecting = std::mem::replace(&mut self.is_collecting_anonymous_lifetimes, true);
let len = self.lifetimes_to_define.len();

let res = f(self);

let lifetimes_to_define = self.lifetimes_to_define.split_off(len);
self.is_collecting_in_band_lifetimes = was_collecting;
self.is_collecting_anonymous_lifetimes = was_collecting;
(lifetimes_to_define, res)
}

Expand All @@ -741,7 +737,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// that collisions are ok here and this shouldn't
// really show up for end-user.
let (str_name, kind) = match hir_name {
ParamName::Plain(ident) => (ident.name, hir::LifetimeParamKind::InBand),
ParamName::Plain(ident) => (ident.name, hir::LifetimeParamKind::Explicit),
ParamName::Fresh(_) => (kw::UnderscoreLifetime, hir::LifetimeParamKind::Elided),
ParamName::Error => (kw::UnderscoreLifetime, hir::LifetimeParamKind::Error),
};
Expand All @@ -765,38 +761,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}
}

/// When there is a reference to some lifetime `'a`, and in-band
/// lifetimes are enabled, then we want to push that lifetime into
/// the vector of names to define later. In that case, it will get
/// added to the appropriate generics.
fn maybe_collect_in_band_lifetime(&mut self, ident: Ident) {
if !self.is_collecting_in_band_lifetimes {
return;
}

if !self.sess.features_untracked().in_band_lifetimes {
return;
}

if self.in_scope_lifetimes.contains(&ParamName::Plain(ident.normalize_to_macros_2_0())) {
return;
}

let hir_name = ParamName::Plain(ident);

if self.lifetimes_to_define.iter().any(|(_, lt_name)| {
lt_name.normalize_to_macros_2_0() == hir_name.normalize_to_macros_2_0()
}) {
return;
}

self.lifetimes_to_define.push((ident.span, hir_name));
}

/// When we have either an elided or `'_` lifetime in an impl
/// header, we convert it to an in-band lifetime.
fn collect_fresh_in_band_lifetime(&mut self, span: Span) -> ParamName {
assert!(self.is_collecting_in_band_lifetimes);
fn collect_fresh_anonymous_lifetime(&mut self, span: Span) -> ParamName {
assert!(self.is_collecting_anonymous_lifetimes);
let index = self.lifetimes_to_define.len() + self.in_scope_lifetimes.len();
let hir_name = ParamName::Fresh(index);
self.lifetimes_to_define.push((span, hir_name));
Expand Down Expand Up @@ -1938,7 +1906,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}
ident if ident.name == kw::UnderscoreLifetime => match self.anonymous_lifetime_mode {
AnonymousLifetimeMode::CreateParameter => {
let fresh_name = self.collect_fresh_in_band_lifetime(span);
let fresh_name = self.collect_fresh_anonymous_lifetime(span);
self.new_named_lifetime(l.id, span, hir::LifetimeName::Param(fresh_name))
}

Expand All @@ -1949,7 +1917,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
AnonymousLifetimeMode::ReportError => self.new_error_lifetime(Some(l.id), span),
},
ident => {
self.maybe_collect_in_band_lifetime(ident);
let param_name = ParamName::Plain(self.lower_ident(ident));
self.new_named_lifetime(l.id, span, hir::LifetimeName::Param(param_name))
}
Expand Down Expand Up @@ -1993,8 +1960,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {

let (name, kind) = match param.kind {
GenericParamKind::Lifetime => {
let was_collecting_in_band = self.is_collecting_in_band_lifetimes;
self.is_collecting_in_band_lifetimes = false;
let was_collecting_in_band = self.is_collecting_anonymous_lifetimes;
self.is_collecting_anonymous_lifetimes = false;

let lt = self
.with_anonymous_lifetime_mode(AnonymousLifetimeMode::ReportError, |this| {
Expand All @@ -2017,7 +1984,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let kind =
hir::GenericParamKind::Lifetime { kind: hir::LifetimeParamKind::Explicit };

self.is_collecting_in_band_lifetimes = was_collecting_in_band;
self.is_collecting_anonymous_lifetimes = was_collecting_in_band;

(param_name, kind)
}
Expand Down Expand Up @@ -2376,7 +2343,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// Hence `impl Foo for &u32` becomes `impl<'f> Foo for &'f u32` for some fresh
// `'f`.
AnonymousLifetimeMode::CreateParameter => {
let fresh_name = self.collect_fresh_in_band_lifetime(span);
let fresh_name = self.collect_fresh_anonymous_lifetime(span);
hir::Lifetime {
hir_id: self.next_id(),
span: self.lower_span(span),
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_error_codes/src/error_codes/E0687.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#### Note: this error code is no longer emitted by the compiler.

In-band lifetimes cannot be used in `fn`/`Fn` syntax.

Erroneous code examples:

```compile_fail,E0687
```ignore (feature got removed)
#![feature(in_band_lifetimes)]

fn foo(x: fn(&'a u32)) {} // error!
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_error_codes/src/error_codes/E0688.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#### Note: this error code is no longer emitted by the compiler.

In-band lifetimes were mixed with explicit lifetime binders.

Erroneous code example:

```compile_fail,E0688
```ignore (feature got removed)
#![feature(in_band_lifetimes)]

fn foo<'a>(x: &'a u32, y: &'b u32) {} // error!
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,6 @@ declare_features! (
(active, if_let_guard, "1.47.0", Some(51114), None),
/// Allows using imported `main` function
(active, imported_main, "1.53.0", Some(28937), None),
/// Allows in-band quantification of lifetime bindings (e.g., `fn foo(x: &'a u8) -> &'a u8`).
(active, in_band_lifetimes, "1.23.0", Some(44524), None),
/// Allows inferring `'static` outlives requirements (RFC 2093).
(active, infer_static_outlives_requirements, "1.26.0", Some(54185), None),
/// Allows associated types in inherent impls.
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_feature/src/removed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ declare_features! (
(removed, impl_trait_in_bindings, "1.55.0", Some(63065), None,
Some("the implementation was not maintainable, the feature may get reintroduced once the current refactorings are done")),
(removed, import_shadowing, "1.0.0", None, None, None),
/// Allows in-band quantification of lifetime bindings (e.g., `fn foo(x: &'a u8) -> &'a u8`).
(removed, in_band_lifetimes, "1.23.0", Some(44524), None,
Some("removed due to unsolved ergonomic questions and added lifetime resolution complexity")),
/// Lazily evaluate constants. This allows constants to depend on type parameters.
(removed, lazy_normalization_consts, "1.46.0", Some(72219), None, Some("superseded by `generic_const_exprs`")),
/// Allows using the `#[link_args]` attribute.
Expand Down
5 changes: 0 additions & 5 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,11 +470,6 @@ pub enum LifetimeParamKind {
// `fn foo<'a>(x: &'a u8) -> &'a u8 { x }`).
Explicit,

// Indicates that the lifetime definition was synthetically added
// as a result of an in-band lifetime usage (e.g., in
// `fn foo(x: &'a u8) -> &'a u8 { x }`).
InBand,

// Indication that the lifetime was elided (e.g., in both cases in
// `fn foo(x: &u8) -> &'_ u8 { x }`).
Elided,
Expand Down
3 changes: 0 additions & 3 deletions compiler/rustc_middle/src/middle/resolve_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ use rustc_macros::HashStable;
pub enum LifetimeDefOrigin {
// Explicit binders like `fn foo<'a>(x: &'a u8)` or elided like `impl Foo<&u32>`
ExplicitOrElided,
// In-band declarations like `fn foo(x: &'a u8)`
InBand,
// Some kind of erroneous origin
Error,
}
Expand All @@ -25,7 +23,6 @@ impl LifetimeDefOrigin {
pub fn from_param(param: &GenericParam<'_>) -> Self {
match param.kind {
GenericParamKind::Lifetime { kind } => match kind {
LifetimeParamKind::InBand => LifetimeDefOrigin::InBand,
LifetimeParamKind::Explicit => LifetimeDefOrigin::ExplicitOrElided,
LifetimeParamKind::Elided => LifetimeDefOrigin::ExplicitOrElided,
LifetimeParamKind::Error => LifetimeDefOrigin::Error,
Expand Down
11 changes: 0 additions & 11 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1844,7 +1844,6 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
lifetime_ref
);
err.span_label(lifetime_ref.span, "undeclared lifetime");
let mut suggests_in_band = false;
let mut suggested_spans = vec![];
for missing in &self.missing_named_lifetime_spots {
match missing {
Expand All @@ -1860,7 +1859,6 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
}) {
(param.span.shrink_to_lo(), format!("{}, ", lifetime_ref))
} else {
suggests_in_band = true;
(generics.span, format!("<{}>", lifetime_ref))
};
if suggested_spans.contains(&span) {
Expand Down Expand Up @@ -1895,15 +1893,6 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
_ => {}
}
}
if self.tcx.sess.is_nightly_build()
&& !self.tcx.features().in_band_lifetimes
&& suggests_in_band
{
err.help(
"if you want to experiment with in-band lifetime bindings, \
add `#![feature(in_band_lifetimes)]` to the crate attributes",
);
}
err.emit();
}

Expand Down
70 changes: 2 additions & 68 deletions compiler/rustc_resolve/src/late/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc_hir::def_id::{DefIdMap, LocalDefId};
use rustc_hir::hir_id::ItemLocalId;
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{GenericArg, GenericParam, LifetimeName, Node, ParamName, QPath};
use rustc_hir::{GenericParamKind, HirIdMap, HirIdSet, LifetimeParamKind};
use rustc_hir::{GenericParamKind, HirIdMap, HirIdSet};
use rustc_middle::hir::map::Map;
use rustc_middle::hir::nested_filter;
use rustc_middle::middle::resolve_lifetime::*;
Expand Down Expand Up @@ -1325,9 +1325,6 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
}

fn visit_generics(&mut self, generics: &'tcx hir::Generics<'tcx>) {
if !self.trait_definition_only {
check_mixed_explicit_and_in_band_defs(self.tcx, &generics.params);
}
let scope = Scope::TraitRefBoundary { s: self.scope };
self.with(scope, |_, this| {
for param in generics.params {
Expand Down Expand Up @@ -1535,30 +1532,6 @@ impl ShadowKind {
}
}

fn check_mixed_explicit_and_in_band_defs(tcx: TyCtxt<'_>, params: &[hir::GenericParam<'_>]) {
let lifetime_params: Vec<_> = params
.iter()
.filter_map(|param| match param.kind {
GenericParamKind::Lifetime { kind, .. } => Some((kind, param.span)),
_ => None,
})
.collect();
let explicit = lifetime_params.iter().find(|(kind, _)| *kind == LifetimeParamKind::Explicit);
let in_band = lifetime_params.iter().find(|(kind, _)| *kind == LifetimeParamKind::InBand);

if let (Some((_, explicit_span)), Some((_, in_band_span))) = (explicit, in_band) {
struct_span_err!(
tcx.sess,
*in_band_span,
E0688,
"cannot mix in-band and explicit lifetime definitions"
)
.span_label(*in_band_span, "in-band lifetime definition here")
.span_label(*explicit_span, "explicit lifetime definition here")
.emit();
}
}

fn signal_shadowing_problem(tcx: TyCtxt<'_>, name: Symbol, orig: Original, shadower: Shadower) {
let mut err = if let (ShadowKind::Lifetime, ShadowKind::Lifetime) = (orig.kind, shadower.kind) {
// lifetime/lifetime shadowing is an error
Expand Down Expand Up @@ -1845,13 +1818,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
fn lifetime_deletion_span(&self, name: Ident, generics: &hir::Generics<'_>) -> Option<Span> {
generics.params.iter().enumerate().find_map(|(i, param)| {
if param.name.ident() == name {
let in_band = matches!(
param.kind,
hir::GenericParamKind::Lifetime { kind: hir::LifetimeParamKind::InBand }
);
if in_band {
Some(param.span)
} else if generics.params.len() == 1 {
if generics.params.len() == 1 {
// if sole lifetime, remove the entire `<>` brackets
Some(generics.span)
} else {
Expand Down Expand Up @@ -2337,39 +2304,6 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
}
}

// Check for fn-syntax conflicts with in-band lifetime definitions
if !self.trait_definition_only && self.is_in_fn_syntax {
match def {
Region::EarlyBound(_, _, LifetimeDefOrigin::InBand)
| Region::LateBound(_, _, _, LifetimeDefOrigin::InBand) => {
struct_span_err!(
self.tcx.sess,
lifetime_ref.span,
E0687,
"lifetimes used in `fn` or `Fn` syntax must be \
explicitly declared using `<...>` binders"
)
.span_label(lifetime_ref.span, "in-band lifetime definition")
.emit();
}

Region::Static
| Region::EarlyBound(
_,
_,
LifetimeDefOrigin::ExplicitOrElided | LifetimeDefOrigin::Error,
)
| Region::LateBound(
_,
_,
_,
LifetimeDefOrigin::ExplicitOrElided | LifetimeDefOrigin::Error,
)
| Region::LateBoundAnon(..)
| Region::Free(..) => {}
}
}

self.insert_lifetime(lifetime_ref, def);
} else {
self.emit_undeclared_lifetime_error(lifetime_ref);
Expand Down
11 changes: 0 additions & 11 deletions src/test/ui/async-await/generics-and-bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// edition:2018
// compile-flags: --crate-type lib

#![feature(in_band_lifetimes)]

use std::future::Future;

pub async fn simple_generic<T>() {}
Expand Down Expand Up @@ -73,10 +71,6 @@ pub fn call_with_ref_block<'a>(f: &'a (impl Foo + 'a)) -> impl Future<Output = (
async move { f.foo() }
}

pub fn call_with_ref_block_in_band(f: &'a (impl Foo + 'a)) -> impl Future<Output = ()> + 'a {
async move { f.foo() }
}

pub fn async_block_with_same_generic_params_unifies() {
let mut a = call_generic_bound_block(FooType);
a = call_generic_bound_block(FooType);
Expand All @@ -91,9 +85,4 @@ pub fn async_block_with_same_generic_params_unifies() {
let f_two = FooType;
let mut d = call_with_ref_block(&f_one);
d = call_with_ref_block(&f_two);

let f_one = FooType;
let f_two = FooType;
let mut d = call_with_ref_block_in_band(&f_one);
d = call_with_ref_block_in_band(&f_two);
}
4 changes: 0 additions & 4 deletions src/test/ui/error-codes/E0261.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ LL | fn foo(x: &'a str) { }
| - ^^ undeclared lifetime
| |
| help: consider introducing lifetime `'a` here: `<'a>`
|
= help: if you want to experiment with in-band lifetime bindings, add `#![feature(in_band_lifetimes)]` to the crate attributes

error[E0261]: use of undeclared lifetime name `'a`
--> $DIR/E0261.rs:5:9
Expand All @@ -15,8 +13,6 @@ LL | struct Foo {
| - help: consider introducing lifetime `'a` here: `<'a>`
LL | x: &'a str,
| ^^ undeclared lifetime
|
= help: if you want to experiment with in-band lifetime bindings, add `#![feature(in_band_lifetimes)]` to the crate attributes

error: aborting due to 2 previous errors

Expand Down
Loading