Skip to content

Commit 4ca5368

Browse files
committed
defer array len printing to const arg printing
1 parent e08b379 commit 4ca5368

31 files changed

+59
-66
lines changed

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

Lines changed: 4 additions & 20 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,10 +1286,10 @@ 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-
_ => {
1292+
DefKind::AnonConst => {
13101293
if def.is_local() {
13111294
let span = self.tcx().def_span(def.did);
13121295
if let Ok(snip) = self.tcx().sess.source_map().span_to_snippet(span) {
@@ -1318,6 +1301,7 @@ pub trait PrettyPrinter<'tcx>:
13181301
print_underscore!()
13191302
}
13201303
}
1304+
defkind => bug!("`{:?}` has unexpcted defkind {:?}", ct, defkind),
13211305
}
13221306
}
13231307
ty::ConstKind::Infer(infer_ct) => {

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

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

tests/ui/const-generics/issues/issue-62878.min.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0770]: the type of const parameters must not depend on other generic para
44
LL | fn foo<const N: usize, const A: [u8; N]>() {}
55
| ^ the type must not depend on the parameter `N`
66

7-
error: `[u8; _]` is forbidden as the type of a const generic parameter
7+
error: `[u8; N]` is forbidden as the type of a const generic parameter
88
--> $DIR/issue-62878.rs:5:33
99
|
1010
LL | fn foo<const N: usize, const A: [u8; N]>() {}

tests/ui/const-generics/issues/issue-62878.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

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

99
fn main() {
1010
foo::<_, { [1] }>();

tests/ui/const-generics/issues/issue-71169.min.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0770]: the type of const parameters must not depend on other generic para
44
LL | fn foo<const LEN: usize, const DATA: [u8; LEN]>() {}
55
| ^^^ the type must not depend on the parameter `LEN`
66

7-
error: `[u8; _]` is forbidden as the type of a const generic parameter
7+
error: `[u8; LEN]` is forbidden as the type of a const generic parameter
88
--> $DIR/issue-71169.rs:5:38
99
|
1010
LL | fn foo<const LEN: usize, const DATA: [u8; LEN]>() {}

tests/ui/const-generics/issues/issue-71169.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
fn foo<const LEN: usize, const DATA: [u8; LEN]>() {}
66
//~^ ERROR the type of const parameters must not
7-
//[min]~^^ ERROR `[u8; _]` is forbidden as the type of a const generic parameter
7+
//[min]~^^ ERROR `[u8; LEN]` is forbidden as the type of a const generic parameter
88
fn main() {
99
const DATA: [u8; 4] = *b"ABCD";
1010
foo::<4, DATA>();

0 commit comments

Comments
 (0)