Skip to content

Commit 48934c4

Browse files
authored
Rollup merge of rust-lang#108909 - spastorino:new-rpitit-7, r=compiler-errors
Fix object safety checks for new RPITITs This one goes on top of rust-lang#108869 r? `@compiler-errors`
2 parents 5037836 + 8b9344a commit 48934c4

File tree

11 files changed

+134
-15
lines changed

11 files changed

+134
-15
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
14401440
tcx.associated_items(pred.def_id())
14411441
.in_definition_order()
14421442
.filter(|item| item.kind == ty::AssocKind::Type)
1443+
.filter(|item| tcx.opt_rpitit_info(item.def_id).is_none())
14431444
.map(|item| item.def_id),
14441445
);
14451446
}

compiler/rustc_trait_selection/src/traits/object_safety.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use super::{elaborate_predicates, elaborate_trait_ref};
1313
use crate::infer::TyCtxtInferExt;
1414
use crate::traits::query::evaluate_obligation::InferCtxtExt;
1515
use crate::traits::{self, Obligation, ObligationCause};
16-
use hir::def::DefKind;
1716
use rustc_errors::{DelayDm, FatalError, MultiSpan};
1817
use rustc_hir as hir;
1918
use rustc_hir::def_id::DefId;
@@ -157,6 +156,7 @@ fn object_safety_violations_for_trait(
157156
.in_definition_order()
158157
.filter(|item| item.kind == ty::AssocKind::Type)
159158
.filter(|item| !tcx.generics_of(item.def_id).params.is_empty())
159+
.filter(|item| tcx.opt_rpitit_info(item.def_id).is_none())
160160
.map(|item| {
161161
let ident = item.ident(tcx);
162162
ObjectSafetyViolation::GAT(ident.name, ident.span)
@@ -854,7 +854,7 @@ fn contains_illegal_self_type_reference<'tcx, T: TypeVisitable<TyCtxt<'tcx>>>(
854854
}
855855
}
856856
ty::Alias(ty::Projection, ref data)
857-
if self.tcx.def_kind(data.def_id) == DefKind::ImplTraitPlaceholder =>
857+
if self.tcx.is_impl_trait_in_trait(data.def_id) =>
858858
{
859859
// We'll deny these later in their own pass
860860
ControlFlow::Continue(())
@@ -921,7 +921,7 @@ pub fn contains_illegal_impl_trait_in_trait<'tcx>(
921921
ty.skip_binder().walk().find_map(|arg| {
922922
if let ty::GenericArgKind::Type(ty) = arg.unpack()
923923
&& let ty::Alias(ty::Projection, proj) = ty.kind()
924-
&& tcx.def_kind(proj.def_id) == DefKind::ImplTraitPlaceholder
924+
&& tcx.is_impl_trait_in_trait(proj.def_id)
925925
{
926926
Some(MethodViolationCode::ReferencesImplTraitInTrait(tcx.def_span(proj.def_id)))
927927
} else {

tests/ui/async-await/in-trait/object-safety.stderr renamed to tests/ui/async-await/in-trait/object-safety.current.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: the feature `async_fn_in_trait` is incomplete and may not be safe to use and/or cause compiler crashes
2-
--> $DIR/object-safety.rs:3:12
2+
--> $DIR/object-safety.rs:5:12
33
|
44
LL | #![feature(async_fn_in_trait)]
55
| ^^^^^^^^^^^^^^^^^
@@ -8,13 +8,13 @@ LL | #![feature(async_fn_in_trait)]
88
= note: `#[warn(incomplete_features)]` on by default
99

1010
error[E0038]: the trait `Foo` cannot be made into an object
11-
--> $DIR/object-safety.rs:11:12
11+
--> $DIR/object-safety.rs:13:12
1212
|
1313
LL | let x: &dyn Foo = todo!();
1414
| ^^^^^^^^ `Foo` cannot be made into an object
1515
|
1616
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>
17-
--> $DIR/object-safety.rs:7:14
17+
--> $DIR/object-safety.rs:9:14
1818
|
1919
LL | trait Foo {
2020
| --- this trait cannot be made into an object...
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
warning: the feature `async_fn_in_trait` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/object-safety.rs:5:12
3+
|
4+
LL | #![feature(async_fn_in_trait)]
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #91611 <https://github.com/rust-lang/rust/issues/91611> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
error[E0038]: the trait `Foo` cannot be made into an object
11+
--> $DIR/object-safety.rs:13:12
12+
|
13+
LL | let x: &dyn Foo = todo!();
14+
| ^^^^^^^^ `Foo` cannot be made into an object
15+
|
16+
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>
17+
--> $DIR/object-safety.rs:9:14
18+
|
19+
LL | trait Foo {
20+
| --- this trait cannot be made into an object...
21+
LL | async fn foo(&self);
22+
| ^^^ ...because method `foo` is `async`
23+
= help: consider moving `foo` to another trait
24+
25+
error: aborting due to previous error; 1 warning emitted
26+
27+
For more information about this error, try `rustc --explain E0038`.

tests/ui/async-await/in-trait/object-safety.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// edition:2021
2+
// [next] compile-flags: -Zlower-impl-trait-in-trait-to-assoc-ty
3+
// revisions: current next
24

35
#![feature(async_fn_in_trait)]
46
//~^ WARN the feature `async_fn_in_trait` is incomplete and may not be safe to use and/or cause compiler crashes

tests/ui/impl-trait/in-trait/issue-102140.stderr renamed to tests/ui/impl-trait/in-trait/issue-102140.current.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0277]: the trait bound `&dyn MyTrait: MyTrait` is not satisfied
2-
--> $DIR/issue-102140.rs:23:22
2+
--> $DIR/issue-102140.rs:26:22
33
|
44
LL | MyTrait::foo(&self)
55
| ------------ ^^^^^ the trait `MyTrait` is not implemented for `&dyn MyTrait`
@@ -13,15 +13,15 @@ LL + MyTrait::foo(self)
1313
|
1414

1515
error[E0277]: the trait bound `&dyn MyTrait: MyTrait` is not satisfied
16-
--> $DIR/issue-102140.rs:23:9
16+
--> $DIR/issue-102140.rs:26:9
1717
|
1818
LL | MyTrait::foo(&self)
1919
| ^^^^^^^^^^^^^^^^^^^ the trait `MyTrait` is not implemented for `&dyn MyTrait`
2020
|
2121
= help: the trait `MyTrait` is implemented for `Outer`
2222

2323
error[E0277]: the trait bound `&dyn MyTrait: MyTrait` is not satisfied
24-
--> $DIR/issue-102140.rs:23:9
24+
--> $DIR/issue-102140.rs:26:9
2525
|
2626
LL | MyTrait::foo(&self)
2727
| ^^^^^^^^^^^^ the trait `MyTrait` is not implemented for `&dyn MyTrait`
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error[E0277]: the trait bound `&dyn MyTrait: MyTrait` is not satisfied
2+
--> $DIR/issue-102140.rs:26: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: consider removing the leading `&`-reference
10+
|
11+
LL - MyTrait::foo(&self)
12+
LL + MyTrait::foo(self)
13+
|
14+
15+
error[E0277]: the trait bound `&dyn MyTrait: MyTrait` is not satisfied
16+
--> $DIR/issue-102140.rs:26:9
17+
|
18+
LL | MyTrait::foo(&self)
19+
| ^^^^^^^^^^^^^^^^^^^ the trait `MyTrait` is not implemented for `&dyn MyTrait`
20+
|
21+
= help: the trait `MyTrait` is implemented for `Outer`
22+
23+
error[E0277]: the trait bound `&dyn MyTrait: MyTrait` is not satisfied
24+
--> $DIR/issue-102140.rs:26:9
25+
|
26+
LL | MyTrait::foo(&self)
27+
| ^^^^^^^^^^^^ the trait `MyTrait` is not implemented for `&dyn MyTrait`
28+
|
29+
= help: the trait `MyTrait` is implemented for `Outer`
30+
31+
error: aborting due to 3 previous errors
32+
33+
For more information about this error, try `rustc --explain E0277`.

tests/ui/impl-trait/in-trait/issue-102140.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// [next] compile-flags: -Zlower-impl-trait-in-trait-to-assoc-ty
2+
// revisions: current next
3+
14
#![feature(return_position_impl_trait_in_trait)]
25
#![allow(incomplete_features)]
36

tests/ui/impl-trait/in-trait/object-safety.stderr renamed to tests/ui/impl-trait/in-trait/object-safety.current.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0038]: the trait `Foo` cannot be made into an object
2-
--> $DIR/object-safety.rs:17:33
2+
--> $DIR/object-safety.rs:20:33
33
|
44
LL | let i = Box::new(42_u32) as Box<dyn Foo>;
55
| ^^^^^^^^^^^^ `Foo` cannot be made into an object
66
|
77
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/object-safety.rs:7:22
8+
--> $DIR/object-safety.rs:10:22
99
|
1010
LL | trait Foo {
1111
| --- this trait cannot be made into an object...
@@ -14,13 +14,13 @@ LL | fn baz(&self) -> impl Debug;
1414
= help: consider moving `baz` to another trait
1515

1616
error[E0038]: the trait `Foo` cannot be made into an object
17-
--> $DIR/object-safety.rs:20:13
17+
--> $DIR/object-safety.rs:23:13
1818
|
1919
LL | let s = i.baz();
2020
| ^^^^^^^ `Foo` cannot be made into an object
2121
|
2222
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>
23-
--> $DIR/object-safety.rs:7:22
23+
--> $DIR/object-safety.rs:10:22
2424
|
2525
LL | trait Foo {
2626
| --- this trait cannot be made into an object...
@@ -29,13 +29,13 @@ LL | fn baz(&self) -> impl Debug;
2929
= help: consider moving `baz` to another trait
3030

3131
error[E0038]: the trait `Foo` cannot be made into an object
32-
--> $DIR/object-safety.rs:17:13
32+
--> $DIR/object-safety.rs:20:13
3333
|
3434
LL | let i = Box::new(42_u32) as Box<dyn Foo>;
3535
| ^^^^^^^^^^^^^^^^ `Foo` cannot be made into an object
3636
|
3737
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>
38-
--> $DIR/object-safety.rs:7:22
38+
--> $DIR/object-safety.rs:10:22
3939
|
4040
LL | trait Foo {
4141
| --- this trait cannot be made into an object...
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
error[E0038]: the trait `Foo` cannot be made into an object
2+
--> $DIR/object-safety.rs:20:33
3+
|
4+
LL | let i = Box::new(42_u32) as Box<dyn Foo>;
5+
| ^^^^^^^^^^^^ `Foo` 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/object-safety.rs:10:22
9+
|
10+
LL | trait Foo {
11+
| --- this trait cannot be made into an object...
12+
LL | fn baz(&self) -> impl Debug;
13+
| ^^^^^^^^^^ ...because method `baz` references an `impl Trait` type in its return type
14+
= help: consider moving `baz` to another trait
15+
16+
error[E0038]: the trait `Foo` cannot be made into an object
17+
--> $DIR/object-safety.rs:23:13
18+
|
19+
LL | let s = i.baz();
20+
| ^^^^^^^ `Foo` cannot be made into an object
21+
|
22+
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>
23+
--> $DIR/object-safety.rs:10:22
24+
|
25+
LL | trait Foo {
26+
| --- this trait cannot be made into an object...
27+
LL | fn baz(&self) -> impl Debug;
28+
| ^^^^^^^^^^ ...because method `baz` references an `impl Trait` type in its return type
29+
= help: consider moving `baz` to another trait
30+
31+
error[E0038]: the trait `Foo` cannot be made into an object
32+
--> $DIR/object-safety.rs:20:13
33+
|
34+
LL | let i = Box::new(42_u32) as Box<dyn Foo>;
35+
| ^^^^^^^^^^^^^^^^ `Foo` cannot be made into an object
36+
|
37+
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>
38+
--> $DIR/object-safety.rs:10:22
39+
|
40+
LL | trait Foo {
41+
| --- this trait cannot be made into an object...
42+
LL | fn baz(&self) -> impl Debug;
43+
| ^^^^^^^^^^ ...because method `baz` references an `impl Trait` type in its return type
44+
= help: consider moving `baz` to another trait
45+
= note: required for `Box<u32>` to implement `CoerceUnsized<Box<dyn Foo>>`
46+
= note: required by cast to type `Box<dyn Foo>`
47+
48+
error: aborting due to 3 previous errors
49+
50+
For more information about this error, try `rustc --explain E0038`.

tests/ui/impl-trait/in-trait/object-safety.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// [next] compile-flags: -Zlower-impl-trait-in-trait-to-assoc-ty
2+
// revisions: current next
3+
14
#![feature(return_position_impl_trait_in_trait)]
25
#![allow(incomplete_features)]
36

0 commit comments

Comments
 (0)