Skip to content

Commit

Permalink
No more associated type bounds in dyn trait
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Feb 8, 2024
1 parent 1280928 commit f6a17c5
Show file tree
Hide file tree
Showing 22 changed files with 57 additions and 340 deletions.
6 changes: 3 additions & 3 deletions compiler/rustc_ast_lowering/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ ast_lowering_arbitrary_expression_in_pattern =
ast_lowering_argument = argument
ast_lowering_assoc_ty_binding_in_dyn =
associated type bounds are not allowed in `dyn` types
ast_lowering_assoc_ty_parentheses =
parenthesized generic arguments cannot be used in associated type constraints
Expand Down Expand Up @@ -100,9 +103,6 @@ ast_lowering_match_arm_with_no_body =
`match` arm with no body
.suggestion = add a body after the pattern
ast_lowering_misplaced_assoc_ty_binding =
associated type bounds are only allowed in where clauses and function signatures, not in {$position}
ast_lowering_misplaced_double_dot =
`..` patterns are not allowed here
.note = only allowed in tuple, tuple struct, and slice patterns
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_ast_lowering/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,10 @@ pub struct MisplacedImplTrait<'a> {
}

#[derive(Diagnostic)]
#[diag(ast_lowering_misplaced_assoc_ty_binding)]
pub struct MisplacedAssocTyBinding<'a> {
#[diag(ast_lowering_assoc_ty_binding_in_dyn)]
pub struct MisplacedAssocTyBinding {
#[primary_span]
pub span: Span,
pub position: DiagnosticArgFromDisplay<'a>,
}

