Skip to content

Commit d7e9cc2

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 f0b79e3 commit d7e9cc2

File tree

5 files changed

+70
-11
lines changed

5 files changed

+70
-11
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_metasized: bool,
344346
metasized: bool,
345347
}
346348

@@ -389,21 +391,36 @@ 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()) == metasized_trait {
401+
sizedness_of.const_metasized = 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
debug!(?ty, ?sizedness);
397411
if !tcx.features().sized_hierarchy() {
398-
if sizedness.sized {
412+
if sizedness.const_sized || sizedness.sized {
399413
// Maybe a default bound, don't write anything.
400414
} else {
401415
pretty_predicates.push(format!("{ty}: ?Sized"));
402416
}
403417
} else {
404-
if sizedness.sized {
418+
if sizedness.const_sized {
405419
// Maybe a default bound, don't write anything.
420+
} else if sizedness.sized {
406421
pretty_predicates.push(format!("{ty}: Sized"));
422+
} else if sizedness.const_metasized {
423+
pretty_predicates.push(format!("{ty}: const MetaSized"));
407424
} else if sizedness.metasized {
408425
pretty_predicates.push(format!("{ty}: MetaSized"));
409426
} 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.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)