Skip to content
/ rust Public
forked from rust-lang/rust

Commit 898ccdb

Browse files
Dont create object type when more than one principal is present
1 parent ff1737b commit 898ccdb

File tree

9 files changed

+21
-194
lines changed

9 files changed

+21
-194
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,16 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
9292

9393
let (mut auto_traits, regular_traits): (Vec<_>, Vec<_>) =
9494
expanded_traits.partition(|i| tcx.trait_is_auto(i.trait_ref().def_id()));
95+
96+
// We don't support >1 principal
9597
if regular_traits.len() > 1 {
96-
let _ = self.report_trait_object_addition_traits_error(&regular_traits);
97-
} else if regular_traits.is_empty() && auto_traits.is_empty() {
98-
let reported = self.report_trait_object_with_no_traits_error(span, &trait_bounds);
99-
return Ty::new_error(tcx, reported);
98+
let guar = self.report_trait_object_addition_traits_error(&regular_traits);
99+
return Ty::new_error(tcx, guar);
100+
}
101+
// We don't support empty trait objects.
102+
if regular_traits.is_empty() && auto_traits.is_empty() {
103+
let guar = self.report_trait_object_with_no_traits_error(span, &trait_bounds);
104+
return Ty::new_error(tcx, guar);
100105
}
101106

102107
// Check that there are no gross dyn-compatibility violations;

tests/ui/associated-types/issue-22560.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ trait Sub<Rhs=Self> {
77
}
88

99
type Test = dyn Add + Sub;
10-
//~^ ERROR E0393
11-
//~| ERROR E0191
12-
//~| ERROR E0393
13-
//~| ERROR E0225
10+
//~^ ERROR E0225
1411

1512
fn main() { }

tests/ui/associated-types/issue-22560.stderr

+2-52
Original file line numberDiff line numberDiff line change
@@ -9,56 +9,6 @@ LL | type Test = dyn Add + Sub;
99
= help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Add + Sub {}`
1010
= 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>
1111

12-
error[E0191]: the value of the associated types `Output` in `Add`, `Output` in `Sub` must be specified
13-
--> $DIR/issue-22560.rs:9:17
14-
|
15-
LL | type Output;
16-
| ----------- `Output` defined here
17-
...
18-
LL | type Output;
19-
| ----------- `Output` defined here
20-
...
21-
LL | type Test = dyn Add + Sub;
22-
| ^^^ ^^^ associated type `Output` must be specified
23-
| |
24-
| associated type `Output` must be specified
25-
|
26-
help: specify the associated types
27-
|
28-
LL | type Test = dyn Add<Output = Type> + Sub<Output = Type>;
29-
| ~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~
30-
31-
error[E0393]: the type parameter `Rhs` must be explicitly specified
32-
--> $DIR/issue-22560.rs:9:17
33-
|
34-
LL | trait Add<Rhs=Self> {
35-
| ------------------- type parameter `Rhs` must be specified for this
36-
...
37-
LL | type Test = dyn Add + Sub;
38-
| ^^^
39-
|
40-
= note: because of the default `Self` reference, type parameters must be specified on object types
41-
help: set the type parameter to the desired type
42-
|
43-
LL | type Test = dyn Add<Rhs> + Sub;
44-
| +++++
45-
46-
error[E0393]: the type parameter `Rhs` must be explicitly specified
47-
--> $DIR/issue-22560.rs:9:23
48-
|
49-
LL | trait Sub<Rhs=Self> {
50-
| ------------------- type parameter `Rhs` must be specified for this
51-
...
52-
LL | type Test = dyn Add + Sub;
53-
| ^^^
54-
|
55-
= note: because of the default `Self` reference, type parameters must be specified on object types
56-
help: set the type parameter to the desired type
57-
|
58-
LL | type Test = dyn Add + Sub<Rhs>;
59-
| +++++
60-
61-
error: aborting due to 4 previous errors
12+
error: aborting due to 1 previous error
6213

63-
Some errors have detailed explanations: E0191, E0225, E0393.
64-
For more information about an error, try `rustc --explain E0191`.
14+
For more information about this error, try `rustc --explain E0225`.

tests/ui/associated-types/missing-associated-types.rs

-4
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,12 @@ trait Fine<Rhs>: Div<Rhs, Output = Rhs> {}
1111

1212
type Foo<Rhs> = dyn Add<Rhs> + Sub<Rhs> + X<Rhs> + Y<Rhs>;
1313
//~^ ERROR only auto traits can be used as additional traits in a trait object
14-
//~| ERROR the value of the associated types
1514
type Bar<Rhs> = dyn Add<Rhs> + Sub<Rhs> + X<Rhs> + Z<Rhs>;
1615
//~^ ERROR only auto traits can be used as additional traits in a trait object
17-
//~| ERROR the value of the associated types
1816
type Baz<Rhs> = dyn Add<Rhs> + Sub<Rhs> + Y<Rhs>;
1917
//~^ ERROR only auto traits can be used as additional traits in a trait object
20-
//~| ERROR the value of the associated types
2118
type Bat<Rhs> = dyn Add<Rhs> + Sub<Rhs> + Fine<Rhs>;
2219
//~^ ERROR only auto traits can be used as additional traits in a trait object
23-
//~| ERROR the value of the associated types
2420
type Bal<Rhs> = dyn X<Rhs>;
2521
//~^ ERROR the value of the associated types
2622

tests/ui/associated-types/missing-associated-types.stderr

+5-78
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,8 @@ LL | type Foo<Rhs> = dyn Add<Rhs> + Sub<Rhs> + X<Rhs> + Y<Rhs>;
99
= 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> {}`
1010
= 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>
1111

