Skip to content

Commit 1ab9e58

Browse files
zachs18lcnr
andcommitted
Update compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs
Add suggestions from code review Co-authored-by: lcnr <rust@lcnr.de>
1 parent 10328b3 commit 1ab9e58

5 files changed

Lines changed: 22 additions & 18 deletions

File tree

compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,14 @@ fn dyn_compatibility_violations_for_trait(
8989
.collect();
9090

9191
// Check the trait itself.
92-
let has_sized_self = trait_has_sized_self(tcx, trait_def_id);
93-
if has_sized_self {
92+
if trait_has_sized_self(tcx, trait_def_id) {
9493
// We don't want to include the requirement from `Sized` itself to be `Sized` in the list.
9594
let spans = get_sized_bounds(tcx, trait_def_id);
9695
violations.push(DynCompatibilityViolation::SizedSelf(spans));
97-
}
98-
// only report dyn-incompatible supertraits if there is no `Self: Sized`,
99-
// to avoid duplicate notes about `Sized`.
100-
let trait_def = tcx.trait_def(trait_def_id);
101-
if !has_sized_self && let Some(span) = trait_def.force_dyn_incompatible {
96+
} else if let Some(span) = tcx.trait_def(trait_def_id).force_dyn_incompatible {
10297
violations.push(DynCompatibilityViolation::ExplicitlyDynIncompatible([span].into()));
10398
}
99+
104100
let spans = predicates_reference_self(tcx, trait_def_id, false);
105101
if !spans.is_empty() {
106102
violations.push(DynCompatibilityViolation::SupertraitSelf(spans));

tests/ui/traits/dyn-metasized.rs renamed to tests/ui/traits/object/sizedness/dyn-metasized.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//@ run-pass
2+
//! This test and `dyn-sized.rs` and `dyn-pointeesized.rs` test that dyn-compatibility
3+
//! correctly handles sizedness traits, which are special in several parts of the compiler.
24
#![feature(sized_hierarchy)]
35
use std::marker::MetaSized;
46

tests/ui/traits/dyn-pointeesized.rs renamed to tests/ui/traits/object/sizedness/dyn-pointeesized.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//@ run-pass
2+
//! This test and `dyn-sized.rs` and `dyn-metaesized.rs` test that dyn-compatibility
3+
//! correctly handles sizedness traits, which are special in several parts of the compiler.
24
#![feature(sized_hierarchy)]
3-
// PointeeSized is effectively removed before reaching the trait solver.
5+
// PointeeSized is effectively removed before reaching the trait solver,
6+
// so it's as though it wasn't even mentioned in the trait list.
47
use std::marker::PointeeSized;
58

69
fn main() {

tests/ui/traits/dyn-sized.rs renamed to tests/ui/traits/object/sizedness/dyn-sized.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//! This test and `dyn-metasized.rs` and `dyn-pointeesized.rs` test that dyn-compatibility
2+
//! correctly handles sizedness traits, which are special in several parts of the compiler.
3+
14
trait Foo: std::fmt::Debug + Sized {}
25

36
impl<T: std::fmt::Debug + Sized> Foo for T {}

tests/ui/traits/dyn-sized.stderr renamed to tests/ui/traits/object/sizedness/dyn-sized.stderr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0038]: the trait `Sized` is not dyn compatible
2-
--> $DIR/dyn-sized.rs:5:47
2+
--> $DIR/dyn-sized.rs:8:47
33
|
44
LL | fn unsize_sized<T: 'static>(x: Box<T>) -> Box<dyn Sized> {
55
| ^^^^^^^^^ `Sized` is not dyn compatible
@@ -9,22 +9,22 @@ LL | fn unsize_sized<T: 'static>(x: Box<T>) -> Box<dyn Sized> {
99
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
1010

1111
error[E0038]: the trait `Foo` is not dyn compatible
12-
--> $DIR/dyn-sized.rs:10:27
12+
--> $DIR/dyn-sized.rs:13:27
1313
|
1414
LL | fn unsize_subtrait(x: Box<dyn Foo>) -> Box<dyn Sized> {
1515
| ^^^^^^^ `Foo` is not dyn compatible
1616
|
1717
note: for a trait to be dyn compatible it needs to allow building a vtable
1818
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
19-
--> $DIR/dyn-sized.rs:1:30
19+
--> $DIR/dyn-sized.rs:4:30
2020
|
2121
LL | trait Foo: std::fmt::Debug + Sized {}
2222
| --- ^^^^^ ...because it requires `Self: Sized`
2323
| |
2424
| this trait is not dyn compatible...
2525

2626
error[E0038]: the trait `Sized` is not dyn compatible
27-
--> $DIR/dyn-sized.rs:10:44
27+
--> $DIR/dyn-sized.rs:13:44
2828
|
2929
LL | fn unsize_subtrait(x: Box<dyn Foo>) -> Box<dyn Sized> {
3030
| ^^^^^^^^^ `Sized` is not dyn compatible
@@ -34,7 +34,7 @@ LL | fn unsize_subtrait(x: Box<dyn Foo>) -> Box<dyn Sized> {
3434
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
3535

3636
error[E0038]: the trait `Sized` is not dyn compatible
37-
--> $DIR/dyn-sized.rs:17:15
37+
--> $DIR/dyn-sized.rs:20:15
3838
|
3939
LL | let _bx = unsize_sized(Box::new(vec![1, 2, 3]));
4040
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Sized` is not dyn compatible
@@ -44,37 +44,37 @@ LL | let _bx = unsize_sized(Box::new(vec![1, 2, 3]));
4444
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
4545

4646
error[E0038]: the trait `Foo` is not dyn compatible
47-
--> $DIR/dyn-sized.rs:20:21
47+
--> $DIR/dyn-sized.rs:23:21
4848
|
4949
LL | let bx: Box<dyn Foo> = Box::new(vec![1, 2, 3]);
5050
| ^^^ `Foo` is not dyn compatible
5151
|
5252
note: for a trait to be dyn compatible it needs to allow building a vtable
5353
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
54-
--> $DIR/dyn-sized.rs:1:30
54+
--> $DIR/dyn-sized.rs:4:30
5555
|
5656
LL | trait Foo: std::fmt::Debug + Sized {}
5757
| --- ^^^^^ ...because it requires `Self: Sized`
5858
| |
5959
| this trait is not dyn compatible...
6060

6161
error[E0038]: the trait `Foo` is not dyn compatible
62-
--> $DIR/dyn-sized.rs:23:31
62+
--> $DIR/dyn-sized.rs:26:31
6363
|
6464
LL | let _bx = unsize_subtrait(bx);
6565
| ^^ `Foo` is not dyn compatible
6666
|
6767
note: for a trait to be dyn compatible it needs to allow building a vtable
6868
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
69-
--> $DIR/dyn-sized.rs:1:30
69+
--> $DIR/dyn-sized.rs:4:30
7070
|
7171
LL | trait Foo: std::fmt::Debug + Sized {}
7272
| --- ^^^^^ ...because it requires `Self: Sized`
7373
| |
7474
| this trait is not dyn compatible...
7575

7676
error[E0038]: the trait `Sized` is not dyn compatible
77-
--> $DIR/dyn-sized.rs:23:15
77+
--> $DIR/dyn-sized.rs:26:15
7878
|
7979
LL | let _bx = unsize_subtrait(bx);
8080
| ^^^^^^^^^^^^^^^^^^^ `Sized` is not dyn compatible

0 commit comments

Comments
 (0)