From 6a49b523be28e3c2a408d7f8c95ab56f575eae85 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Thu, 7 Nov 2019 14:56:49 +0100 Subject: TAIT: remove `OpaqueTy` in AST. --- src/libsyntax/ast.rs | 6 ------ src/libsyntax/mut_visit.rs | 5 ----- src/libsyntax/print/pprust.rs | 20 -------------------- src/libsyntax/visit.rs | 7 ------- 4 files changed, 38 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 13bf6752ba2..4cfa9988570 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1579,7 +1579,6 @@ pub enum ImplItemKind { Const(P, P), Method(FnSig, P), TyAlias(P), - OpaqueTy(GenericBounds), Macro(Mac), } @@ -2483,10 +2482,6 @@ pub enum ItemKind { /// /// E.g., `type Foo = Bar;`. TyAlias(P, Generics), - /// An opaque `impl Trait` type alias. - /// - /// E.g., `type Foo = impl Bar + Boo;`. - OpaqueTy(GenericBounds, Generics), /// An enum definition (`enum`). /// /// E.g., `enum Foo { C, D }`. @@ -2540,7 +2535,6 @@ impl ItemKind { ItemKind::ForeignMod(..) => "foreign module", ItemKind::GlobalAsm(..) => "global asm", ItemKind::TyAlias(..) => "type alias", - ItemKind::OpaqueTy(..) => "opaque type", ItemKind::Enum(..) => "enum", ItemKind::Struct(..) => "struct", ItemKind::Union(..) => "union", diff --git a/src/libsyntax/mut_visit.rs b/src/libsyntax/mut_visit.rs index 376323a83ea..f4ef993edb9 100644 --- a/src/libsyntax/mut_visit.rs +++ b/src/libsyntax/mut_visit.rs @@ -887,10 +887,6 @@ pub fn noop_visit_item_kind(kind: &mut ItemKind, vis: &mut T) { vis.visit_ty(ty); vis.visit_generics(generics); } - ItemKind::OpaqueTy(bounds, generics) => { - visit_bounds(bounds, vis); - vis.visit_generics(generics); - } ItemKind::Enum(EnumDef { variants }, generics) => { variants.flat_map_in_place(|variant| vis.flat_map_variant(variant)); vis.visit_generics(generics); @@ -970,7 +966,6 @@ pub fn noop_flat_map_impl_item(mut item: ImplItem, visitor: &mut visitor.visit_block(body); } ImplItemKind::TyAlias(ty) => visitor.visit_ty(ty), - ImplItemKind::OpaqueTy(bounds) => visit_bounds(bounds, visitor), ImplItemKind::Macro(mac) => visitor.visit_mac(mac), } visitor.visit_span(span); diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index f154b7bde98..ef65a744e83 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1255,19 +1255,6 @@ impl<'a> State<'a> { self.s.word(";"); self.end(); // end the outer ibox } - ast::ItemKind::OpaqueTy(ref bounds, ref generics) => { - self.head(visibility_qualified(&item.vis, "type")); - self.print_ident(item.ident); - self.word_space("= impl"); - self.print_generic_params(&generics.params); - self.end(); // end the inner ibox - - self.print_where_clause(&generics.where_clause); - self.s.space(); - self.print_type_bounds(":", bounds); - self.s.word(";"); - self.end(); // end the outer ibox - } ast::ItemKind::Enum(ref enum_definition, ref params) => { self.print_enum_def( enum_definition, @@ -1620,13 +1607,6 @@ impl<'a> State<'a> { ast::ImplItemKind::TyAlias(ref ty) => { self.print_associated_type(ii.ident, None, Some(ty)); } - ast::ImplItemKind::OpaqueTy(ref bounds) => { - self.word_space("type"); - self.print_ident(ii.ident); - self.word_space("= impl"); - self.print_type_bounds(":", bounds); - self.s.word(";"); - } ast::ImplItemKind::Macro(ref mac) => { self.print_mac(mac); match mac.delim { diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index ea2dc357e6e..2597ffd750d 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -263,10 +263,6 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) { visitor.visit_ty(typ); visitor.visit_generics(generics) } - ItemKind::OpaqueTy(ref bounds, ref generics) => { - walk_list!(visitor, visit_param_bound, bounds); - visitor.visit_generics(generics) - } ItemKind::Enum(ref enum_definition, ref generics) => { visitor.visit_generics(generics); visitor.visit_enum_def(enum_definition, generics, item.id, item.span) @@ -628,9 +624,6 @@ pub fn walk_impl_item<'a, V: Visitor<'a>>(visitor: &mut V, impl_item: &'a ImplIt ImplItemKind::TyAlias(ref ty) => { visitor.visit_ty(ty); } - ImplItemKind::OpaqueTy(ref bounds) => { - walk_list!(visitor, visit_param_bound, bounds); - } ImplItemKind::Macro(ref mac) => { visitor.visit_mac(mac); } -- cgit 1.4.1-3-g733a5 From 6d8e300e350a7b91c86a616477e40d678cb71f5a Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Thu, 7 Nov 2019 17:59:58 +0100 Subject: TAIT: feature gate recursive locations --- src/libsyntax/feature_gate/check.rs | 46 +++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 20 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/feature_gate/check.rs b/src/libsyntax/feature_gate/check.rs index 3bf1e24bf12..b50b65faaac 100644 --- a/src/libsyntax/feature_gate/check.rs +++ b/src/libsyntax/feature_gate/check.rs @@ -318,6 +318,27 @@ impl<'a> PostExpansionVisitor<'a> { ); } } + + /// Feature gate `impl Trait` inside `type Alias = $type_expr;`. + fn check_impl_trait(&self, ty: &ast::Ty) { + struct ImplTraitVisitor<'a> { + vis: &'a PostExpansionVisitor<'a>, + } + impl Visitor<'_> for ImplTraitVisitor<'_> { + fn visit_ty(&mut self, ty: &ast::Ty) { + if let ast::TyKind::ImplTrait(..) = ty.kind { + gate_feature_post!( + &self.vis, + type_alias_impl_trait, + ty.span, + "`impl Trait` in type aliases is unstable" + ); + } + visit::walk_ty(self, ty); + } + } + ImplTraitVisitor { vis: self }.visit_ty(ty); + } } impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { @@ -452,14 +473,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { gate_feature_post!(&self, decl_macro, i.span, msg); } - ast::ItemKind::OpaqueTy(..) => { - gate_feature_post!( - &self, - type_alias_impl_trait, - i.span, - "`impl Trait` in type aliases is unstable" - ); - } + ast::ItemKind::TyAlias(ref ty, ..) => self.check_impl_trait(&ty), _ => {} } @@ -633,9 +647,8 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } } ast::TraitItemKind::Type(_, ref default) => { - // We use three if statements instead of something like match guards so that all - // of these errors can be emitted if all cases apply. - if default.is_some() { + if let Some(ty) = default { + self.check_impl_trait(ty); gate_feature_post!(&self, associated_type_defaults, ti.span, "associated type defaults are unstable"); } @@ -660,15 +673,8 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { "C-variadic functions are unstable"); } } - ast::ImplItemKind::OpaqueTy(..) => { - gate_feature_post!( - &self, - type_alias_impl_trait, - ii.span, - "`impl Trait` in type aliases is unstable" - ); - } - ast::ImplItemKind::TyAlias(_) => { + ast::ImplItemKind::TyAlias(ref ty) => { + self.check_impl_trait(ty); self.check_gat(&ii.generics, ii.span); } _ => {} -- cgit 1.4.1-3-g733a5 From aa6a72f4a5f53f7e81a5cb15f1f129e697667964 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Thu, 7 Nov 2019 19:13:22 +0100 Subject: TAIT: use hack in ->HIR to avoid more changes --- src/librustc/hir/lowering.rs | 1 - src/librustc/hir/lowering/item.rs | 62 ++++++++++++++++++++--------------- src/librustc/hir/map/def_collector.rs | 5 ++- src/libsyntax/ast.rs | 9 +++++ 4 files changed, 46 insertions(+), 31 deletions(-) (limited to 'src/libsyntax') diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index 2c8e14f2add..9a840022a3d 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -452,7 +452,6 @@ impl<'a> LoweringContext<'a> { | ItemKind::Union(_, ref generics) | ItemKind::Enum(_, ref generics) | ItemKind::TyAlias(_, ref generics) - | ItemKind::OpaqueTy(_, ref generics) | ItemKind::Trait(_, _, ref generics, ..) => { let def_id = self.lctx.resolver.definitions().local_def_id(item.id); let count = generics diff --git a/src/librustc/hir/lowering/item.rs b/src/librustc/hir/lowering/item.rs index aafdd78e3e8..356adcdf2a9 100644 --- a/src/librustc/hir/lowering/item.rs +++ b/src/librustc/hir/lowering/item.rs @@ -335,20 +335,22 @@ impl LoweringContext<'_> { ItemKind::Mod(ref m) => hir::ItemKind::Mod(self.lower_mod(m)), 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::TyAlias(ref t, ref generics) => hir::ItemKind::TyAlias( - self.lower_ty(t, ImplTraitContext::disallowed()), - self.lower_generics(generics, ImplTraitContext::disallowed()), - ), - ItemKind::OpaqueTy(ref b, ref generics) => hir::ItemKind::OpaqueTy( - hir::OpaqueTy { - generics: self.lower_generics(generics, - ImplTraitContext::OpaqueTy(None)), - bounds: self.lower_param_bounds(b, - ImplTraitContext::OpaqueTy(None)), - impl_trait_fn: None, - origin: hir::OpaqueTyOrigin::TypeAlias, + ItemKind::TyAlias(ref ty, ref generics) => match ty.kind.opaque_top_hack() { + None => { + let ty = self.lower_ty(ty, ImplTraitContext::disallowed()); + let generics = self.lower_generics(generics, ImplTraitContext::disallowed()); + hir::ItemKind::TyAlias(ty, generics) }, - ), + Some(bounds) => { + let ty = hir::OpaqueTy { + generics: self.lower_generics(generics, ImplTraitContext::OpaqueTy(None)), + bounds: self.lower_param_bounds(bounds, ImplTraitContext::OpaqueTy(None)), + impl_trait_fn: None, + origin: hir::OpaqueTyOrigin::TypeAlias, + }; + hir::ItemKind::OpaqueTy(ty) + } + } ItemKind::Enum(ref enum_definition, ref generics) => { hir::ItemKind::Enum( hir::EnumDef { @@ -914,16 +916,20 @@ impl LoweringContext<'_> { (generics, hir::ImplItemKind::Method(sig, body_id)) } - ImplItemKind::TyAlias(ref ty) => ( - self.lower_generics(&i.generics, ImplTraitContext::disallowed()), - hir::ImplItemKind::TyAlias(self.lower_ty(ty, ImplTraitContext::disallowed())), - ), - ImplItemKind::OpaqueTy(ref bounds) => ( - self.lower_generics(&i.generics, ImplTraitContext::disallowed()), - hir::ImplItemKind::OpaqueTy( - self.lower_param_bounds(bounds, ImplTraitContext::disallowed()), - ), - ), + ImplItemKind::TyAlias(ref ty) => { + let generics = self.lower_generics(&i.generics, ImplTraitContext::disallowed()); + let kind = match ty.kind.opaque_top_hack() { + None => { + let ty = self.lower_ty(ty, ImplTraitContext::disallowed()); + hir::ImplItemKind::TyAlias(ty) + } + 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"), }; @@ -948,11 +954,13 @@ impl LoweringContext<'_> { span: i.span, vis: self.lower_visibility(&i.vis, Some(i.id)), defaultness: self.lower_defaultness(i.defaultness, true /* [1] */), - kind: match i.kind { + kind: match &i.kind { ImplItemKind::Const(..) => hir::AssocItemKind::Const, - ImplItemKind::TyAlias(..) => hir::AssocItemKind::Type, - ImplItemKind::OpaqueTy(..) => hir::AssocItemKind::OpaqueTy, - ImplItemKind::Method(ref sig, _) => hir::AssocItemKind::Method { + ImplItemKind::TyAlias(ty) => match ty.kind.opaque_top_hack() { + None => hir::AssocItemKind::Type, + Some(_) => hir::AssocItemKind::OpaqueTy, + }, + ImplItemKind::Method(sig, _) => hir::AssocItemKind::Method { has_self: sig.decl.has_self(), }, ImplItemKind::Macro(..) => unimplemented!(), diff --git a/src/librustc/hir/map/def_collector.rs b/src/librustc/hir/map/def_collector.rs index d858e00a2e9..cfd90f50b1b 100644 --- a/src/librustc/hir/map/def_collector.rs +++ b/src/librustc/hir/map/def_collector.rs @@ -107,7 +107,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { } ItemKind::Mod(..) | ItemKind::Trait(..) | ItemKind::TraitAlias(..) | ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) | - ItemKind::OpaqueTy(..) | ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) | + ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) | ItemKind::TyAlias(..) => DefPathData::TypeNs(i.ident.name), ItemKind::Fn(sig, generics, body) if sig.header.asyncness.node.is_async() => { return self.visit_async_fn( @@ -239,8 +239,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { } ImplItemKind::Method(..) | ImplItemKind::Const(..) => DefPathData::ValueNs(ii.ident.name), - ImplItemKind::TyAlias(..) | - ImplItemKind::OpaqueTy(..) => DefPathData::TypeNs(ii.ident.name), + ImplItemKind::TyAlias(..) => DefPathData::TypeNs(ii.ident.name), ImplItemKind::Macro(..) => return self.visit_macro_invoc(ii.id), }; diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 4cfa9988570..d358efbe543 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1815,6 +1815,15 @@ impl TyKind { 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. -- cgit 1.4.1-3-g733a5