Skip to content

Commit d59e5c6

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 13273e0 commit d59e5c6

File tree

3 files changed

+66
-27
lines changed

3 files changed

+66
-27
lines changed

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

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,10 +1064,12 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
10641064
let mut fn_traits = FxIndexMap::default();
10651065
let mut lifetimes = SmallVec::<[ty::Region<'tcx>; 1]>::new();
10661066

1067-
let mut has_sized_bound = false;
1068-
let mut has_negative_sized_bound = false;
1069-
let mut has_metasized_bound = false;
1070-
let mut has_pointeesized_bound = false;
1067+
let mut has_sized_pred = false;
1068+
let mut has_const_sized_pred = false;
1069+
let mut has_negative_sized_pred = false;
1070+
let mut has_metasized_pred = false;
1071+
let mut has_const_metasized_pred = false;
1072+
let mut has_pointeesized_pred = false;
10711073

10721074
for (predicate, _) in bounds.iter_instantiated_copied(tcx, args) {
10731075
let bound_predicate = predicate.kind();
@@ -1079,17 +1081,17 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
10791081
if tcx.is_lang_item(pred.def_id(), LangItem::Sized) {
10801082
match pred.polarity {
10811083
ty::PredicatePolarity::Positive => {
1082-
has_sized_bound = true;
1084+
has_sized_pred = true;
10831085
continue;
10841086
}
1085-
ty::PredicatePolarity::Negative => has_negative_sized_bound = true,
1087+
ty::PredicatePolarity::Negative => has_negative_sized_pred = true,
10861088
}
10871089
} else if tcx.is_lang_item(pred.def_id(), LangItem::MetaSized) {
1088-
has_metasized_bound = true;
1090+
has_metasized_pred = true;
10891091
continue;
10901092
} else if tcx.is_lang_item(pred.def_id(), LangItem::PointeeSized) {
10911093
// Unexpected - `PointeeSized` is the absence of bounds.
1092-
has_pointeesized_bound = true;
1094+
has_pointeesized_pred = true;
10931095
continue;
10941096
}
10951097

@@ -1117,6 +1119,13 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
11171119
ty::ClauseKind::TypeOutlives(outlives) => {
11181120
lifetimes.push(outlives.1);
11191121
}
1122+
ty::ClauseKind::HostEffect(pred) => {
1123+
if tcx.is_lang_item(pred.def_id(), LangItem::Sized) {
1124+
has_const_sized_pred = true;
1125+
} else if tcx.is_lang_item(pred.def_id(), LangItem::MetaSized) {
1126+
has_const_metasized_pred = true;
1127+
}
1128+
}
11201129
_ => {}
11211130
}
11221131
}
@@ -1125,7 +1134,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
11251134

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

11301139
for ((bound_args_and_self_ty, is_async), entry) in fn_traits {
11311140
write!(self, "{}", if first { "" } else { " + " })?;
@@ -1260,26 +1269,31 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
12601269
}
12611270

12621271
let using_sized_hierarchy = self.tcx().features().sized_hierarchy();
1263-
let add_sized = has_sized_bound && (first || has_negative_sized_bound);
1272+
let add_sized = has_sized_pred && (first || has_negative_sized_pred);
12641273
let add_maybe_sized =
1265-
has_metasized_bound && !has_negative_sized_bound && !using_sized_hierarchy;
1274+
has_metasized_pred && !has_negative_sized_pred && !using_sized_hierarchy;
12661275
// Set `has_pointeesized_bound` if there were no `Sized` or `MetaSized` bounds.
1267-
has_pointeesized_bound = has_pointeesized_bound
1268-
|| (!has_sized_bound && !has_metasized_bound && !has_negative_sized_bound);
1276+
has_pointeesized_pred = has_pointeesized_pred
1277+
|| (!has_sized_pred && !has_metasized_pred && !has_negative_sized_pred);
12691278
if add_sized || add_maybe_sized {
12701279
if !first {
12711280
write!(self, " + ")?;
12721281
}
12731282
if add_maybe_sized {
12741283
write!(self, "?")?;
1284+
} else if has_const_sized_pred && using_sized_hierarchy {
1285+
write!(self, "const ")?;
12751286
}
12761287
write!(self, "Sized")?;
1277-
} else if has_metasized_bound && using_sized_hierarchy {
1288+
} else if has_metasized_pred && using_sized_hierarchy {
12781289
if !first {
12791290
write!(self, " + ")?;
12801291
}
1292+
if has_const_metasized_pred && using_sized_hierarchy {
1293+
write!(self, "const ")?;
1294+
}
12811295
write!(self, "MetaSized")?;
1282-
} else if has_pointeesized_bound && using_sized_hierarchy {
1296+
} else if has_pointeesized_pred && using_sized_hierarchy {
12831297
if !first {
12841298
write!(self, " + ")?;
12851299
}

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)