#[derive(Diagnostic, Clone, Copy)]
Expand Down
28 changes: 8 additions & 20 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1086,14 +1086,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
hir::TypeBindingKind::Equality { term }
}
AssocConstraintKind::Bound { bounds } => {
enum DesugarKind<'a> {
enum DesugarKind {
ImplTrait,
Error(&'a ImplTraitPosition),
Error,
Bound,
}

// Piggy-back on the `impl Trait` context to figure out the correct behavior.
let desugar_kind = match itctx {
_ if self.is_in_dyn_type => DesugarKind::Error,

// We are in the return position:
//
// fn foo() -> impl Iterator<Item: Debug>
Expand All @@ -1104,19 +1106,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
ImplTraitContext::ReturnPositionOpaqueTy { .. }
| ImplTraitContext::TypeAliasesOpaqueTy { .. } => DesugarKind::ImplTrait,

// We are in the argument position, but within a dyn type:
//
// fn foo(x: dyn Iterator<Item: Debug>)
//
// so desugar to
//
// fn foo(x: dyn Iterator<Item = impl Debug>)
ImplTraitContext::Universal if self.is_in_dyn_type => DesugarKind::ImplTrait,

ImplTraitContext::Disallowed(position) if self.is_in_dyn_type => {
DesugarKind::Error(position)
}

// We are in the parameter position, but not within a dyn type:
//
// fn foo(x: impl Iterator<Item: Debug>)
Expand Down Expand Up @@ -1161,11 +1150,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {

hir::TypeBindingKind::Constraint { bounds }
}
DesugarKind::Error(position) => {
let guar = self.dcx().emit_err(errors::MisplacedAssocTyBinding {
span: constraint.span,
position: DiagnosticArgFromDisplay(position),
});
DesugarKind::Error => {
let guar = self
.dcx()
.emit_err(errors::MisplacedAssocTyBinding { span: constraint.span });
let err_ty =
&*self.arena.alloc(self.ty(constraint.span, hir::TyKind::Err(guar)));
hir::TypeBindingKind::Equality { term: err_ty.into() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ impl Bar for AssocNoCopy {

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

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

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ trait B {
fn f()
where
dyn for<'j> B<AssocType: 'j>:,
//~^ ERROR associated type bounds are only allowed in where clauses and function signatures
//~^ ERROR associated type bounds are not allowed in `dyn` types
{
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: associated type bounds are only allowed in where clauses and function signatures, not in bounds
error: associated type bounds are not allowed in `dyn` types
--> $DIR/bad-universal-in-dyn-in-where-clause.rs:9:19
|
LL | dyn for<'j> B<AssocType: 'j>:,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ trait Trait2 {}

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

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: associated type bounds are only allowed in where clauses and function signatures, not in impl headers
error: associated type bounds are not allowed in `dyn` types
--> $DIR/bad-universal-in-impl-sig.rs:10:16
|
LL | impl dyn Trait<Item: Trait2> {}
Expand Down
7 changes: 0 additions & 7 deletions tests/ui/associated-type-bounds/duplicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,4 @@ trait TRA3 {
//~^ ERROR the value of the associated type `Item` in trait `Iterator` is already specified [E0719]
}

type TADyn1 = dyn Iterator<Item: Copy, Item: Send>;
//~^ ERROR the value of the associated type `Item` in trait `Iterator` is already specified [E0719]
type TADyn2 = Box<dyn Iterator<Item: Copy, Item: Copy>>;
//~^ ERROR the value of the associated type `Item` in trait `Iterator` is already specified [E0719]
type TADyn3 = dyn Iterator<Item: 'static, Item: 'static>;
//~^ ERROR the value of the associated type `Item` in trait `Iterator` is already specified [E0719]

fn main() {}
26 changes: 1 addition & 25 deletions tests/ui/associated-type-bounds/duplicate.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,6 @@ LL | struct SI1<T: Iterator<Item: Copy, Item: Send>> {
| |
| `Item` bound here first

error[E0719]: the value of the associated type `Item` in trait `Iterator` is already specified
--> $DIR/duplicate.rs:267:40
|
LL | type TADyn1 = dyn Iterator<Item: Copy, Item: Send>;
| ---------- ^^^^^^^^^^ re-bound here
| |
| `Item` bound here first

error[E0719]: the value of the associated type `Item` in trait `Iterator` is already specified
--> $DIR/duplicate.rs:269:44
|
LL | type TADyn2 = Box<dyn Iterator<Item: Copy, Item: Copy>>;
| ---------- ^^^^^^^^^^ re-bound here
| |
| `Item` bound here first

error[E0719]: the value of the associated type `Item` in trait `Iterator` is already specified
--> $DIR/duplicate.rs:271:43
|
LL | type TADyn3 = dyn Iterator<Item: 'static, Item: 'static>;
| ------------- ^^^^^^^^^^^^^ re-bound here
| |
| `Item` bound here first

error[E0719]: the value of the associated type `Item` in trait `Iterator` is already specified
--> $DIR/duplicate.rs:11:36
|
Expand Down Expand Up @@ -665,7 +641,7 @@ help: consider further restricting the associated type
LL | trait TRA2 where <<Self as TRA2>::A as Iterator>::Item: Copy {
| +++++++++++++++++++++++++++++++++++++++++++++++++

error: aborting due to 78 previous errors
error: aborting due to 75 previous errors

Some errors have detailed explanations: E0277, E0282, E0719.
For more information about an error, try `rustc --explain E0277`.
66 changes: 0 additions & 66 deletions tests/ui/associated-type-bounds/dyn-impl-trait-type.rs

This file was deleted.

73 changes: 0 additions & 73 deletions tests/ui/associated-type-bounds/dyn-rpit-and-let.rs

This file was deleted.

4 changes: 2 additions & 2 deletions tests/ui/associated-type-bounds/elision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

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

fn main() {}
15 changes: 4 additions & 11 deletions tests/ui/associated-type-bounds/elision.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,12 @@ help: consider introducing a named lifetime parameter
LL | fn f<'a>(x: &'a mut dyn Iterator<Item: Iterator<Item = &'a ()>>) -> Option<&'a ()> { x.next() }
| ++++ ++ ~~ ~~

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

error: aborting due to 2 previous errors

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

0 comments on commit f6a17c5

Please sign in to comment.