Skip to content

Commit 9e46c24

Browse files
authored
Rollup merge of #106873 - BoxyUwU:ty_const_formatting, r=compiler-errors
dont randomly use `_` to print out const generic arguments const generics seem to get printed out as `_` for no reason a lot of the time, as someone who spends a lot of time with const generics this has gotten ✨ very annoying ✨. Latest example would be #106423 where the ICE messaged formatted a `ty::Const` containing no infer vars, as `_`. For some reason printing of the const argument on arrays was custom instead of using the existing logic for printing `ty::Const`. Additionally the existing logic for printing `ty::Const` would print out `_` for anon consts that are in a separate crate leading to weird diagnostics (see second commit). There ought to be less cases of consts randomly getting printed as `_` hiding valuable info now.
2 parents d26e07b + 1171fe5 commit 9e46c24

39 files changed

+112
-80
lines changed

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

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -854,24 +854,7 @@ pub trait PrettyPrinter<'tcx>:
854854
}
855855
p!("]");
856856
}
857-
ty::Array(ty, sz) => {
858-
p!("[", print(ty), "; ");
859-
if self.should_print_verbose() {
860-
p!(write("{:?}", sz));
861-
} else if let ty::ConstKind::Unevaluated(..) = sz.kind() {
862-
// Do not try to evaluate unevaluated constants. If we are const evaluating an
863-
// array length anon const, rustc will (with debug assertions) print the
864-
// constant's path. Which will end up here again.
865-
p!("_");
866-
} else if let Some(n) = sz.kind().try_to_bits(self.tcx().data_layout.pointer_size) {
867-
p!(write("{}", n));
868-
} else if let ty::ConstKind::Param(param) = sz.kind() {
869-
p!(print(param));
870-
} else {
871-
p!("_");
872-
}
873-
p!("]")
874-
}
857+
ty::Array(ty, sz) => p!("[", print(ty), "; ", print(sz), "]"),
875858
ty::Slice(ty) => p!("[", print(ty), "]"),
876859
}
877860

