Skip to content

Commit 4e49e67

Browse files
committed
Stop special casing top level TAIT
1 parent d77e861 commit 4e49e67

Some content is hidden

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

44 files changed

+323
-424
lines changed

src/librustc_ast/ast.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,15 +1858,6 @@ impl TyKind {
18581858
pub fn is_unit(&self) -> bool {
18591859
if let TyKind::Tup(ref tys) = *self { tys.is_empty() } else { false }
18601860
}
1861-
1862-
/// HACK(type_alias_impl_trait, Centril): A temporary crutch used
1863-
/// in lowering to avoid making larger changes there and beyond.
1864-
pub fn opaque_top_hack(&self) -> Option<&GenericBounds> {
1865-
match self {
1866-
Self::ImplTrait(_, bounds) => Some(bounds),
1867-
_ => None,
1868-
}
1869-
}
18701861
}
18711862

18721863
/// Syntax used to declare a trait object.

src/librustc_ast_lowering/item.rs

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::{AnonymousLifetimeMode, LoweringContext, ParamMode};
2-
use super::{ImplTraitContext, ImplTraitPosition, ImplTraitTypeIdVisitor};
2+
use super::{ImplTraitContext, ImplTraitPosition};
33
use crate::Arena;
44

55
use rustc_ast::ast::*;
@@ -165,13 +165,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
165165
}
166166
ItemKind::MacroDef(..) => SmallVec::new(),
167167
ItemKind::Fn(..) | ItemKind::Impl { of_trait: None, .. } => smallvec![i.id],
168-
ItemKind::Static(ref ty, ..) | ItemKind::Const(_, ref ty, ..) => {
169-
let mut ids = smallvec![i.id];
170-
if self.sess.features_untracked().impl_trait_in_bindings {
171-
ImplTraitTypeIdVisitor { ids: &mut ids }.visit_ty(ty);
172-
}
173-
ids
174-
}
175168
_ => smallvec![i.id],
176169
};
177170

