Skip to content

Commit c7f5ebf

Browse files
committed
middle: const {Meta,Pointee}Sized in opaques
These traits are now const and that needs to be reflected in their printing in opaques.
1 parent 6962b1d commit c7f5ebf

File tree

3 files changed

+67
-26
lines changed

3 files changed

+67
-26
lines changed

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,10 +1004,12 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
10041004
let mut fn_traits = FxIndexMap::default();
10051005
let mut lifetimes = SmallVec::<[ty::Region<'tcx>; 1]>::new();
10061006

1007-
let mut has_sized_bound = false;
1008-
let mut has_negative_sized_bound = false;
1009-
let mut has_metasized_bound = false;
1010-
let mut has_pointeesized_bound = false;
1007+
let mut has_sized_pred = false;
1008+
let mut has_const_sized_pred = false;
1009+
let mut has_negative_sized_pred = false;
1010+
let mut has_metasized_pred = false;
1011+
let mut has_const_metasized_pred = false;
1012+
let mut has_pointeesized_pred = false;
10111013

10121014
for (predicate, _) in bounds.iter_instantiated_copied(tcx, args) {
10131015
let bound_predicate = predicate.kind();
@@ -1019,17 +1021,17 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
10191021
if tcx.is_lang_item(pred.def_id(), LangItem::Sized) {
10201022
match pred.polarity {
10211023
ty::PredicatePolarity::Positive => {
1022-
has_sized_bound = true;
1024+
has_sized_pred = true;
10231025
continue;
10241026
}
1025-
ty::PredicatePolarity::Negative => has_negative_sized_bound = true,
1027+
ty::PredicatePolarity::Negative => has_negative_sized_pred = true,
10261028
}
10271029
} else if tcx.is_lang_item(pred.def_id(), LangItem::MetaSized) {
1028-
has_metasized_bound = true;
1030+
has_metasized_pred = true;
10291031
continue;
10301032
} else if tcx.is_lang_item(pred.def_id(), LangItem::PointeeSized) {
10311033
// Unexpected - `PointeeSized` is the absence of bounds.
1032-
has_pointeesized_bound = true;
1034+
has_pointeesized_pred = true;
10331035
continue;
10341036
}
10351037

@@ -1057,6 +1059,13 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
10571059
ty::ClauseKind::TypeOutlives(outlives) => {
10581060
lifetimes.push(outlives.1);
10591061
}
1062+
ty::ClauseKind::HostEffect(pred) => {
1063+
if tcx.is_lang_item(pred.def_id(), LangItem::Sized) {
1064+
has_const_sized_pred = true;
1065+
} else if tcx.is_lang_item(pred.def_id(), LangItem::MetaSized) {
1066+
has_const_metasized_pred = true;
1067+
}
1068+
}
10601069
_ => {}
10611070
}
10621071
}
@@ -1065,7 +1074,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
10651074

10661075
let mut first = true;
10671076
// Insert parenthesis around (Fn(A, B) -> C) if the opaque ty has more than one other trait
1068-
let paren_needed = fn_traits.len() > 1 || traits.len() > 0 || !has_sized_bound;
1077+
let paren_needed = fn_traits.len() > 1 || traits.len() > 0 || !has_sized_pred;
10691078

10701079
for ((bound_args_and_self_ty, is_async), entry) in fn_traits {
10711080
write!(self, "{}", if first { "" } else { " + " })?;
@@ -1196,24 +1205,31 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
11961205
}
11971206

