From 6d8e300e350a7b91c86a616477e40d678cb71f5a Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Thu, 7 Nov 2019 17:59:58 +0100 Subject: [PATCH] TAIT: feature gate recursive locations --- src/libsyntax/feature_gate/check.rs | 46 ++++++++++++++++------------- 1 file changed, 26 insertions(+), 20 deletions(-) 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 @@ fn check_gat(&self, generics: &ast::Generics, span: Span) { ); } } + + /// 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 @@ fn visit_item(&mut self, i: &'a ast::Item) { 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 @@ fn visit_trait_item(&mut self, ti: &'a ast::TraitItem) { } } 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 @@ fn visit_impl_item(&mut self, ii: &'a ast::ImplItem) { "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); } _ => {}