Skip to content

Commit 012bc48

Browse files
No more associated type bounds in dyn trait
1 parent 98aa362 commit 012bc48

22 files changed

+56
-339
lines changed

compiler/rustc_ast_lowering/messages.ftl

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ ast_lowering_arbitrary_expression_in_pattern =
88
99
ast_lowering_argument = argument
1010
11+
ast_lowering_assoc_ty_binding_in_dyn =
12+
associated type bounds are not allowed in `dyn` types
13+
1114
ast_lowering_assoc_ty_parentheses =
1215
parenthesized generic arguments cannot be used in associated type constraints
1316
@@ -100,9 +103,6 @@ ast_lowering_match_arm_with_no_body =
100103
`match` arm with no body
101104
.suggestion = add a body after the pattern
102105
103-
ast_lowering_misplaced_assoc_ty_binding =
104-
associated type bounds are only allowed in where clauses and function signatures, not in {$position}
105-
106106
ast_lowering_misplaced_double_dot =
107107
`..` patterns are not allowed here
108108
.note = only allowed in tuple, tuple struct, and slice patterns

compiler/rustc_ast_lowering/src/errors.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,10 @@ pub struct MisplacedImplTrait<'a> {
9494
}
9595

9696
#[derive(Diagnostic)]
97-
#[diag(ast_lowering_misplaced_assoc_ty_binding)]
98-
pub struct MisplacedAssocTyBinding<'a> {
97+
#[diag(ast_lowering_assoc_ty_binding_in_dyn)]
98+
pub struct MisplacedAssocTyBinding {
9999
#[primary_span]
100100
pub span: Span,
101-
pub position: DiagnosticArgFromDisplay<'a>,
102101
}
103102

104103
#[derive(Diagnostic, Clone, Copy)]

compiler/rustc_ast_lowering/src/lib.rs

