Skip to content

Commit

Permalink
Improve impl trait disallowed context error text
Browse files Browse the repository at this point in the history
  • Loading branch information
spastorino committed Jul 19, 2021
1 parent e0745e8 commit 3e857f5
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 100 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1466,7 +1466,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
t.span,
E0562,
"`impl Trait` not allowed outside of {}",
"function and inherent method return types",
"function and method return types",
);
err.emit();
hir::TyKind::Err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,20 @@ fn _rpit_dyn() -> Box<dyn Tr1<As1: Copy>> { Box::new(S1) }

const _cdef: impl Tr1<As1: Copy> = S1;
//~^ ERROR associated type bounds are unstable
//~| ERROR `impl Trait` not allowed outside of function and inherent method return types [E0562]
//~| ERROR `impl Trait` not allowed outside of function and method return types [E0562]
// FIXME: uncomment when `impl_trait_in_bindings` feature is fixed.
// const _cdef_dyn: &dyn Tr1<As1: Copy> = &S1;

static _sdef: impl Tr1<As1: Copy> = S1;
//~^ ERROR associated type bounds are unstable
//~| ERROR `impl Trait` not allowed outside of function and inherent method return types [E0562]
//~| ERROR `impl Trait` not allowed outside of function and method return types [E0562]
// FIXME: uncomment when `impl_trait_in_bindings` feature is fixed.
// static _sdef_dyn: &dyn Tr1<As1: Copy> = &S1;