12-
error[E0191]: the value of the associated types `A` in `Y`, `Output` in `Add`, `Output` in `Mul`, `Output` in `Sub` must be specified
13-
--> $DIR/missing-associated-types.rs:12:21
14-
|
15-
LL | type A;
16-
| ------ `A` defined here
17-
...
18-
LL | type Foo<Rhs> = dyn Add<Rhs> + Sub<Rhs> + X<Rhs> + Y<Rhs>;
19-
| ^^^^^^^^ ^^^^^^^^ ^^^^^^ ^^^^^^ associated type `A` must be specified
20-
| | | |
21-
| | | associated type `Output` must be specified
22-
| | associated type `Output` must be specified
23-
| associated type `Output` must be specified
24-
|
25-
help: specify the associated types
26-
|
27-
LL | type Foo<Rhs> = dyn Add<Rhs, Output = Type> + Sub<Rhs, Output = Type> + X<Rhs, Output = Type> + Y<Rhs, A = Type>;
28-
| ~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~
29-
3012
error[E0225]: only auto traits can be used as additional traits in a trait object
31-
--> $DIR/missing-associated-types.rs:15:32
13+
--> $DIR/missing-associated-types.rs:14:32
3214
|
3315
LL | type Bar<Rhs> = dyn Add<Rhs> + Sub<Rhs> + X<Rhs> + Z<Rhs>;
3416
| -------- ^^^^^^^^ additional non-auto trait
@@ -38,33 +20,8 @@ LL | type Bar<Rhs> = dyn Add<Rhs> + Sub<Rhs> + X<Rhs> + Z<Rhs>;
3820
= 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> {}`
3921
= 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>
4022

41-
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
42-
--> $DIR/missing-associated-types.rs:15:21
43-
|
44-
LL | type A;
45-
| ------ `A` defined here
46-
LL | type B;
47-
| ------ `B` defined here
48-
...
49-
LL | type Bar<Rhs> = dyn Add<Rhs> + Sub<Rhs> + X<Rhs> + Z<Rhs>;
50-
| ^^^^^^^^ ^^^^^^^^ ^^^^^^ ^^^^^^ associated types `A`, `B`, `Output` must be specified
51-
| | | |
52-
| | | associated types `Output` (from trait `Div`), `Output` (from trait `Mul`) must be specified
53-
| | associated type `Output` must be specified
54-
| associated type `Output` must be specified
55-
|
56-
help: consider introducing a new type parameter, adding `where` constraints using the fully-qualified path to the associated types
57-
--> $DIR/missing-associated-types.rs:15:43
58-
|
59-
LL | type Bar<Rhs> = dyn Add<Rhs> + Sub<Rhs> + X<Rhs> + Z<Rhs>;
60-
| ^^^^^^
61-
help: specify the associated types
62-
|
63-
LL | type Bar<Rhs> = dyn Add<Rhs, Output = Type> + Sub<Rhs, Output = Type> + X<Rhs> + Z<Rhs, A = Type, B = Type, Output = Type>;
64-
| ~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65-
6623
error[E0225]: only auto traits can be used as additional traits in a trait object
67-
--> $DIR/missing-associated-types.rs:18:32
24+
--> $DIR/missing-associated-types.rs:16:32
6825
|
6926
LL | type Baz<Rhs> = dyn Add<Rhs> + Sub<Rhs> + Y<Rhs>;
7027
| -------- ^^^^^^^^ additional non-auto trait
@@ -74,25 +31,8 @@ LL | type Baz<Rhs> = dyn Add<Rhs> + Sub<Rhs> + Y<Rhs>;
7431
= 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> {}`
7532
= 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>
7633