+7-19
Original file line numberDiff line numberDiff line change
@@ -1088,12 +1088,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10881088
AssocConstraintKind::Bound { bounds } => {
10891089
enum DesugarKind {
10901090
ImplTrait,
1091-
Error(ImplTraitPosition),
1091+
Error,
10921092
Bound,
10931093
}
10941094

10951095
// Piggy-back on the `impl Trait` context to figure out the correct behavior.
10961096
let desugar_kind = match itctx {
1097+
_ if self.is_in_dyn_type => DesugarKind::Error,
1098+
10971099
// We are in the return position:
10981100
//
10991101
// fn foo() -> impl Iterator<Item: Debug>
@@ -1104,19 +1106,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11041106
ImplTraitContext::ReturnPositionOpaqueTy { .. }
11051107
| ImplTraitContext::TypeAliasesOpaqueTy { .. } => DesugarKind::ImplTrait,
11061108

1107-
// We are in the argument position, but within a dyn type:
1108-
//
1109-
// fn foo(x: dyn Iterator<Item: Debug>)
1110-
//
1111-
// so desugar to
1112-
//
1113-
// fn foo(x: dyn Iterator<Item = impl Debug>)
1114-
ImplTraitContext::Universal if self.is_in_dyn_type => DesugarKind::ImplTrait,
1115-
1116-
ImplTraitContext::Disallowed(position) if self.is_in_dyn_type => {
1117-
DesugarKind::Error(position)
1118-
}
1119-
11201109
// We are in the parameter position, but not within a dyn type:
11211110
//
11221111
// fn foo(x: impl Iterator<Item: Debug>)
@@ -1161,11 +1150,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11611150

11621151
hir::TypeBindingKind::Constraint { bounds }
11631152
}
1164-
DesugarKind::Error(position) => {
1165-
let guar = self.dcx().emit_err(errors::MisplacedAssocTyBinding {
1166-
span: constraint.span,
1167-
position: DiagnosticArgFromDisplay(&position),
1168-
});
1153+
DesugarKind::Error => {
1154+
let guar = self
1155+
.dcx()
1156+
.emit_err(errors::MisplacedAssocTyBinding { span: constraint.span });
11691157
let err_ty =
11701158
&*self.arena.alloc(self.ty(constraint.span, hir::TyKind::Err(guar)));
11711159
hir::TypeBindingKind::Equality { term: err_ty.into() }

tests/ui/associated-type-bounds/assoc-type-eq-with-dyn-atb-fail.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ impl Bar for AssocNoCopy {
2828

2929
impl Thing for AssocNoCopy {
3030
type Out = Box<dyn Bar<Assoc: Copy>>;
31+
//~^ ERROR associated type bounds are not allowed in `dyn` types
3132

3233
fn func() -> Self::Out {
33-
//~^ ERROR the trait bound `String: Copy` is not satisfied
3434
Box::new(AssocNoCopy)
3535
}
3636
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
error[E0277]: the trait bound `String: Copy` is not satisfied
2-
--> $DIR/assoc-type-eq-with-dyn-atb-fail.rs:32:18
1+
error: associated type bounds are not allowed in `dyn` types
2+
--> $DIR/assoc-type-eq-with-dyn-atb-fail.rs:30:28
33
|
4-
LL | fn func() -> Self::Out {
5-
| ^^^^^^^^^ the trait `Copy` is not implemented for `String`
4+
LL | type Out = Box<dyn Bar<Assoc: Copy>>;
5+
| ^^^^^^^^^^^
66

77
error: aborting due to 1 previous error
88

9-
For more information about this error, try `rustc --explain E0277`.

tests/ui/associated-type-bounds/bad-universal-in-dyn-in-where-clause.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ trait B {
77
fn f()
88
where
99
dyn for<'j> B<AssocType: 'j>:,
10-
//~^ ERROR associated type bounds are only allowed in where clauses and function signatures
10+
//~^ ERROR associated type bounds are not allowed in `dyn` types
1111
{
1212
}
1313

tests/ui/associated-type-bounds/bad-universal-in-dyn-in-where-clause.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: associated type bounds are only allowed in where clauses and function signatures, not in bounds
1+
error: associated type bounds are not allowed in `dyn` types
22
--> $DIR/bad-universal-in-dyn-in-where-clause.rs:9:19
33
|
44
LL | dyn for<'j> B<AssocType: 'j>:,

tests/ui/associated-type-bounds/bad-universal-in-impl-sig.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ trait Trait2 {}
88

99
// It's not possible to insert a universal `impl Trait` here!
1010
impl dyn Trait<Item: Trait2> {}
11-
//~^ ERROR associated type bounds are only allowed in where clauses and function signatures
11+
//~^ ERROR associated type bounds are not allowed in `dyn` types
1212

1313
fn main() {}

tests/ui/associated-type-bounds/bad-universal-in-impl-sig.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: associated type bounds are only allowed in where clauses and function signatures, not in impl headers
1+
error: associated type bounds are not allowed in `dyn` types
22
--> $DIR/bad-universal-in-impl-sig.rs:10:16
33
|
44
LL | impl dyn Trait<Item: Trait2> {}

tests/ui/associated-type-bounds/duplicate.rs

-7
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,4 @@ trait TRA3 {
264264
//~^ ERROR the value of the associated type `Item` in trait `Iterator` is already specified [E0719]
265265
}
266266

267-
type TADyn1 = dyn Iterator<Item: Copy, Item: Send>;
268-
//~^ ERROR the value of the associated type `Item` in trait `Iterator` is already specified [E0719]
269-
type TADyn2 = Box<dyn Iterator<Item: Copy, Item: Copy>>;
270-
//~^ ERROR the value of the associated type `Item` in trait `Iterator` is already specified [E0719]
271-
type TADyn3 = dyn Iterator<Item: 'static, Item: 'static>;
272-
//~^ ERROR the value of the associated type `Item` in trait `Iterator` is already specified [E0719]
273-
274267
fn main() {}

tests/ui/associated-type-bounds/duplicate.stderr

+1-25
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,6 @@ LL | struct SI1<T: Iterator<Item: Copy, Item: Send>> {
66
| |
77
| `Item` bound here first
88

9-
error[E0719]: the value of the associated type `Item` in trait `Iterator` is already specified
10-
--> $DIR/duplicate.rs:267:40
11-
|
12-
LL | type TADyn1 = dyn Iterator<Item: Copy, Item: Send>;
13-
| ---------- ^^^^^^^^^^ re-bound here
14-
| |
15-
| `Item` bound here first
16-
17-
error[E0719]: the value of the associated type `Item` in trait `Iterator` is already specified
18-
--> $DIR/duplicate.rs:269:44
19-
|
20-
LL | type TADyn2 = Box<dyn Iterator<Item: Copy, Item: Copy>>;
21-
| ---------- ^^^^^^^^^^ re-bound here
22-
| |
23-
| `Item` bound here first
24-
25-
error[E0719]: the value of the associated type `Item` in trait `Iterator` is already specified
26-
--> $DIR/duplicate.rs:271:43
27-
|
28-
LL | type TADyn3 = dyn Iterator<Item: 'static, Item: 'static>;
29-
| ------------- ^^^^^^^^^^^^^ re-bound here
30-
| |
31-
| `Item` bound here first
32-
339
error[E0719]: the value of the associated type `Item` in trait `Iterator` is already specified
3410
--> $DIR/duplicate.rs:11:36
3511
|
@@ -665,7 +641,7 @@ help: consider further restricting the associated type
665641
LL | trait TRA2 where <<Self as TRA2>::A as Iterator>::Item: Copy {
666642
| +++++++++++++++++++++++++++++++++++++++++++++++++
667643

668-
error: aborting due to 78 previous errors
644+
error: aborting due to 75 previous errors
669645

670646
Some errors have detailed explanations: E0277, E0282, E0719.
671647
For more information about an error, try `rustc --explain E0277`.

tests/ui/associated-type-bounds/dyn-impl-trait-type.rs

-66
This file was deleted.

tests/ui/associated-type-bounds/dyn-rpit-and-let.rs

-73
This file was deleted.

tests/ui/associated-type-bounds/elision.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
// The same thing should happen for constraints in dyn trait.
55
fn f(x: &mut dyn Iterator<Item: Iterator<Item = &'_ ()>>) -> Option<&'_ ()> { x.next() }
6-
//~^ ERROR missing lifetime specifier
7-
//~| ERROR mismatched types
6+
//~^ ERROR associated type bounds are not allowed in `dyn` types
7+
//~| ERROR missing lifetime specifier
88

99
fn main() {}

tests/ui/associated-type-bounds/elision.stderr

+4-11
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,12 @@ help: consider introducing a named lifetime parameter
1010
LL | fn f<'a>(x: &'a mut dyn Iterator<Item: Iterator<Item = &'a ()>>) -> Option<&'a ()> { x.next() }
1111
| ++++ ++ ~~ ~~
1212

13-
error[E0308]: mismatched types
14-
--> $DIR/elision.rs:5:79
13+
error: associated type bounds are not allowed in `dyn` types
14+
--> $DIR/elision.rs:5:27
1515
|
1616
LL | fn f(x: &mut dyn Iterator<Item: Iterator<Item = &'_ ()>>) -> Option<&'_ ()> { x.next() }
17-
| ----------------------------- -------------- ^^^^^^^^ expected `Option<&()>`, found `Option<impl Iterator<Item = &'_ ()>>`
18-
| | |
19-
| | expected `Option<&()>` because of return type
20-
| found this type parameter
21-
|
22-
= note: expected enum `Option<&()>`
23-
found enum `Option<impl Iterator<Item = &'_ ()>>`
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2418

2519
error: aborting due to 2 previous errors
2620

27-
Some errors have detailed explanations: E0106, E0308.
28-
For more information about an error, try `rustc --explain E0106`.
21+
For more information about this error, try `rustc --explain E0106`.

0 commit comments

Comments
 (0)