fn main() {
let _: impl Tr1<As1: Copy> = S1;
//~^ ERROR associated type bounds are unstable
//~| ERROR `impl Trait` not allowed outside of function and inherent method return types [E0562]
//~| ERROR `impl Trait` not allowed outside of function and method return types [E0562]
// FIXME: uncomment when `impl_trait_in_bindings` feature is fixed.
// let _: &dyn Tr1<As1: Copy> = &S1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,19 @@ LL | let _: impl Tr1<As1: Copy> = S1;
= note: see issue #52662 <https://github.com/rust-lang/rust/issues/52662> for more information
= help: add `#![feature(associated_type_bounds)]` to the crate attributes to enable

error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
error[E0562]: `impl Trait` not allowed outside of function and method return types
--> $DIR/feature-gate-associated_type_bounds.rs:58:14
|
LL | const _cdef: impl Tr1<As1: Copy> = S1;
| ^^^^^^^^^^^^^^^^^^^

error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
error[E0562]: `impl Trait` not allowed outside of function and method return types
--> $DIR/feature-gate-associated_type_bounds.rs:64:15
|
LL | static _sdef: impl Tr1<As1: Copy> = S1;
| ^^^^^^^^^^^^^^^^^^^

error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
error[E0562]: `impl Trait` not allowed outside of function and method return types
--> $DIR/feature-gate-associated_type_bounds.rs:71:12
|
LL | let _: impl Tr1<As1: Copy> = S1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ LL | type Baa = (Vec<impl Debug>, impl Debug, impl Iterator<Item = impl Debu
= note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information
= help: add `#![feature(min_type_alias_impl_trait)]` to the crate attributes to enable

error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
error[E0562]: `impl Trait` not allowed outside of function and method return types
--> $DIR/feature-gate-min_type_alias_impl_trait.rs:23:18
|
LL | type Assoc = impl Debug;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
struct Foo<T = impl Copy>(T);
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types

type Result<T, E = impl std::error::Error> = std::result::Result<T, E>;
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types

// should not cause ICE
fn x() -> Foo {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
error[E0562]: `impl Trait` not allowed outside of function and method return types
--> $DIR/issue-83929-impl-trait-in-generic-default.rs:1:16
|
LL | struct Foo<T = impl Copy>(T);
| ^^^^^^^^^

error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
error[E0562]: `impl Trait` not allowed outside of function and method return types
--> $DIR/issue-83929-impl-trait-in-generic-default.rs:4:20
|
LL | type Result<T, E = impl std::error::Error> = std::result::Result<T, E>;
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/impl-trait/nested_impl_trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ LL | fn bad(x: impl Into<u32>) -> impl Into<impl Debug> { x }
| | nested `impl Trait` here
| outer `impl Trait`

error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
error[E0562]: `impl Trait` not allowed outside of function and method return types
--> $DIR/nested_impl_trait.rs:8:32
|
LL | 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
error[E0562]: `impl Trait` not allowed outside of function and method return types
--> $DIR/nested_impl_trait.rs:25:42
|
LL | fn allowed_in_ret_type() -> impl Fn() -> impl Into<u32> {
Expand Down
82 changes: 41 additions & 41 deletions src/test/ui/impl-trait/where-allowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,61 +13,61 @@ fn in_adt_in_parameters(_: Vec<impl Debug>) { panic!() }

// Disallowed
fn in_fn_parameter_in_parameters(_: fn(impl Debug)) { panic!() }
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types

// Disallowed
fn in_fn_return_in_parameters(_: fn() -> impl Debug) { panic!() }
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types

// Disallowed
fn in_fn_parameter_in_return() -> fn(impl Debug) { panic!() }
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types

// Disallowed
fn in_fn_return_in_return() -> fn() -> impl Debug { panic!() }
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types

// Disallowed
fn in_dyn_Fn_parameter_in_parameters(_: &dyn Fn(impl Debug)) { panic!() }
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types

// Disallowed
fn in_dyn_Fn_return_in_parameters(_: &dyn Fn() -> impl Debug) { panic!() }
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types

// Disallowed
fn in_dyn_Fn_parameter_in_return() -> &'static dyn Fn(impl Debug) { panic!() }
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types

// Disallowed
fn in_dyn_Fn_return_in_return() -> &'static dyn Fn() -> impl Debug { panic!() }
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types

// 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 `impl Trait` not allowed outside of function and method return types
//~^^ ERROR nested `impl Trait` is not allowed

// Disallowed
fn in_impl_Fn_return_in_parameters(_: &impl Fn() -> impl Debug) { panic!() }
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types

// 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 `impl Trait` not allowed outside of function and method return types
//~| ERROR nested `impl Trait` is not allowed

// Disallowed
fn in_impl_Fn_return_in_return() -> &'static impl Fn() -> impl Debug { panic!() }
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types

// Disallowed
fn in_Fn_parameter_in_generics<F: Fn(impl Debug)> (_: F) { panic!() }
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types

// Disallowed
fn in_Fn_return_in_generics<F: Fn() -> impl Debug> (_: F) { panic!() }
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types


// Allowed
Expand All @@ -80,22 +80,22 @@ fn in_impl_Trait_in_return() -> impl IntoIterator<Item = impl IntoIterator> {

// Disallowed
struct InBraceStructField { x: impl Debug }
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types

// Disallowed
struct InAdtInBraceStructField { x: Vec<impl Debug> }
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types

// Disallowed
struct InTupleStructField(impl Debug);
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types

// Disallowed
enum InEnum {
InBraceVariant { x: impl Debug },
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types
InTupleVariant(impl Debug),
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types
}

// Allowed
Expand All @@ -106,7 +106,7 @@ trait InTraitDefnParameters {
// Disallowed
trait InTraitDefnReturn {
fn in_return() -> impl Debug;
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types
}

// Allowed and disallowed in trait impls
Expand All @@ -123,7 +123,7 @@ impl DummyTrait for () {
// Allowed

fn in_trait_impl_return() -> impl Debug { () }
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types
}

// Allowed
Expand All @@ -136,10 +136,10 @@ impl DummyType {
// Disallowed
extern "C" {
fn in_foreign_parameters(_: impl Debug);
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types

fn in_foreign_return() -> impl Debug;
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types
}

// Allowed
Expand All @@ -155,96 +155,96 @@ type InTypeAlias<R> = impl Debug;
//~^ ERROR `impl Trait` in type aliases is unstable

type InReturnInTypeAlias<R> = fn() -> impl Debug;
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~| ERROR `impl Trait` in type aliases is unstable

// Disallowed in impl headers
impl PartialEq<impl Debug> for () {
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types
}

// Disallowed in impl headers
impl PartialEq<()> for impl Debug {
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types
}

// Disallowed in inherent impls
impl impl Debug {
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types
}

// Disallowed in inherent impls
struct InInherentImplAdt<T> { t: T }
impl InInherentImplAdt<impl Debug> {
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types
}

// Disallowed in where clauses
fn in_fn_where_clause()
where impl Debug: Debug
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types
{
}

// Disallowed in where clauses
fn in_adt_in_fn_where_clause()
where Vec<impl Debug>: Debug
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types
{
}

// Disallowed
fn in_trait_parameter_in_fn_where_clause<T>()
where T: PartialEq<impl Debug>
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types
{
}

// Disallowed
fn in_Fn_parameter_in_fn_where_clause<T>()
where T: Fn(impl Debug)
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types
{
}

// Disallowed
fn in_Fn_return_in_fn_where_clause<T>()
where T: Fn() -> impl Debug
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types
{
}

// Disallowed
struct InStructGenericParamDefault<T = impl Debug>(T);
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types

// Disallowed
enum InEnumGenericParamDefault<T = impl Debug> { Variant(T) }
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types

// Disallowed
trait InTraitGenericParamDefault<T = impl Debug> {}
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types

// Disallowed
type InTypeAliasGenericParamDefault<T = impl Debug> = T;
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types

// Disallowed
impl <T = impl Debug> T {}
//~^ ERROR defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
//~| WARNING this was previously accepted by the compiler but is being phased out
//~| ERROR `impl Trait` not allowed outside of function and inherent method return types
//~| ERROR `impl Trait` not allowed outside of function and method return types

// Disallowed
fn in_method_generic_param_default<T = impl Debug>(_: T) {}
//~^ ERROR defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
//~| WARNING this was previously accepted by the compiler but is being phased out
//~| ERROR `impl Trait` not allowed outside of function and inherent method return types
//~| ERROR `impl Trait` not allowed outside of function and method return types

fn main() {
let _in_local_variable: impl Fn() = || {};
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types
let _in_return_in_local_variable = || -> impl Fn() { || {} };
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~^ ERROR `impl Trait` not allowed outside of function and method return types
}
Loading

0 comments on commit 3e857f5

Please sign in to comment.