Skip to content

Commit

Permalink
Rollup merge of #106873 - BoxyUwU:ty_const_formatting, r=compiler-errors
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
matthiaskrgr committed Jan 18, 2023
2 parents d26e07b + 1171fe5 commit 9e46c24
Show file tree
Hide file tree
Showing 39 changed files with 112 additions and 80 deletions.
45 changes: 16 additions & 29 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,24 +854,7 @@ pub trait PrettyPrinter<'tcx>:
}
p!("]");
}
ty::Array(ty, sz) => {
p!("[", print(ty), "; ");
if self.should_print_verbose() {
p!(write("{:?}", sz));
} else if let ty::ConstKind::Unevaluated(..) = sz.kind() {
// Do not try to evaluate unevaluated constants. If we are const evaluating an
// array length anon const, rustc will (with debug assertions) print the
// constant's path. Which will end up here again.
p!("_");
} else if let Some(n) = sz.kind().try_to_bits(self.tcx().data_layout.pointer_size) {
p!(write("{}", n));
} else if let ty::ConstKind::Param(param) = sz.kind() {
p!(print(param));
} else {
p!("_");
}
p!("]")
}
ty::Array(ty, sz) => p!("[", print(ty), "; ", print(sz), "]"),
ty::Slice(ty) => p!("[", print(ty), "]"),
}

