TAIT: use hack in ->HIR to avoid more changes

This commit is contained in:
Mazdak Farrokhzad 2019-11-07 19:13:22 +01:00
parent 0e8e176b69
commit aa6a72f4a5
4 changed files with 46 additions and 31 deletions

View File

@ -452,7 +452,6 @@ impl<'a> LoweringContext<'a> {
| ItemKind::Union(_, ref generics) | ItemKind::Union(_, ref generics)
| ItemKind::Enum(_, ref generics) | ItemKind::Enum(_, ref generics)
| ItemKind::TyAlias(_, ref generics) | ItemKind::TyAlias(_, ref generics)
| ItemKind::OpaqueTy(_, ref generics)
| ItemKind::Trait(_, _, ref generics, ..) => { | ItemKind::Trait(_, _, ref generics, ..) => {
let def_id = self.lctx.resolver.definitions().local_def_id(item.id); let def_id = self.lctx.resolver.definitions().local_def_id(item.id);
let count = generics let count = generics

View File

@ -335,20 +335,22 @@ impl LoweringContext<'_> {
ItemKind::Mod(ref m) => hir::ItemKind::Mod(self.lower_mod(m)), ItemKind::Mod(ref m) => hir::ItemKind::Mod(self.lower_mod(m)),
ItemKind::ForeignMod(ref nm) => hir::ItemKind::ForeignMod(self.lower_foreign_mod(nm)), ItemKind::ForeignMod(ref nm) => hir::ItemKind::ForeignMod(self.lower_foreign_mod(nm)),
ItemKind::GlobalAsm(ref ga) => hir::ItemKind::GlobalAsm(self.lower_global_asm(ga)), ItemKind::GlobalAsm(ref ga) => hir::ItemKind::GlobalAsm(self.lower_global_asm(ga)),
ItemKind::TyAlias(ref t, ref generics) => hir::ItemKind::TyAlias( ItemKind::TyAlias(ref ty, ref generics) => match ty.kind.opaque_top_hack() {
self.lower_ty(t, ImplTraitContext::disallowed()), None => {
self.lower_generics(generics, ImplTraitContext::disallowed()), let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
), let generics = self.lower_generics(generics, ImplTraitContext::disallowed());
ItemKind::OpaqueTy(ref b, ref generics) => hir::ItemKind::OpaqueTy( hir::ItemKind::TyAlias(ty, generics)
hir::OpaqueTy { },
generics: self.lower_generics(generics, Some(bounds) => {
ImplTraitContext::OpaqueTy(None)), let ty = hir::OpaqueTy {
bounds: self.lower_param_bounds(b, generics: self.lower_generics(generics, ImplTraitContext::OpaqueTy(None)),
ImplTraitContext::OpaqueTy(None)), bounds: self.lower_param_bounds(bounds, ImplTraitContext::OpaqueTy(None)),
impl_trait_fn: None, impl_trait_fn: None,
origin: hir::OpaqueTyOrigin::TypeAlias, origin: hir::OpaqueTyOrigin::TypeAlias,
}, };
), hir::ItemKind::OpaqueTy(ty)
}
}
ItemKind::Enum(ref enum_definition, ref generics) => { ItemKind::Enum(ref enum_definition, ref generics) => {
hir::ItemKind::Enum( hir::ItemKind::Enum(
hir::EnumDef { hir::EnumDef {
@ -914,16 +916,20 @@ impl LoweringContext<'_> {
(generics, hir::ImplItemKind::Method(sig, body_id)) (generics, hir::ImplItemKind::Method(sig, body_id))
} }
ImplItemKind::TyAlias(ref ty) => ( ImplItemKind::TyAlias(ref ty) => {
self.lower_generics(&i.generics, ImplTraitContext::disallowed()), let generics = self.lower_generics(&i.generics, ImplTraitContext::disallowed());
hir::ImplItemKind::TyAlias(self.lower_ty(ty, ImplTraitContext::disallowed())), let kind = match ty.kind.opaque_top_hack() {
), None => {
ImplItemKind::OpaqueTy(ref bounds) => ( let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
self.lower_generics(&i.generics, ImplTraitContext::disallowed()), hir::ImplItemKind::TyAlias(ty)
hir::ImplItemKind::OpaqueTy( }
self.lower_param_bounds(bounds, ImplTraitContext::disallowed()), Some(bs) => {
), let bounds = self.lower_param_bounds(bs, ImplTraitContext::disallowed());
), hir::ImplItemKind::OpaqueTy(bounds)
}
};
(generics, kind)
},
ImplItemKind::Macro(..) => bug!("`TyMac` should have been expanded by now"), ImplItemKind::Macro(..) => bug!("`TyMac` should have been expanded by now"),
}; };
@ -948,11 +954,13 @@ impl LoweringContext<'_> {
span: i.span, span: i.span,
vis: self.lower_visibility(&i.vis, Some(i.id)), vis: self.lower_visibility(&i.vis, Some(i.id)),
defaultness: self.lower_defaultness(i.defaultness, true /* [1] */), defaultness: self.lower_defaultness(i.defaultness, true /* [1] */),
kind: match i.kind { kind: match &i.kind {
ImplItemKind::Const(..) => hir::AssocItemKind::Const, ImplItemKind::Const(..) => hir::AssocItemKind::Const,
ImplItemKind::TyAlias(..) => hir::AssocItemKind::Type, ImplItemKind::TyAlias(ty) => match ty.kind.opaque_top_hack() {
ImplItemKind::OpaqueTy(..) => hir::AssocItemKind::OpaqueTy, None => hir::AssocItemKind::Type,
ImplItemKind::Method(ref sig, _) => hir::AssocItemKind::Method { Some(_) => hir::AssocItemKind::OpaqueTy,
},
ImplItemKind::Method(sig, _) => hir::AssocItemKind::Method {
has_self: sig.decl.has_self(), has_self: sig.decl.has_self(),
}, },
ImplItemKind::Macro(..) => unimplemented!(), ImplItemKind::Macro(..) => unimplemented!(),

View File

@ -107,7 +107,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
} }
ItemKind::Mod(..) | ItemKind::Trait(..) | ItemKind::TraitAlias(..) | ItemKind::Mod(..) | ItemKind::Trait(..) | ItemKind::TraitAlias(..) |
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) | ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) |
ItemKind::OpaqueTy(..) | ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) | ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) |
ItemKind::TyAlias(..) => DefPathData::TypeNs(i.ident.name), ItemKind::TyAlias(..) => DefPathData::TypeNs(i.ident.name),
ItemKind::Fn(sig, generics, body) if sig.header.asyncness.node.is_async() => { ItemKind::Fn(sig, generics, body) if sig.header.asyncness.node.is_async() => {
return self.visit_async_fn( return self.visit_async_fn(
@ -239,8 +239,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
} }
ImplItemKind::Method(..) | ImplItemKind::Method(..) |
ImplItemKind::Const(..) => DefPathData::ValueNs(ii.ident.name), ImplItemKind::Const(..) => DefPathData::ValueNs(ii.ident.name),
ImplItemKind::TyAlias(..) | ImplItemKind::TyAlias(..) => DefPathData::TypeNs(ii.ident.name),
ImplItemKind::OpaqueTy(..) => DefPathData::TypeNs(ii.ident.name),
ImplItemKind::Macro(..) => return self.visit_macro_invoc(ii.id), ImplItemKind::Macro(..) => return self.visit_macro_invoc(ii.id),
}; };

View File

@ -1815,6 +1815,15 @@ impl TyKind {
false false
} }
} }
/// HACK(type_alias_impl_trait, Centril): A temporary crutch used
/// in lowering to avoid making larger changes there and beyond.
pub fn opaque_top_hack(&self) -> Option<&GenericBounds> {
match self {
Self::ImplTrait(_, bounds) => Some(bounds),
_ => None,
}
}
} }
/// Syntax used to declare a trait object. /// Syntax used to declare a trait object.