Skip to content

Commit

Permalink
Split note, fix const/static impl trait error
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Jan 7, 2024
1 parent 0f39574 commit 7e38b70
Show file tree
Hide file tree
Showing 39 changed files with 257 additions and 129 deletions.
3 changes: 2 additions & 1 deletion compiler/rustc_ast_lowering/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ ast_lowering_misplaced_double_dot =
.note = only allowed in tuple, tuple struct, and slice patterns
ast_lowering_misplaced_impl_trait =
`impl Trait` only allowed in function and inherent method argument and return types, not in {$position}
`impl Trait` is not allowed in {$position}
.note = `impl Trait` is only allowed in arguments and return types of functions and methods
ast_lowering_misplaced_relax_trait_bound =
`?Trait` bounds are only permitted at the point where a type parameter is declared
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_ast_lowering/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ pub enum AssocTyParenthesesSub {

#[derive(Diagnostic)]
#[diag(ast_lowering_misplaced_impl_trait, code = "E0562")]
#[note]
pub struct MisplacedImplTrait<'a> {
#[primary_span]
pub span: Span,
Expand Down
10 changes: 7 additions & 3 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.lower_use_tree(use_tree, &prefix, id, vis_span, ident, attrs)
}
ItemKind::Static(box ast::StaticItem { ty: t, mutability: m, expr: e }) => {
let (ty, body_id) = self.lower_const_item(t, span, e.as_deref());
let (ty, body_id) =
self.lower_const_item(t, span, e.as_deref(), ImplTraitPosition::StaticTy);
hir::ItemKind::Static(ty, *m, body_id)
}
ItemKind::Const(box ast::ConstItem { generics, ty, expr, .. }) => {
Expand All @@ -191,7 +192,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
Const::No,
id,
&ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
|this| this.lower_const_item(ty, span, expr.as_deref()),
|this| {
this.lower_const_item(ty, span, expr.as_deref(), ImplTraitPosition::ConstTy)
},
);
hir::ItemKind::Const(ty, generics, body_id)
}
Expand Down Expand Up @@ -448,8 +451,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
ty: &Ty,
span: Span,
body: Option<&Expr>,
impl_trait_position: ImplTraitPosition,
) -> (&'hir hir::Ty<'hir>, hir::BodyId) {
let ty = self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
let ty = self.lower_ty(ty, &ImplTraitContext::Disallowed(impl_trait_position));
(ty, self.lower_const_body(span, body))
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ impl std::fmt::Display for ImplTraitPosition {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let name = match self {
ImplTraitPosition::Path => "paths",
ImplTraitPosition::Variable => "variable bindings",
ImplTraitPosition::Variable => "the type of variable bindings",
ImplTraitPosition::Trait => "traits",
ImplTraitPosition::AsyncBlock => "async blocks",
ImplTraitPosition::Bound => "bounds",
Expand All @@ -334,7 +334,7 @@ impl std::fmt::Display for ImplTraitPosition {
ImplTraitPosition::ExternFnReturn => "`extern fn` return types",
ImplTraitPosition::ClosureReturn => "closure return types",
ImplTraitPosition::PointerReturn => "`fn` pointer return types",
ImplTraitPosition::FnTraitReturn => "the return types of `Fn` trait bounds",
ImplTraitPosition::FnTraitReturn => "the return type of `Fn` trait bounds",
ImplTraitPosition::GenericDefault => "generic parameter defaults",
ImplTraitPosition::ConstTy => "const types",
ImplTraitPosition::StaticTy => "static types",
Expand Down
4 changes: 3 additions & 1 deletion tests/ui/associated-consts/issue-105330.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ LL | fn main<A: TraitWAssocConst<A=32>>() {
= note: see issue #92827 <https://github.com/rust-lang/rust/issues/92827> for more information
= help: add `#![feature(associated_const_equality)]` to the crate attributes to enable

error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in impl headers
error[E0562]: `impl Trait` is not allowed in impl headers
--> $DIR/issue-105330.rs:6:27
|
LL | impl TraitWAssocConst for impl Demo {
| ^^^^^^^^^
|
= note: `impl Trait` is only allowed in arguments and return types of functions and methods

error[E0131]: `main` function is not allowed to have generic parameters
--> $DIR/issue-105330.rs:15:8
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/feature-gates/feature-gate-associated_type_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,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 argument and return types
//~| ERROR `impl Trait` is not allowed in const types
// 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 argument and return types
//~| ERROR `impl Trait` is not allowed in static types
// 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 argument and return types
//~| ERROR `impl Trait` is not allowed in the type of variable bindings
// 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,23 +115,29 @@ 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 argument and return types, not in const types
error[E0562]: `impl Trait` is not allowed in const types
--> $DIR/feature-gate-associated_type_bounds.rs:55:14
|
LL | const _cdef: impl Tr1<As1: Copy> = S1;
| ^^^^^^^^^^^^^^^^^^^
|
= note: `impl Trait` is only allowed in arguments and return types of functions and methods

error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in const types
error[E0562]: `impl Trait` is not allowed in static types
--> $DIR/feature-gate-associated_type_bounds.rs:61:15
|
LL | static _sdef: impl Tr1<As1: Copy> = S1;
| ^^^^^^^^^^^^^^^^^^^
|
= note: `impl Trait` is only allowed in arguments and return types of functions and methods

error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in variable bindings
error[E0562]: `impl Trait` is not allowed in the type of variable bindings
--> $DIR/feature-gate-associated_type_bounds.rs:68:12
|
LL | let _: impl Tr1<As1: Copy> = S1;
| ^^^^^^^^^^^^^^^^^^^
|
= note: `impl Trait` is only allowed in arguments and return types of functions and methods

error[E0277]: the trait bound `<<Self as _Tr3>::A as Iterator>::Item: Copy` is not satisfied
--> $DIR/feature-gate-associated_type_bounds.rs:12:28
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 argument and return types, not in the return types of `Fn` trait bounds
//~^ ERROR `impl Trait` is not allowed in the return type of `Fn` trait bounds
fn g() -> &'static dyn Fn() -> impl Sized { &|| () }
//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types, not in the return types of `Fn` trait bounds
//~^ ERROR `impl Trait` is not allowed in the return type of `Fn` trait bounds

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in the return types of `Fn` trait bounds
error[E0562]: `impl Trait` is not allowed in the return type of `Fn` trait bounds
--> $DIR/feature-gate-impl_trait_in_fn_trait_return.rs:1:24
|
LL | fn f() -> impl Fn() -> impl Sized { || () }
| ^^^^^^^^^^
|
= note: `impl Trait` is only allowed in arguments and return types of functions and methods
= note: see issue #99697 <https://github.com/rust-lang/rust/issues/99697> for more information
= help: add `#![feature(impl_trait_in_fn_trait_return)]` to the crate attributes to enable

error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in the return types of `Fn` trait bounds
error[E0562]: `impl Trait` is not allowed in the return type of `Fn` trait bounds
--> $DIR/feature-gate-impl_trait_in_fn_trait_return.rs:3:32
|
LL | fn g() -> &'static dyn Fn() -> impl Sized { &|| () }
| ^^^^^^^^^^
|
= note: `impl Trait` is only allowed in arguments and return types of functions and methods
= note: see issue #99697 <https://github.com/rust-lang/rust/issues/99697> for more information
= help: add `#![feature(impl_trait_in_fn_trait_return)]` to the crate attributes to enable

Expand Down
2 changes: 1 addition & 1 deletion tests/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 argument and return types
//~^ `impl Trait` is not allowed in the type of variable bindings
println!("{:?}", x);
}
4 changes: 3 additions & 1 deletion tests/ui/impl-trait/issues/issue-54600.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in variable bindings
error[E0562]: `impl Trait` is not allowed in the type of variable bindings
--> $DIR/issue-54600.rs:4:19
|
LL | let x: Option<impl Debug> = Some(44_u32);
| ^^^^^^^^^^
|
= note: `impl Trait` is only allowed in arguments and return types of functions and methods

error: aborting due to 1 previous error

Expand Down
2 changes: 1 addition & 1 deletion tests/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 argument and return types
//~^ `impl Trait` is not allowed in the type of variable bindings
}
4 changes: 3 additions & 1 deletion tests/ui/impl-trait/issues/issue-54840.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in variable bindings
error[E0562]: `impl Trait` is not allowed in the type of variable bindings
--> $DIR/issue-54840.rs:5:13
|
LL | let j: &impl Add = &i;
| ^^^^^^^^
|
= note: `impl Trait` is only allowed in arguments and return types of functions and methods

error: aborting due to 1 previous error

Expand Down
2 changes: 1 addition & 1 deletion tests/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 Coroutine<Return=!, Yield=()> {

fn main() {
let gens: [impl Coroutine<Return=!, Yield=()>;2] = [ mk_gen(), mk_gen() ];
//~^ `impl Trait` only allowed in function and inherent method argument and return types
//~^ `impl Trait` is not allowed in the type of variable bindings
}
4 changes: 3 additions & 1 deletion tests/ui/impl-trait/issues/issue-58504.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in variable bindings
error[E0562]: `impl Trait` is not allowed in the type of variable bindings
--> $DIR/issue-58504.rs:10:16
|
LL | let gens: [impl Coroutine<Return=!, Yield=()>;2] = [ mk_gen(), mk_gen() ];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `impl Trait` is only allowed in arguments and return types of functions and methods

error: aborting due to 1 previous error

Expand Down
4 changes: 2 additions & 2 deletions tests/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 argument and return types
//~^ `impl Trait` is not allowed in const types
let x: Wrap<impl Lam> = Wrap(B);
//~^ `impl Trait` only allowed in function and inherent method argument and return types
//~^ `impl Trait` is not allowed in the type of variable bindings
x.0
};

Expand Down
8 changes: 6 additions & 2 deletions tests/ui/impl-trait/issues/issue-58956.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in const types
error[E0562]: `impl Trait` is not allowed in const types
--> $DIR/issue-58956.rs:7:11
|
LL | const _A: impl Lam = {
| ^^^^^^^^
|
= note: `impl Trait` is only allowed in arguments and return types of functions and methods

error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in variable bindings
error[E0562]: `impl Trait` is not allowed in the type of variable bindings
--> $DIR/issue-58956.rs:9:17
|
LL | let x: Wrap<impl Lam> = Wrap(B);
| ^^^^^^^^
|
= note: `impl Trait` is only allowed in arguments and return types of functions and methods

error: aborting due to 2 previous errors

Expand Down
2 changes: 1 addition & 1 deletion tests/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 argument and return types
//~^ `impl Trait` is not allowed in the type of variable bindings
}
4 changes: 3 additions & 1 deletion tests/ui/impl-trait/issues/issue-70971.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in variable bindings
error[E0562]: `impl Trait` is not allowed in the type of variable bindings
--> $DIR/issue-70971.rs:2:14
|
LL | let x : (impl Copy,) = (true,);
| ^^^^^^^^^
|
= note: `impl Trait` is only allowed in arguments and return types of functions and methods

error: aborting due to 1 previous error

Expand Down
2 changes: 1 addition & 1 deletion tests/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 argument and return types
//~^ `impl Trait` is not allowed in the type of variable bindings
//~| expected identifier
1
}],
Expand Down
4 changes: 3 additions & 1 deletion tests/ui/impl-trait/issues/issue-79099.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ 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 argument and return types, not in variable bindings
error[E0562]: `impl Trait` is not allowed in the type of variable bindings
--> $DIR/issue-79099.rs:3:16
|
LL | let f: impl core::future::Future<Output = u8> = async { 1 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `impl Trait` is only allowed in arguments and return types of functions and methods

error: aborting due to 2 previous errors

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 argument and return types
//~^ ERROR `impl Trait` is not allowed in generic parameter defaults

type Result<T, E = impl std::error::Error> = std::result::Result<T, E>;
//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types
//~^ ERROR `impl Trait` is not allowed in generic parameter defaults

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

error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in generic parameter defaults
error[E0562]: `impl Trait` is not allowed in generic parameter defaults
--> $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>;
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: `impl Trait` is only allowed in arguments and return types of functions and methods

error: aborting due to 2 previous errors

Expand Down
2 changes: 1 addition & 1 deletion tests/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 argument and return types
//~^ `impl Trait` is not allowed in the type of variable bindings
}

fn main() {}
4 changes: 3 additions & 1 deletion tests/ui/impl-trait/issues/issue-84919.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in variable bindings
error[E0562]: `impl Trait` is not allowed in the type of variable bindings
--> $DIR/issue-84919.rs:5:13
|
LL | let _x: impl Trait = ();
| ^^^^^^^^^^
|
= note: `impl Trait` is only allowed in arguments and return types of functions and methods

error: aborting due to 1 previous error

Expand Down
2 changes: 1 addition & 1 deletion tests/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 argument and return types
//~^ `impl Trait` is not allowed in static types
let res = (move |source| Ok(source))(source);
let res = res.or((move |source| Ok(source))(source));
res
Expand Down
4 changes: 3 additions & 1 deletion tests/ui/impl-trait/issues/issue-86642.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in const types
error[E0562]: `impl Trait` is not allowed in static types
--> $DIR/issue-86642.rs:1:11
|
LL | static x: impl Fn(&str) -> Result<&str, ()> = move |source| {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `impl Trait` is only allowed in arguments and return types of functions and methods

error: aborting due to 1 previous error

Expand Down
2 changes: 1 addition & 1 deletion tests/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 argument and return types
//~^ `impl Trait` is not allowed in the type of variable bindings
}
4 changes: 3 additions & 1 deletion tests/ui/impl-trait/issues/issue-87295.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in variable bindings
error[E0562]: `impl Trait` is not allowed in the type of variable bindings
--> $DIR/issue-87295.rs:16:31
|
LL | let _do_not_waste: Struct<impl Trait<Output = i32>> = Struct::new(());
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `impl Trait` is only allowed in arguments and return types of functions and methods

error: aborting due to 1 previous error

Expand Down
Loading

0 comments on commit 7e38b70

Please sign in to comment.