11981207
let using_sized_hierarchy = self.tcx().features().sized_hierarchy();
1199-
let add_sized = has_sized_bound && (first || has_negative_sized_bound);
1200-
let add_maybe_sized = has_metasized_bound && !has_negative_sized_bound && !using_sized_hierarchy;
1208+
let add_sized = has_sized_pred && (first || has_negative_sized_pred);
1209+
let add_maybe_sized =
1210+
has_metasized_pred && !has_negative_sized_pred && !using_sized_hierarchy;
12011211
// Set `has_pointeesized_bound` if there were no `Sized` or `MetaSized` bounds.
1202-
has_pointeesized_bound = has_pointeesized_bound || (!has_sized_bound && !has_metasized_bound && !has_negative_sized_bound);
1212+
has_pointeesized_pred = has_pointeesized_pred
1213+
|| (!has_sized_pred && !has_metasized_pred && !has_negative_sized_pred);
12031214
if add_sized || add_maybe_sized {
12041215
if !first {
12051216
write!(self, " + ")?;
12061217
}
12071218
if add_maybe_sized {
12081219
write!(self, "?")?;
1220+
} else if has_const_sized_pred && using_sized_hierarchy {
1221+
write!(self, "const ")?;
12091222
}
12101223
write!(self, "Sized")?;
1211-
} else if has_metasized_bound && using_sized_hierarchy {
1224+
} else if has_metasized_pred && using_sized_hierarchy {
12121225
if !first {
12131226
write!(self, " + ")?;
12141227
}
1228+
if has_const_metasized_pred && using_sized_hierarchy {
1229+
write!(self, "const ")?;
1230+
}
12151231
write!(self, "MetaSized")?;
1216-
} else if has_pointeesized_bound && using_sized_hierarchy {
1232+
} else if has_pointeesized_pred && using_sized_hierarchy {
12171233
if !first {
12181234
write!(self, " + ")?;
12191235
}

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ compile-flags: --crate-type=lib
2-
#![feature(sized_hierarchy)]
2+
#![feature(const_trait_impl, sized_hierarchy)]
33

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

@@ -14,11 +14,19 @@ pub fn sized() -> Box<impl Tr + Sized> {
1414
Box::new(1u32)
1515
}
1616

17+
pub fn const_sized() -> Box<impl Tr + const Sized> {
18+
if true {
19+
let x = const_sized();
20+
let y: Box<dyn Tr> = x;
21+
}
22+
Box::new(1u32)
23+
}
24+
1725
pub fn neg_sized() -> Box<impl Tr + ?Sized> {
1826
if true {
1927
let x = neg_sized();
2028
let y: Box<dyn Tr> = x;
21-
//~^ ERROR: the size for values of type `impl Tr + MetaSized` cannot be known
29+
//~^ ERROR: the size for values of type `impl Tr + const MetaSized` cannot be known
2230
}
2331
Box::new(1u32)
2432
}
@@ -32,6 +40,15 @@ pub fn metasized() -> Box<impl Tr + MetaSized> {
3240
Box::new(1u32)
3341
}
3442

43+
pub fn const_metasized() -> Box<impl Tr + const MetaSized> {
44+
if true {
45+
let x = const_metasized();
46+
let y: Box<dyn Tr> = x;
47+
//~^ ERROR: the size for values of type `impl Tr + const MetaSized` cannot be known
48+
}
49+
Box::new(1u32)
50+
}
51+
3552
pub fn pointeesized() -> Box<impl Tr + PointeeSized> {
3653
//~^ ERROR: the size for values of type `impl Tr + PointeeSized` cannot be known
3754
if true {
@@ -42,4 +59,3 @@ pub fn pointeesized() -> Box<impl Tr + PointeeSized> {
4259
}
4360
Box::new(1u32)
4461
}
45-
Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0277]: the size for values of type `impl Tr + PointeeSized` cannot be known
2-
--> $DIR/pretty-print-opaque.rs:35:26
2+
--> $DIR/pretty-print-opaque.rs:52:26
33
|
44
LL | pub fn pointeesized() -> Box<impl Tr + PointeeSized> {
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a known size
@@ -8,26 +8,35 @@ LL | pub fn pointeesized() -> Box<impl Tr + PointeeSized> {
88
note: required by a bound in `Box`
99
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL
1010

11-
error[E0277]: the size for values of type `impl Tr + MetaSized` cannot be known at compilation time
12-
--> $DIR/pretty-print-opaque.rs:20:30
11+
error[E0277]: the size for values of type `impl Tr + const MetaSized` cannot be known at compilation time
12+
--> $DIR/pretty-print-opaque.rs:28:30
1313
|
1414
LL | let y: Box<dyn Tr> = x;
1515
| ^ doesn't have a size known at compile-time
1616
|
17-
= help: the trait `Sized` is not implemented for `impl Tr + MetaSized`
18-
= note: required for the cast from `Box<impl Tr + MetaSized>` to `Box<dyn Tr>`
17+
= help: the trait `Sized` is not implemented for `impl Tr + const MetaSized`
18+
= note: required for the cast from `Box<impl Tr + const MetaSized>` to `Box<dyn Tr>`
1919

2020
error[E0277]: the size for values of type `impl Tr + MetaSized` cannot be known at compilation time
21-
--> $DIR/pretty-print-opaque.rs:29:30
21+
--> $DIR/pretty-print-opaque.rs:37:30
2222
|
2323
LL | let y: Box<dyn Tr> = x;
2424
| ^ doesn't have a size known at compile-time
2525
|
2626
= help: the trait `Sized` is not implemented for `impl Tr + MetaSized`
2727
= note: required for the cast from `Box<impl Tr + MetaSized>` to `Box<dyn Tr>`
2828

29+
error[E0277]: the size for values of type `impl Tr + const MetaSized` cannot be known at compilation time
30+
--> $DIR/pretty-print-opaque.rs:46:30
31+
|
32+
LL | let y: Box<dyn Tr> = x;
33+
| ^ doesn't have a size known at compile-time
34+
|
35+
= help: the trait `Sized` is not implemented for `impl Tr + const MetaSized`
36+
= note: required for the cast from `Box<impl Tr + const MetaSized>` to `Box<dyn Tr>`
37+
2938
error[E0277]: the size for values of type `impl Tr + PointeeSized` cannot be known
30-
--> $DIR/pretty-print-opaque.rs:38:17
39+
--> $DIR/pretty-print-opaque.rs:55:17
3140
|
3241
LL | let x = pointeesized();
3342
| ^^^^^^^^^^^^^^ doesn't have a known size
@@ -37,14 +46,14 @@ note: required by a bound in `Box`
3746
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL
3847

3948
error[E0277]: the size for values of type `impl Tr + PointeeSized` cannot be known at compilation time
40-
--> $DIR/pretty-print-opaque.rs:40:30
49+
--> $DIR/pretty-print-opaque.rs:57:30
4150
|
4251
LL | let y: Box<dyn Tr> = x;
4352
| ^ doesn't have a size known at compile-time
4453
|
4554
= help: the trait `Sized` is not implemented for `impl Tr + PointeeSized`
4655
= note: required for the cast from `Box<impl Tr + PointeeSized>` to `Box<dyn Tr>`
4756

48-
error: aborting due to 5 previous errors
57+
error: aborting due to 6 previous errors
4958

5059
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)