From c026d19baf4b2ea96b218fd5d83275ae6e8220a1 Mon Sep 17 00:00:00 2001 From: Taylor Cramer Date: Wed, 20 Dec 2017 10:18:37 -0800 Subject: [PATCH] Add a feature gate for nested uses of `impl Trait` --- src/libsyntax/feature_gate.rs | 72 ++++++++++++++++++- src/test/compile-fail/E0657.rs | 2 +- .../feature-gate-nested_impl_trait.rs | 39 ++++++++++ .../compile-fail/impl-trait/where-allowed.rs | 2 +- src/test/run-pass/impl-trait/lifetimes.rs | 2 +- 5 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 src/test/compile-fail/feature-gate-nested_impl_trait.rs diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 1f398ff155c..c44f5334463 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -441,6 +441,9 @@ pub fn new() -> Features { // `foo.rs` as an alternative to `foo/mod.rs` (active, non_modrs_mods, "1.24.0", Some(44660)), + + // Nested `impl Trait` + (active, nested_impl_trait, "1.24.0", Some(34511)), ); declare_features! ( @@ -1314,8 +1317,73 @@ fn contains_novel_literal(item: &ast::MetaItem) -> bool { } } +// Bans nested `impl Trait`, e.g. `impl Into`. +// Nested `impl Trait` _is_ allowed in associated type position, +// e.g `impl Iterator` +struct NestedImplTraitVisitor<'a> { + context: &'a Context<'a>, + is_in_impl_trait: bool, +} + +impl<'a> NestedImplTraitVisitor<'a> { + fn with_impl_trait(&mut self, is_in_impl_trait: bool, f: F) + where F: FnOnce(&mut NestedImplTraitVisitor<'a>) + { + let old_is_in_impl_trait = self.is_in_impl_trait; + self.is_in_impl_trait = is_in_impl_trait; + f(self); + self.is_in_impl_trait = old_is_in_impl_trait; + } +} + + +impl<'a> Visitor<'a> for NestedImplTraitVisitor<'a> { + fn visit_ty(&mut self, t: &'a ast::Ty) { + if let ast::TyKind::ImplTrait(_) = t.node { + if self.is_in_impl_trait { + gate_feature_post!(&self, nested_impl_trait, t.span, + "nested `impl Trait` is experimental" + ); + } + self.with_impl_trait(true, |this| visit::walk_ty(this, t)); + } else { + visit::walk_ty(self, t); + } + } + fn visit_path_parameters(&mut self, _: Span, path_parameters: &'a ast::PathParameters) { + match *path_parameters { + ast::PathParameters::AngleBracketed(ref params) => { + for type_ in ¶ms.types { + self.visit_ty(type_); + } + for type_binding in ¶ms.bindings { + // Type bindings such as `Item=impl Debug` in `Iterator` + // are allowed to contain nested `impl Trait`. + self.with_impl_trait(false, |this| visit::walk_ty(this, &type_binding.ty)); + } + } + ast::PathParameters::Parenthesized(ref params) => { + for type_ in ¶ms.inputs { + self.visit_ty(type_); + } + if let Some(ref type_) = params.output { + // `-> Foo` syntax is essentially an associated type binding, + // so it is also allowed to contain nested `impl Trait`. + self.with_impl_trait(false, |this| visit::walk_ty(this, type_)); + } + } + } + } +} + impl<'a> PostExpansionVisitor<'a> { - fn whole_crate_feature_gates(&mut self) { + fn whole_crate_feature_gates(&mut self, krate: &ast::Crate) { + visit::walk_crate( + &mut NestedImplTraitVisitor { + context: self.context, + is_in_impl_trait: false, + }, krate); + for &(ident, span) in &*self.context.parse_sess.non_modrs_mods.borrow() { if !span.allows_unstable() { let cx = &self.context; @@ -1892,7 +1960,7 @@ pub fn check_crate(krate: &ast::Crate, plugin_attributes, }; let visitor = &mut PostExpansionVisitor { context: &ctx }; - visitor.whole_crate_feature_gates(); + visitor.whole_crate_feature_gates(krate); visit::walk_crate(visitor, krate); } diff --git a/src/test/compile-fail/E0657.rs b/src/test/compile-fail/E0657.rs index b72a8f03089..4595e413081 100644 --- a/src/test/compile-fail/E0657.rs +++ b/src/test/compile-fail/E0657.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. #![allow(warnings)] -#![feature(conservative_impl_trait)] +#![feature(conservative_impl_trait, nested_impl_trait)] trait Id {} trait Lt<'a> {} diff --git a/src/test/compile-fail/feature-gate-nested_impl_trait.rs b/src/test/compile-fail/feature-gate-nested_impl_trait.rs new file mode 100644 index 00000000000..7c35263d05d --- /dev/null +++ b/src/test/compile-fail/feature-gate-nested_impl_trait.rs @@ -0,0 +1,39 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. +#![feature(conservative_impl_trait, universal_impl_trait)] + +use std::fmt::Debug; + +fn fine(x: impl Into) -> impl Into { x } + +fn bad_in_ret_position(x: impl Into) -> impl Into { x } +//~^ ERROR nested `impl Trait` is experimental + +fn bad_in_fn_syntax(x: fn() -> impl Into) {} +//~^ ERROR nested `impl Trait` is experimental + +fn bad_in_arg_position(_: impl Into) { } +//~^ ERROR nested `impl Trait` is experimental + +struct X; +impl X { + fn bad(x: impl Into) -> impl Into { x } + //~^ ERROR nested `impl Trait` is experimental +} + +fn allowed_in_assoc_type() -> impl Iterator { + vec![|| println!("woot")].into_iter() +} + +fn allowed_in_ret_type() -> impl Fn() -> impl Into { + || 5 +} + +fn main() {} diff --git a/src/test/compile-fail/impl-trait/where-allowed.rs b/src/test/compile-fail/impl-trait/where-allowed.rs index af83a2d0a23..a9fe1e04664 100644 --- a/src/test/compile-fail/impl-trait/where-allowed.rs +++ b/src/test/compile-fail/impl-trait/where-allowed.rs @@ -10,7 +10,7 @@ //! A simple test for testing many permutations of allowedness of //! impl Trait -#![feature(conservative_impl_trait, universal_impl_trait, dyn_trait)] +#![feature(conservative_impl_trait, nested_impl_trait, universal_impl_trait, dyn_trait)] use std::fmt::Debug; // Allowed diff --git a/src/test/run-pass/impl-trait/lifetimes.rs b/src/test/run-pass/impl-trait/lifetimes.rs index a56f083e08f..1f2d76f2894 100644 --- a/src/test/run-pass/impl-trait/lifetimes.rs +++ b/src/test/run-pass/impl-trait/lifetimes.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![feature(conservative_impl_trait, underscore_lifetimes, universal_impl_trait)] +#![feature(conservative_impl_trait, underscore_lifetimes, universal_impl_trait, nested_impl_trait)] #![allow(warnings)] use std::fmt::Debug; -- GitLab