Skip to content

Commit 2b147f2

Browse files
committed
trait_sel: const {Meta,}Sized in impl headers
Now that sizedness traits have been made const, pretty printing of impl headers should print the constness of the sizedness trait.
1 parent 8467e8b commit 2b147f2

File tree

7 files changed

+103
-17
lines changed

7 files changed

+103
-17
lines changed

compiler/rustc_middle/src/ty/predicate.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,15 @@ impl<'tcx> Clause<'tcx> {
208208
}
209209
}
210210

211+
pub fn as_host_effect_clause(self) -> Option<ty::Binder<'tcx, HostEffectPredicate<'tcx>>> {
212+
let clause = self.kind();
213+
if let ty::ClauseKind::HostEffect(host_effect_clause) = clause.skip_binder() {
214+
Some(clause.rebind(host_effect_clause))
215+
} else {
216+
None
217+
}
218+
}
219+
211220
pub fn as_projection_clause(self) -> Option<ty::Binder<'tcx, ProjectionPredicate<'tcx>>> {
212221
let clause = self.kind();
213222
if let ty::ClauseKind::Projection(projection_clause) = clause.skip_binder() {

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,9 @@ pub(crate) fn to_pretty_impl_header(tcx: TyCtxt<'_>, impl_def_id: DefId) -> Opti
340340

341341
#[derive(Debug, Default)]
342342
struct SizednessFound {
343+
const_sized: bool,
343344
sized: bool,
345+
const_meta_sized: bool,
344346
meta_sized: bool,
345347
}
346348

@@ -389,20 +391,35 @@ pub(crate) fn to_pretty_impl_header(tcx: TyCtxt<'_>, impl_def_id: DefId) -> Opti
389391
}
390392
}
391393

394+
if let Some(host_effect_clause) = p.as_host_effect_clause() {
395+
let self_ty = host_effect_clause.self_ty().skip_binder();
396+
let sizedness_of = types_with_sizedness_bounds.entry(self_ty).or_default();
397+
if Some(host_effect_clause.def_id()) == sized_trait {
398+
sizedness_of.const_sized = true;
399+
continue;
400+
} else if Some(host_effect_clause.def_id()) == meta_sized_trait {
401+
sizedness_of.const_meta_sized = true;
402+
continue;
403+
}
404+
}
405+
392406
pretty_predicates.push(p.to_string());
393407
}
394408

395409
for (ty, sizedness) in types_with_sizedness_bounds {
396410
if !tcx.features().sized_hierarchy() {
397-
if sizedness.sized {
411+
if sizedness.const_sized || sizedness.sized {
398412
// Maybe a default bound, don't write anything.
399413
} else {
400414
pretty_predicates.push(format!("{ty}: ?Sized"));
401415
}
402416
} else {
403-
if sizedness.sized {
417+
if sizedness.const_sized {
404418
// Maybe a default bound, don't write anything.
419+
} else if sizedness.sized {
405420
pretty_predicates.push(format!("{ty}: Sized"));
421+
} else if sizedness.const_meta_sized {
422+
pretty_predicates.push(format!("{ty}: const MetaSized"));
406423
} else if sizedness.meta_sized {
407424
pretty_predicates.push(format!("{ty}: MetaSized"));
408425
} else {

tests/ui/sized-hierarchy/auxiliary/pretty-print-dep.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
#![feature(sized_hierarchy)]
1+
#![feature(const_trait_impl, sized_hierarchy)]
22

33
use std::marker::{MetaSized, PointeeSized};
44

5+
pub trait ConstSizedTr {}
6+
7+
impl<T: const Sized> ConstSizedTr for T {}
8+
59
pub trait SizedTr {}
610

711
impl<T: Sized> SizedTr for T {}
@@ -10,6 +14,10 @@ pub trait NegSizedTr {}
1014

1115
impl<T: ?Sized> NegSizedTr for T {}
1216

17+
pub trait ConstMetaSizedTr {}
18+
19+
impl<T: const MetaSized> ConstMetaSizedTr for T {}
20+
1321
pub trait MetaSizedTr {}
1422

1523
impl<T: MetaSized> MetaSizedTr for T {}

tests/ui/sized-hierarchy/pretty-print-no-feat-dep-has-feat.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
//@ compile-flags: --crate-type=lib
33

44
extern crate pretty_print_dep;
5-
use pretty_print_dep::{SizedTr, NegSizedTr, MetaSizedTr, PointeeSizedTr};
5+
use pretty_print_dep::{
6+
ConstSizedTr, SizedTr, NegSizedTr, ConstMetaSizedTr, MetaSizedTr, PointeeSizedTr
7+
};
68

79
// Test that printing the sizedness trait bounds in the conflicting impl error without enabling
810
// `sized_hierarchy` will continue to print `?Sized`, even if the dependency is compiled with
@@ -13,12 +15,18 @@ use pretty_print_dep::{SizedTr, NegSizedTr, MetaSizedTr, PointeeSizedTr};
1315

1416
struct X<T>(T);
1517

18+
impl<T: Sized> ConstSizedTr for X<T> {}
19+
//~^ ERROR conflicting implementations of trait `ConstSizedTr` for type `X<_>`
20+
1621
impl<T: Sized> SizedTr for X<T> {}
1722
//~^ ERROR conflicting implementations of trait `SizedTr` for type `X<_>`
1823

1924
impl<T: ?Sized> NegSizedTr for X<T> {}
2025
//~^ ERROR conflicting implementations of trait `NegSizedTr` for type `X<_>`
2126

27+
impl<T: ?Sized> ConstMetaSizedTr for X<T> {}
28+
//~^ ERROR conflicting implementations of trait `ConstMetaSizedTr` for type `X<_>`
29+
2230
impl<T: ?Sized> MetaSizedTr for X<T> {}
2331
//~^ ERROR conflicting implementations of trait `MetaSizedTr` for type `X<_>`
2432

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
error[E0119]: conflicting implementations of trait `ConstSizedTr` for type `X<_>`
2+
--> $DIR/pretty-print-no-feat-dep-has-feat.rs:18:1
3+
|
4+
LL | impl<T: Sized> ConstSizedTr for X<T> {}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: conflicting implementation in crate `pretty_print_dep`:
8+
- impl<T> ConstSizedTr for T;
9+
110
error[E0119]: conflicting implementations of trait `SizedTr` for type `X<_>`
2-
--> $DIR/pretty-print-no-feat-dep-has-feat.rs:16:1
11+
--> $DIR/pretty-print-no-feat-dep-has-feat.rs:21:1
312
|
413
LL | impl<T: Sized> SizedTr for X<T> {}
514
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -8,7 +17,7 @@ LL | impl<T: Sized> SizedTr for X<T> {}
817
- impl<T> SizedTr for T;
918

1019
error[E0119]: conflicting implementations of trait `NegSizedTr` for type `X<_>`
11-
--> $DIR/pretty-print-no-feat-dep-has-feat.rs:19:1
20+
--> $DIR/pretty-print-no-feat-dep-has-feat.rs:24:1
1221
|
1322
LL | impl<T: ?Sized> NegSizedTr for X<T> {}
1423
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -17,8 +26,18 @@ LL | impl<T: ?Sized> NegSizedTr for X<T> {}
1726
- impl<T> NegSizedTr for T
1827
where T: ?Sized;
1928

29+
error[E0119]: conflicting implementations of trait `ConstMetaSizedTr` for type `X<_>`
30+
--> $DIR/pretty-print-no-feat-dep-has-feat.rs:27:1
31+
|
32+
LL | impl<T: ?Sized> ConstMetaSizedTr for X<T> {}
33+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
34+
|
35+
= note: conflicting implementation in crate `pretty_print_dep`:
36+
- impl<T> ConstMetaSizedTr for T
37+
where T: ?Sized;
38+
2039
error[E0119]: conflicting implementations of trait `MetaSizedTr` for type `X<_>`
21-
--> $DIR/pretty-print-no-feat-dep-has-feat.rs:22:1
40+
--> $DIR/pretty-print-no-feat-dep-has-feat.rs:30:1
2241
|
2342
LL | impl<T: ?Sized> MetaSizedTr for X<T> {}
2443
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -28,7 +47,7 @@ LL | impl<T: ?Sized> MetaSizedTr for X<T> {}
2847
where T: ?Sized;
2948

3049
error[E0119]: conflicting implementations of trait `PointeeSizedTr` for type `X<_>`
31-
--> $DIR/pretty-print-no-feat-dep-has-feat.rs:25:1
50+
--> $DIR/pretty-print-no-feat-dep-has-feat.rs:33:1
3251
|
3352
LL | impl<T: ?Sized> PointeeSizedTr for X<T> {}
3453
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -37,6 +56,6 @@ LL | impl<T: ?Sized> PointeeSizedTr for X<T> {}
3756
- impl<T> PointeeSizedTr for T
3857
where T: ?Sized;
3958

40-
error: aborting due to 4 previous errors
59+
error: aborting due to 6 previous errors
4160

4261
For more information about this error, try `rustc --explain E0119`.

tests/ui/sized-hierarchy/pretty-print.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ aux-build:pretty-print-dep.rs
22
//@ compile-flags: --crate-type=lib
3-
#![feature(sized_hierarchy)]
3+
#![feature(const_trait_impl, sized_hierarchy)]
44

55
// Test that printing the sizedness trait bounds in the conflicting impl error with
66
// `sized_hierarchy` enabled prints all of the appropriate bounds.
@@ -11,16 +11,22 @@
1111
use std::marker::{MetaSized, PointeeSized};
1212

1313
extern crate pretty_print_dep;
14-
use pretty_print_dep::{SizedTr, MetaSizedTr, PointeeSizedTr};
14+
use pretty_print_dep::{ConstSizedTr, SizedTr, ConstMetaSizedTr, MetaSizedTr, PointeeSizedTr};
1515

1616
struct X<T>(T);
1717

18+
impl<T: const Sized> ConstSizedTr for X<T> {}
19+
//~^ ERROR conflicting implementations of trait `ConstSizedTr` for type `X<_>`
20+
1821
impl<T: Sized> SizedTr for X<T> {}
1922
//~^ ERROR conflicting implementations of trait `SizedTr` for type `X<_>`
2023

2124
impl<T: ?Sized> pretty_print_dep::NegSizedTr for X<T> {}
2225
//~^ ERROR conflicting implementations of trait `NegSizedTr` for type `X<_>`
2326

27+
impl<T: const MetaSized> ConstMetaSizedTr for X<T> {}
28+
//~^ ERROR conflicting implementations of trait `ConstMetaSizedTr` for type `X<_>`
29+
2430
impl<T: MetaSized> MetaSizedTr for X<T> {}
2531
//~^ ERROR conflicting implementations of trait `MetaSizedTr` for type `X<_>`
2632

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1-
error[E0119]: conflicting implementations of trait `SizedTr` for type `X<_>`
1+
error[E0119]: conflicting implementations of trait `ConstSizedTr` for type `X<_>`
22
--> $DIR/pretty-print.rs:18:1
33
|
4+
LL | impl<T: const Sized> ConstSizedTr for X<T> {}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: conflicting implementation in crate `pretty_print_dep`:
8+
- impl<T> ConstSizedTr for T;
9+
10+
error[E0119]: conflicting implementations of trait `SizedTr` for type `X<_>`
11+
--> $DIR/pretty-print.rs:21:1
12+
|
413
LL | impl<T: Sized> SizedTr for X<T> {}
514
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
615
|
@@ -9,17 +18,27 @@ LL | impl<T: Sized> SizedTr for X<T> {}
918
where T: Sized;
1019

1120
error[E0119]: conflicting implementations of trait `NegSizedTr` for type `X<_>`
12-
--> $DIR/pretty-print.rs:21:1
21+
--> $DIR/pretty-print.rs:24:1
1322
|
1423
LL | impl<T: ?Sized> pretty_print_dep::NegSizedTr for X<T> {}
1524
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1625
|
1726
= note: conflicting implementation in crate `pretty_print_dep`:
1827
- impl<T> NegSizedTr for T
19-
where T: MetaSized;
28+
where T: const MetaSized;
29+
30+
error[E0119]: conflicting implementations of trait `ConstMetaSizedTr` for type `X<_>`
31+
--> $DIR/pretty-print.rs:27:1
32+
|
33+
LL | impl<T: const MetaSized> ConstMetaSizedTr for X<T> {}
34+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
35+
|
36+
= note: conflicting implementation in crate `pretty_print_dep`:
37+
- impl<T> ConstMetaSizedTr for T
38+
where T: const MetaSized;
2039

2140
error[E0119]: conflicting implementations of trait `MetaSizedTr` for type `X<_>`
22-
--> $DIR/pretty-print.rs:24:1
41+
--> $DIR/pretty-print.rs:30:1
2342
|
2443
LL | impl<T: MetaSized> MetaSizedTr for X<T> {}
2544
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -29,7 +48,7 @@ LL | impl<T: MetaSized> MetaSizedTr for X<T> {}
2948
where T: MetaSized;
3049

3150
error[E0119]: conflicting implementations of trait `PointeeSizedTr` for type `X<_>`
32-
--> $DIR/pretty-print.rs:27:1
51+
--> $DIR/pretty-print.rs:33:1
3352
|
3453
LL | impl<T: PointeeSized> PointeeSizedTr for X<T> {}
3554
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -38,6 +57,6 @@ LL | impl<T: PointeeSized> PointeeSizedTr for X<T> {}
3857
- impl<T> PointeeSizedTr for T
3958
where T: PointeeSized;
4059

41-
error: aborting due to 4 previous errors
60+
error: aborting due to 6 previous errors
4261

4362
For more information about this error, try `rustc --explain E0119`.

0 commit comments

Comments
 (0)