Skip to content

Commit 4dcaf90

Browse files
Rollup merge of #51979 - oli-obk:lowering_cleanups4, r=nikomatsakis
Get rid of `TyImplTraitExistential` cc @eddyb r? @nikomatsakis
2 parents 55caead + 75a6fde commit 4dcaf90

File tree

10 files changed

+144
-177
lines changed

10 files changed

+144
-177
lines changed

src/librustc/hir/intravisit.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -607,13 +607,6 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
607607
}
608608
visitor.visit_lifetime(lifetime);
609609
}
610-
TyImplTraitExistential(_, def_id, ref lifetimes) => {
611-
// we are not recursing into the `existential` item, because it is already being visited
612-
// as part of the surrounding module. The `NodeId` just exists so we don't have to look
613-
// it up everywhere else in the compiler
614-
visitor.visit_def_mention(Def::Existential(def_id));
615-
walk_list!(visitor, visit_lifetime, lifetimes);
616-
}
617610
TyTypeof(ref expression) => {
618611
visitor.visit_anon_const(expression)
619612
}

src/librustc/hir/lowering.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,13 +1307,20 @@ impl<'a> LoweringContext<'a> {
13071307
lctx.items.insert(exist_ty_id.node_id, exist_ty_item);
13081308

13091309
// `impl Trait` now just becomes `Foo<'a, 'b, ..>`
1310-
hir::TyImplTraitExistential(
1311-
hir::ItemId {
1312-
id: exist_ty_id.node_id
1313-
},
1314-
DefId::local(exist_ty_def_index),
1315-
lifetimes,
1316-
)
1310+
let path = P(hir::Path {
1311+
span: exist_ty_span,
1312+
def: Def::Existential(DefId::local(exist_ty_def_index)),
1313+
segments: hir_vec![hir::PathSegment {
1314+
infer_types: false,
1315+
ident: Ident::new(keywords::Invalid.name(), exist_ty_span),
1316+
args: Some(P(hir::GenericArgs {
1317+
parenthesized: false,
1318+
bindings: HirVec::new(),
1319+
args: lifetimes,
1320+
}))
1321+
}],
1322+
});
1323+
hir::TyPath(hir::QPath::Resolved(None, path))
13171324
})
13181325
}
13191326