Expand Down Expand Up @@ -1303,21 +1286,25 @@ pub trait PrettyPrinter<'tcx>:
match ct.kind() {
ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, substs }) => {
match self.tcx().def_kind(def.did) {
DefKind::Static(..) | DefKind::Const | DefKind::AssocConst => {
DefKind::Const | DefKind::AssocConst => {
p!(print_value_path(def.did, substs))
}
_ => {
if def.is_local() {
let span = self.tcx().def_span(def.did);
if let Ok(snip) = self.tcx().sess.source_map().span_to_snippet(span) {
p!(write("{}", snip))
} else {
print_underscore!()
}
DefKind::AnonConst => {
if def.is_local()
&& let span = self.tcx().def_span(def.did)
&& let Ok(snip) = self.tcx().sess.source_map().span_to_snippet(span)
{
p!(write("{}", snip))
} else {
print_underscore!()
// Do not call `print_value_path` as if a parent of this anon const is an impl it will
// attempt to print out the impl trait ref i.e. `<T as Trait>::{constant#0}`. This would
// cause printing to enter an infinite recursion if the anon const is in the self type i.e.
// `impl<T: Default> Default for [T; 32 - 1 - 1 - 1] {`
// where we would try to print `<[T; /* print `constant#0` again */] as Default>::{constant#0}`
p!(write("{}::{}", self.tcx().crate_name(def.did.krate), self.tcx().def_path(def.did).to_string_no_crate_verbose()))
}
}
defkind => bug!("`{:?}` has unexpcted defkind {:?}", ct, defkind),
}
}
ty::ConstKind::Infer(infer_ct) => {
Expand All @@ -1339,7 +1326,7 @@ pub trait PrettyPrinter<'tcx>:
ty::ConstKind::Placeholder(placeholder) => p!(write("Placeholder({:?})", placeholder)),
// FIXME(generic_const_exprs):
// write out some legible representation of an abstract const?
ty::ConstKind::Expr(_) => p!("[Const Expr]"),
ty::ConstKind::Expr(_) => p!("[const expr]"),
ty::ConstKind::Error(_) => p!("[const error]"),
};
Ok(self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
|
fn main() -> () {
let mut _0: (); // return place in scope 0 at $DIR/region_subtyping_basic.rs:+0:11: +0:11
let mut _1: [usize; Const { ty: usize, kind: Value(Leaf(0x00000003)) }]; // in scope 0 at $DIR/region_subtyping_basic.rs:+1:9: +1:14
let mut _1: [usize; Const(Value(Leaf(0x00000003)): usize)]; // in scope 0 at $DIR/region_subtyping_basic.rs:+1:9: +1:14
let _3: usize; // in scope 0 at $DIR/region_subtyping_basic.rs:+2:16: +2:17
let mut _4: usize; // in scope 0 at $DIR/region_subtyping_basic.rs:+2:14: +2:18
let mut _5: bool; // in scope 0 at $DIR/region_subtyping_basic.rs:+2:14: +2:18
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
|
fn main() -> () {
let mut _0: (); // return place in scope 0 at $DIR/region_subtyping_basic.rs:+0:11: +0:11
let mut _1: [usize; Const { ty: usize, kind: Value(Leaf(0x0000000000000003)) }]; // in scope 0 at $DIR/region_subtyping_basic.rs:+1:9: +1:14
let mut _1: [usize; Const(Value(Leaf(0x0000000000000003)): usize)]; // in scope 0 at $DIR/region_subtyping_basic.rs:+1:9: +1:14
let _3: usize; // in scope 0 at $DIR/region_subtyping_basic.rs:+2:16: +2:17
let mut _4: usize; // in scope 0 at $DIR/region_subtyping_basic.rs:+2:14: +2:18
let mut _5: bool; // in scope 0 at $DIR/region_subtyping_basic.rs:+2:14: +2:18
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ error[E0770]: the type of const parameters must not depend on other generic para
LL | pub struct SelfDependent<const N: [u8; N]>;
| ^ the type must not depend on the parameter `N`

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

error: `[u8; _]` is forbidden as the type of a const generic parameter
error: `[u8; N]` is forbidden as the type of a const generic parameter
--> $DIR/const-param-type-depends-on-const-param.rs:15:35
|
LL | pub struct SelfDependent<const N: [u8; N]>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

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

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

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: the trait bound `[Adt; _]: Foo` is not satisfied
error[E0277]: the trait bound `[Adt; std::mem::size_of::<Self::Assoc>()]: Foo` is not satisfied
--> $DIR/dont-evaluate-array-len-on-err-1.rs:15:9
|
LL | <[Adt; std::mem::size_of::<Self::Assoc>()] as Foo>::bar()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `[Adt; _]`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `[Adt; std::mem::size_of::<Self::Assoc>()]`

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(generic_const_exprs)]
#![allow(incomplete_features)]

pub struct Foo<const N: usize>;

pub fn foo<const N: usize>() -> Foo<{ N + 1 }> {
Foo
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ LL | ArrayHolder([0; Self::SIZE])
| arguments to this struct are incorrect
|
= note: expected array `[u32; X]`
found array `[u32; _]`
found array `[u32; Self::SIZE]`
note: tuple struct defined here
--> $DIR/issue-62504.rs:14:8
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ error[E0308]: mismatched types
--> $DIR/issue-79518-default_trait_method_normalization.rs:16:32
|
LL | Self::AssocInstance == [(); std::mem::size_of::<Self::Assoc>()];
| ------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected associated type, found array `[(); _]`
| ------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected associated type, found array `[(); std::mem::size_of::<Self::Assoc>()]`
| |
| expected because this is `<Self as Foo>::Assoc`
|
= note: expected associated type `<Self as Foo>::Assoc`
found array `[(); _]`
= help: consider constraining the associated type `<Self as Foo>::Assoc` to `[(); _]` or calling a method that returns `<Self as Foo>::Assoc`
found array `[(); std::mem::size_of::<Self::Assoc>()]`
= 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`
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html

error: aborting due to previous error
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// aux-build:anon_const_non_local.rs

#![feature(generic_const_exprs)]
#![allow(incomplete_features)]

extern crate anon_const_non_local;

fn bar<const M: usize>()
where
[(); M + 1]:,
{
let _: anon_const_non_local::Foo<2> = anon_const_non_local::foo::<M>();
//~^ ERROR: mismatched types
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0308]: mismatched types
--> $DIR/non_local_anon_const_diagnostics.rs:12:43
|
LL | let _: anon_const_non_local::Foo<2> = anon_const_non_local::foo::<M>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `2`, found `anon_const_non_local::::foo::{constant#0}`
|
= note: expected constant `2`
found constant `anon_const_non_local::::foo::{constant#0}`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
2 changes: 1 addition & 1 deletion tests/ui/const-generics/issues/issue-62878.min.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0770]: the type of const parameters must not depend on other generic para
LL | fn foo<const N: usize, const A: [u8; N]>() {}
| ^ the type must not depend on the parameter `N`

error: `[u8; _]` is forbidden as the type of a const generic parameter
error: `[u8; N]` is forbidden as the type of a const generic parameter
--> $DIR/issue-62878.rs:5:33
|
LL | fn foo<const N: usize, const A: [u8; N]>() {}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/const-generics/issues/issue-62878.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

fn foo<const N: usize, const A: [u8; N]>() {}
//~^ ERROR the type of const parameters must not
//[min]~| ERROR `[u8; _]` is forbidden as the type of a const generic parameter
//[min]~| ERROR `[u8; N]` is forbidden as the type of a const generic parameter

fn main() {
foo::<_, { [1] }>();
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/const-generics/issues/issue-71169.min.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0770]: the type of const parameters must not depend on other generic para
LL | fn foo<const LEN: usize, const DATA: [u8; LEN]>() {}
| ^^^ the type must not depend on the parameter `LEN`

error: `[u8; _]` is forbidden as the type of a const generic parameter
error: `[u8; LEN]` is forbidden as the type of a const generic parameter
--> $DIR/issue-71169.rs:5:38
|
LL | fn foo<const LEN: usize, const DATA: [u8; LEN]>() {}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/const-generics/issues/issue-71169.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

fn foo<const LEN: usize, const DATA: [u8; LEN]>() {}
//~^ ERROR the type of const parameters must not
//[min]~^^ ERROR `[u8; _]` is forbidden as the type of a const generic parameter
//[min]~^^ ERROR `[u8; LEN]` is forbidden as the type of a const generic parameter
fn main() {
const DATA: [u8; 4] = *b"ABCD";
foo::<4, DATA>();
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/const-generics/issues/issue-73491.min.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: `[u32; _]` is forbidden as the type of a const generic parameter
error: `[u32; LEN]` is forbidden as the type of a const generic parameter
--> $DIR/issue-73491.rs:8:19
|
LL | fn hoge<const IN: [u32; LEN]>() {}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/const-generics/issues/issue-73491.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
const LEN: usize = 1024;

fn hoge<const IN: [u32; LEN]>() {}
//[min]~^ ERROR `[u32; _]` is forbidden as the type of a const generic parameter
//[min]~^ ERROR `[u32; LEN]` is forbidden as the type of a const generic parameter

fn main() {}
4 changes: 2 additions & 2 deletions tests/ui/const-generics/issues/issue-74101.min.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: `[u8; _]` is forbidden as the type of a const generic parameter
error: `[u8; 1 + 2]` is forbidden as the type of a const generic parameter
--> $DIR/issue-74101.rs:6:18
|
LL | fn test<const N: [u8; 1 + 2]>() {}
Expand All @@ -7,7 +7,7 @@ LL | fn test<const N: [u8; 1 + 2]>() {}
= note: the only supported types are integers, `bool` and `char`
= help: more complex types are supported with `#![feature(adt_const_params)]`

error: `[u8; _]` is forbidden as the type of a const generic parameter
error: `[u8; 1 + 2]` is forbidden as the type of a const generic parameter
--> $DIR/issue-74101.rs:9:21
|
LL | struct Foo<const N: [u8; 1 + 2]>;
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/const-generics/issues/issue-74101.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#![cfg_attr(full, allow(incomplete_features))]

fn test<const N: [u8; 1 + 2]>() {}
//[min]~^ ERROR `[u8; _]` is forbidden as the type of a const generic parameter
//[min]~^ ERROR `[u8; 1 + 2]` is forbidden as the type of a const generic parameter

struct Foo<const N: [u8; 1 + 2]>;
//[min]~^ ERROR `[u8; _]` is forbidden as the type of a const generic parameter
//[min]~^ ERROR `[u8; 1 + 2]` is forbidden as the type of a const generic parameter

fn main() {}
2 changes: 1 addition & 1 deletion tests/ui/const-generics/issues/issue-75047.min.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: `[u8; _]` is forbidden as the type of a const generic parameter
error: `[u8; Bar::<u32>::value()]` is forbidden as the type of a const generic parameter
--> $DIR/issue-75047.rs:14:21
|
LL | struct Foo<const N: [u8; Bar::<u32>::value()]>;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/const-generics/issues/issue-75047.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ impl<T> Bar<T> {
}

struct Foo<const N: [u8; Bar::<u32>::value()]>;
//[min]~^ ERROR `[u8; _]` is forbidden as the type of a const generic parameter
//[min]~^ ERROR `[u8; Bar::<u32>::value()]` is forbidden as the type of a const generic parameter

fn main() {}
12 changes: 11 additions & 1 deletion tests/ui/const-generics/nested-type.min.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
error: `[u8; _]` is forbidden as the type of a const generic parameter
error: `[u8; {
struct Foo<const N: usize>;

impl<const N: usize> Foo<N> {
fn value() -> usize {
N
}
}

Foo::<17>::value()
}]` is forbidden as the type of a const generic parameter
--> $DIR/nested-type.rs:6:21
|
LL | struct Foo<const N: [u8; {
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/consts/const-size_of-cycle.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ note: ...which requires const-evaluating + checking `Foo::bytes::{constant#0}`..
LL | bytes: [u8; std::mem::size_of::<Foo>()]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: ...which requires computing layout of `Foo`...
= note: ...which requires computing layout of `[u8; _]`...
= note: ...which requires normalizing `[u8; _]`...
= note: ...which requires computing layout of `[u8; std::mem::size_of::<Foo>()]`...
= note: ...which requires normalizing `[u8; std::mem::size_of::<Foo>()]`...
= note: ...which again requires evaluating type-level constant, completing the cycle
note: cycle used when checking that `Foo` is well-formed
--> $DIR/const-size_of-cycle.rs:3:1
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/consts/issue-44415.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ note: ...which requires const-evaluating + checking `Foo::bytes::{constant#0}`..
LL | bytes: [u8; unsafe { intrinsics::size_of::<Foo>() }],
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: ...which requires computing layout of `Foo`...
= note: ...which requires computing layout of `[u8; _]`...
= note: ...which requires normalizing `[u8; _]`...
= note: ...which requires computing layout of `[u8; unsafe { intrinsics::size_of::<Foo>() }]`...
= note: ...which requires normalizing `[u8; unsafe { intrinsics::size_of::<Foo>() }]`...
= note: ...which again requires evaluating type-level constant, completing the cycle
note: cycle used when checking that `Foo` is well-formed
--> $DIR/issue-44415.rs:5:1
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/consts/too_generic_eval_ice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ impl<A, B> Foo<A, B> {
[5; Self::HOST_SIZE] == [6; 0]
//~^ ERROR constant expression depends on a generic parameter
//~| ERROR constant expression depends on a generic parameter
//~| ERROR can't compare `[{integer}; _]` with `[{integer}; 0]`
//~| ERROR can't compare `[{integer}; Self::HOST_SIZE]` with `[{integer}; 0]`
}
}

Expand Down
6 changes: 3 additions & 3 deletions tests/ui/consts/too_generic_eval_ice.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ LL | [5; Self::HOST_SIZE] == [6; 0]
|
= note: this may fail depending on what value the parameter takes

error[E0277]: can't compare `[{integer}; _]` with `[{integer}; 0]`
error[E0277]: can't compare `[{integer}; Self::HOST_SIZE]` with `[{integer}; 0]`
--> $DIR/too_generic_eval_ice.rs:7:30
|
LL | [5; Self::HOST_SIZE] == [6; 0]
| ^^ no implementation for `[{integer}; _] == [{integer}; 0]`
| ^^ no implementation for `[{integer}; Self::HOST_SIZE] == [{integer}; 0]`
|
= help: the trait `PartialEq<[{integer}; 0]>` is not implemented for `[{integer}; _]`
= help: the trait `PartialEq<[{integer}; 0]>` is not implemented for `[{integer}; Self::HOST_SIZE]`
= help: the following other types implement trait `PartialEq<Rhs>`:
<&[B] as PartialEq<[A; N]>>
<&[T] as PartialEq<Vec<U, A>>>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: values of the type `[u8; SIZE]` are too big for the current architecture
error: values of the type `[u8; usize::MAX]` are too big for the current architecture

error: aborting due to previous error

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: values of the type `[u8; SIZE]` are too big for the current architecture
error: values of the type `[u8; usize::MAX]` are too big for the current architecture

error: aborting due to previous error

2 changes: 1 addition & 1 deletion tests/ui/inference/issue-83606.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ fn foo<const N: usize>(_: impl std::fmt::Display) -> [usize; N] {

fn main() {
let _ = foo("foo");
//~^ ERROR: type annotations needed for `[usize; _]`
//~^ ERROR: type annotations needed for `[usize; N]`
}
4 changes: 2 additions & 2 deletions tests/ui/inference/issue-83606.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
error[E0282]: type annotations needed for `[usize; _]`
error[E0282]: type annotations needed for `[usize; N]`
--> $DIR/issue-83606.rs:8:9
|
LL | let _ = foo("foo");
| ^
|
help: consider giving this pattern a type, where the the value of const parameter `N` is specified
|
LL | let _: [usize; _] = foo("foo");
LL | let _: [usize; N] = foo("foo");
| ++++++++++++

error: aborting due to previous error
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/limits/issue-15919-32.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: values of the type `[usize; 4294967295]` are too big for the current architecture
error: values of the type `[usize; usize::MAX]` are too big for the current architecture
--> $DIR/issue-15919-32.rs:9:9
|
LL | let x = [0usize; 0xffff_ffff];
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/limits/issue-15919-64.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: values of the type `[usize; 18446744073709551615]` are too big for the current architecture
error: values of the type `[usize; usize::MAX]` are too big for the current architecture
--> $DIR/issue-15919-64.rs:9:9
|
LL | let x = [0usize; 0xffff_ffff_ffff_ffff];
Expand Down
Loading

0 comments on commit 9e46c24

Please sign in to comment.