Skip to content

Commit 7e38b70

Browse files
Split note, fix const/static impl trait error
1 parent 0f39574 commit 7e38b70

39 files changed

+257
-129
lines changed

compiler/rustc_ast_lowering/messages.ftl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ ast_lowering_misplaced_double_dot =
106106
.note = only allowed in tuple, tuple struct, and slice patterns
107107
108108
ast_lowering_misplaced_impl_trait =
109-
`impl Trait` only allowed in function and inherent method argument and return types, not in {$position}
109+
`impl Trait` is not allowed in {$position}
110+
.note = `impl Trait` is only allowed in arguments and return types of functions and methods
110111
111112
ast_lowering_misplaced_relax_trait_bound =
112113
`?Trait` bounds are only permitted at the point where a type parameter is declared

compiler/rustc_ast_lowering/src/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ pub enum AssocTyParenthesesSub {
9090

9191
#[derive(Diagnostic)]
9292
#[diag(ast_lowering_misplaced_impl_trait, code = "E0562")]
93+
#[note]
9394
pub struct MisplacedImplTrait<'a> {
9495
#[primary_span]
9596
pub span: Span,

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
182182
self.lower_use_tree(use_tree, &prefix, id, vis_span, ident, attrs)
183183
}
184184
ItemKind::Static(box ast::StaticItem { ty: t, mutability: m, expr: e }) => {
185-
let (ty, body_id) = self.lower_const_item(t, span, e.as_deref());
185+
let (ty, body_id) =
186+
self.lower_const_item(t, span, e.as_deref(), ImplTraitPosition::StaticTy);
186187
hir::ItemKind::Static(ty, *m, body_id)
187188
}
188189
ItemKind::Const(box ast::ConstItem { generics, ty, expr, .. }) => {
@@ -191,7 +192,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
191192
Const::No,
192193
id,
193194
&ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
194-
|this| this.lower_const_item(ty, span, expr.as_deref()),
195+
|this| {
196+
this.lower_const_item(ty, span, expr.as_deref(), ImplTraitPosition::ConstTy)
197+
},
195198
);
196199
hir::ItemKind::Const(ty, generics, body_id)
197200
}
@@ -448,8 +451,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
448451
ty: &Ty,
449452
span: Span,
450453
body: Option<&Expr>,
454+
impl_trait_position: ImplTraitPosition,
451455
) -> (&'hir hir::Ty<'hir>, hir::BodyId) {
452-
let ty = self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
456+
let ty = self.lower_ty(ty, &ImplTraitContext::Disallowed(impl_trait_position));
453457
(ty, self.lower_const_body(span, body))
454458
}
455459

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ impl std::fmt::Display for ImplTraitPosition {
322322
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
323323
let name = match self {
324324
ImplTraitPosition::Path => "paths",
325-
ImplTraitPosition::Variable => "variable bindings",
325+
ImplTraitPosition::Variable => "the type of variable bindings",
326326
ImplTraitPosition::Trait => "traits",
327327
ImplTraitPosition::AsyncBlock => "async blocks",
328328
ImplTraitPosition::Bound => "bounds",
@@ -334,7 +334,7 @@ impl std::fmt::Display for ImplTraitPosition {
334334
ImplTraitPosition::ExternFnReturn => "`extern fn` return types",
335335
ImplTraitPosition::ClosureReturn => "closure return types",
336336
ImplTraitPosition::PointerReturn => "`fn` pointer return types",
337-
ImplTraitPosition::FnTraitReturn => "the return types of `Fn` trait bounds",
337+
ImplTraitPosition::FnTraitReturn => "the return type of `Fn` trait bounds",
338338
ImplTraitPosition::GenericDefault => "generic parameter defaults",
339339
ImplTraitPosition::ConstTy => "const types",
340340
ImplTraitPosition::StaticTy => "static types",

tests/ui/associated-consts/issue-105330.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ LL | fn main<A: TraitWAssocConst<A=32>>() {
3333
= note: see issue #92827 <https://github.com/rust-lang/rust/issues/92827> for more information
3434
= help: add `#![feature(associated_const_equality)]` to the crate attributes to enable
3535

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

4244
error[E0131]: `main` function is not allowed to have generic parameters
4345
--> $DIR/issue-105330.rs:15:8

tests/ui/feature-gates/feature-gate-associated_type_bounds.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,20 @@ fn _rpit_dyn() -> Box<dyn Tr1<As1: Copy>> { Box::new(S1) }
5454

5555
const _cdef: impl Tr1<As1: Copy> = S1;
5656
//~^ ERROR associated type bounds are unstable
57-
//~| ERROR `impl Trait` only allowed in function and inherent method argument and return types
57+
//~| ERROR `impl Trait` is not allowed in const types
5858
// FIXME: uncomment when `impl_trait_in_bindings` feature is fixed.
5959
// const _cdef_dyn: &dyn Tr1<As1: Copy> = &S1;
6060

6161
static _sdef: impl Tr1<As1: Copy> = S1;
6262
//~^ ERROR associated type bounds are unstable
63-
//~| ERROR `impl Trait` only allowed in function and inherent method argument and return types
63+
//~| ERROR `impl Trait` is not allowed in static types
6464
// FIXME: uncomment when `impl_trait_in_bindings` feature is fixed.
6565
// static _sdef_dyn: &dyn Tr1<As1: Copy> = &S1;
6666

6767
fn main() {
6868
let _: impl Tr1<As1: Copy> = S1;
6969
//~^ ERROR associated type bounds are unstable
70-
//~| ERROR `impl Trait` only allowed in function and inherent method argument and return types
70+
//~| ERROR `impl Trait` is not allowed in the type of variable bindings
7171
// FIXME: uncomment when `impl_trait_in_bindings` feature is fixed.
7272
// let _: &dyn Tr1<As1: Copy> = &S1;
7373
}

tests/ui/feature-gates/feature-gate-associated_type_bounds.stderr

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,23 +115,29 @@ LL | let _: impl Tr1<As1: Copy> = S1;
115115
= note: see issue #52662 <https://github.com/rust-lang/rust/issues/52662> for more information
116116
= help: add `#![feature(associated_type_bounds)]` to the crate attributes to enable
117117

118-
error[E0562]: `impl Trait` only allowed in function and inherent method argument and return types, not in const types
118+
error[E0562]: `impl Trait` is not allowed in const types
119119
--> $DIR/feature-gate-associated_type_bounds.rs:55:14
120120
|
121121
LL | const _cdef: impl Tr1<As1: Copy> = S1;
122122
| ^^^^^^^^^^^^^^^^^^^
123+
|
124+
= note: `impl Trait` is only allowed in arguments and return types of functions and methods
123125

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

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

136142
error[E0277]: the trait bound `<<Self as _Tr3>::A as Iterator>::Item: Copy` is not satisfied
137143
--> $DIR/feature-gate-associated_type_bounds.rs:12:28
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
fn f() -> impl Fn() -> impl Sized { || () }
2-
//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types, not in the return types of `Fn` trait bounds
2+
//~^ ERROR `impl Trait` is not allowed in the return type of `Fn` trait bounds
33
fn g() -> &'static dyn Fn() -> impl Sized { &|| () }
4-
//~^ ERROR `impl Trait` only allowed in function and inherent method argument and return types, not in the return types of `Fn` trait bounds
4+
//~^ ERROR `impl Trait` is not allowed in the return type of `Fn` trait bounds
55

66
fn main() {}

tests/ui/feature-gates/feature-gate-impl_trait_in_fn_trait_return.stderr

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

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

tests/ui/impl-trait/issues/issue-54600.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ use std::fmt::Debug;
22

33
fn main() {
44
let x: Option<impl Debug> = Some(44_u32);
5-
//~^ `impl Trait` only allowed in function and inherent method argument and return types
5+
//~^ `impl Trait` is not allowed in the type of variable bindings
66
println!("{:?}", x);
77
}

0 commit comments

Comments
 (0)