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

Commit cfa8fcb

Browse files
Dont create trait object if it has errors in it
1 parent 898ccdb commit cfa8fcb

14 files changed

+40
-261
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use rustc_lint_defs::builtin::UNUSED_ASSOCIATED_TYPE_BOUNDS;
88
use rustc_middle::span_bug;
99
use rustc_middle::ty::fold::BottomUpFolder;
1010
use rustc_middle::ty::{
11-
self, DynKind, ExistentialPredicateStableCmpExt as _, Ty, TyCtxt, TypeFoldable, Upcast,
11+
self, DynKind, ExistentialPredicateStableCmpExt as _, Ty, TyCtxt, TypeFoldable,
12+
TypeVisitableExt, Upcast,
1213
};
1314
use rustc_span::{ErrorGuaranteed, Span};
1415
use rustc_trait_selection::error_reporting::traits::report_dyn_incompatibility;
@@ -103,6 +104,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
103104
let guar = self.report_trait_object_with_no_traits_error(span, &trait_bounds);
104105
return Ty::new_error(tcx, guar);
105106
}
107+
// Don't create a dyn trait if we have errors in the principal.
108+
if let Err(guar) = trait_bounds.error_reported() {
109+
return Ty::new_error(tcx, guar);
110+
}
106111

107112
// Check that there are no gross dyn-compatibility violations;
108113
// most importantly, that the supertraits don't contain `Self`,

tests/crashes/130521.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@ known-bug: #130521
22

