Skip to content

Commit

Permalink
Dont create object type when more than one principal is present
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Nov 23, 2024
1 parent ff1737b commit 898ccdb
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,16 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {

let (mut auto_traits, regular_traits): (Vec<_>, Vec<_>) =
expanded_traits.partition(|i| tcx.trait_is_auto(i.trait_ref().def_id()));

// We don't support >1 principal
if regular_traits.len() > 1 {
let _ = self.report_trait_object_addition_traits_error(&regular_traits);
} else if regular_traits.is_empty() && auto_traits.is_empty() {
let reported = self.report_trait_object_with_no_traits_error(span, &trait_bounds);
return Ty::new_error(tcx, reported);
let guar = self.report_trait_object_addition_traits_error(&regular_traits);
return Ty::new_error(tcx, guar);
}
// We don't support empty trait objects.
if regular_traits.is_empty() && auto_traits.is_empty() {
let guar = self.report_trait_object_with_no_traits_error(span, &trait_bounds);
return Ty::new_error(tcx, guar);
}

// Check that there are no gross dyn-compatibility violations;
Expand Down
5 changes: 1 addition & 4 deletions tests/ui/associated-types/issue-22560.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ trait Sub<Rhs=Self> {
}

type Test = dyn Add + Sub;
//~^ ERROR E0393
//~| ERROR E0191
//~| ERROR E0393
//~| ERROR E0225
//~^ ERROR E0225

fn main() { }
54 changes: 2 additions & 52 deletions tests/ui/associated-types/issue-22560.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,6 @@ LL | type Test = dyn Add + Sub;
= help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Add + Sub {}`
= note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>

error[E0191]: the value of the associated types `Output` in `Add`, `Output` in `Sub` must be specified
--> $DIR/issue-22560.rs:9:17
|
LL | type Output;
| ----------- `Output` defined here
...
LL | type Output;
| ----------- `Output` defined here
...
LL | type Test = dyn Add + Sub;
| ^^^ ^^^ associated type `Output` must be specified
| |
| associated type `Output` must be specified
|
help: specify the associated types
|
LL | type Test = dyn Add<Output = Type> + Sub<Output = Type>;
| ~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~

error[E0393]: the type parameter `Rhs` must be explicitly specified
--> $DIR/issue-22560.rs:9:17
|
LL | trait Add<Rhs=Self> {
| ------------------- type parameter `Rhs` must be specified for this
...
LL | type Test = dyn Add + Sub;
| ^^^
|
= note: because of the default `Self` reference, type parameters must be specified on object types
help: set the type parameter to the desired type
|
LL | type Test = dyn Add<Rhs> + Sub;
| +++++

error[E0393]: the type parameter `Rhs` must be explicitly specified
--> $DIR/issue-22560.rs:9:23
|
LL | trait Sub<Rhs=Self> {
| ------------------- type parameter `Rhs` must be specified for this
...
LL | type Test = dyn Add + Sub;
| ^^^
|
= note: because of the default `Self` reference, type parameters must be specified on object types
help: set the type parameter to the desired type
|
LL | type Test = dyn Add + Sub<Rhs>;
| +++++

error: aborting due to 4 previous errors
error: aborting due to 1 previous error

Some errors have detailed explanations: E0191, E0225, E0393.
For more information about an error, try `rustc --explain E0191`.
For more information about this error, try `rustc --explain E0225`.
4 changes: 0 additions & 4 deletions tests/ui/associated-types/missing-associated-types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,12 @@ trait Fine<Rhs>: Div<Rhs, Output = Rhs> {}

type Foo<Rhs> = dyn Add<Rhs> + Sub<Rhs> + X<Rhs> + Y<Rhs>;
//~^ ERROR only auto traits can be used as additional traits in a trait object
//~| ERROR the value of the associated types
type Bar<Rhs> = dyn Add<Rhs> + Sub<Rhs> + X<Rhs> + Z<Rhs>;
//~^ ERROR only auto traits can be used as additional traits in a trait object
//~| ERROR the value of the associated types
type Baz<Rhs> = dyn Add<Rhs> + Sub<Rhs> + Y<Rhs>;
//~^ ERROR only auto traits can be used as additional traits in a trait object
//~| ERROR the value of the associated types
type Bat<Rhs> = dyn Add<Rhs> + Sub<Rhs> + Fine<Rhs>;
//~^ ERROR only auto traits can be used as additional traits in a trait object
//~| ERROR the value of the associated types
type Bal<Rhs> = dyn X<Rhs>;
//~^ ERROR the value of the associated types

Expand Down
83 changes: 5 additions & 78 deletions tests/ui/associated-types/missing-associated-types.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,8 @@ LL | type Foo<Rhs> = dyn Add<Rhs> + Sub<Rhs> + X<Rhs> + Y<Rhs>;
= help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Add<Rhs> + Sub<Rhs> + X<Rhs> + Y<Rhs> {}`
= note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>

error[E0191]: the value of the associated types `A` in `Y`, `Output` in `Add`, `Output` in `Mul`, `Output` in `Sub` must be specified
--> $DIR/missing-associated-types.rs:12:21
|
LL | type A;
| ------ `A` defined here
...
LL | type Foo<Rhs> = dyn Add<Rhs> + Sub<Rhs> + X<Rhs> + Y<Rhs>;
| ^^^^^^^^ ^^^^^^^^ ^^^^^^ ^^^^^^ associated type `A` must be specified
| | | |
| | | associated type `Output` must be specified
| | associated type `Output` must be specified
| associated type `Output` must be specified
|
help: specify the associated types
|
LL | type Foo<Rhs> = dyn Add<Rhs, Output = Type> + Sub<Rhs, Output = Type> + X<Rhs, Output = Type> + Y<Rhs, A = Type>;
| ~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~

error[E0225]: only auto traits can be used as additional traits in a trait object
--> $DIR/missing-associated-types.rs:15:32
--> $DIR/missing-associated-types.rs:14:32
|
LL | type Bar<Rhs> = dyn Add<Rhs> + Sub<Rhs> + X<Rhs> + Z<Rhs>;
| -------- ^^^^^^^^ additional non-auto trait
Expand All @@ -38,33 +20,8 @@ LL | type Bar<Rhs> = dyn Add<Rhs> + Sub<Rhs> + X<Rhs> + Z<Rhs>;
= help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Add<Rhs> + Sub<Rhs> + X<Rhs> + Z<Rhs> {}`
= note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>

error[E0191]: the value of the associated types `A` and `B` in `Z`, `Output` and `Output` in `Div`, `Output` in `Add`, `Output` in `Mul`, `Output` in `Sub` must be specified
--> $DIR/missing-associated-types.rs:15:21
|
LL | type A;
| ------ `A` defined here
LL | type B;
| ------ `B` defined here
...
LL | type Bar<Rhs> = dyn Add<Rhs> + Sub<Rhs> + X<Rhs> + Z<Rhs>;
| ^^^^^^^^ ^^^^^^^^ ^^^^^^ ^^^^^^ associated types `A`, `B`, `Output` must be specified
| | | |
| | | associated types `Output` (from trait `Div`), `Output` (from trait `Mul`) must be specified
| | associated type `Output` must be specified
| associated type `Output` must be specified
|
help: consider introducing a new type parameter, adding `where` constraints using the fully-qualified path to the associated types
--> $DIR/missing-associated-types.rs:15:43
|
LL | type Bar<Rhs> = dyn Add<Rhs> + Sub<Rhs> + X<Rhs> + Z<Rhs>;
| ^^^^^^
help: specify the associated types
|
LL | type Bar<Rhs> = dyn Add<Rhs, Output = Type> + Sub<Rhs, Output = Type> + X<Rhs> + Z<Rhs, A = Type, B = Type, Output = Type>;
| ~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0225]: only auto traits can be used as additional traits in a trait object
--> $DIR/missing-associated-types.rs:18:32
--> $DIR/missing-associated-types.rs:16:32
|
LL | type Baz<Rhs> = dyn Add<Rhs> + Sub<Rhs> + Y<Rhs>;
| -------- ^^^^^^^^ additional non-auto trait
Expand All @@ -74,25 +31,8 @@ LL | type Baz<Rhs> = dyn Add<Rhs> + Sub<Rhs> + Y<Rhs>;
= help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Add<Rhs> + Sub<Rhs> + Y<Rhs> {}`
= note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>

error[E0191]: the value of the associated types `A` in `Y`, `Output` in `Add`, `Output` in `Sub` must be specified
--> $DIR/missing-associated-types.rs:18:21
|
LL | type A;
| ------ `A` defined here
...
LL | type Baz<Rhs> = dyn Add<Rhs> + Sub<Rhs> + Y<Rhs>;
| ^^^^^^^^ ^^^^^^^^ ^^^^^^ associated type `A` must be specified
| | |
| | associated type `Output` must be specified
| associated type `Output` must be specified
|
help: specify the associated types
|
LL | type Baz<Rhs> = dyn Add<Rhs, Output = Type> + Sub<Rhs, Output = Type> + Y<Rhs, A = Type>;
| ~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~

error[E0225]: only auto traits can be used as additional traits in a trait object
--> $DIR/missing-associated-types.rs:21:32
--> $DIR/missing-associated-types.rs:18:32
|
LL | type Bat<Rhs> = dyn Add<Rhs> + Sub<Rhs> + Fine<Rhs>;
| -------- ^^^^^^^^ additional non-auto trait
Expand All @@ -102,28 +42,15 @@ LL | type Bat<Rhs> = dyn Add<Rhs> + Sub<Rhs> + Fine<Rhs>;
= help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Add<Rhs> + Sub<Rhs> + Fine<Rhs> {}`
= note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>

error[E0191]: the value of the associated types `Output` in `Add`, `Output` in `Sub` must be specified
--> $DIR/missing-associated-types.rs:21:21
|
LL | type Bat<Rhs> = dyn Add<Rhs> + Sub<Rhs> + Fine<Rhs>;
| ^^^^^^^^ ^^^^^^^^ associated type `Output` must be specified
| |
| associated type `Output` must be specified
|
help: specify the associated types
|
LL | type Bat<Rhs> = dyn Add<Rhs, Output = Type> + Sub<Rhs, Output = Type> + Fine<Rhs>;
| ~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~

error[E0191]: the value of the associated types `Output` in `Div`, `Output` in `Mul` must be specified
--> $DIR/missing-associated-types.rs:24:21
--> $DIR/missing-associated-types.rs:20:21
|
LL | type Bal<Rhs> = dyn X<Rhs>;
| ^^^^^^ associated types `Output` (from trait `Div`), `Output` (from trait `Mul`) must be specified
|
= help: consider introducing a new type parameter, adding `where` constraints using the fully-qualified path to the associated types

error: aborting due to 9 previous errors
error: aborting due to 5 previous errors

Some errors have detailed explanations: E0191, E0225.
For more information about an error, try `rustc --explain E0191`.
3 changes: 0 additions & 3 deletions tests/ui/traits/bad-sized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,4 @@ trait Trait {}
pub fn main() {
let x: Vec<dyn Trait + Sized> = Vec::new();
//~^ ERROR only auto traits can be used as additional traits in a trait object
//~| ERROR the size for values of type
//~| ERROR the size for values of type
//~| ERROR the size for values of type
}
35 changes: 2 additions & 33 deletions tests/ui/traits/bad-sized.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,6 @@ LL | let x: Vec<dyn Trait + Sized> = Vec::new();
= help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Trait + Sized {}`
= note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>

error[E0277]: the size for values of type `dyn Trait` cannot be known at compilation time
--> $DIR/bad-sized.rs:4:12
|
LL | let x: Vec<dyn Trait + Sized> = Vec::new();
| ^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `dyn Trait`
note: required by an implicit `Sized` bound in `Vec`
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL

error[E0277]: the size for values of type `dyn Trait` cannot be known at compilation time
--> $DIR/bad-sized.rs:4:37
|
LL | let x: Vec<dyn Trait + Sized> = Vec::new();
| ^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `dyn Trait`
note: required by a bound in `Vec::<T>::new`
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL

error[E0277]: the size for values of type `dyn Trait` cannot be known at compilation time
--> $DIR/bad-sized.rs:4:37
|
LL | let x: Vec<dyn Trait + Sized> = Vec::new();
| ^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `dyn Trait`
note: required by an implicit `Sized` bound in `Vec`
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL

error: aborting due to 4 previous errors
error: aborting due to 1 previous error

Some errors have detailed explanations: E0225, E0277.
For more information about an error, try `rustc --explain E0225`.
For more information about this error, try `rustc --explain E0225`.
1 change: 0 additions & 1 deletion tests/ui/traits/issue-32963.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ fn size_of_copy<T: Copy+?Sized>() -> usize { mem::size_of::<T>() }
fn main() {
size_of_copy::<dyn Misc + Copy>();
//~^ ERROR only auto traits can be used as additional traits in a trait object
//~| ERROR the trait bound `dyn Misc: Copy` is not satisfied
}
17 changes: 2 additions & 15 deletions tests/ui/traits/issue-32963.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,6 @@ LL | size_of_copy::<dyn Misc + Copy>();
= help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Misc + Copy {}`
= note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>

error[E0277]: the trait bound `dyn Misc: Copy` is not satisfied
--> $DIR/issue-32963.rs:8:20
|
LL | size_of_copy::<dyn Misc + Copy>();
| ^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `dyn Misc`
|
note: required by a bound in `size_of_copy`
--> $DIR/issue-32963.rs:5:20
|
LL | fn size_of_copy<T: Copy+?Sized>() -> usize { mem::size_of::<T>() }
| ^^^^ required by this bound in `size_of_copy`

error: aborting due to 2 previous errors
error: aborting due to 1 previous error

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

0 comments on commit 898ccdb

Please sign in to comment.