@@ -1303,21 +1286,25 @@ pub trait PrettyPrinter<'tcx>:
13031286
match ct.kind() {
13041287
ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, substs }) => {
13051288
match self.tcx().def_kind(def.did) {
1306-
DefKind::Static(..) | DefKind::Const | DefKind::AssocConst => {
1289+
DefKind::Const | DefKind::AssocConst => {
13071290
p!(print_value_path(def.did, substs))
13081291
}
1309-
_ => {
1310-
if def.is_local() {
1311-
let span = self.tcx().def_span(def.did);
1312-
if let Ok(snip) = self.tcx().sess.source_map().span_to_snippet(span) {
1313-
p!(write("{}", snip))
1314-
} else {
1315-
print_underscore!()
1316-
}
1292+
DefKind::AnonConst => {
1293+
if def.is_local()
1294+
&& let span = self.tcx().def_span(def.did)
1295+
&& let Ok(snip) = self.tcx().sess.source_map().span_to_snippet(span)
1296+
{
1297+
p!(write("{}", snip))
13171298
} else {
1318-
print_underscore!()
1299+
// Do not call `print_value_path` as if a parent of this anon const is an impl it will
1300+
// attempt to print out the impl trait ref i.e. `<T as Trait>::{constant#0}`. This would
1301+
// cause printing to enter an infinite recursion if the anon const is in the self type i.e.
1302+
// `impl<T: Default> Default for [T; 32 - 1 - 1 - 1] {`
1303+
// where we would try to print `<[T; /* print `constant#0` again */] as Default>::{constant#0}`
1304+
p!(write("{}::{}", self.tcx().crate_name(def.did.krate), self.tcx().def_path(def.did).to_string_no_crate_verbose()))
13191305
}
13201306
}
1307+
defkind => bug!("`{:?}` has unexpcted defkind {:?}", ct, defkind),
13211308
}
13221309
}
13231310
ty::ConstKind::Infer(infer_ct) => {
@@ -1339,7 +1326,7 @@ pub trait PrettyPrinter<'tcx>:
13391326
ty::ConstKind::Placeholder(placeholder) => p!(write("Placeholder({:?})", placeholder)),
13401327
// FIXME(generic_const_exprs):
13411328
// write out some legible representation of an abstract const?
1342-
ty::ConstKind::Expr(_) => p!("[Const Expr]"),
1329+
ty::ConstKind::Expr(_) => p!("[const expr]"),
13431330
ty::ConstKind::Error(_) => p!("[const error]"),
13441331
};
13451332
Ok(self)

tests/mir-opt/nll/region_subtyping_basic.main.nll.0.32bit.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
|
2323
fn main() -> () {
2424
let mut _0: (); // return place in scope 0 at $DIR/region_subtyping_basic.rs:+0:11: +0:11
25-
let mut _1: [usize; Const { ty: usize, kind: Value(Leaf(0x00000003)) }]; // in scope 0 at $DIR/region_subtyping_basic.rs:+1:9: +1:14
25+
let mut _1: [usize; Const(Value(Leaf(0x00000003)): usize)]; // in scope 0 at $DIR/region_subtyping_basic.rs:+1:9: +1:14
2626
let _3: usize; // in scope 0 at $DIR/region_subtyping_basic.rs:+2:16: +2:17
2727
let mut _4: usize; // in scope 0 at $DIR/region_subtyping_basic.rs:+2:14: +2:18
2828
let mut _5: bool; // in scope 0 at $DIR/region_subtyping_basic.rs:+2:14: +2:18

tests/mir-opt/nll/region_subtyping_basic.main.nll.0.64bit.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
|
2323
fn main() -> () {
2424
let mut _0: (); // return place in scope 0 at $DIR/region_subtyping_basic.rs:+0:11: +0:11
25-
let mut _1: [usize; Const { ty: usize, kind: Value(Leaf(0x0000000000000003)) }]; // in scope 0 at $DIR/region_subtyping_basic.rs:+1:9: +1:14
25+
let mut _1: [usize; Const(Value(Leaf(0x0000000000000003)): usize)]; // in scope 0 at $DIR/region_subtyping_basic.rs:+1:9: +1:14
2626
let _3: usize; // in scope 0 at $DIR/region_subtyping_basic.rs:+2:16: +2:17
2727
let mut _4: usize; // in scope 0 at $DIR/region_subtyping_basic.rs:+2:14: +2:18
2828
let mut _5: bool; // in scope 0 at $DIR/region_subtyping_basic.rs:+2:14: +2:18

tests/ui/const-generics/const-param-type-depends-on-const-param.min.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error[E0770]: the type of const parameters must not depend on other generic para
1010
LL | pub struct SelfDependent<const N: [u8; N]>;
1111
| ^ the type must not depend on the parameter `N`
1212

13-
error: `[u8; _]` is forbidden as the type of a const generic parameter
13+
error: `[u8; N]` is forbidden as the type of a const generic parameter
1414
--> $DIR/const-param-type-depends-on-const-param.rs:11:47
1515
|
1616
LL | pub struct Dependent<const N: usize, const X: [u8; N]>([(); N]);
@@ -19,7 +19,7 @@ LL | pub struct Dependent<const N: usize, const X: [u8; N]>([(); N]);
1919
= note: the only supported types are integers, `bool` and `char`
2020
= help: more complex types are supported with `#![feature(adt_const_params)]`
2121

22-
error: `[u8; _]` is forbidden as the type of a const generic parameter
22+
error: `[u8; N]` is forbidden as the type of a const generic parameter
2323
--> $DIR/const-param-type-depends-on-const-param.rs:15:35
2424
|
2525
LL | pub struct SelfDependent<const N: [u8; N]>;

tests/ui/const-generics/const-param-type-depends-on-const-param.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
pub struct Dependent<const N: usize, const X: [u8; N]>([(); N]);
1212
//~^ ERROR: the type of const parameters must not depend on other generic parameters
13-
//[min]~^^ ERROR `[u8; _]` is forbidden
13+
//[min]~^^ ERROR `[u8; N]` is forbidden
1414

1515
pub struct SelfDependent<const N: [u8; N]>;
1616
//~^ ERROR: the type of const parameters must not depend on other generic parameters
17-
//[min]~^^ ERROR `[u8; _]` is forbidden
17+
//[min]~^^ ERROR `[u8; N]` is forbidden
1818

1919
fn main() {}

tests/ui/const-generics/dont-evaluate-array-len-on-err-1.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0277]: the trait bound `[Adt; _]: Foo` is not satisfied
1+
error[E0277]: the trait bound `[Adt; std::mem::size_of::<Self::Assoc>()]: Foo` is not satisfied
22
--> $DIR/dont-evaluate-array-len-on-err-1.rs:15:9
33
|
44
LL | <[Adt; std::mem::size_of::<Self::Assoc>()] as Foo>::bar()
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `[Adt; _]`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `[Adt; std::mem::size_of::<Self::Assoc>()]`
66

77
error: aborting due to previous error
88

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![feature(generic_const_exprs)]
2+
#![allow(incomplete_features)]
3+
4+
pub struct Foo<const N: usize>;
5+
6+
pub fn foo<const N: usize>() -> Foo<{ N + 1 }> {
7+
Foo
8+
}

tests/ui/const-generics/generic_const_exprs/issue-62504.min.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ LL | ArrayHolder([0; Self::SIZE])
1515
| arguments to this struct are incorrect
1616
|
1717
= note: expected array `[u32; X]`
18-
found array `[u32; _]`
18+
found array `[u32; Self::SIZE]`
1919
note: tuple struct defined here
2020
--> $DIR/issue-62504.rs:14:8
2121
|

tests/ui/const-generics/generic_const_exprs/issue-79518-default_trait_method_normalization.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ error[E0308]: mismatched types
22
--> $DIR/issue-79518-default_trait_method_normalization.rs:16:32
33
|
44
LL | Self::AssocInstance == [(); std::mem::size_of::<Self::Assoc>()];
5-
| ------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected associated type, found array `[(); _]`
5+
| ------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected associated type, found array `[(); std::mem::size_of::<Self::Assoc>()]`
66
| |
77
| expected because this is `<Self as Foo>::Assoc`
88
|
99
= note: expected associated type `<Self as Foo>::Assoc`
10-
found array `[(); _]`
11-
= help: consider constraining the associated type `<Self as Foo>::Assoc` to `[(); _]` or calling a method that returns `<Self as Foo>::Assoc`
10+
found array `[(); std::mem::size_of::<Self::Assoc>()]`
11+
= help: consider constraining the associated type `<Self as Foo>::Assoc` to `[(); std::mem::size_of::<Self::Assoc>()]` or calling a method that returns `<Self as Foo>::Assoc`
1212
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
1313

1414
error: aborting due to previous error
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// aux-build:anon_const_non_local.rs
2+
3+
#![feature(generic_const_exprs)]
4+
#![allow(incomplete_features)]
5+
6+
extern crate anon_const_non_local;
7+
8+
fn bar<const M: usize>()
9+
where
10+
[(); M + 1]:,
11+
{
12+
let _: anon_const_non_local::Foo<2> = anon_const_non_local::foo::<M>();
13+
//~^ ERROR: mismatched types
14+
}
15+
16+
fn main() {}

0 commit comments

Comments
 (0)