77-
error[E0191]: the value of the associated types `A` in `Y`, `Output` in `Add`, `Output` in `Sub` must be specified
78-
--> $DIR/missing-associated-types.rs:18:21
79-
|
80-
LL | type A;
81-
| ------ `A` defined here
82-
...
83-
LL | type Baz<Rhs> = dyn Add<Rhs> + Sub<Rhs> + Y<Rhs>;
84-
| ^^^^^^^^ ^^^^^^^^ ^^^^^^ associated type `A` must be specified
85-
| | |
86-
| | associated type `Output` must be specified
87-
| associated type `Output` must be specified
88-
|
89-
help: specify the associated types
90-
|
91-
LL | type Baz<Rhs> = dyn Add<Rhs, Output = Type> + Sub<Rhs, Output = Type> + Y<Rhs, A = Type>;
92-
| ~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~
93-
9434
error[E0225]: only auto traits can be used as additional traits in a trait object
95-
--> $DIR/missing-associated-types.rs:21:32
35+
--> $DIR/missing-associated-types.rs:18:32
9636
|
9737
LL | type Bat<Rhs> = dyn Add<Rhs> + Sub<Rhs> + Fine<Rhs>;
9838
| -------- ^^^^^^^^ additional non-auto trait
@@ -102,28 +42,15 @@ LL | type Bat<Rhs> = dyn Add<Rhs> + Sub<Rhs> + Fine<Rhs>;
10242
= 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> {}`
10343
= 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>
10444

105-
error[E0191]: the value of the associated types `Output` in `Add`, `Output` in `Sub` must be specified
106-
--> $DIR/missing-associated-types.rs:21:21
107-
|
108-
LL | type Bat<Rhs> = dyn Add<Rhs> + Sub<Rhs> + Fine<Rhs>;
109-
| ^^^^^^^^ ^^^^^^^^ associated type `Output` must be specified
110-
| |
111-
| associated type `Output` must be specified
112-
|
113-
help: specify the associated types
114-
|
115-
LL | type Bat<Rhs> = dyn Add<Rhs, Output = Type> + Sub<Rhs, Output = Type> + Fine<Rhs>;
116-
| ~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~
117-
11845
error[E0191]: the value of the associated types `Output` in `Div`, `Output` in `Mul` must be specified
119-
--> $DIR/missing-associated-types.rs:24:21
46+
--> $DIR/missing-associated-types.rs:20:21
12047
|
12148
LL | type Bal<Rhs> = dyn X<Rhs>;
12249
| ^^^^^^ associated types `Output` (from trait `Div`), `Output` (from trait `Mul`) must be specified
12350
|
12451
= help: consider introducing a new type parameter, adding `where` constraints using the fully-qualified path to the associated types
12552

126-
error: aborting due to 9 previous errors
53+
error: aborting due to 5 previous errors
12754

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

tests/ui/traits/bad-sized.rs

-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,4 @@ trait Trait {}
33
pub fn main() {
44
let x: Vec<dyn Trait + Sized> = Vec::new();
55
//~^ ERROR only auto traits can be used as additional traits in a trait object
6-
//~| ERROR the size for values of type
7-
//~| ERROR the size for values of type
8-
//~| ERROR the size for values of type
96
}

tests/ui/traits/bad-sized.stderr

+2-33
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,6 @@ LL | let x: Vec<dyn Trait + Sized> = Vec::new();
99
= help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Trait + Sized {}`
1010
= 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>
1111