@@ -1322,7 +1329,7 @@ impl<'a> LoweringContext<'a> {
13221329
exist_ty_id: NodeId,
13231330
parent_index: DefIndex,
13241331
bounds: &hir::GenericBounds,
1325-
) -> (HirVec<hir::Lifetime>, HirVec<hir::GenericParam>) {
1332+
) -> (HirVec<hir::GenericArg>, HirVec<hir::GenericParam>) {
13261333
// This visitor walks over impl trait bounds and creates defs for all lifetimes which
13271334
// appear in the bounds, excluding lifetimes that are created within the bounds.
13281335
// e.g. 'a, 'b, but not 'c in `impl for<'c> SomeTrait<'a, 'b, 'c>`
@@ -1333,7 +1340,7 @@ impl<'a> LoweringContext<'a> {
13331340
collect_elided_lifetimes: bool,
13341341
currently_bound_lifetimes: Vec<hir::LifetimeName>,
13351342
already_defined_lifetimes: HashSet<hir::LifetimeName>,
1336-
output_lifetimes: Vec<hir::Lifetime>,
1343+
output_lifetimes: Vec<hir::GenericArg>,
13371344
output_lifetime_params: Vec<hir::GenericParam>,
13381345
}
13391346

@@ -1417,11 +1424,11 @@ impl<'a> LoweringContext<'a> {
14171424
&& !self.already_defined_lifetimes.contains(&name) {
14181425
self.already_defined_lifetimes.insert(name);
14191426

1420-
self.output_lifetimes.push(hir::Lifetime {
1427+
self.output_lifetimes.push(hir::GenericArg::Lifetime(hir::Lifetime {
14211428
id: self.context.next_id().node_id,
14221429
span: lifetime.span,
14231430
name,
1424-
});
1431+
}));
14251432

14261433
// We need to manually create the ids here, because the
14271434
// definitions will go into the explicit `existential type`

src/librustc/hir/mod.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,18 +1692,6 @@ pub enum Ty_ {
16921692
/// A trait object type `Bound1 + Bound2 + Bound3`
16931693
/// where `Bound` is a trait or a lifetime.
16941694
TyTraitObject(HirVec<PolyTraitRef>, Lifetime),
1695-
/// An existentially quantified (there exists a type satisfying) `impl
1696-
/// Bound1 + Bound2 + Bound3` type where `Bound` is a trait or a lifetime.
1697-
///
1698-
/// The `Item` is the generated
1699-
/// `existential type Foo<'a, 'b>: MyTrait<'a, 'b>;`.
1700-
///
1701-
/// The `HirVec<Lifetime>` is the list of lifetimes applied as parameters
1702-
/// to the `abstract type`, e.g. the `'c` and `'d` in `-> Foo<'c, 'd>`.
1703-
/// This list is only a list of lifetimes and not type parameters
1704-
/// because all in-scope type parameters are captured by `impl Trait`,
1705-
/// so they are resolved directly through the parent `Generics`.
1706-
TyImplTraitExistential(ItemId, DefId, HirVec<Lifetime>),
17071695
/// Unused for now
17081696
TyTypeof(AnonConst),
17091697
/// TyInfer means the type should be inferred instead of it having been

src/librustc/hir/print.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -420,15 +420,6 @@ impl<'a> State<'a> {
420420
self.print_lifetime(lifetime)?;
421421
}
422422
}
423-
hir::TyImplTraitExistential(hir_id, _def_id, ref _lifetimes) => {
424-
match self.ann.try_fetch_item(hir_id.id).map(|it| &it.node) {
425-
None => self.word_space("impl {{Trait}}")?,
426-
Some(&hir::ItemExistential(ref exist_ty)) => {
427-
self.print_bounds("impl", &exist_ty.bounds)?;
428-
},
429-
other => bug!("impl Trait pointed to {:#?}", other),
430-
}
431-
}
432423
hir::TyArray(ref ty, ref length) => {
433424
self.s.word("[")?;
434425
self.print_type(&ty)?;

src/librustc/ich/impls_hir.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ impl_stable_hash_for!(enum hir::Ty_ {
340340
TyTup(ts),
341341
TyPath(qpath),
342342
TyTraitObject(trait_refs, lifetime),
343-
TyImplTraitExistential(existty, def_id, lifetimes),
344343
TyTypeof(body_id),
345344
TyErr,
346345
TyInfer

src/librustc/middle/resolve_lifetime.rs

Lines changed: 100 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -625,122 +625,131 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
625625
};
626626
self.with(scope, |_, this| this.visit_ty(&mt.ty));
627627
}
628-
hir::TyImplTraitExistential(item_id, _, ref lifetimes) => {
629-
// Resolve the lifetimes that are applied to the existential type.
630-
// These are resolved in the current scope.
631-
// `fn foo<'a>() -> impl MyTrait<'a> { ... }` desugars to
632-
// `fn foo<'a>() -> MyAnonTy<'a> { ... }`
633-
// ^ ^this gets resolved in the current scope
634-
for lifetime in lifetimes {
635-
self.visit_lifetime(lifetime);
636-
637-
// Check for predicates like `impl for<'a> SomeTrait<impl OtherTrait<'a>>`
638-
// and ban them. Type variables instantiated inside binders aren't
639-
// well-supported at the moment, so this doesn't work.
640-
// In the future, this should be fixed and this error should be removed.
641-
let def = self.map.defs.get(&lifetime.id).cloned();
642-
if let Some(Region::LateBound(_, def_id, _)) = def {
643-
if let Some(node_id) = self.tcx.hir.as_local_node_id(def_id) {
644-
// Ensure that the parent of the def is an item, not HRTB
645-
let parent_id = self.tcx.hir.get_parent_node(node_id);
646-
let parent_impl_id = hir::ImplItemId { node_id: parent_id };
647-
let parent_trait_id = hir::TraitItemId { node_id: parent_id };
648-
let krate = self.tcx.hir.forest.krate();
649-
if !(krate.items.contains_key(&parent_id)
650-
|| krate.impl_items.contains_key(&parent_impl_id)
651-
|| krate.trait_items.contains_key(&parent_trait_id))
652-
{
653-
span_err!(
654-
self.tcx.sess,
655-
lifetime.span,
656-
E0657,
657-
"`impl Trait` can only capture lifetimes \
658-
bound at the fn or impl level"
659-
);
660-
self.uninsert_lifetime_on_error(lifetime, def.unwrap());
628+
hir::TyPath(hir::QPath::Resolved(None, ref path)) => {
629+
if let Def::Existential(exist_ty_did) = path.def {
630+
assert!(exist_ty_did.is_local());
631+
// Resolve the lifetimes that are applied to the existential type.
632+
// These are resolved in the current scope.
633+
// `fn foo<'a>() -> impl MyTrait<'a> { ... }` desugars to
634+
// `fn foo<'a>() -> MyAnonTy<'a> { ... }`
635+
// ^ ^this gets resolved in the current scope
636+
for lifetime in &path.segments[0].args.as_ref().unwrap().args {
637+
if let hir::GenericArg::Lifetime(lifetime) = lifetime {
638+
self.visit_lifetime(lifetime);
639+
640+
// Check for predicates like `impl for<'a> Trait<impl OtherTrait<'a>>`
641+
// and ban them. Type variables instantiated inside binders aren't
642+
// well-supported at the moment, so this doesn't work.
643+
// In the future, this should be fixed and this error should be removed.
644+
let def = self.map.defs.get(&lifetime.id).cloned();
645+
if let Some(Region::LateBound(_, def_id, _)) = def {
646+
if let Some(node_id) = self.tcx.hir.as_local_node_id(def_id) {
647+
// Ensure that the parent of the def is an item, not HRTB
648+
let parent_id = self.tcx.hir.get_parent_node(node_id);
649+
let parent_impl_id = hir::ImplItemId { node_id: parent_id };
650+
let parent_trait_id = hir::TraitItemId { node_id: parent_id };
651+
let krate = self.tcx.hir.forest.krate();
652+
if !(krate.items.contains_key(&parent_id)
653+
|| krate.impl_items.contains_key(&parent_impl_id)
654+
|| krate.trait_items.contains_key(&parent_trait_id))
655+
{
656+
span_err!(
657+
self.tcx.sess,
658+
lifetime.span,
659+
E0657,
660+
"`impl Trait` can only capture lifetimes \
661+
bound at the fn or impl level"
662+
);
663+
self.uninsert_lifetime_on_error(lifetime, def.unwrap());
664+
}
665+
}
661666
}
662667
}
663668
}
664-
}
665669

666-
// Resolve the lifetimes in the bounds to the lifetime defs in the generics.
667-
// `fn foo<'a>() -> impl MyTrait<'a> { ... }` desugars to
668-
// `abstract type MyAnonTy<'b>: MyTrait<'b>;`
669-
// ^ ^ this gets resolved in the scope of
670-
// the exist_ty generics
671-
let (generics, bounds) = match self.tcx.hir.expect_item(item_id.id).node {
672-
hir::ItemExistential(hir::ExistTy{ ref generics, ref bounds, .. }) => (
673-
generics,
674-
bounds,
675-
),
676-
ref i => bug!("impl Trait pointed to non-existential type?? {:#?}", i),
677-
};
670+
let id = self.tcx.hir.as_local_node_id(exist_ty_did).unwrap();
671+
672+
// Resolve the lifetimes in the bounds to the lifetime defs in the generics.
673+
// `fn foo<'a>() -> impl MyTrait<'a> { ... }` desugars to
674+
// `abstract type MyAnonTy<'b>: MyTrait<'b>;`
675+
// ^ ^ this gets resolved in the scope of
676+
// the exist_ty generics
677+
let (generics, bounds) = match self.tcx.hir.expect_item(id).node {
678+
hir::ItemExistential(hir::ExistTy{ ref generics, ref bounds, .. }) => (
679+
generics,
680+
bounds,
681+
),
682+
ref i => bug!("impl Trait pointed to non-existential type?? {:#?}", i),
683+
};
678684

679-
// We want to start our early-bound indices at the end of the parent scope,
680-
// not including any parent `impl Trait`s.
681-
let mut index = self.next_early_index_for_abstract_type();
682-
debug!("visit_ty: index = {}", index);
685+
// We want to start our early-bound indices at the end of the parent scope,
686+
// not including any parent `impl Trait`s.
687+
let mut index = self.next_early_index_for_abstract_type();
688+
debug!("visit_ty: index = {}", index);
683689

684-
let mut elision = None;
685-
let mut lifetimes = FxHashMap();
686-
let mut type_count = 0;
687-
for param in &generics.params {
688-
match param.kind {
689-
GenericParamKind::Lifetime { .. } => {
690-
let (name, reg) = Region::early(&self.tcx.hir, &mut index, &param);
691-
if let hir::ParamName::Plain(param_name) = name {
692-
if param_name.name == keywords::UnderscoreLifetime.name() {
693-
// Pick the elided lifetime "definition" if one exists
694-
// and use it to make an elision scope.
695-
elision = Some(reg);
690+
let mut elision = None;
691+
let mut lifetimes = FxHashMap();
692+
let mut type_count = 0;
693+
for param in &generics.params {
694+
match param.kind {
695+
GenericParamKind::Lifetime { .. } => {
696+
let (name, reg) = Region::early(&self.tcx.hir, &mut index, &param);
697+
if let hir::ParamName::Plain(param_name) = name {
698+
if param_name.name == keywords::UnderscoreLifetime.name() {
699+
// Pick the elided lifetime "definition" if one exists
700+
// and use it to make an elision scope.
701+
elision = Some(reg);
702+
} else {
703+
lifetimes.insert(name, reg);
704+
}
696705
} else {
697706
lifetimes.insert(name, reg);
698707
}
699-
} else {
700-
lifetimes.insert(name, reg);
701708
}
702-
}
703-
GenericParamKind::Type { .. } => {
704-
type_count += 1;
709+
GenericParamKind::Type { .. } => {
710+
type_count += 1;
711+
}
705712
}
706713
}
707-
}
708-
let next_early_index = index + type_count;
714+
let next_early_index = index + type_count;
709715

710-
if let Some(elision_region) = elision {
711-
let scope = Scope::Elision {
712-
elide: Elide::Exact(elision_region),
713-
s: self.scope,
714-
};
715-
self.with(scope, |_old_scope, this| {
716+
if let Some(elision_region) = elision {
717+
let scope = Scope::Elision {
718+
elide: Elide::Exact(elision_region),
719+
s: self.scope,
720+
};
721+
self.with(scope, |_old_scope, this| {
722+
let scope = Scope::Binder {
723+
lifetimes,
724+
next_early_index,
725+
s: this.scope,
726+
track_lifetime_uses: true,
727+
abstract_type_parent: false,
728+
};
729+
this.with(scope, |_old_scope, this| {
730+
this.visit_generics(generics);
731+
for bound in bounds {
732+
this.visit_param_bound(bound);
733+
}
734+
});
735+
});
736+
} else {
716737
let scope = Scope::Binder {
717738
lifetimes,
718739
next_early_index,
719-
s: this.scope,
740+
s: self.scope,
720741
track_lifetime_uses: true,
721742
abstract_type_parent: false,
722743
};
723-
this.with(scope, |_old_scope, this| {
744+
self.with(scope, |_old_scope, this| {
724745
this.visit_generics(generics);
725746
for bound in bounds {
726747
this.visit_param_bound(bound);
727748
}
728749
});
729-
});
750+
}
730751
} else {
731-
let scope = Scope::Binder {
732-
lifetimes,
733-
next_early_index,
734-
s: self.scope,
735-
track_lifetime_uses: true,
736-
abstract_type_parent: false,
737-
};
738-
self.with(scope, |_old_scope, this| {
739-
this.visit_generics(generics);
740-
for bound in bounds {
741-
this.visit_param_bound(bound);
742-
}
743-
});
752+
intravisit::walk_ty(self, ty)
744753
}
745754
}
746755
_ => intravisit::walk_ty(self, ty),

0 commit comments

Comments
 (0)