Skip to content

Commit c7ca53a

Browse files
authored
Unrolled build for rust-lang#133660
Rollup merge of rust-lang#133660 - compiler-errors:trait-obj-missing-assoc, r=lcnr Do not create trait object type if missing associated types r? lcnr
2 parents 8ac313b + fa7449d commit c7ca53a

15 files changed

+35
-205
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
219219
def_ids.retain(|def_id| !tcx.generics_require_sized_self(def_id));
220220
}
221221

222-
self.complain_about_missing_assoc_tys(
222+
if let Err(guar) = self.check_for_required_assoc_tys(
223223
associated_types,
224224
potential_assoc_types,
225225
hir_trait_bounds,
226-
);
226+
) {
227+
return Ty::new_error(tcx, guar);
228+
}
227229

228230
// De-duplicate auto traits so that, e.g., `dyn Trait + Send + Send` is the same as
229231
// `dyn Trait + Send`.

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -714,14 +714,14 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
714714
/// reasonable suggestion on how to write it. For the case of multiple associated types in the
715715
/// same trait bound have the same name (as they come from different supertraits), we instead
716716
/// emit a generic note suggesting using a `where` clause to constraint instead.
717-
pub(crate) fn complain_about_missing_assoc_tys(
717+
pub(crate) fn check_for_required_assoc_tys(
718718
&self,
719719
associated_types: FxIndexMap<Span, FxIndexSet<DefId>>,
720720
potential_assoc_types: Vec<usize>,
721721
trait_bounds: &[hir::PolyTraitRef<'_>],
722-
) {
722+
) -> Result<(), ErrorGuaranteed> {
723723
if associated_types.values().all(|v| v.is_empty()) {
724-
return;
724+
return Ok(());
725725
}
726726

727727
let tcx = self.tcx();
@@ -739,7 +739,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
739739
// Account for things like `dyn Foo + 'a`, like in tests `issue-22434.rs` and
740740
// `issue-22560.rs`.
741741
let mut trait_bound_spans: Vec<Span> = vec![];
742-
let mut dyn_compatibility_violations = false;
742+
let mut dyn_compatibility_violations = Ok(());
743743
for (span, items) in &associated_types {
744744
if !items.is_empty() {
745745
trait_bound_spans.push(*span);
@@ -752,13 +752,20 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
752752
let violations =
753753
dyn_compatibility_violations_for_assoc_item(tcx, trait_def_id, *assoc_item);
754754
if !violations.is_empty() {
755-
report_dyn_incompatibility(tcx, *span, None, trait_def_id, &violations).emit();
756-
dyn_compatibility_violations = true;
755+
dyn_compatibility_violations = Err(report_dyn_incompatibility(
756+
tcx,
757+
*span,
758+
None,
759+
trait_def_id,
760+
&violations,
761+
)
762+
.emit());
757763
}
758764
}
759765
}
760-
if dyn_compatibility_violations {
761-
return;
766+
767+
if let Err(guar) = dyn_compatibility_violations {
768+
return Err(guar);
762769
}
763770

764771
// related to issue #91997, turbofishes added only when in an expr or pat
@@ -965,7 +972,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
965972
}
966973
}
967974

968-
err.emit();
975+
Err(err.emit())
969976
}
970977

971978
/// On ambiguous associated type, look for an associated function whose name matches the

tests/crashes/131668.rs

-12
This file was deleted.

tests/ui/associated-type-bounds/overlaping-bound-suggestion.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ trait Item {
55
pub struct Flatten<I> {
66
inner: <IntoIterator<Item: IntoIterator<Item: >>::IntoIterator as Item>::Core,
77
//~^ ERROR E0191
8-
//~| ERROR E0223
98
}
109

1110
fn main() {}

tests/ui/associated-type-bounds/overlaping-bound-suggestion.stderr

+2-14
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,6 @@ error[E0191]: the value of the associated types `Item` and `IntoIter` in `IntoIt
44
LL | inner: <IntoIterator<Item: IntoIterator<Item: >>::IntoIterator as Item>::Core,
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: specify the associated types: `IntoIterator<Item: IntoIterator<Item: >, Item = Type, IntoIter = Type>`
66

7-
error[E0223]: ambiguous associated type
8-
--> $DIR/overlaping-bound-suggestion.rs:6:13
9-
|
10-
LL | inner: <IntoIterator<Item: IntoIterator<Item: >>::IntoIterator as Item>::Core,
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12-
|
13-
help: if there were a trait named `Example` with associated type `IntoIterator` implemented for `(dyn IntoIterator + 'static)`, you could use the fully-qualified path
14-
|
15-
LL | inner: <<(dyn IntoIterator + 'static) as Example>::IntoIterator as Item>::Core,
16-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17-
18-
error: aborting due to 2 previous errors
7+
error: aborting due to 1 previous error
198

20-
Some errors have detailed explanations: E0191, E0223.
21-
For more information about an error, try `rustc --explain E0191`.
9+
For more information about this error, try `rustc --explain E0191`.

tests/ui/async-await/async-fn/dyn-pos.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
#![feature(async_closure)]
44

55
fn foo(x: &dyn async Fn()) {}
6-
//~^ ERROR the trait `AsyncFn` cannot be made into an object
7-
//~| ERROR the trait `AsyncFnMut` cannot be made into an object
8-
//~| ERROR the trait `AsyncFnMut` cannot be made into an object
9-
//~| ERROR the trait `AsyncFnMut` cannot be made into an object
6+
//~^ ERROR the trait `AsyncFnMut` cannot be made into an object
107

118
fn main() {}

tests/ui/async-await/async-fn/dyn-pos.stderr

+1-47
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,6 @@ note: for a trait to be "dyn-compatible" it needs to allow building a vtable to
1313
&mut F
1414
std::boxed::Box<F, A>
1515

16-
error[E0038]: the trait `AsyncFnMut` cannot be made into an object
17-
--> $DIR/dyn-pos.rs:5:16
18-
|
19-
LL | fn foo(x: &dyn async Fn()) {}
20-
| ^^^^^^^^^^ `AsyncFnMut` cannot be made into an object
21-
|
22-
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>
23-
--> $SRC_DIR/core/src/ops/async_function.rs:LL:COL
24-
|
25-
= note: the trait cannot be made into an object because it contains the generic associated type `CallRefFuture`
26-
= help: the following types implement the trait, consider defining an enum where each variant holds one of these types, implementing `AsyncFnMut` for this new enum and using it instead:
27-
&F
28-
&mut F
29-
std::boxed::Box<F, A>
30-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
31-
32-
error[E0038]: the trait `AsyncFnMut` cannot be made into an object
33-
--> $DIR/dyn-pos.rs:5:16
34-
|
35-
LL | fn foo(x: &dyn async Fn()) {}
36-
| ^^^^^^^^^^ `AsyncFnMut` cannot be made into an object
37-
|
38-
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>
39-
--> $SRC_DIR/core/src/ops/async_function.rs:LL:COL
40-
|
41-
= note: the trait cannot be made into an object because it contains the generic associated type `CallRefFuture`
42-
= help: the following types implement the trait, consider defining an enum where each variant holds one of these types, implementing `AsyncFnMut` for this new enum and using it instead:
43-
&F
44-
&mut F
45-
std::boxed::Box<F, A>
46-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
47-
48-
error[E0038]: the trait `AsyncFn` cannot be made into an object
49-
--> $DIR/dyn-pos.rs:5:12
50-
|
51-
LL | fn foo(x: &dyn async Fn()) {}
52-
| ^^^^^^^^^^^^^^ `AsyncFn` cannot be made into an object
53-
|
54-
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>
55-
--> $SRC_DIR/core/src/ops/async_function.rs:LL:COL
56-
|
57-
= note: the trait cannot be made into an object because it contains the generic associated type `CallRefFuture`
58-
= help: the following types implement the trait, consider defining an enum where each variant holds one of these types, implementing `AsyncFn` for this new enum and using it instead:
59-
&F
60-
std::boxed::Box<F, A>
61-
62-
error: aborting due to 4 previous errors
16+
error: aborting due to 1 previous error
6317

6418
For more information about this error, try `rustc --explain E0038`.

tests/ui/dyn-compatibility/missing-assoc-type.rs

-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,5 @@ trait Foo {
33
}
44

55
fn bar(x: &dyn Foo) {} //~ ERROR the trait `Foo` cannot be made into an object
6-
//~^ ERROR the trait `Foo` cannot be made into an object
7-
//~| ERROR the trait `Foo` cannot be made into an object
8-
//~| ERROR the trait `Foo` cannot be made into an object
96

107
fn main() {}

tests/ui/dyn-compatibility/missing-assoc-type.stderr

+1-48
Original file line numberDiff line numberDiff line change
@@ -13,53 +13,6 @@ LL | type Bar<T>;
1313
| ^^^ ...because it contains the generic associated type `Bar`
1414
= help: consider moving `Bar` to another trait
1515

16-
error[E0038]: the trait `Foo` cannot be made into an object
17-
--> $DIR/missing-assoc-type.rs:5:16
18-
|
19-
LL | fn bar(x: &dyn Foo) {}
20-
| ^^^ `Foo` cannot be made into an object
21-
|
22-
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>
23-
--> $DIR/missing-assoc-type.rs:2:10
24-
|
25-
LL | trait Foo {
26-
| --- this trait cannot be made into an object...
27-
LL | type Bar<T>;
28-
| ^^^ ...because it contains the generic associated type `Bar`
29-
= help: consider moving `Bar` to another trait
30-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
31-
32-
error[E0038]: the trait `Foo` cannot be made into an object
33-
--> $DIR/missing-assoc-type.rs:5:16
34-
|
35-
LL | fn bar(x: &dyn Foo) {}
36-
| ^^^ `Foo` cannot be made into an object
37-
|
38-
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>
39-
--> $DIR/missing-assoc-type.rs:2:10
40-
|
41-
LL | trait Foo {
42-
| --- this trait cannot be made into an object...
43-
LL | type Bar<T>;
44-
| ^^^ ...because it contains the generic associated type `Bar`
45-
= help: consider moving `Bar` to another trait
46-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
47-
48-
error[E0038]: the trait `Foo` cannot be made into an object
49-
--> $DIR/missing-assoc-type.rs:5:12
50-
|
51-
LL | fn bar(x: &dyn Foo) {}
52-
| ^^^^^^^ `Foo` cannot be made into an object
53-
|
54-
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>
55-
--> $DIR/missing-assoc-type.rs:2:10
56-
|
57-
LL | trait Foo {
58-
| --- this trait cannot be made into an object...
59-
LL | type Bar<T>;
60-
| ^^^ ...because it contains the generic associated type `Bar`
61-
= help: consider moving `Bar` to another trait
62-
63-
error: aborting due to 4 previous errors
16+
error: aborting due to 1 previous error
6417

6518
For more information about this error, try `rustc --explain E0038`.

tests/ui/issues/issue-21950.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@ impl Add for i32 {
88

99
fn main() {
1010
let x = &10 as &dyn Add;
11-
//~^ ERROR E0393
12-
//~| ERROR E0191
11+
//~^ ERROR E0191
1312
}

tests/ui/issues/issue-21950.stderr

+2-18
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,6 @@ LL | type Output;
77
LL | let x = &10 as &dyn Add;
88
| ^^^ help: specify the associated type: `Add<Output = Type>`
99

10-
error[E0393]: the type parameter `Rhs` must be explicitly specified
11-
--> $DIR/issue-21950.rs:10:25
12-
|
13-
LL | trait Add<Rhs=Self> {
14-
| ------------------- type parameter `Rhs` must be specified for this
15-
...
16-
LL | let x = &10 as &dyn Add;
17-
| ^^^
18-
|
19-
= note: because of the default `Self` reference, type parameters must be specified on object types
20-
help: set the type parameter to the desired type
21-
|
22-
LL | let x = &10 as &dyn Add<Rhs>;
23-
| +++++
24-
25-
error: aborting due to 2 previous errors
10+
error: aborting due to 1 previous error
2611

27-
Some errors have detailed explanations: E0191, E0393.
28-
For more information about an error, try `rustc --explain E0191`.
12+
For more information about this error, try `rustc --explain E0191`.

tests/ui/issues/issue-28344.rs

-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ use std::ops::BitXor;
33
fn main() {
44
let x: u8 = BitXor::bitor(0 as u8, 0 as u8);
55
//~^ ERROR must be specified
6-
//~| no function or associated item named
76
//~| WARN trait objects without an explicit `dyn` are deprecated
87
//~| WARN this is accepted in the current edition
98

109
let g = BitXor::bitor;
1110
//~^ ERROR must be specified
12-
//~| no function or associated item named
1311
//~| WARN trait objects without an explicit `dyn` are deprecated
1412
//~| WARN this is accepted in the current edition
1513
}

tests/ui/issues/issue-28344.stderr

+4-23
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,8 @@ error[E0191]: the value of the associated type `Output` in `BitXor` must be spec
1818
LL | let x: u8 = BitXor::bitor(0 as u8, 0 as u8);
1919
| ^^^^^^ help: specify the associated type: `BitXor::<Output = Type>`
2020

21-
error[E0599]: no function or associated item named `bitor` found for trait object `dyn BitXor<_>` in the current scope
22-
--> $DIR/issue-28344.rs:4:25
23-
|
24-
LL | let x: u8 = BitXor::bitor(0 as u8, 0 as u8);
25-
| ^^^^^ function or associated item not found in `dyn BitXor<_>`
26-
|
27-
help: there is a method `bitxor` with a similar name, but with different arguments
28-
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL
29-
3021
warning: trait objects without an explicit `dyn` are deprecated
31-
--> $DIR/issue-28344.rs:10:13
22+
--> $DIR/issue-28344.rs:9:13
3223
|
3324
LL | let g = BitXor::bitor;
3425
| ^^^^^^
@@ -41,21 +32,11 @@ LL | let g = <dyn BitXor>::bitor;
4132
| ++++ +
4233

4334
error[E0191]: the value of the associated type `Output` in `BitXor` must be specified
44-
--> $DIR/issue-28344.rs:10:13
35+
--> $DIR/issue-28344.rs:9:13
4536
|
4637
LL | let g = BitXor::bitor;
4738
| ^^^^^^ help: specify the associated type: `BitXor::<Output = Type>`
4839

49-
error[E0599]: no function or associated item named `bitor` found for trait object `dyn BitXor<_>` in the current scope
50-
--> $DIR/issue-28344.rs:10:21
51-
|
52-
LL | let g = BitXor::bitor;
53-
| ^^^^^ function or associated item not found in `dyn BitXor<_>`
54-
|
55-
help: there is a method `bitxor` with a similar name
56-
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL
57-
58-
error: aborting due to 4 previous errors; 2 warnings emitted
40+
error: aborting due to 2 previous errors; 2 warnings emitted
5941

60-
Some errors have detailed explanations: E0191, E0599.
61-
For more information about an error, try `rustc --explain E0191`.
42+
For more information about this error, try `rustc --explain E0191`.

tests/ui/suggestions/trait-hidden-method.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// #107983 - testing that `__iterator_get_unchecked` isn't suggested
22
// HELP included so that compiletest errors on the bad suggestion
33
pub fn i_can_has_iterator() -> impl Iterator<Item = u32> {
4-
//~^ ERROR expected `Box<dyn Iterator>`
5-
//~| HELP consider constraining the associated type
64
Box::new(1..=10) as Box<dyn Iterator>
75
//~^ ERROR the value of the associated type `Item`
86
//~| HELP specify the associated type
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
11
error[E0191]: the value of the associated type `Item` in `Iterator` must be specified
2-
--> $DIR/trait-hidden-method.rs:6:33
2+
--> $DIR/trait-hidden-method.rs:4:33
33
|
44
LL | Box::new(1..=10) as Box<dyn Iterator>
55
| ^^^^^^^^ help: specify the associated type: `Iterator<Item = Type>`
66

7-
error[E0271]: expected `Box<dyn Iterator>` to be an iterator that yields `u32`, but it yields `<dyn Iterator as Iterator>::Item`
8-
--> $DIR/trait-hidden-method.rs:3:32
9-
|
10-
LL | pub fn i_can_has_iterator() -> impl Iterator<Item = u32> {
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `u32`, found associated type
12-
...
13-
LL | Box::new(1..=10) as Box<dyn Iterator>
14-
| ------------------------------------- return type was inferred to be `Box<dyn Iterator>` here
15-
|
16-
= note: expected type `u32`
17-
found associated type `<dyn Iterator as Iterator>::Item`
18-
= help: consider constraining the associated type `<dyn Iterator as Iterator>::Item` to `u32`
19-
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
20-
21-
error: aborting due to 2 previous errors
7+
error: aborting due to 1 previous error
228

23-
Some errors have detailed explanations: E0191, E0271.
24-
For more information about an error, try `rustc --explain E0191`.
9+
For more information about this error, try `rustc --explain E0191`.

0 commit comments

Comments
 (0)