Make nested impl Trait a hard error
This commit is contained in:
parent
4d2d3fc5da
commit
75f72c0de1
@ -420,6 +420,75 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
// Bans nested `impl Trait`, e.g. `impl Into<impl Debug>`.
|
||||
// Nested `impl Trait` _is_ allowed in associated type position,
|
||||
// e.g `impl Iterator<Item=impl Debug>`
|
||||
struct NestedImplTraitVisitor<'a> {
|
||||
session: &'a Session,
|
||||
outer_impl_trait: Option<Span>,
|
||||
}
|
||||
|
||||
impl<'a> NestedImplTraitVisitor<'a> {
|
||||
fn with_impl_trait<F>(&mut self, outer_impl_trait: Option<Span>, f: F)
|
||||
where F: FnOnce(&mut NestedImplTraitVisitor<'a>)
|
||||
{
|
||||
let old_outer_impl_trait = self.outer_impl_trait;
|
||||
self.outer_impl_trait = outer_impl_trait;
|
||||
f(self);
|
||||
self.outer_impl_trait = old_outer_impl_trait;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl<'a> Visitor<'a> for NestedImplTraitVisitor<'a> {
|
||||
fn visit_ty(&mut self, t: &'a Ty) {
|
||||
if let TyKind::ImplTrait(_) = t.node {
|
||||
if let Some(outer_impl_trait) = self.outer_impl_trait {
|
||||
struct_span_err!(self.session, t.span, E0666,
|
||||
"nested `impl Trait` is not allowed")
|
||||
.span_label(outer_impl_trait, "outer `impl Trait`")
|
||||
.span_label(t.span, "devilishly nested `impl Trait` here")
|
||||
.emit();
|
||||
|
||||
}
|
||||
self.with_impl_trait(Some(t.span), |this| visit::walk_ty(this, t));
|
||||
} else {
|
||||
visit::walk_ty(self, t);
|
||||
}
|
||||
}
|
||||
fn visit_path_parameters(&mut self, _: Span, path_parameters: &'a PathParameters) {
|
||||
match *path_parameters {
|
||||
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<Item=Debug>`
|
||||
// are allowed to contain nested `impl Trait`.
|
||||
self.with_impl_trait(None, |this| visit::walk_ty(this, &type_binding.ty));
|
||||
}
|
||||
}
|
||||
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(None, |this| visit::walk_ty(this, type_));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn check_crate(session: &Session, krate: &Crate) {
|
||||
visit::walk_crate(
|
||||
&mut NestedImplTraitVisitor {
|
||||
session,
|
||||
outer_impl_trait: None,
|
||||
}, krate);
|
||||
|
||||
visit::walk_crate(&mut AstValidator { session: session }, krate)
|
||||
}
|
||||
|
@ -320,4 +320,5 @@ register_diagnostics! {
|
||||
E0567, // auto traits can not have generic parameters
|
||||
E0568, // auto traits can not have super traits
|
||||
E0642, // patterns aren't allowed in methods without bodies
|
||||
E0666, // nested `impl Trait` is illegal
|
||||
}
|
||||
|
@ -432,9 +432,6 @@ declare_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)),
|
||||
|
||||
// Termination trait in main (RFC 1937)
|
||||
(active, termination_trait, "1.24.0", Some(43301)),
|
||||
|
||||
@ -1352,73 +1349,8 @@ fn contains_novel_literal(item: &ast::MetaItem) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
// Bans nested `impl Trait`, e.g. `impl Into<impl Debug>`.
|
||||
// Nested `impl Trait` _is_ allowed in associated type position,
|
||||
// e.g `impl Iterator<Item=impl Debug>`
|
||||
struct NestedImplTraitVisitor<'a> {
|
||||
context: &'a Context<'a>,
|
||||
is_in_impl_trait: bool,
|
||||
}
|
||||
|
||||
impl<'a> NestedImplTraitVisitor<'a> {
|
||||
fn with_impl_trait<F>(&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<Item=Debug>`
|
||||
// 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, krate: &ast::Crate) {
|
||||
visit::walk_crate(
|
||||
&mut NestedImplTraitVisitor {
|
||||
context: self.context,
|
||||
is_in_impl_trait: false,
|
||||
}, krate);
|
||||
|
||||
fn whole_crate_feature_gates(&mut self, _krate: &ast::Crate) {
|
||||
for &(ident, span) in &*self.context.parse_sess.non_modrs_mods.borrow() {
|
||||
if !span.allows_unstable() {
|
||||
let cx = &self.context;
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
//! A simple test for testing many permutations of allowedness of
|
||||
//! impl Trait
|
||||
#![feature(conservative_impl_trait, nested_impl_trait, universal_impl_trait, dyn_trait)]
|
||||
#![feature(conservative_impl_trait, universal_impl_trait, dyn_trait)]
|
||||
use std::fmt::Debug;
|
||||
|
||||
// Allowed
|
||||
@ -60,6 +60,7 @@ fn in_dyn_Fn_return_in_return() -> &'static dyn Fn() -> impl Debug { panic!() }
|
||||
// Disallowed
|
||||
fn in_impl_Fn_parameter_in_parameters(_: &impl Fn(impl Debug)) { panic!() }
|
||||
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
|
||||
//~^^ ERROR nested `impl Trait` is not allowed
|
||||
|
||||
// Disallowed
|
||||
fn in_impl_Fn_return_in_parameters(_: &impl Fn() -> impl Debug) { panic!() }
|
||||
@ -68,6 +69,7 @@ fn in_impl_Fn_return_in_parameters(_: &impl Fn() -> impl Debug) { panic!() }
|
||||
// Disallowed
|
||||
fn in_impl_Fn_parameter_in_return() -> &'static impl Fn(impl Debug) { panic!() }
|
||||
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
|
||||
//~^^ ERROR nested `impl Trait` is not allowed
|
||||
|
||||
// Disallowed
|
||||
fn in_impl_Fn_return_in_return() -> &'static impl Fn() -> impl Debug { panic!() }
|
||||
|
@ -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, nested_impl_trait)]
|
||||
#![feature(conservative_impl_trait, underscore_lifetimes, universal_impl_trait)]
|
||||
#![allow(warnings)]
|
||||
|
||||
use std::fmt::Debug;
|
||||
@ -55,12 +55,11 @@ fn pass_through_elision_with_fn_ptr(x: &fn(&u32) -> &u32) -> impl Into<&fn(&u32)
|
||||
|
||||
fn pass_through_elision_with_fn_path<T: Fn(&u32) -> &u32>(
|
||||
x: &T
|
||||
) -> impl Into<&impl Fn(&u32) -> &u32> { x }
|
||||
) -> &impl Fn(&u32) -> &u32 { x }
|
||||
|
||||
fn foo(x: &impl Debug) -> impl Into<&impl Debug> { x }
|
||||
fn foo_explicit_lifetime<'a>(x: &'a impl Debug) -> impl Into<&'a impl Debug> { x }
|
||||
fn foo_no_outer_impl(x: &impl Debug) -> &impl Debug { x }
|
||||
fn foo_explicit_arg<T: Debug>(x: &T) -> impl Into<&impl Debug> { x }
|
||||
fn foo(x: &impl Debug) -> &impl Debug { x }
|
||||
fn foo_explicit_lifetime<'a>(x: &'a impl Debug) -> &'a impl Debug { x }
|
||||
fn foo_explicit_arg<T: Debug>(x: &T) -> &impl Debug { x }
|
||||
|
||||
fn mixed_lifetimes<'a>() -> impl for<'b: 'a> Fn(&'b u32) { |_| () }
|
||||
fn mixed_as_static() -> impl Fn(&'static u32) { mixed_lifetimes() }
|
||||
|
@ -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, nested_impl_trait)]
|
||||
#![feature(conservative_impl_trait)]
|
||||
|
||||
trait Id<T> {}
|
||||
trait Lt<'a> {}
|
||||
@ -17,7 +17,7 @@ impl<'a> Lt<'a> for () {}
|
||||
impl<T> Id<T> for T {}
|
||||
|
||||
fn free_fn_capture_hrtb_in_impl_trait()
|
||||
-> impl for<'a> Id<impl Lt<'a>>
|
||||
-> Box<for<'a> Id<impl Lt<'a>>>
|
||||
//~^ ERROR `impl Trait` can only capture lifetimes bound at the fn or impl level [E0657]
|
||||
{
|
||||
()
|
||||
@ -26,7 +26,7 @@ fn free_fn_capture_hrtb_in_impl_trait()
|
||||
struct Foo;
|
||||
impl Foo {
|
||||
fn impl_fn_capture_hrtb_in_impl_trait()
|
||||
-> impl for<'a> Id<impl Lt<'a>>
|
||||
-> Box<for<'a> Id<impl Lt<'a>>>
|
||||
//~^ ERROR `impl Trait` can only capture lifetimes bound at the fn or impl level
|
||||
{
|
||||
()
|
||||
|
@ -14,18 +14,19 @@ use std::fmt::Debug;
|
||||
fn fine(x: impl Into<u32>) -> impl Into<u32> { x }
|
||||
|
||||
fn bad_in_ret_position(x: impl Into<u32>) -> impl Into<impl Debug> { x }
|
||||
//~^ ERROR nested `impl Trait` is experimental
|
||||
//~^ ERROR nested `impl Trait` is not allowed
|
||||
|
||||
fn bad_in_fn_syntax(x: fn() -> impl Into<impl Debug>) {}
|
||||
//~^ ERROR nested `impl Trait` is experimental
|
||||
//~^ ERROR nested `impl Trait` is not allowed
|
||||
//~^^ `impl Trait` not allowed
|
||||
|
||||
fn bad_in_arg_position(_: impl Into<impl Debug>) { }
|
||||
//~^ ERROR nested `impl Trait` is experimental
|
||||
//~^ ERROR nested `impl Trait` is not allowed
|
||||
|
||||
struct X;
|
||||
impl X {
|
||||
fn bad(x: impl Into<u32>) -> impl Into<impl Debug> { x }
|
||||
//~^ ERROR nested `impl Trait` is experimental
|
||||
//~^ ERROR nested `impl Trait` is not allowed
|
||||
}
|
||||
|
||||
fn allowed_in_assoc_type() -> impl Iterator<Item=impl Fn()> {
|
||||
@ -33,6 +34,7 @@ fn allowed_in_assoc_type() -> impl Iterator<Item=impl Fn()> {
|
||||
}
|
||||
|
||||
fn allowed_in_ret_type() -> impl Fn() -> impl Into<u32> {
|
||||
//~^ `impl Trait` not allowed
|
||||
|| 5
|
||||
}
|
||||
|
50
src/test/ui/nested_impl_trait.stderr
Normal file
50
src/test/ui/nested_impl_trait.stderr
Normal file
@ -0,0 +1,50 @@
|
||||
error[E0666]: nested `impl Trait` is not allowed
|
||||
--> $DIR/nested_impl_trait.rs:16:56
|
||||
|
|
||||
16 | fn bad_in_ret_position(x: impl Into<u32>) -> impl Into<impl Debug> { x }
|
||||
| ----------^^^^^^^^^^-
|
||||
| | |
|
||||
| | devilishly nested `impl Trait` here
|
||||
| outer `impl Trait`
|
||||
|
||||
error[E0666]: nested `impl Trait` is not allowed
|
||||
--> $DIR/nested_impl_trait.rs:19:42
|
||||
|
|
||||
19 | fn bad_in_fn_syntax(x: fn() -> impl Into<impl Debug>) {}
|
||||
| ----------^^^^^^^^^^-
|
||||
| | |
|
||||
| | devilishly nested `impl Trait` here
|
||||
| outer `impl Trait`
|
||||
|
||||
error[E0666]: nested `impl Trait` is not allowed
|
||||
--> $DIR/nested_impl_trait.rs:23:37
|
||||
|
|
||||
23 | fn bad_in_arg_position(_: impl Into<impl Debug>) { }
|
||||
| ----------^^^^^^^^^^-
|
||||
| | |
|
||||
| | devilishly nested `impl Trait` here
|
||||
| outer `impl Trait`
|
||||
|
||||
error[E0666]: nested `impl Trait` is not allowed
|
||||
--> $DIR/nested_impl_trait.rs:28:44
|
||||
|
|
||||
28 | fn bad(x: impl Into<u32>) -> impl Into<impl Debug> { x }
|
||||
| ----------^^^^^^^^^^-
|
||||
| | |
|
||||
| | devilishly nested `impl Trait` here
|
||||
| outer `impl Trait`
|
||||
|
||||
error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
|
||||
--> $DIR/nested_impl_trait.rs:19:32
|
||||
|
|
||||
19 | fn bad_in_fn_syntax(x: fn() -> impl Into<impl Debug>) {}
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
|
||||
--> $DIR/nested_impl_trait.rs:36:42
|
||||
|
|
||||
36 | fn allowed_in_ret_type() -> impl Fn() -> impl Into<u32> {
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 6 previous errors
|
||||
|
Loading…
Reference in New Issue
Block a user