@@ -292,23 +285,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
292285
ItemKind::Mod(ref m) => hir::ItemKind::Mod(self.lower_mod(m)),
293286
ItemKind::ForeignMod(ref nm) => hir::ItemKind::ForeignMod(self.lower_foreign_mod(nm)),
294287
ItemKind::GlobalAsm(ref ga) => hir::ItemKind::GlobalAsm(self.lower_global_asm(ga)),
295-
ItemKind::TyAlias(_, ref gen, _, Some(ref ty)) => match ty.kind.opaque_top_hack() {
296-
None => {
297-
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
298-
let generics = self.lower_generics(gen, ImplTraitContext::disallowed());
299-
hir::ItemKind::TyAlias(ty, generics)
300-
}
301-
Some(bounds) => {
302-
let ctx = || ImplTraitContext::OpaqueTy(None, hir::OpaqueTyOrigin::Misc);
303-
let ty = hir::OpaqueTy {
304-
generics: self.lower_generics(gen, ctx()),
305-
bounds: self.lower_param_bounds(bounds, ctx()),
306-
impl_trait_fn: None,
307-
origin: hir::OpaqueTyOrigin::TypeAlias,
308-
};
309-
hir::ItemKind::OpaqueTy(ty)
310-
}
311-
},
288+
ItemKind::TyAlias(_, ref gen, _, Some(ref ty)) => {
289+
let ty =
290+
self.lower_ty(ty, ImplTraitContext::OpaqueTy(None, hir::OpaqueTyOrigin::Misc));
291+
let generics = self.lower_generics(gen, ImplTraitContext::disallowed());
292+
hir::ItemKind::TyAlias(ty, generics)
293+
}
312294
ItemKind::TyAlias(_, ref generics, _, None) => {
313295
let ty = self.arena.alloc(self.ty(span, hir::TyKind::Err));
314296
let generics = self.lower_generics(generics, ImplTraitContext::disallowed());
@@ -844,16 +826,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
844826
let ty = self.arena.alloc(self.ty(i.span, hir::TyKind::Err));
845827
hir::ImplItemKind::TyAlias(ty)
846828
}
847-
Some(ty) => match ty.kind.opaque_top_hack() {
848-
None => {
849-
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
850-
hir::ImplItemKind::TyAlias(ty)
851-
}
852-
Some(bs) => {
853-
let bs = self.lower_param_bounds(bs, ImplTraitContext::disallowed());
854-
hir::ImplItemKind::OpaqueTy(bs)
855-
}
856-
},
829+
Some(ty) => {
830+
let ty = self.lower_ty(
831+
ty,
832+
ImplTraitContext::OpaqueTy(None, hir::OpaqueTyOrigin::Misc),
833+
);
834+
hir::ImplItemKind::TyAlias(ty)
835+
}
857836
};
858837
(generics, kind)
859838
}
@@ -887,12 +866,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
887866
defaultness,
888867
kind: match &i.kind {
889868
AssocItemKind::Const(..) => hir::AssocItemKind::Const,
890-
AssocItemKind::TyAlias(.., ty) => {
891-
match ty.as_deref().and_then(|ty| ty.kind.opaque_top_hack()) {
892-
None => hir::AssocItemKind::Type,
893-
Some(_) => hir::AssocItemKind::OpaqueTy,
894-
}
895-
}
869+
AssocItemKind::TyAlias(..) => hir::AssocItemKind::Type,
896870
AssocItemKind::Fn(_, sig, ..) => {
897871
hir::AssocItemKind::Fn { has_self: sig.decl.has_self() }
898872
}

src/librustc_ast_lowering/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,8 +1371,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13711371

13721372
let hir_bounds = self.with_hir_id_owner(opaque_ty_node_id, lower_bounds);
13731373

1374-
let (lifetimes, lifetime_defs) =
1375-
self.lifetimes_from_impl_trait_bounds(opaque_ty_node_id, opaque_ty_def_id, &hir_bounds);
1374+
let (lifetimes, lifetime_defs): (&[_], &[_]) = if fn_def_id.is_some() {
1375+
self.lifetimes_from_impl_trait_bounds(opaque_ty_node_id, opaque_ty_def_id, &hir_bounds)
1376+
} else {
1377+
// Non return-position impl trait captures all of the lifetimes of
1378+
// the parent item.
1379+
(&[], &[])
1380+
};
13761381

13771382
debug!("lower_opaque_impl_trait: lifetimes={:#?}", lifetimes,);
13781383

src/librustc_lint/builtin.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeAliasBounds {
11021102
hir::ItemKind::TyAlias(ref ty, ref generics) => (&*ty, generics),
11031103
_ => return,
11041104
};
1105+
if let hir::TyKind::Def(..) = ty.kind {
1106+
// Bounds are respected for `type X = impl Trait`
1107+
return;
1108+
}
11051109
let mut suggested_changing_assoc_types = false;
11061110
// There must not be a where clause
11071111
if !type_alias_generics.where_clause.predicates.is_empty() {

src/librustc_middle/hir/map/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,8 @@ impl<'hir> Map<'hir> {
672672
if let Node::Item(Item {
673673
kind:
674674
ItemKind::Fn(..)
675+
| ItemKind::Const(..)
676+
| ItemKind::Static(..)
675677
| ItemKind::Mod(..)
676678
| ItemKind::Enum(..)
677679
| ItemKind::Struct(..)
@@ -700,11 +702,7 @@ impl<'hir> Map<'hir> {
700702
return CRATE_HIR_ID;
701703
}
702704
match self.get(scope) {
703-
Node::Item(Item {
704-
kind: ItemKind::OpaqueTy(OpaqueTy { impl_trait_fn: None, .. }),
705-
..
706-
})
707-
| Node::Block(_) => {}
705+
Node::Block(_) => {}
708706
_ => break,
709707
}
710708
}

src/librustc_passes/layout_test.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ impl ItemLikeVisitor<'tcx> for LayoutTest<'tcx> {
2727
ItemKind::TyAlias(..)
2828
| ItemKind::Enum(..)
2929
| ItemKind::Struct(..)
30-
| ItemKind::Union(..)
31-
| ItemKind::OpaqueTy(..) => {
30+
| ItemKind::Union(..) => {
3231
for attr in self.tcx.get_attrs(item_def_id.to_def_id()).iter() {
3332
if attr.check_name(sym::rustc_layout) {
3433
self.dump_layout_of(item_def_id, item, attr);
@@ -83,9 +82,11 @@ impl LayoutTest<'tcx> {
8382
}
8483

8584
sym::debug => {
85+
let normalized_ty =
86+
self.tcx.normalize_erasing_regions(param_env.with_reveal_all(), ty);
8687
self.tcx.sess.span_err(
8788
item.span,
88-
&format!("layout_of({:?}) = {:#?}", ty, *ty_layout),
89+
&format!("layout_of({:?}) = {:#?}", normalized_ty, *ty_layout),
8990
);
9091
}
9192

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -746,12 +746,11 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
746746
}
747747

748748
// These items live in the type namespace.
749-
ItemKind::TyAlias(_, _, _, ref ty) => {
750-
let def_kind = match ty.as_deref().and_then(|ty| ty.kind.opaque_top_hack()) {
751-
None => DefKind::TyAlias,
752-
Some(_) => DefKind::OpaqueTy,
753-
};
754-
let res = Res::Def(def_kind, self.r.definitions.local_def_id(item.id).to_def_id());
749+
ItemKind::TyAlias(..) => {
750+
let res = Res::Def(
751+
DefKind::TyAlias,
752+
self.r.definitions.local_def_id(item.id).to_def_id(),
753+
);
755754
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
756755
}
757756

src/librustc_resolve/late/lifetimes.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -396,15 +396,12 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
396396
let scope = Scope::Elision { elide: Elide::Exact(Region::Static), s: ROOT_SCOPE };
397397
self.with(scope, |_, this| intravisit::walk_item(this, item));
398398
}
399-
hir::ItemKind::OpaqueTy(hir::OpaqueTy { impl_trait_fn: Some(_), .. }) => {
400-
// Currently opaque type declarations are just generated from `impl Trait`
401-
// items. Doing anything on this node is irrelevant, as we currently don't need
402-
// it.
399+
hir::ItemKind::OpaqueTy(hir::OpaqueTy { .. }) => {
400+
// Opaque types are visited when we visit the `TyKind::Def`, so
401+
// that they have the lifetimes from their parent opaque_ty in
402+
// scope.
403403
}
404404
hir::ItemKind::TyAlias(_, ref generics)
405-
| hir::ItemKind::OpaqueTy(hir::OpaqueTy {
406-
impl_trait_fn: None, ref generics, ..
407-
})
408405
| hir::ItemKind::Enum(_, ref generics)
409406
| hir::ItemKind::Struct(_, ref generics)
410407
| hir::ItemKind::Union(_, ref generics)
@@ -563,17 +560,22 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
563560
// `type MyAnonTy<'b> = impl MyTrait<'b>;`
564561
// ^ ^ this gets resolved in the scope of
565562
// the opaque_ty generics
566-
let (generics, bounds) = match self.tcx.hir().expect_item(item_id.id).kind {
563+
let opaque_ty = self.tcx.hir().expect_item(item_id.id);
564+
let (generics, bounds) = match opaque_ty.kind {
567565
// Named opaque `impl Trait` types are reached via `TyKind::Path`.
568566
// This arm is for `impl Trait` in the types of statics, constants and locals.
569567
hir::ItemKind::OpaqueTy(hir::OpaqueTy { impl_trait_fn: None, .. }) => {
570568
intravisit::walk_ty(self, ty);
569+
intravisit::walk_item(this, opaque_ty);
571570
return;
572571
}
573572
// RPIT (return position impl trait)
574-
hir::ItemKind::OpaqueTy(hir::OpaqueTy { ref generics, bounds, .. }) => {
575-
(generics, bounds)
576-
}
573+
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
574+
impl_trait_fn: Some(_),
575+
ref generics,
576+
bounds,
577+
..
578+
}) => (generics, bounds),
577579
ref i => bug!("`impl Trait` pointed to non-opaque type?? {:#?}", i),
578580
};
579581

@@ -2667,8 +2669,9 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
26672669
// going to make a fresh name, so we cannot
26682670
// necessarily replace a single-use lifetime with
26692671
// `'_`.
2670-
Scope::Elision { elide: Elide::Exact(_), .. } => break false,
2671-
Scope::Elision { elide: Elide::Error(_), .. } => break false,
2672+
Scope::Elision {
2673+
elide: Elide::Exact(_) | Elide::Error(_) | Elide::Forbid, ..
2674+
} => break false,
26722675

26732676
Scope::ObjectLifetimeDefault { s, .. } => scope = s,
26742677
}

src/librustc_trait_selection/opaque_types.rs

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ pub trait InferCtxtExt<'tcx> {
133133
fn generate_member_constraint(
134134
&self,
135135
concrete_ty: Ty<'tcx>,
136-
opaque_type_generics: &ty::Generics,
137136
opaque_defn: &OpaqueTypeDecl<'tcx>,
138137
opaque_type_def_id: DefId,
138+
first_own_region_index: usize,
139139
);
140140

141141
/*private*/
@@ -405,7 +405,16 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
405405

406406
debug!("constrain_opaque_type: concrete_ty={:?}", concrete_ty);
407407

408-
let opaque_type_generics = tcx.generics_of(def_id);
408+
let first_own_region = match opaque_defn.origin {
409+
hir::OpaqueTyOrigin::FnReturn | hir::OpaqueTyOrigin::AsyncFn => {
410+
// For these opaque types, only the item's own lifetime
411+
// parameters are considered.
412+
tcx.generics_of(def_id).parent_count
413+
}
414+
// These opaque type inherit all lifetime parameters from their
415+
// parent.
416+
hir::OpaqueTyOrigin::Misc => 0,
417+
};
409418

410419
let span = tcx.def_span(def_id);
411420

@@ -427,12 +436,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
427436
});
428437
}
429438
if let GenerateMemberConstraints::IfNoStaticBound = mode {
430-
self.generate_member_constraint(
431-
concrete_ty,
432-
opaque_type_generics,
433-
opaque_defn,
434-
def_id,
435-
);
439+
self.generate_member_constraint(concrete_ty, opaque_defn, def_id, first_own_region);
436440
}
437441
return;
438442
}
@@ -445,29 +449,27 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
445449
// `['a]` for the first impl trait and `'b` for the
446450
// second.
447451
let mut least_region = None;
448-
for param in &opaque_type_generics.params {
449-
match param.kind {
450-
GenericParamDefKind::Lifetime => {}
451-
_ => continue,
452-
}
453452

454-
// Get the value supplied for this region from the substs.
455-
let subst_arg = opaque_defn.substs.region_at(param.index as usize);
453+
for subst_arg in &opaque_defn.substs[first_own_region..] {
454+
let subst_region = match subst_arg.unpack() {
455+
GenericArgKind::Lifetime(r) => r,
456+
GenericArgKind::Type(_) | GenericArgKind::Const(_) => continue,
457+
};
456458

457459
// Compute the least upper bound of it with the other regions.
458460
debug!("constrain_opaque_types: least_region={:?}", least_region);
459-
debug!("constrain_opaque_types: subst_arg={:?}", subst_arg);
461+
debug!("constrain_opaque_types: subst_region={:?}", subst_region);
460462
match least_region {
461-
None => least_region = Some(subst_arg),
463+
None => least_region = Some(subst_region),
462464
Some(lr) => {
463-
if free_region_relations.sub_free_regions(self.tcx, lr, subst_arg) {
465+
if free_region_relations.sub_free_regions(self.tcx, lr, subst_region) {
464466
// keep the current least region
465-
} else if free_region_relations.sub_free_regions(self.tcx, subst_arg, lr) {
466-
// switch to `subst_arg`
467-
least_region = Some(subst_arg);
467+
} else if free_region_relations.sub_free_regions(self.tcx, subst_region, lr) {
468+
// switch to `subst_region`
469+
least_region = Some(subst_region);
468470
} else {
469471
// There are two regions (`lr` and
470-
// `subst_arg`) which are not relatable. We
472+
// `subst_region`) which are not relatable. We
471473
// can't find a best choice. Therefore,
472474
// instead of creating a single bound like
473475
// `'r: 'a` (which is our preferred choice),
@@ -476,13 +478,13 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
476478
// regions that appear in the impl trait.
477479

478480
// For now, enforce a feature gate outside of async functions.
479-
self.member_constraint_feature_gate(opaque_defn, def_id, lr, subst_arg);
481+
self.member_constraint_feature_gate(opaque_defn, def_id, lr, subst_region);
480482

481483
return self.generate_member_constraint(
482484
concrete_ty,
483-
opaque_type_generics,
484485
opaque_defn,
485486
def_id,
487+
first_own_region,
486488
);
487489
}
488490
}
@@ -494,12 +496,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
494496

495497
if let GenerateMemberConstraints::IfNoStaticBound = mode {
496498
if least_region != tcx.lifetimes.re_static {
497-
self.generate_member_constraint(
498-
concrete_ty,
499-
opaque_type_generics,
500-
opaque_defn,
501-
def_id,
502-
);
499+
self.generate_member_constraint(concrete_ty, opaque_defn, def_id, first_own_region);
503500
}
504501
}
505502
concrete_ty.visit_with(&mut ConstrainOpaqueTypeRegionVisitor {
@@ -518,22 +515,20 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
518515
fn generate_member_constraint(
519516
&self,
520517
concrete_ty: Ty<'tcx>,
521-
opaque_type_generics: &ty::Generics,
522518
opaque_defn: &OpaqueTypeDecl<'tcx>,
523519
opaque_type_def_id: DefId,
520+
first_own_region: usize,
524521
) {
525522
// Create the set of choice regions: each region in the hidden
526523
// type can be equal to any of the region parameters of the
527524
// opaque type definition.
528525
let choice_regions: Lrc<Vec<ty::Region<'tcx>>> = Lrc::new(
529-
opaque_type_generics
530-
.params
526+
opaque_defn.substs[first_own_region..]
531527
.iter()
532-
.filter(|param| match param.kind {
533-
GenericParamDefKind::Lifetime => true,
534-
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const => false,
528+
.filter_map(|arg| match arg.unpack() {
529+
GenericArgKind::Lifetime(r) => Some(r),
530+
GenericArgKind::Type(_) | GenericArgKind::Const(_) => None,
535531
})
536-
.map(|param| opaque_defn.substs.region_at(param.index as usize))
537532
.chain(std::iter::once(self.tcx.lifetimes.re_static))
538533
.collect(),
539534
);

0 commit comments

Comments
 (0)