Skip to content

Commit aa6a72f

Browse files
committed
TAIT: use hack in ->HIR to avoid more changes
1 parent 0e8e176 commit aa6a72f

File tree

4 files changed

+46
-31
lines changed

4 files changed

+46
-31
lines changed

Diff for: src/librustc/hir/lowering.rs

-1
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,6 @@ impl<'a> LoweringContext<'a> {
452452
| ItemKind::Union(_, ref generics)
453453
| ItemKind::Enum(_, ref generics)
454454
| ItemKind::TyAlias(_, ref generics)
455-
| ItemKind::OpaqueTy(_, ref generics)
456455
| ItemKind::Trait(_, _, ref generics, ..) => {
457456
let def_id = self.lctx.resolver.definitions().local_def_id(item.id);
458457
let count = generics

Diff for: src/librustc/hir/lowering/item.rs

+35-27
Original file line numberDiff line numberDiff line change
@@ -335,20 +335,22 @@ impl LoweringContext<'_> {
335335
ItemKind::Mod(ref m) => hir::ItemKind::Mod(self.lower_mod(m)),
336336
ItemKind::ForeignMod(ref nm) => hir::ItemKind::ForeignMod(self.lower_foreign_mod(nm)),
337337
ItemKind::GlobalAsm(ref ga) => hir::ItemKind::GlobalAsm(self.lower_global_asm(ga)),
338-
ItemKind::TyAlias(ref t, ref generics) => hir::ItemKind::TyAlias(
339-
self.lower_ty(t, ImplTraitContext::disallowed()),
340-
self.lower_generics(generics, ImplTraitContext::disallowed()),
341-
),
342-
ItemKind::OpaqueTy(ref b, ref generics) => hir::ItemKind::OpaqueTy(
343-
hir::OpaqueTy {
344-
generics: self.lower_generics(generics,
345-
ImplTraitContext::OpaqueTy(None)),
346-
bounds: self.lower_param_bounds(b,
347-
ImplTraitContext::OpaqueTy(None)),
348-
impl_trait_fn: None,
349-
origin: hir::OpaqueTyOrigin::TypeAlias,
338+
ItemKind::TyAlias(ref ty, ref generics) => match ty.kind.opaque_top_hack() {
339+
None => {
340+
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
341+
let generics = self.lower_generics(generics, ImplTraitContext::disallowed());
342+
hir::ItemKind::TyAlias(ty, generics)
350343
},
351-
),
344+
Some(bounds) => {
345+
let ty = hir::OpaqueTy {
346+
generics: self.lower_generics(generics, ImplTraitContext::OpaqueTy(None)),
347+
bounds: self.lower_param_bounds(bounds, ImplTraitContext::OpaqueTy(None)),
348+
impl_trait_fn: None,
349+
origin: hir::OpaqueTyOrigin::TypeAlias,
350+
};
351+
hir::ItemKind::OpaqueTy(ty)
352+
}
353+
}
352354
ItemKind::Enum(ref enum_definition, ref generics) => {
353355
hir::ItemKind::Enum(
354356
hir::EnumDef {
@@ -914,16 +916,20 @@ impl LoweringContext<'_> {
914916

915917
(generics, hir::ImplItemKind::Method(sig, body_id))
916918
}
917-
ImplItemKind::TyAlias(ref ty) => (
918-
self.lower_generics(&i.generics, ImplTraitContext::disallowed()),
919-
hir::ImplItemKind::TyAlias(self.lower_ty(ty, ImplTraitContext::disallowed())),
920-
),
921-
ImplItemKind::OpaqueTy(ref bounds) => (
922-
self.lower_generics(&i.generics, ImplTraitContext::disallowed()),
923-
hir::ImplItemKind::OpaqueTy(
924-
self.lower_param_bounds(bounds, ImplTraitContext::disallowed()),
925-
),
926-
),
919+
ImplItemKind::TyAlias(ref ty) => {
920+
let generics = self.lower_generics(&i.generics, ImplTraitContext::disallowed());
921+
let kind = match ty.kind.opaque_top_hack() {
922+
None => {
923+
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
924+
hir::ImplItemKind::TyAlias(ty)
925+
}
926+
Some(bs) => {
927+
let bounds = self.lower_param_bounds(bs, ImplTraitContext::disallowed());
928+
hir::ImplItemKind::OpaqueTy(bounds)
929+
}
930+
};
931+
(generics, kind)
932+
},
927933
ImplItemKind::Macro(..) => bug!("`TyMac` should have been expanded by now"),
928934
};
929935

@@ -948,11 +954,13 @@ impl LoweringContext<'_> {
948954
span: i.span,
949955
vis: self.lower_visibility(&i.vis, Some(i.id)),
950956
defaultness: self.lower_defaultness(i.defaultness, true /* [1] */),
951-
kind: match i.kind {
957+
kind: match &i.kind {
952958
ImplItemKind::Const(..) => hir::AssocItemKind::Const,
953-
ImplItemKind::TyAlias(..) => hir::AssocItemKind::Type,
954-
ImplItemKind::OpaqueTy(..) => hir::AssocItemKind::OpaqueTy,
955-
ImplItemKind::Method(ref sig, _) => hir::AssocItemKind::Method {
959+
ImplItemKind::TyAlias(ty) => match ty.kind.opaque_top_hack() {
960+
None => hir::AssocItemKind::Type,
961+
Some(_) => hir::AssocItemKind::OpaqueTy,
962+
},
963+
ImplItemKind::Method(sig, _) => hir::AssocItemKind::Method {
956964
has_self: sig.decl.has_self(),
957965
},
958966
ImplItemKind::Macro(..) => unimplemented!(),

Diff for: src/librustc/hir/map/def_collector.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
107107
}
108108
ItemKind::Mod(..) | ItemKind::Trait(..) | ItemKind::TraitAlias(..) |
109109
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) |
110-
ItemKind::OpaqueTy(..) | ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) |
110+
ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) |
111111
ItemKind::TyAlias(..) => DefPathData::TypeNs(i.ident.name),
112112
ItemKind::Fn(sig, generics, body) if sig.header.asyncness.node.is_async() => {
113113
return self.visit_async_fn(
@@ -239,8 +239,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
239239
}
240240
ImplItemKind::Method(..) |
241241
ImplItemKind::Const(..) => DefPathData::ValueNs(ii.ident.name),
242-
ImplItemKind::TyAlias(..) |
243-
ImplItemKind::OpaqueTy(..) => DefPathData::TypeNs(ii.ident.name),
242+
ImplItemKind::TyAlias(..) => DefPathData::TypeNs(ii.ident.name),
244243
ImplItemKind::Macro(..) => return self.visit_macro_invoc(ii.id),
245244
};
246245

Diff for: src/libsyntax/ast.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1815,6 +1815,15 @@ impl TyKind {
18151815
false
18161816
}
18171817
}
1818+
1819+
/// HACK(type_alias_impl_trait, Centril): A temporary crutch used
1820+
/// in lowering to avoid making larger changes there and beyond.
1821+
pub fn opaque_top_hack(&self) -> Option<&GenericBounds> {
1822+
match self {
1823+
Self::ImplTrait(_, bounds) => Some(bounds),
1824+
_ => None,
1825+
}
1826+
}
18181827
}
18191828

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

0 commit comments

Comments
 (0)