12-
error[E0277]: the size for values of type `dyn Trait` cannot be known at compilation time
13-
--> $DIR/bad-sized.rs:4:12
14-
|
15-
LL | let x: Vec<dyn Trait + Sized> = Vec::new();
16-
| ^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
17-
|
18-
= help: the trait `Sized` is not implemented for `dyn Trait`
19-
note: required by an implicit `Sized` bound in `Vec`
20-
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
21-
22-
error[E0277]: the size for values of type `dyn Trait` cannot be known at compilation time
23-
--> $DIR/bad-sized.rs:4:37
24-
|
25-
LL | let x: Vec<dyn Trait + Sized> = Vec::new();
26-
| ^^^^^^^^^^ doesn't have a size known at compile-time
27-
|
28-
= help: the trait `Sized` is not implemented for `dyn Trait`
29-
note: required by a bound in `Vec::<T>::new`
30-
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
31-
32-
error[E0277]: the size for values of type `dyn Trait` cannot be known at compilation time
33-
--> $DIR/bad-sized.rs:4:37
34-
|
35-
LL | let x: Vec<dyn Trait + Sized> = Vec::new();
36-
| ^^^ doesn't have a size known at compile-time
37-
|
38-
= help: the trait `Sized` is not implemented for `dyn Trait`
39-
note: required by an implicit `Sized` bound in `Vec`
40-
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
41-
42-
error: aborting due to 4 previous errors
12+
error: aborting due to 1 previous error
4313

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

tests/ui/traits/issue-32963.rs

-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ fn size_of_copy<T: Copy+?Sized>() -> usize { mem::size_of::<T>() }
77
fn main() {
88
size_of_copy::<dyn Misc + Copy>();
99
//~^ ERROR only auto traits can be used as additional traits in a trait object
10-
//~| ERROR the trait bound `dyn Misc: Copy` is not satisfied
1110
}

tests/ui/traits/issue-32963.stderr

+2-15
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,6 @@ LL | size_of_copy::<dyn Misc + Copy>();
99
= help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Misc + Copy {}`
1010
= 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>
1111

12-
error[E0277]: the trait bound `dyn Misc: Copy` is not satisfied
13-
--> $DIR/issue-32963.rs:8:20
14-
|
15-
LL | size_of_copy::<dyn Misc + Copy>();
16-
| ^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `dyn Misc`
17-
|
18-
note: required by a bound in `size_of_copy`
19-
--> $DIR/issue-32963.rs:5:20
20-
|
21-
LL | fn size_of_copy<T: Copy+?Sized>() -> usize { mem::size_of::<T>() }
22-
| ^^^^ required by this bound in `size_of_copy`
23-
24-
error: aborting due to 2 previous errors
12+
error: aborting due to 1 previous error
2513

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

0 commit comments

Comments
 (0)