Skip to content

Commit 594ba79

Browse files
authored
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 e8b5899 + 03e9eff commit 594ba79

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
@@ -132,7 +132,10 @@ pub fn report_object_safety_error<'tcx>(
132132
};
133133
let externally_visible = if !impls.is_empty()
134134
&& let Some(def_id) = trait_def_id.as_local()
135-
&& tcx.effective_visibilities(()).is_exported(def_id)
135+
// We may be executing this during typeck, which would result in cycle
136+
// if we used effective_visibilities query, which looks into opaque types
137+
// (and therefore calls typeck).
138+
&& tcx.resolutions(()).effective_visibilities.is_exported(def_id)
136139
{
137140
true
138141
} 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[E0038]: the trait `MyTrait` cannot be made into an object
2+
--> $DIR/cycle-effective-visibilities-during-object-safety.rs:16:6
3+
|
4+
LL | impl dyn MyTrait {
5+
| ^^^^^^^^^^^ `MyTrait` cannot be made into an object
6+
|
7+
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>
8+
--> $DIR/cycle-effective-visibilities-during-object-safety.rs:5:22
9+
|
10+
LL | trait MyTrait {
11+
| ------- this trait cannot be made into an object...
12+
LL | fn foo(&self) -> impl Marker;
13+
| ^^^^^^^^^^^ ...because method `foo` references an `impl Trait` type in its return type
14+
= help: consider moving `foo` to another trait
15+
= help: only type `Outer` implements the trait, consider using it directly instead
16+
17+
error[E0038]: the trait `MyTrait` cannot be made into an object
18+
--> $DIR/cycle-effective-visibilities-during-object-safety.rs:18:15
19+
|
20+
LL | fn other(&self) -> impl Marker {
21+
| ^^^^ `MyTrait` cannot be made into an object
22+
|
23+
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>
24+
--> $DIR/cycle-effective-visibilities-during-object-safety.rs:5:22
25+
|
26+
LL | trait MyTrait {
27+
| ------- this trait cannot be made into an object...
28+
LL | fn foo(&self) -> impl Marker;
29+
| ^^^^^^^^^^^ ...because method `foo` references an `impl Trait` type in its return type
30+
= help: consider moving `foo` to another trait
31+
= help: only type `Outer` implements the trait, consider using it directly instead
32+
33+
error[E0277]: the trait bound `&dyn MyTrait: MyTrait` is not satisfied
34+
--> $DIR/cycle-effective-visibilities-during-object-safety.rs:20:22
35+
|
36+
LL | MyTrait::foo(&self)
37+
| ------------ ^^^^^ the trait `MyTrait` is not implemented for `&dyn MyTrait`
38+
| |
39+
| required by a bound introduced by this call
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:20:9
45+
|
46+
LL | MyTrait::foo(&self)
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[E0277]: the trait bound `&dyn MyTrait: MyTrait` is not satisfied
60+
--> $DIR/cycle-effective-visibilities-during-object-safety.rs:20:9
61+
|
62+
LL | MyTrait::foo(&self)
63+
| ^^^^^^^^^^^^^^^^^^^ the trait `MyTrait` is not implemented for `&dyn MyTrait`
64+
|
65+
= help: the trait `MyTrait` is implemented for `Outer`
66+
67+
error[E0277]: the trait bound `&dyn MyTrait: MyTrait` is not satisfied
68+
--> $DIR/cycle-effective-visibilities-during-object-safety.rs:20:9
69+
|
70+
LL | MyTrait::foo(&self)
71+
| ^^^^^^^^^^^^ the trait `MyTrait` is not implemented for `&dyn MyTrait`
72+
|
73+
= help: the trait `MyTrait` is implemented for `Outer`
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)