33
#![feature(dyn_compatible_for_dispatch)]
4-
struct Vtable(dyn Cap);
4+
struct Vtable(dyn Cap<'static>);
55

66
trait Cap<'a> {}
77

tests/rustdoc-ui/unable-fulfill-trait.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
pub struct Foo<'a, 'b, T> {
44
field1: dyn Bar<'a, 'b>,
55
//~^ ERROR
6-
//~| ERROR
76
}
87

98
pub trait Bar<'x, 's, U>

tests/rustdoc-ui/unable-fulfill-trait.stderr

+3-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | field1: dyn Bar<'a, 'b>,
55
| ^^^ expected 1 generic argument
66
|
77
note: trait defined here, with 1 generic parameter: `U`
8-
--> $DIR/unable-fulfill-trait.rs:9:11
8+
--> $DIR/unable-fulfill-trait.rs:8:11
99
|
1010
LL | pub trait Bar<'x, 's, U>
1111
| ^^^ -
@@ -14,13 +14,6 @@ help: add missing generic argument
1414
LL | field1: dyn Bar<'a, 'b, U>,
1515
| +++
1616

17-
error[E0227]: ambiguous lifetime bound, explicit lifetime bound required
18-
--> $DIR/unable-fulfill-trait.rs:4:13
19-
|
20-
LL | field1: dyn Bar<'a, 'b>,
21-
| ^^^^^^^^^^^^^^^
22-
23-
error: aborting due to 2 previous errors
17+
error: aborting due to 1 previous error
2418

25-
Some errors have detailed explanations: E0107, E0227.
26-
For more information about an error, try `rustc --explain E0107`.
19+
For more information about this error, try `rustc --explain E0107`.

tests/ui/const-generics/not_wf_param_in_rpitit.rs

-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
trait Trait<const N: dyn Trait = bar> {
44
//~^ ERROR: cannot find value `bar` in this scope
55
//~| ERROR: cycle detected when computing type of `Trait::N`
6-
//~| ERROR: the trait `Trait` cannot be made into an object
7-
//~| ERROR: the trait `Trait` cannot be made into an object
8-
//~| ERROR: the trait `Trait` cannot be made into an object
96
async fn a() {}
107
}
118

tests/ui/const-generics/not_wf_param_in_rpitit.stderr

+3-73
Original file line numberDiff line numberDiff line change
@@ -18,77 +18,7 @@ LL | trait Trait<const N: dyn Trait = bar> {
1818
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1919
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
2020

21-
error[E0038]: the trait `Trait` cannot be made into an object
22-
--> $DIR/not_wf_param_in_rpitit.rs:3:22
23-
|
24-
LL | trait Trait<const N: dyn Trait = bar> {
25-
| ^^^^^^^^^ `Trait` cannot be made into an object
26-
|
27-
note: for a trait to be "dyn-compatible" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
28-
--> $DIR/not_wf_param_in_rpitit.rs:9:14
29-
|
30-
LL | trait Trait<const N: dyn Trait = bar> {
31-
| ----- this trait cannot be made into an object...
32-
...
33-
LL | async fn a() {}
34-
| ^ ...because associated function `a` has no `self` parameter
35-
help: consider turning `a` into a method by giving it a `&self` argument
36-
|
37-
LL | async fn a(&self) {}
38-
| +++++
39-
help: alternatively, consider constraining `a` so it does not apply to trait objects
40-
|
41-
LL | async fn a() where Self: Sized {}
42-
| +++++++++++++++++
43-
44-
error[E0038]: the trait `Trait` cannot be made into an object
45-
--> $DIR/not_wf_param_in_rpitit.rs:3:13
46-
|
47-
LL | trait Trait<const N: dyn Trait = bar> {
48-
| ^^^^^^^^^^^^^^^^^^^^^^^^ `Trait` cannot be made into an object
49-
|
50-
note: for a trait to be "dyn-compatible" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
51-
--> $DIR/not_wf_param_in_rpitit.rs:9:14
52-
|
53-
LL | trait Trait<const N: dyn Trait = bar> {
54-
| ----- this trait cannot be made into an object...
55-
...
56-
LL | async fn a() {}
57-
| ^ ...because associated function `a` has no `self` parameter
58-
help: consider turning `a` into a method by giving it a `&self` argument
59-
|
60-
LL | async fn a(&self) {}
61-
| +++++
62-
help: alternatively, consider constraining `a` so it does not apply to trait objects
63-
|
64-
LL | async fn a() where Self: Sized {}
65-
| +++++++++++++++++
66-
67-
error[E0038]: the trait `Trait` cannot be made into an object
68-
--> $DIR/not_wf_param_in_rpitit.rs:3:13
69-
|
70-
LL | trait Trait<const N: dyn Trait = bar> {
71-
| ^^^^^^^^^^^^^^^^^^^^^^^^ `Trait` cannot be made into an object
72-
|
73-
note: for a trait to be "dyn-compatible" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
74-
--> $DIR/not_wf_param_in_rpitit.rs:9:14
75-
|
76-
LL | trait Trait<const N: dyn Trait = bar> {
77-
| ----- this trait cannot be made into an object...
78-
...
79-
LL | async fn a() {}
80-
| ^ ...because associated function `a` has no `self` parameter
81-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
82-
help: consider turning `a` into a method by giving it a `&self` argument
83-
|
84-
LL | async fn a(&self) {}
85-
| +++++
86-
help: alternatively, consider constraining `a` so it does not apply to trait objects
87-
|
88-
LL | async fn a() where Self: Sized {}
89-
| +++++++++++++++++
90-
91-
error: aborting due to 5 previous errors
21+
error: aborting due to 2 previous errors
9222

93-
Some errors have detailed explanations: E0038, E0391, E0425.
94-
For more information about an error, try `rustc --explain E0038`.
23+
Some errors have detailed explanations: E0391, E0425.
24+
For more information about an error, try `rustc --explain E0391`.

tests/ui/issues/issue-23024.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@ fn main()
88
println!("{:?}",(vfnfer[0] as dyn Fn)(3));
99
//~^ ERROR the precise format of `Fn`-family traits'
1010
//~| ERROR missing generics for trait `Fn`
11-
//~| ERROR the value of the associated type `Output` in `FnOnce`
1211
}

tests/ui/issues/issue-23024.stderr

+2-8
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,7 @@ help: add missing generic argument
1919
LL | println!("{:?}",(vfnfer[0] as dyn Fn<Args>)(3));
2020
| ++++++
2121

22-
error[E0191]: the value of the associated type `Output` in `FnOnce` must be specified
23-
--> $DIR/issue-23024.rs:8:39
24-
|
25-
LL | println!("{:?}",(vfnfer[0] as dyn Fn)(3));
26-
| ^^ help: specify the associated type: `Fn::<Output = Type>`
27-
28-
error: aborting due to 3 previous errors
22+
error: aborting due to 2 previous errors
2923

30-
Some errors have detailed explanations: E0107, E0191, E0658.
24+
Some errors have detailed explanations: E0107, E0658.
3125
For more information about an error, try `rustc --explain E0107`.

tests/ui/issues/issue-34373.rs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ trait Trait<T> {
66

77
pub struct Foo<T = Box<Trait<DefaultFoo>>>; //~ ERROR cycle detected
88
//~^ ERROR `T` is never used
9-
//~| ERROR `Trait` cannot be made into an object
109
type DefaultFoo = Foo;
1110

1211
fn main() {

tests/ui/issues/issue-34373.stderr

+4-26
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | pub struct Foo<T = Box<Trait<DefaultFoo>>>;
55
| ^^^^^^^^^^
66
|
77
note: ...which requires expanding type alias `DefaultFoo`...
8-
--> $DIR/issue-34373.rs:10:19
8+
--> $DIR/issue-34373.rs:9:19
99
|
1010
LL | type DefaultFoo = Foo;
1111
| ^^^
@@ -17,28 +17,6 @@ LL | pub struct Foo<T = Box<Trait<DefaultFoo>>>;
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1818
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
1919

20-
error[E0038]: the trait `Trait` cannot be made into an object
21-
--> $DIR/issue-34373.rs:7:24
22-
|
23-
LL | pub struct Foo<T = Box<Trait<DefaultFoo>>>;
24-
| ^^^^^^^^^^^^^^^^^ `Trait` cannot be made into an object
25-
|
26-
note: for a trait to be "dyn-compatible" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
27-
--> $DIR/issue-34373.rs:4:8
28-
|
29-
LL | trait Trait<T> {
30-
| ----- this trait cannot be made into an object...
31-
LL | fn foo(_: T) {}
32-
| ^^^ ...because associated function `foo` has no `self` parameter
33-
help: consider turning `foo` into a method by giving it a `&self` argument
34-
|
35-
LL | fn foo(&self, _: T) {}
36-
| ++++++
37-
help: alternatively, consider constraining `foo` so it does not apply to trait objects
38-
|
39-
LL | fn foo(_: T) where Self: Sized {}
40-
| +++++++++++++++++
41-
4220
error[E0392]: type parameter `T` is never used
4321
--> $DIR/issue-34373.rs:7:16
4422
|
@@ -48,7 +26,7 @@ LL | pub struct Foo<T = Box<Trait<DefaultFoo>>>;
4826
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
4927
= help: if you intended `T` to be a const parameter, use `const T: /* Type */` instead
5028

51-
error: aborting due to 3 previous errors
29+
error: aborting due to 2 previous errors
5230

53-
Some errors have detailed explanations: E0038, E0391, E0392.
54-
For more information about an error, try `rustc --explain E0038`.
31+
Some errors have detailed explanations: E0391, E0392.
32+
For more information about an error, try `rustc --explain E0391`.

tests/ui/suggestions/use-type-argument-instead-of-assoc-type.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ pub trait T<X, Y> {
55
}
66
pub struct Foo {
77
i: Box<dyn T<usize, usize, usize, usize, B=usize>>,
8-
//~^ ERROR must be specified
9-
//~| ERROR trait takes 2 generic arguments but 4 generic arguments were supplied
8+
//~^ ERROR trait takes 2 generic arguments but 4 generic arguments were supplied
109
}
1110

1211

tests/ui/suggestions/use-type-argument-instead-of-assoc-type.stderr

+2-15
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,6 @@ help: replace the generic bounds with the associated types
1414
LL | i: Box<dyn T<usize, usize, A = usize, C = usize, B=usize>>,
1515
| +++ +++
1616

17-
error[E0191]: the value of the associated types `C` and `A` in `T` must be specified
18-
--> $DIR/use-type-argument-instead-of-assoc-type.rs:7:16
19-
|
20-
LL | type A;
21-
| ------ `A` defined here
22-
LL | type B;
23-
LL | type C;
24-
| ------ `C` defined here
25-
...
26-
LL | i: Box<dyn T<usize, usize, usize, usize, B=usize>>,
27-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ associated types `A`, `C` must be specified
28-
29-
error: aborting due to 2 previous errors
17+
error: aborting due to 1 previous error
3018

31-
Some errors have detailed explanations: E0107, E0191.
32-
For more information about an error, try `rustc --explain E0107`.
19+
For more information about this error, try `rustc --explain E0107`.

tests/ui/wf/ice-hir-wf-check-anon-const-issue-122199.rs

-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
trait Trait<const N: Trait = bar> {
22
//~^ ERROR cannot find value `bar` in this scope
33
//~| ERROR cycle detected when computing type of `Trait::N`
4-
//~| ERROR the trait `Trait` cannot be made into an object
5-
//~| ERROR the trait `Trait` cannot be made into an object
6-
//~| ERROR the trait `Trait` cannot be made into an object
7-
//~| WARN trait objects without an explicit `dyn` are deprecated [bare_trait_objects]
8-
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
94
//~| WARN trait objects without an explicit `dyn` are deprecated [bare_trait_objects]
105
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
116
fn fnc<const N: Trait = u32>(&self) -> Trait {
127
//~^ ERROR the name `N` is already used for a generic parameter in this item's generic parameters
138
//~| ERROR expected value, found builtin type `u32`
149
//~| ERROR defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
1510
//~| ERROR associated item referring to unboxed trait object for its own trait
16-
//~| ERROR the trait `Trait` cannot be made into an object
17-
//~| WARN trait objects without an explicit `dyn` are deprecated [bare_trait_objects]
18-
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
1911
//~| WARN trait objects without an explicit `dyn` are deprecated [bare_trait_objects]
2012
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
2113
//~| WARN trait objects without an explicit `dyn` are deprecated [bare_trait_objects]

0 commit comments

Comments
 (0)