Skip to content

Commit 5350b0d

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 11fe79c commit 5350b0d

File tree

3 files changed

+67
-28
lines changed

3 files changed

+67
-28
lines changed

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

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

1074-
let mut has_sized_bound = false;
1075-
let mut has_negative_sized_bound = false;
1076-
let mut has_meta_sized_bound = false;
1077-
let mut has_pointee_sized_bound = false;
1074+
let mut has_sized_pred = false;
1075+
let mut has_const_sized_pred = false;
1076+
let mut has_negative_sized_pred = false;
1077+
let mut has_meta_sized_pred = false;
1078+
let mut has_const_meta_sized_pred = false;
1079+
let mut has_pointee_sized_pred = false;
10781080

10791081
for (predicate, _) in bounds.iter_instantiated_copied(tcx, args) {
10801082
let bound_predicate = predicate.kind();
@@ -1086,17 +1088,17 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
10861088
if tcx.is_lang_item(pred.def_id(), LangItem::Sized) {
10871089
match pred.polarity {
10881090
ty::PredicatePolarity::Positive => {
1089-
has_sized_bound = true;
1091+
has_sized_pred = true;
10901092
continue;
10911093
}
1092-
ty::PredicatePolarity::Negative => has_negative_sized_bound = true,
1094+
ty::PredicatePolarity::Negative => has_negative_sized_pred = true,
10931095
}
10941096
} else if tcx.is_lang_item(pred.def_id(), LangItem::MetaSized) {
1095-
has_meta_sized_bound = true;
1097+
has_meta_sized_pred = true;
10961098
continue;
10971099
} else if tcx.is_lang_item(pred.def_id(), LangItem::PointeeSized) {
10981100
// Unexpected - `PointeeSized` is the absence of bounds.
1099-
has_pointee_sized_bound = true;
1101+
has_pointee_sized_pred = true;
11001102
continue;
11011103
}
11021104

@@ -1124,6 +1126,13 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
11241126
ty::ClauseKind::TypeOutlives(outlives) => {
11251127
lifetimes.push(outlives.1);
11261128
}
1129+
ty::ClauseKind::HostEffect(pred) => {
1130+
if tcx.is_lang_item(pred.def_id(), LangItem::Sized) {
1131+
has_const_sized_pred = true;
1132+
} else if tcx.is_lang_item(pred.def_id(), LangItem::MetaSized) {
1133+
has_const_meta_sized_pred = true;
1134+
}
1135+
}
11271136
_ => {}
11281137
}
11291138
}
@@ -1132,7 +1141,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
11321141

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

11371146
for ((bound_args_and_self_ty, is_async), entry) in fn_traits {
11381147
write!(self, "{}", if first { "" } else { " + " })?;
@@ -1267,26 +1276,31 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
12671276
}
12681277

12691278
let using_sized_hierarchy = self.tcx().features().sized_hierarchy();
1270-
let add_sized = has_sized_bound && (first || has_negative_sized_bound);
1279+
let add_sized = has_sized_pred && (first || has_negative_sized_pred);
12711280
let add_maybe_sized =
1272-
has_meta_sized_bound && !has_negative_sized_bound && !using_sized_hierarchy;
1281+
has_meta_sized_pred && !has_negative_sized_pred && !using_sized_hierarchy;
12731282
// Set `has_pointee_sized_bound` if there were no `Sized` or `MetaSized` bounds.
1274-
has_pointee_sized_bound = has_pointee_sized_bound
1275-
|| (!has_sized_bound && !has_meta_sized_bound && !has_negative_sized_bound);
1283+
has_pointee_sized_pred = has_pointee_sized_pred
1284+
|| (!has_sized_pred && !has_meta_sized_pred && !has_negative_sized_pred);
12761285
if add_sized || add_maybe_sized {
12771286
if !first {
12781287
write!(self, " + ")?;
12791288
}
12801289
if add_maybe_sized {
12811290
write!(self, "?")?;
1291+
} else if has_const_sized_pred && using_sized_hierarchy {
1292+
write!(self, "const ")?;
12821293
}
12831294
write!(self, "Sized")?;
1284-
} else if has_meta_sized_bound && using_sized_hierarchy {
1295+
} else if has_meta_sized_pred && using_sized_hierarchy {
12851296
if !first {
12861297
write!(self, " + ")?;
12871298
}
1299+
if has_const_meta_sized_pred && using_sized_hierarchy {
1300+
write!(self, "const ")?;
1301+
}
12881302
write!(self, "MetaSized")?;
1289-
} else if has_pointee_sized_bound && using_sized_hierarchy {
1303+
} else if has_pointee_sized_pred && using_sized_hierarchy {
12901304
if !first {
12911305
write!(self, " + ")?;
12921306
}

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 {
@@ -43,4 +60,3 @@ pub fn pointeesized() -> Box<impl Tr + PointeeSized> {
4360
}
4461
Box::new(1u32)
4562
}
46-
Lines changed: 19 additions & 10 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,7 +46,7 @@ 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
@@ -46,14 +55,14 @@ LL | let y: Box<dyn Tr> = x;
4655
= note: required for the cast from `Box<impl Tr + PointeeSized>` to `Box<dyn Tr>`
4756

4857
error[E0277]: the size for values of type `impl Tr + PointeeSized` cannot be known
49-
--> $DIR/pretty-print-opaque.rs:40:30
58+
--> $DIR/pretty-print-opaque.rs:57:30
5059
|
5160
LL | let y: Box<dyn Tr> = x;
5261
| ^ doesn't have a known size
5362
|
5463
= help: the trait `MetaSized` is not implemented for `impl Tr + PointeeSized`
5564
= note: required for the cast from `Box<impl Tr + PointeeSized>` to `Box<dyn Tr>`
5665

57-
error: aborting due to 6 previous errors
66+
error: aborting due to 7 previous errors
5867

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

0 commit comments

Comments
 (0)