Skip to content

Commit f901df3

Browse files
committed
Handle TAIT.
1 parent f385f85 commit f901df3

File tree

2 files changed

+21
-96
lines changed

2 files changed

+21
-96
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{Arena, FnDeclKind};
55
use rustc_ast::ptr::P;
66
use rustc_ast::visit::AssocCtxt;
77
use rustc_ast::*;
8-
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
8+
use rustc_data_structures::fx::FxHashMap;
99
use rustc_data_structures::sorted_map::SortedMap;
1010
use rustc_errors::struct_span_err;
1111
use rustc_hir as hir;
@@ -340,12 +340,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
340340
//
341341
// type Foo = Foo1
342342
// opaque type Foo1: Trait
343-
let ty = self.lower_ty(
344-
ty,
345-
ImplTraitContext::TypeAliasesOpaqueTy {
346-
capturable_lifetimes: &mut FxHashSet::default(),
347-
},
348-
);
343+
let ty = self.lower_ty(ty, ImplTraitContext::TypeAliasesOpaqueTy);
349344
let mut generics = generics.clone();
350345
add_ty_alias_where_clause(&mut generics, where_clauses, true);
351346
let generics = self.lower_generics(
@@ -982,12 +977,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
982977
hir::ImplItemKind::TyAlias(ty)
983978
}
984979
Some(ty) => {
985-
let ty = self.lower_ty(
986-
ty,
987-
ImplTraitContext::TypeAliasesOpaqueTy {
988-
capturable_lifetimes: &mut FxHashSet::default(),
989-
},
990-
);
980+
let ty = self.lower_ty(ty, ImplTraitContext::TypeAliasesOpaqueTy);
991981
hir::ImplItemKind::TyAlias(ty)
992982
}
993983
};

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 18 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -269,29 +269,11 @@ enum ImplTraitContext<'b, 'a> {
269269
/// equivalent to a new opaque type like `type T = impl Debug; fn foo() -> T`.
270270
///
271271
ReturnPositionOpaqueTy {
272-
/// `DefId` for the parent function, used to look up necessary
273-
/// information later.
274-
fn_def_id: LocalDefId,
275272
/// Origin: Either OpaqueTyOrigin::FnReturn or OpaqueTyOrigin::AsyncFn,
276273
origin: hir::OpaqueTyOrigin,
277274
},
278275
/// Impl trait in type aliases.
279-
TypeAliasesOpaqueTy {
280-
/// Set of lifetimes that this opaque type can capture, if it uses
281-
/// them. This includes lifetimes bound since we entered this context.
282-
/// For example:
283-
///
284-
/// ```
285-
/// type A<'b> = impl for<'a> Trait<'a, Out = impl Sized + 'a>;
286-
/// ```
287-
///
288-
/// Here the inner opaque type captures `'a` because it uses it. It doesn't
289-
/// need to capture `'b` because it already inherits the lifetime
290-
/// parameter from `A`.
291-
// FIXME(impl_trait): but `required_region_bounds` will ICE later
292-
// anyway.
293-
capturable_lifetimes: &'b mut FxHashSet<hir::ParamName>,
294-
},
276+
TypeAliasesOpaqueTy,
295277
/// `impl Trait` is not accepted in this position.
296278
Disallowed(ImplTraitPosition),
297279
}
@@ -325,12 +307,8 @@ impl<'a> ImplTraitContext<'_, 'a> {
325307
use self::ImplTraitContext::*;
326308
match self {
327309
Universal(params, parent) => Universal(params, *parent),
328-
ReturnPositionOpaqueTy { fn_def_id, origin } => {
329-
ReturnPositionOpaqueTy { fn_def_id: *fn_def_id, origin: *origin }
330-
}
331-
TypeAliasesOpaqueTy { capturable_lifetimes } => {
332-
TypeAliasesOpaqueTy { capturable_lifetimes }
333-
}
310+
ReturnPositionOpaqueTy { origin } => ReturnPositionOpaqueTy { origin: *origin },
311+
TypeAliasesOpaqueTy => TypeAliasesOpaqueTy,
334312
Disallowed(pos) => Disallowed(*pos),
335313
}
336314
}
@@ -1011,7 +989,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1011989
hir::TypeBindingKind::Equality { term }
1012990
}
1013991
AssocConstraintKind::Bound { ref bounds } => {
1014-
let mut capturable_lifetimes;
1015992
let mut parent_def_id = self.current_hir_id_owner;
1016993
// Piggy-back on the `impl Trait` context to figure out the correct behavior.
1017994
let (desugar_to_impl_trait, itctx) = match itctx {
@@ -1044,13 +1021,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10441021
//
10451022
// FIXME: this is only needed until `impl Trait` is allowed in type aliases.
10461023
ImplTraitContext::Disallowed(_) if self.is_in_dyn_type => {
1047-
capturable_lifetimes = FxHashSet::default();
1048-
(
1049-
true,
1050-
ImplTraitContext::TypeAliasesOpaqueTy {
1051-
capturable_lifetimes: &mut capturable_lifetimes,
1052-
},
1053-
)
1024+
(true, ImplTraitContext::TypeAliasesOpaqueTy)
10541025
}
10551026

10561027
// We are in the parameter position, but not within a dyn type:
@@ -1309,28 +1280,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13091280
TyKind::ImplTrait(def_node_id, ref bounds) => {
13101281
let span = t.span;
13111282
match itctx {
1312-
ImplTraitContext::ReturnPositionOpaqueTy { fn_def_id, origin } => self
1313-
.lower_opaque_impl_trait(
1314-
span,
1315-
Some(fn_def_id),
1316-
origin,
1317-
def_node_id,
1318-
None,
1319-
|this| this.lower_param_bounds(bounds, itctx),
1320-
),
1321-
ImplTraitContext::TypeAliasesOpaqueTy { ref capturable_lifetimes } => {
1322-
// Reset capturable lifetimes, any nested impl trait
1323-
// types will inherit lifetimes from this opaque type,
1324-
// so don't need to capture them again.
1325-
let nested_itctx = ImplTraitContext::TypeAliasesOpaqueTy {
1326-
capturable_lifetimes: &mut FxHashSet::default(),
1327-
};
1283+
ImplTraitContext::ReturnPositionOpaqueTy { origin } => self
1284+
.lower_opaque_impl_trait(span, origin, def_node_id, |this| {
1285+
this.lower_param_bounds(bounds, itctx)
1286+
}),
1287+
ImplTraitContext::TypeAliasesOpaqueTy => {
1288+
let nested_itctx = ImplTraitContext::TypeAliasesOpaqueTy;
13281289
self.lower_opaque_impl_trait(
13291290
span,
1330-
None,
13311291
hir::OpaqueTyOrigin::TyAlias,
13321292
def_node_id,
1333-
Some(capturable_lifetimes),
13341293
|this| this.lower_param_bounds(bounds, nested_itctx),
13351294
)
13361295
}
@@ -1392,10 +1351,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13921351
fn lower_opaque_impl_trait(
13931352
&mut self,
13941353
span: Span,
1395-
fn_def_id: Option<LocalDefId>,
13961354
origin: hir::OpaqueTyOrigin,
13971355
opaque_ty_node_id: NodeId,
1398-
capturable_lifetimes: Option<&FxHashSet<hir::ParamName>>,
13991356
lower_bounds: impl FnOnce(&mut Self) -> hir::GenericBounds<'hir>,
14001357
) -> hir::TyKind<'hir> {
14011358
// Make sure we know that some funky desugaring has been going on here.
@@ -1409,19 +1366,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14091366

14101367
let mut collected_lifetimes = FxHashMap::default();
14111368
self.with_hir_id_owner(opaque_ty_node_id, |lctx| {
1412-
let lifetime_stash = std::mem::replace(
1413-
&mut lctx.captured_lifetimes,
1414-
Some((opaque_ty_def_id, FxHashMap::default(), FxHashSet::default())),
1415-
);
1369+
let capture_framework = if origin == hir::OpaqueTyOrigin::TyAlias {
1370+
None
1371+
} else {
1372+
Some((opaque_ty_def_id, FxHashMap::default(), FxHashSet::default()))
1373+
};
1374+
let lifetime_stash = std::mem::replace(&mut lctx.captured_lifetimes, capture_framework);
14161375
let hir_bounds = lower_bounds(lctx);
1417-
collected_lifetimes =
1418-
std::mem::replace(&mut lctx.captured_lifetimes, lifetime_stash).unwrap().1;
1419-
1420-
if let Some(capturable_lifetimes) = capturable_lifetimes {
1421-
collected_lifetimes.retain(|_, (_, _, p_name, _)| {
1422-
capturable_lifetimes.contains(&p_name.normalize_to_macros_2_0())
1423-
});
1424-
}
1376+
collected_lifetimes = std::mem::replace(&mut lctx.captured_lifetimes, lifetime_stash)
1377+
.map_or_else(FxHashMap::default, |c| c.1);
14251378
debug!(?collected_lifetimes);
14261379

14271380
let lifetime_defs = lctx.arena.alloc_from_iter(collected_lifetimes.iter().map(
@@ -1586,7 +1539,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15861539
Some((node_id, _)) if kind.impl_trait_return_allowed() => {
15871540
let fn_def_id = self.resolver.local_def_id(node_id);
15881541
ImplTraitContext::ReturnPositionOpaqueTy {
1589-
fn_def_id,
15901542
origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id),
15911543
}
15921544
}
@@ -1867,7 +1819,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18671819
// `impl Future` opaque type that `async fn` implicitly
18681820
// generates.
18691821
let context = ImplTraitContext::ReturnPositionOpaqueTy {
1870-
fn_def_id,
18711822
origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id),
18721823
};
18731824
self.lower_ty(ty, context)
@@ -2118,28 +2069,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21182069
self.lower_generic_params(&p.bound_generic_params, itctx.reborrow());
21192070

21202071
let trait_ref = self.with_in_scope_lifetime_defs(&p.bound_generic_params, |this| {
2121-
// Any impl Trait types defined within this scope can capture
2122-
// lifetimes bound on this predicate.
2123-
let lt_def_names = p.bound_generic_params.iter().filter_map(|param| match param.kind {
2124-
GenericParamKind::Lifetime { .. } => {
2125-
Some(ParamName::Plain(param.ident.normalize_to_macros_2_0()))
2126-
}
2127-
_ => None,
2128-
});
2129-
if let ImplTraitContext::TypeAliasesOpaqueTy { ref mut capturable_lifetimes } = itctx {
2130-
capturable_lifetimes.extend(lt_def_names.clone());
2131-
}
21322072
if let Some((_, _, binders)) = &mut this.captured_lifetimes {
21332073
binders.insert(p.trait_ref.ref_id);
21342074
}
21352075

21362076
let trait_ref = this.lower_trait_ref(&p.trait_ref, itctx.reborrow());
21372077

2138-
if let ImplTraitContext::TypeAliasesOpaqueTy { ref mut capturable_lifetimes } = itctx {
2139-
for param in lt_def_names {
2140-
capturable_lifetimes.remove(&param);
2141-
}
2142-
}
21432078
if let Some((_, _, binders)) = &mut this.captured_lifetimes {
21442079
binders.remove(&p.trait_ref.ref_id);
21452080
}

0 commit comments

Comments
 (0)