Skip to content

Allow impl Trait inside where clauses #105043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1503,14 +1503,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
}) => hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
hir_id: self.next_id(),
bound_generic_params: self.lower_generic_params(bound_generic_params),
bounded_ty: self
.lower_ty(bounded_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type)),
bounds: self.arena.alloc_from_iter(bounds.iter().map(|bound| {
self.lower_param_bound(
bound,
&ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
)
})),
bounded_ty: self.lower_ty(bounded_ty, &ImplTraitContext::Universal),
bounds: self.arena.alloc_from_iter(
bounds
.iter()
.map(|bound| self.lower_param_bound(bound, &ImplTraitContext::Universal)),
),
span: self.lower_span(*span),
origin: PredicateOrigin::WhereClause,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ast_lowering_assoc_ty_parentheses =
ast_lowering_remove_parentheses = remove these parentheses

ast_lowering_misplaced_impl_trait =
`impl Trait` only allowed in function and inherent method return types, not in {$position}
`impl Trait` isn't allowed within {$position}

ast_lowering_rustc_box_attribute_error =
#[rustc_box] requires precisely one argument and no other attributes are allowed
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/async-await/in-trait/fn-not-async-err2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait MyTrait {

impl MyTrait for i32 {
fn foo(&self) -> impl Future<Output = i32> {
//~^ ERROR `impl Trait` only allowed in function and inherent method return types, not in `impl` method return [E0562]
//~^ ERROR `impl Trait` isn't allowed within `impl` method return [E0562]
async {
*self
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/async-await/in-trait/fn-not-async-err2.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `impl` method return
error[E0562]: `impl Trait` isn't allowed within `impl` method return
--> $DIR/fn-not-async-err2.rs:13:22
|
LL | fn foo(&self) -> impl Future<Output = i32> {
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` only allowed in function and inherent method return types
//~| ERROR `impl Trait` isn't allowed within type [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` only allowed in function and inherent method return types
//~| ERROR `impl Trait` isn't allowed within type [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` only allowed in function and inherent method return types
//~| ERROR `impl Trait` isn't allowed within variable binding [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` only allowed in function and inherent method return types, not in type
error[E0562]: `impl Trait` isn't allowed within type
--> $DIR/feature-gate-associated_type_bounds.rs:58:14
|
LL | const _cdef: impl Tr1<As1: Copy> = S1;
| ^^^^^^^^^^^^^^^^^^^

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

error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in variable binding
error[E0562]: `impl Trait` isn't allowed within variable binding
--> $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
@@ -1,6 +1,6 @@
fn f() -> impl Fn() -> impl Sized { || () }
//~^ ERROR `impl Trait` only allowed in function and inherent method return types, not in `Fn` trait return
//~^ ERROR `impl Trait` isn't allowed within `Fn` trait return
fn g() -> &'static dyn Fn() -> impl Sized { &|| () }
//~^ ERROR `impl Trait` only allowed in function and inherent method return types, not in `Fn` trait return
//~^ ERROR `impl Trait` isn't allowed within `Fn` trait return

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `Fn` trait return
error[E0562]: `impl Trait` isn't allowed within `Fn` trait return
--> $DIR/feature-gate-impl_trait_in_fn_trait_return.rs:1:24
|
LL | fn f() -> impl Fn() -> impl Sized { || () }
| ^^^^^^^^^^

error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `Fn` trait return
error[E0562]: `impl Trait` isn't allowed within `Fn` trait return
--> $DIR/feature-gate-impl_trait_in_fn_trait_return.rs:3:32
|
LL | fn g() -> &'static dyn Fn() -> impl Sized { &|| () }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
#![feature(async_fn_in_trait)]

trait Foo {
fn bar() -> impl Sized; //~ ERROR `impl Trait` only allowed in function and inherent method return types, not in trait method return
fn baz() -> Box<impl std::fmt::Display>; //~ ERROR `impl Trait` only allowed in function and inherent method return types, not in trait method return
fn bar() -> impl Sized; //~ ERROR `impl Trait` isn't allowed within trait method return
fn baz() -> Box<impl std::fmt::Display>; //~ ERROR `impl Trait` isn't allowed within trait method return
}

// Both return_position_impl_trait_in_trait and async_fn_in_trait are required for this (see also
// feature-gate-async_fn_in_trait.rs)
trait AsyncFoo {
async fn bar() -> impl Sized; //~ ERROR `impl Trait` only allowed in function and inherent method return types, not in trait method return
async fn bar() -> impl Sized; //~ ERROR `impl Trait` isn't allowed within trait method return [E0562]
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in trait method return
error[E0562]: `impl Trait` isn't allowed within trait method return
--> $DIR/feature-gate-return_position_impl_trait_in_trait.rs:8:17
|
LL | fn bar() -> impl Sized;
Expand All @@ -7,7 +7,7 @@ LL | fn bar() -> impl Sized;
= note: see issue #91611 <https://github.com/rust-lang/rust/issues/91611> for more information
= help: add `#![feature(return_position_impl_trait_in_trait)]` to the crate attributes to enable

error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in trait method return
error[E0562]: `impl Trait` isn't allowed within trait method return
--> $DIR/feature-gate-return_position_impl_trait_in_trait.rs:9:21
|
LL | fn baz() -> Box<impl std::fmt::Display>;
Expand All @@ -16,7 +16,7 @@ LL | fn baz() -> Box<impl std::fmt::Display>;
= note: see issue #91611 <https://github.com/rust-lang/rust/issues/91611> for more information
= help: add `#![feature(return_position_impl_trait_in_trait)]` to the crate attributes to enable

error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in trait method return
error[E0562]: `impl Trait` isn't allowed within trait method return
--> $DIR/feature-gate-return_position_impl_trait_in_trait.rs:15:23
|
LL | async fn bar() -> impl Sized;
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/impl-trait/issues/issue-54600.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ use std::fmt::Debug;

fn main() {
let x: Option<impl Debug> = Some(44_u32);
//~^ `impl Trait` only allowed in function and inherent method return types
//~^ `impl Trait` isn't allowed within variable binding [E0562]
println!("{:?}", x);
}
2 changes: 1 addition & 1 deletion src/test/ui/impl-trait/issues/issue-54600.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in variable binding
error[E0562]: `impl Trait` isn't allowed within variable binding
--> $DIR/issue-54600.rs:4:19
|
LL | let x: Option<impl Debug> = Some(44_u32);
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/impl-trait/issues/issue-54840.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ use std::ops::Add;
fn main() {
let i: i32 = 0;
let j: &impl Add = &i;
//~^ `impl Trait` only allowed in function and inherent method return types
//~^ `impl Trait` isn't allowed within variable binding [E0562]
}
2 changes: 1 addition & 1 deletion src/test/ui/impl-trait/issues/issue-54840.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in variable binding
error[E0562]: `impl Trait` isn't allowed within variable binding
--> $DIR/issue-54840.rs:5:13
|
LL | let j: &impl Add = &i;
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/impl-trait/issues/issue-58504.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ fn mk_gen() -> impl Generator<Return=!, Yield=()> {

fn main() {
let gens: [impl Generator<Return=!, Yield=()>;2] = [ mk_gen(), mk_gen() ];
//~^ `impl Trait` only allowed in function and inherent method return types
//~^ `impl Trait` isn't allowed within variable binding
}
2 changes: 1 addition & 1 deletion src/test/ui/impl-trait/issues/issue-58504.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in variable binding
error[E0562]: `impl Trait` isn't allowed within variable binding
--> $DIR/issue-58504.rs:10:16
|
LL | let gens: [impl Generator<Return=!, Yield=()>;2] = [ mk_gen(), mk_gen() ];
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/impl-trait/issues/issue-58956.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ impl Lam for B {}
pub struct Wrap<T>(T);

const _A: impl Lam = {
//~^ `impl Trait` only allowed in function and inherent method return types
//~^ `impl Trait` isn't allowed within type
let x: Wrap<impl Lam> = Wrap(B);
//~^ `impl Trait` only allowed in function and inherent method return types
//~^ `impl Trait` isn't allowed within variable binding
x.0
};

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/impl-trait/issues/issue-58956.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
error[E0562]: `impl Trait` isn't allowed within type
--> $DIR/issue-58956.rs:7:11
|
LL | const _A: impl Lam = {
| ^^^^^^^^

error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in variable binding
error[E0562]: `impl Trait` isn't allowed within variable binding
--> $DIR/issue-58956.rs:9:17
|
LL | let x: Wrap<impl Lam> = Wrap(B);
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/impl-trait/issues/issue-70971.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn main() {
let x : (impl Copy,) = (true,);
//~^ `impl Trait` only allowed in function and inherent method return types
//~^ `impl Trait` isn't allowed within variable binding
}
2 changes: 1 addition & 1 deletion src/test/ui/impl-trait/issues/issue-70971.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in variable binding
error[E0562]: `impl Trait` isn't allowed within variable binding
--> $DIR/issue-70971.rs:2:14
|
LL | let x : (impl Copy,) = (true,);
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/impl-trait/issues/issue-79099.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
struct Bug {
V1: [(); {
let f: impl core::future::Future<Output = u8> = async { 1 };
//~^ `impl Trait` only allowed in function and inherent method return types
//~^ `impl Trait` isn't allowed within variable binding [E0562]
//~| expected identifier
1
}],
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/impl-trait/issues/issue-79099.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ LL | let f: impl core::future::Future<Output = u8> = async { 1 };
= help: pass `--edition 2021` to `rustc`
= note: for more on editions, read https://doc.rust-lang.org/edition-guide

error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in variable binding
error[E0562]: `impl Trait` isn't allowed within variable binding
--> $DIR/issue-79099.rs:3:16
|
LL | let f: impl core::future::Future<Output = u8> = async { 1 };
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` only allowed in function and inherent method return types
//~^ ERROR `impl Trait` isn't allowed within type [E0562]

type Result<T, E = impl std::error::Error> = std::result::Result<T, E>;
//~^ ERROR `impl Trait` only allowed in function and inherent method return types
//~^ ERROR `impl Trait` isn't allowed within type [E0562]

// 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` only allowed in function and inherent method return types, not in type
error[E0562]: `impl Trait` isn't allowed within type
--> $DIR/issue-83929-impl-trait-in-generic-default.rs:1:16
|
LL | struct Foo<T = impl Copy>(T);
| ^^^^^^^^^

error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
error[E0562]: `impl Trait` isn't allowed within type
--> $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
2 changes: 1 addition & 1 deletion src/test/ui/impl-trait/issues/issue-84919.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ impl Trait for () {}

fn foo<'a: 'a>() {
let _x: impl Trait = ();
//~^ `impl Trait` only allowed in function and inherent method return types
//~^ `impl Trait` isn't allowed within variable binding [E0562]
}

fn main() {}
2 changes: 1 addition & 1 deletion src/test/ui/impl-trait/issues/issue-84919.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in variable binding
error[E0562]: `impl Trait` isn't allowed within variable binding
--> $DIR/issue-84919.rs:5:13
|
LL | let _x: impl Trait = ();
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/impl-trait/issues/issue-86642.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
static x: impl Fn(&str) -> Result<&str, ()> = move |source| {
//~^ `impl Trait` only allowed in function and inherent method return types
//~^ `impl Trait` isn't allowed within type [E0562]
let res = (move |source| Ok(source))(source);
let res = res.or((move |source| Ok(source))(source));
res
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/impl-trait/issues/issue-86642.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
error[E0562]: `impl Trait` isn't allowed within type
--> $DIR/issue-86642.rs:1:11
|
LL | static x: impl Fn(&str) -> Result<&str, ()> = move |source| {
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/impl-trait/issues/issue-87295.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ impl<F> Struct<F> {

fn main() {
let _do_not_waste: Struct<impl Trait<Output = i32>> = Struct::new(());
//~^ `impl Trait` only allowed in function and inherent method return types
//~^ `impl Trait` isn't allowed within variable binding [E0562]
}
2 changes: 1 addition & 1 deletion src/test/ui/impl-trait/issues/issue-87295.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in variable binding
error[E0562]: `impl Trait` isn't allowed within variable binding
--> $DIR/issue-87295.rs:16:31
|
LL | let _do_not_waste: Struct<impl Trait<Output = i32>> = Struct::new(());
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/impl-trait/nested_impl_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn bad_in_ret_position(x: impl Into<u32>) -> impl Into<impl Debug> { x }

fn bad_in_fn_syntax(x: fn() -> impl Into<impl Debug>) {}
//~^ ERROR nested `impl Trait` is not allowed
//~| `impl Trait` only allowed in function and inherent method return types
//~| `impl Trait` isn't allowed within `fn` pointer return [E0562]

fn bad_in_arg_position(_: impl Into<impl Debug>) { }
//~^ ERROR nested `impl Trait` is not allowed
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/impl-trait/nested_impl_trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ LL | fn bad(x: impl Into<u32>) -> impl Into<impl Debug> { x }
| | nested `impl Trait` here
| outer `impl Trait`

error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `fn` pointer return
error[E0562]: `impl Trait` isn't allowed within `fn` pointer return
--> $DIR/nested_impl_trait.rs:10:32
|
LL | fn bad_in_fn_syntax(x: fn() -> impl Into<impl Debug>) {}
Expand Down
Loading