Skip to content

Commit ce71f8a

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 386d673 commit ce71f8a

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
@@ -1020,10 +1020,12 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
10201020
let mut fn_traits = FxIndexMap::default();
10211021
let mut lifetimes = SmallVec::<[ty::Region<'tcx>; 1]>::new();
10221022

1023-
let mut has_sized_bound = false;
1024-
let mut has_negative_sized_bound = false;
1025-
let mut has_metasized_bound = false;
1026-
let mut has_pointeesized_bound = false;
1023+
let mut has_sized_pred = false;
1024+
let mut has_const_sized_pred = false;
1025+
let mut has_negative_sized_pred = false;
1026+
let mut has_metasized_pred = false;
1027+
let mut has_const_metasized_pred = false;
1028+
let mut has_pointeesized_pred = false;
10271029

10281030
for (predicate, _) in bounds.iter_instantiated_copied(tcx, args) {
10291031
let bound_predicate = predicate.kind();
@@ -1035,17 +1037,17 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
10351037
if tcx.is_lang_item(pred.def_id(), LangItem::Sized) {
10361038
match pred.polarity {
10371039
ty::PredicatePolarity::Positive => {
1038-
has_sized_bound = true;
1040+
has_sized_pred = true;
10391041
continue;
10401042
}
1041-
ty::PredicatePolarity::Negative => has_negative_sized_bound = true,
1043+
ty::PredicatePolarity::Negative => has_negative_sized_pred = true,
10421044
}
10431045
} else if tcx.is_lang_item(pred.def_id(), LangItem::MetaSized) {
1044-
has_metasized_bound = true;
1046+
has_metasized_pred = true;
10451047
continue;
10461048
} else if tcx.is_lang_item(pred.def_id(), LangItem::PointeeSized) {
10471049
// Unexpected - `PointeeSized` is the absence of bounds.
1048-
has_pointeesized_bound = true;
1050+
has_pointeesized_pred = true;
10491051
continue;
10501052
}
10511053

@@ -1073,6 +1075,13 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
10731075
ty::ClauseKind::TypeOutlives(outlives) => {
10741076
lifetimes.push(outlives.1);
10751077
}
1078+
ty::ClauseKind::HostEffect(pred) => {
1079+
if tcx.is_lang_item(pred.def_id(), LangItem::Sized) {
1080+
has_const_sized_pred = true;
1081+
} else if tcx.is_lang_item(pred.def_id(), LangItem::MetaSized) {
1082+
has_const_metasized_pred = true;
1083+
}
1084+
}
10761085
_ => {}
10771086
}
10781087
}
@@ -1081,7 +1090,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
10811090

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

10861095
for ((bound_args_and_self_ty, is_async), entry) in fn_traits {
10871096
write!(self, "{}", if first { "" } else { " + " })?;
@@ -1216,24 +1225,31 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
12161225
}
12171226

12181227
let using_sized_hierarchy = self.tcx().features().sized_hierarchy();
1219-
let add_sized = has_sized_bound && (first || has_negative_sized_bound);
1220-
let add_maybe_sized = has_metasized_bound && !has_negative_sized_bound && !using_sized_hierarchy;
1228+
let add_sized = has_sized_pred && (first || has_negative_sized_pred);
1229+
let add_maybe_sized =
1230+
has_metasized_pred && !has_negative_sized_pred && !using_sized_hierarchy;
12211231
// Set `has_pointeesized_bound` if there were no `Sized` or `MetaSized` bounds.
1222-
has_pointeesized_bound = has_pointeesized_bound || (!has_sized_bound && !has_metasized_bound && !has_negative_sized_bound);
1232+
has_pointeesized_pred = has_pointeesized_pred
1233+
|| (!has_sized_pred && !has_metasized_pred && !has_negative_sized_pred);
12231234
if add_sized || add_maybe_sized {
12241235
if !first {
12251236
write!(self, " + ")?;
12261237
}
12271238
if add_maybe_sized {
12281239
write!(self, "?")?;
1240+
} else if has_const_sized_pred && using_sized_hierarchy {
1241+
write!(self, "const ")?;
12291242
}
12301243
write!(self, "Sized")?;
1231-
} else if has_metasized_bound && using_sized_hierarchy {
1244+
} else if has_metasized_pred && using_sized_hierarchy {
12321245
if !first {
12331246
write!(self, " + ")?;
12341247
}
1248+
if has_const_metasized_pred && using_sized_hierarchy {
1249+
write!(self, "const ")?;
1250+
}
12351251
write!(self, "MetaSized")?;
1236-
} else if has_pointeesized_bound && using_sized_hierarchy {
1252+
} else if has_pointeesized_pred && using_sized_hierarchy {
12371253
if !first {
12381254
write!(self, " + ")?;
12391255
}

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)