Skip to content

Commit

Permalink
Unrolled build for rust-lang#133394
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#133394 - compiler-errors:dyn-more-errors, r=lcnr

Bail on more errors in dyn ty lowering

If we have more than one principal trait, or if we have a principal trait with errors in it, then bail with `TyKind::Error` rather than attempting lowering. Lowering a dyn trait with more than one principal just arbitrarily chooses the first one and drops the subsequent ones, and lowering a dyn trait path with errors in it is just kinda useless.

This suppresses unnecessary errors which I think is net-good, but also is important to make sure that we don't end up leaking `{type error}` in rust-lang#133388 error messaging :)

r? types
  • Loading branch information
rust-timer authored Nov 27, 2024
2 parents dff3e7c + cfa8fcb commit 681e3c5
Show file tree
Hide file tree
Showing 22 changed files with 61 additions and 455 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use rustc_lint_defs::builtin::UNUSED_ASSOCIATED_TYPE_BOUNDS;
use rustc_middle::span_bug;
use rustc_middle::ty::fold::BottomUpFolder;
use rustc_middle::ty::{
self, DynKind, ExistentialPredicateStableCmpExt as _, Ty, TyCtxt, TypeFoldable, Upcast,
self, DynKind, ExistentialPredicateStableCmpExt as _, Ty, TyCtxt, TypeFoldable,
TypeVisitableExt, Upcast,
};
use rustc_span::{ErrorGuaranteed, Span};
use rustc_trait_selection::error_reporting::traits::report_dyn_incompatibility;
Expand Down Expand Up @@ -92,11 +93,20 @@ 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);
}
// Don't create a dyn trait if we have errors in the principal.
if let Err(guar) = trait_bounds.error_reported() {
return Ty::new_error(tcx, guar);
}

// Check that there are no gross dyn-compatibility violations;
Expand Down
2 changes: 1 addition & 1 deletion tests/crashes/130521.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//@ known-bug: #130521

#![feature(dyn_compatible_for_dispatch)]
struct Vtable(dyn Cap);
struct Vtable(dyn Cap<'static>);

trait Cap<'a> {}

Expand Down
1 change: 0 additions & 1 deletion tests/rustdoc-ui/unable-fulfill-trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
pub struct Foo<'a, 'b, T> {
field1: dyn Bar<'a, 'b>,
//~^ ERROR
//~| ERROR
}

pub trait Bar<'x, 's, U>
Expand Down
13 changes: 3 additions & 10 deletions tests/rustdoc-ui/unable-fulfill-trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | field1: dyn Bar<'a, 'b>,
| ^^^ expected 1 generic argument
|
note: trait defined here, with 1 generic parameter: `U`
--> $DIR/unable-fulfill-trait.rs:9:11
--> $DIR/unable-fulfill-trait.rs:8:11
|
LL | pub trait Bar<'x, 's, U>
| ^^^ -
Expand All @@ -14,13 +14,6 @@ help: add missing generic argument
LL | field1: dyn Bar<'a, 'b, U>,
| +++

error[E0227]: ambiguous lifetime bound, explicit lifetime bound required
--> $DIR/unable-fulfill-trait.rs:4:13
|
LL | field1: dyn Bar<'a, 'b>,
| ^^^^^^^^^^^^^^^

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

Some errors have detailed explanations: E0107, E0227.
For more information about an error, try `rustc --explain E0107`.
For more information about this error, try `rustc --explain E0107`.
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/const-generics/not_wf_param_in_rpitit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
trait Trait<const N: dyn Trait = bar> {
//~^ ERROR: cannot find value `bar` in this scope
//~| ERROR: cycle detected when computing type of `Trait::N`
//~| ERROR: the trait `Trait` cannot be made into an object
//~| ERROR: the trait `Trait` cannot be made into an object
//~| ERROR: the trait `Trait` cannot be made into an object
async fn a() {}
}

Expand Down
76 changes: 3 additions & 73 deletions tests/ui/const-generics/not_wf_param_in_rpitit.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,77 +18,7 @@ LL | trait Trait<const N: dyn Trait = bar> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= 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

error[E0038]: the trait `Trait` cannot be made into an object
--> $DIR/not_wf_param_in_rpitit.rs:3:22
|
LL | trait Trait<const N: dyn Trait = bar> {
| ^^^^^^^^^ `Trait` cannot be made into an object
|
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>
--> $DIR/not_wf_param_in_rpitit.rs:9:14
|
LL | trait Trait<const N: dyn Trait = bar> {
| ----- this trait cannot be made into an object...
...
LL | async fn a() {}
| ^ ...because associated function `a` has no `self` parameter
help: consider turning `a` into a method by giving it a `&self` argument
|
LL | async fn a(&self) {}
| +++++
help: alternatively, consider constraining `a` so it does not apply to trait objects
|
LL | async fn a() where Self: Sized {}
| +++++++++++++++++

error[E0038]: the trait `Trait` cannot be made into an object
--> $DIR/not_wf_param_in_rpitit.rs:3:13
|
LL | trait Trait<const N: dyn Trait = bar> {
| ^^^^^^^^^^^^^^^^^^^^^^^^ `Trait` cannot be made into an object
|
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>
--> $DIR/not_wf_param_in_rpitit.rs:9:14
|
LL | trait Trait<const N: dyn Trait = bar> {
| ----- this trait cannot be made into an object...
...
LL | async fn a() {}
| ^ ...because associated function `a` has no `self` parameter
help: consider turning `a` into a method by giving it a `&self` argument
|
LL | async fn a(&self) {}
| +++++
help: alternatively, consider constraining `a` so it does not apply to trait objects
|
LL | async fn a() where Self: Sized {}
| +++++++++++++++++

error[E0038]: the trait `Trait` cannot be made into an object
--> $DIR/not_wf_param_in_rpitit.rs:3:13
|
LL | trait Trait<const N: dyn Trait = bar> {
| ^^^^^^^^^^^^^^^^^^^^^^^^ `Trait` cannot be made into an object
|
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>
--> $DIR/not_wf_param_in_rpitit.rs:9:14
|
LL | trait Trait<const N: dyn Trait = bar> {
| ----- this trait cannot be made into an object...
...
LL | async fn a() {}
| ^ ...because associated function `a` has no `self` parameter
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
help: consider turning `a` into a method by giving it a `&self` argument
|
LL | async fn a(&self) {}
| +++++
help: alternatively, consider constraining `a` so it does not apply to trait objects
|
LL | async fn a() where Self: Sized {}
| +++++++++++++++++

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

Some errors have detailed explanations: E0038, E0391, E0425.
For more information about an error, try `rustc --explain E0038`.
Some errors have detailed explanations: E0391, E0425.
For more information about an error, try `rustc --explain E0391`.
1 change: 0 additions & 1 deletion tests/ui/issues/issue-23024.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ fn main()
println!("{:?}",(vfnfer[0] as dyn Fn)(3));
//~^ ERROR the precise format of `Fn`-family traits'
//~| ERROR missing generics for trait `Fn`
//~| ERROR the value of the associated type `Output` in `FnOnce`
}
10 changes: 2 additions & 8 deletions tests/ui/issues/issue-23024.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ help: add missing generic argument
LL | println!("{:?}",(vfnfer[0] as dyn Fn<Args>)(3));
| ++++++

error[E0191]: the value of the associated type `Output` in `FnOnce` must be specified
--> $DIR/issue-23024.rs:8:39
|
LL | println!("{:?}",(vfnfer[0] as dyn Fn)(3));
| ^^ help: specify the associated type: `Fn::<Output = Type>`

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

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

0 comments on commit 681e3c5

Please sign in to comment.