Skip to content

Commit 973bbfb

Browse files
No more associated type bounds in dyn trait
1 parent b5c46dc commit 973bbfb

22 files changed

+55
-373
lines changed

Diff for: 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

Diff for: 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)]

Diff for: compiler/rustc_ast_lowering/src/lib.rs

+6-53
Original file line numberDiff line numberDiff line change
@@ -1085,33 +1085,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10851085
}
10861086
AssocConstraintKind::Bound { bounds } => {
10871087
enum DesugarKind {
1088-
ImplTrait,
1089-
Error(ImplTraitPosition),
1088+
Error,
10901089
Bound,
10911090
}
10921091

10931092
// Piggy-back on the `impl Trait` context to figure out the correct behavior.
10941093
let desugar_kind = match itctx {
1095-
// in an argument, RPIT, or TAIT, if we are within a dyn type:
1096-
//
1097-
// fn foo(x: dyn Iterator<Item: Debug>)
1098-
//
1099-
// then desugar to:
1100-
//
1101-
// fn foo(x: dyn Iterator<Item = impl Debug>)
1102-
//
1103-
// This is because dyn traits must have all of their associated types specified.
1104-
ImplTraitContext::ReturnPositionOpaqueTy { .. }
1105-
| ImplTraitContext::TypeAliasesOpaqueTy { .. }
1106-
| ImplTraitContext::Universal
1107-
if self.is_in_dyn_type =>
1108-
{
1109-
DesugarKind::ImplTrait
1110-
}
1111-
1112-
ImplTraitContext::Disallowed(position) if self.is_in_dyn_type => {
1113-
DesugarKind::Error(position)
1114-
}
1094+
_ if self.is_in_dyn_type => DesugarKind::Error,
11151095

11161096
// We are in the parameter position, but not within a dyn type:
11171097
//
@@ -1124,44 +1104,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11241104
};
11251105

11261106
match desugar_kind {
1127-
DesugarKind::ImplTrait => {
1128-
// Desugar `AssocTy: Bounds` into `AssocTy = impl Bounds`. We do this by
1129-
// constructing the HIR for `impl bounds...` and then lowering that.
1130-
1131-
let impl_trait_node_id = self.next_node_id();
1132-
// Shift `impl Trait` lifetime captures from the associated type bound's
1133-
// node id to the opaque node id, so that the opaque can actually use
1134-
// these lifetime bounds.
1135-
self.resolver
1136-
.remap_extra_lifetime_params(constraint.id, impl_trait_node_id);
1137-
1138-
self.with_dyn_type_scope(false, |this| {
1139-
let node_id = this.next_node_id();
1140-
let ty = this.lower_ty(
1141-
&Ty {
1142-
id: node_id,
1143-
kind: TyKind::ImplTrait(impl_trait_node_id, bounds.clone()),
1144-
span: this.lower_span(constraint.span),
1145-
tokens: None,
1146-
},
1147-
itctx,
1148-
);
1149-
1150-
hir::TypeBindingKind::Equality { term: ty.into() }
1151-
})
1152-
}
11531107
DesugarKind::Bound => {
11541108
// Desugar `AssocTy: Bounds` into a type binding where the
11551109
// later desugars into a trait predicate.
11561110
let bounds = self.lower_param_bounds(bounds, itctx);
11571111

11581112
hir::TypeBindingKind::Constraint { bounds }
11591113
}
1160-
DesugarKind::Error(position) => {
1161-
let guar = self.dcx().emit_err(errors::MisplacedAssocTyBinding {
1162-
span: constraint.span,
1163-
position: DiagnosticArgFromDisplay(&position),
1164-
});
1114+
DesugarKind::Error => {
1115+
let guar = self
1116+
.dcx()
1117+
.emit_err(errors::MisplacedAssocTyBinding { span: constraint.span });
11651118
let err_ty =
11661119
&*self.arena.alloc(self.ty(constraint.span, hir::TyKind::Err(guar)));
11671120
hir::TypeBindingKind::Equality { term: err_ty.into() }

Diff for: 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`.

Diff for: 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

Diff for: 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>:,

Diff for: 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() {}

Diff for: 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> {}

Diff for: tests/ui/associated-type-bounds/duplicate.rs

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

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

Diff for: 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:264: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:266: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:268: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
|
@@ -631,7 +607,7 @@ LL | Self: Iterator<Item: 'static, Item: 'static>,
631607
|
632608
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
633609

634-
error: aborting due to 75 previous errors
610+
error: aborting due to 72 previous errors
635611

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

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

-66
This file was deleted.

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

-73
This file was deleted.

Diff for: 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() {}

Diff for: 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)