Skip to content

Commit 3294c64

Browse files
authored
Unrolled build for rust-lang#119506
Rollup merge of rust-lang#119506 - compiler-errors:visibilities-for-object-safety-error, r=Nilstrieb Use `resolutions(()).effective_visiblities` to avoid cycle errors in `report_object_error` Inside of `report_object_error`, using the `effective_visibilities` query causes cycles since it calls `type_of`, which itself may call `typeck`, which may end up reporting its own object-safety errors. Fixes rust-lang#119346 Fixes rust-lang#119502
2 parents b8c2074 + 6a2bd5a commit 3294c64

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed

compiler/rustc_infer/src/traits/error_reporting/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,10 @@ pub fn report_object_safety_error<'tcx>(
152152
};
153153
let externally_visible = if !impls.is_empty()
154154
&& let Some(def_id) = trait_def_id.as_local()
155-
&& tcx.effective_visibilities(()).is_exported(def_id)
155+
// We may be executing this during typeck, which would result in cycle
156+
// if we used effective_visibilities query, which looks into opaque types
157+
// (and therefore calls typeck).
158+
&& tcx.resolutions(()).effective_visibilities.is_exported(def_id)
156159
{
157160
true
158161
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
trait Marker {}
2+
impl Marker for u32 {}
3+
4+
trait MyTrait {
5+
fn foo(&self) -> impl Marker;
6+
}
7+
8+
struct Outer;
9+
10+
impl MyTrait for Outer {
11+
fn foo(&self) -> impl Marker {
12+
42
13+
}
14+
}
15+
16+
impl dyn MyTrait {
17+
//~^ ERROR the trait `MyTrait` cannot be made into an object
18+
fn other(&self) -> impl Marker {
19+
//~^ ERROR the trait `MyTrait` cannot be made into an object
20+
MyTrait::foo(&self)
21+
//~^ ERROR the trait bound `&dyn MyTrait: MyTrait` is not satisfied
22+
//~| ERROR the trait bound `&dyn MyTrait: MyTrait` is not satisfied
23+
//~| ERROR the trait bound `&dyn MyTrait: MyTrait` is not satisfied
24+
//~| ERROR the trait `MyTrait` cannot be made into an object
25+
}
26+
}
27+
28+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
error[E0277]: the trait bound `&dyn MyTrait: MyTrait` is not satisfied
2+
--> $DIR/cycle-effective-visibilities-during-object-safety.rs:20:22
3+
|
4+
LL | MyTrait::foo(&self)
5+
| ------------ ^^^^^ the trait `MyTrait` is not implemented for `&dyn MyTrait`
6+
| |
7+
| required by a bound introduced by this call
8+
|
9+
= help: the trait `MyTrait` is implemented for `Outer`
10+
11+
error[E0038]: the trait `MyTrait` cannot be made into an object
12+
--> $DIR/cycle-effective-visibilities-during-object-safety.rs:20:9
13+
|
14+
LL | MyTrait::foo(&self)
15+
| ^^^^^^^^^^^^ `MyTrait` cannot be made into an object
16+
|
17+
note: for a trait to be "object safe" 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>
18+
--> $DIR/cycle-effective-visibilities-during-object-safety.rs:5:22
19+
|
20+
LL | trait MyTrait {
21+
| ------- this trait cannot be made into an object...
22+
LL | fn foo(&self) -> impl Marker;
23+
| ^^^^^^^^^^^ ...because method `foo` references an `impl Trait` type in its return type
24+
= help: consider moving `foo` to another trait
25+
= help: only type `Outer` implements the trait, consider using it directly instead
26+
27+
error[E0277]: the trait bound `&dyn MyTrait: MyTrait` is not satisfied
28+
--> $DIR/cycle-effective-visibilities-during-object-safety.rs:20:9
29+
|
30+
LL | MyTrait::foo(&self)
31+
| ^^^^^^^^^^^^^^^^^^^ the trait `MyTrait` is not implemented for `&dyn MyTrait`
32+
|
33+
= help: the trait `MyTrait` is implemented for `Outer`
34+
35+
error[E0277]: the trait bound `&dyn MyTrait: MyTrait` is not satisfied
36+
--> $DIR/cycle-effective-visibilities-during-object-safety.rs:20:9
37+
|
38+
LL | MyTrait::foo(&self)
39+
| ^^^^^^^^^^^^ the trait `MyTrait` is not implemented for `&dyn MyTrait`
40+
|
41+
= help: the trait `MyTrait` is implemented for `Outer`
42+
43+
error[E0038]: the trait `MyTrait` cannot be made into an object
44+
--> $DIR/cycle-effective-visibilities-during-object-safety.rs:16:6
45+
|
46+
LL | impl dyn MyTrait {
47+
| ^^^^^^^^^^^ `MyTrait` cannot be made into an object
48+
|
49+
note: for a trait to be "object safe" 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>
50+
--> $DIR/cycle-effective-visibilities-during-object-safety.rs:5:22
51+
|
52+
LL | trait MyTrait {
53+
| ------- this trait cannot be made into an object...
54+
LL | fn foo(&self) -> impl Marker;
55+
| ^^^^^^^^^^^ ...because method `foo` references an `impl Trait` type in its return type
56+
= help: consider moving `foo` to another trait
57+
= help: only type `Outer` implements the trait, consider using it directly instead
58+
59+
error[E0038]: the trait `MyTrait` cannot be made into an object
60+
--> $DIR/cycle-effective-visibilities-during-object-safety.rs:18:15
61+
|
62+
LL | fn other(&self) -> impl Marker {
63+
| ^^^^ `MyTrait` cannot be made into an object
64+
|
65+
note: for a trait to be "object safe" 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>
66+
--> $DIR/cycle-effective-visibilities-during-object-safety.rs:5:22
67+
|
68+
LL | trait MyTrait {
69+
| ------- this trait cannot be made into an object...
70+
LL | fn foo(&self) -> impl Marker;
71+
| ^^^^^^^^^^^ ...because method `foo` references an `impl Trait` type in its return type
72+
= help: consider moving `foo` to another trait
73+
= help: only type `Outer` implements the trait, consider using it directly instead
74+
75+
error: aborting due to 6 previous errors
76+
77+
Some errors have detailed explanations: E0038, E0277.
78+
For more information about an error, try `rustc --explain E0038`.

0 commit comments

Comments
 (0)