Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion compiler/rustc_middle/src/ty/consts/valtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,12 @@ impl<'tcx> Value<'tcx> {
_ => return None,
}

Some(tcx.arena.alloc_from_iter(self.to_branch().into_iter().map(|ct| ct.to_leaf().to_u8())))
// We create an iterator that yields `Option<u8>`
let iterator = self.to_branch().into_iter().map(|ct| Some(ct.try_to_leaf()?.to_u8()));
// If there is `None` in the iterator, then the array is not a valid array of u8s and we return `None`
let bytes: Vec<u8> = iterator.collect::<Option<Vec<u8>>>()?;

Some(tcx.arena.alloc_from_iter(bytes))
}

/// Converts to a `ValTreeKind::Leaf` value, `panic`'ing
Expand Down
10 changes: 6 additions & 4 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1911,14 +1911,16 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
return Ok(());
}
},
(ty::ValTreeKind::Branch(_), ty::Array(t, _)) if t == u8_type => {
let bytes = cv.try_to_raw_bytes(self.tcx()).unwrap_or_else(|| {
bug!("expected to convert valtree to raw bytes for type {:?}", t)
});
// If it is a branch with an array, and this array can be printed as raw bytes, then dump its bytes
(ty::ValTreeKind::Branch(_), ty::Array(t, _))
if t == u8_type
&& let Some(bytes) = cv.try_to_raw_bytes(self.tcx()) =>
{
write!(self, "*")?;
self.pretty_print_byte_str(bytes)?;
return Ok(());
}
// Otherwise, print the array separated by commas (or if it's a tuple)
(ty::ValTreeKind::Branch(fields), ty::Array(..) | ty::Tuple(..)) => {
let fields_iter = fields.iter().copied();

Expand Down
13 changes: 13 additions & 0 deletions tests/ui/const-generics/mgca/wrong_type_const_arr_diag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// This test causes ERROR: mismatched types [E0308]
// and makes rustc to print array from const arguments
#![feature(min_generic_const_args, adt_const_params)]
#![allow(incomplete_features)]

struct TakesArr<const N: [u8; 1]>;

fn foo<const N: u8>() {
let _: TakesArr<{ [N] }> = TakesArr::<{ [1] }>;
//~^ ERROR: mismatched types [E0308]
}

fn main() {}
14 changes: 14 additions & 0 deletions tests/ui/const-generics/mgca/wrong_type_const_arr_diag.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0308]: mismatched types
--> $DIR/wrong_type_const_arr_diag.rs:9:32
|
LL | let _: TakesArr<{ [N] }> = TakesArr::<{ [1] }>;
| ----------------- ^^^^^^^^^^^^^^^^^^^ expected `[N]`, found `*b"\x01"`
| |
| expected due to this
|
= note: expected struct `TakesArr<[N]>`
found struct `TakesArr<*b"\x01">`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
21 changes: 21 additions & 0 deletions tests/ui/const-generics/mgca/wrong_type_const_arr_diag_trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// This test causes ERROR: mismatched types [E0308]
// and makes rustc to print array from const arguments
#![feature(min_generic_const_args, adt_const_params, trivial_bounds)]
#![allow(incomplete_features)]

trait Trait {
#[type_const]
const ASSOC: u8;
}

struct TakesArr<const N: [u8; 1]>;

fn foo<const N: u8>()
where
u8: Trait
{
let _: TakesArr<{ [<u8 as Trait>::ASSOC] }> = TakesArr::<{ [1] }>;
//~^ ERROR: mismatched types [E0308]
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0308]: mismatched types
--> $DIR/wrong_type_const_arr_diag_trait.rs:17:51
|
LL | let _: TakesArr<{ [<u8 as Trait>::ASSOC] }> = TakesArr::<{ [1] }>;
| ------------------------------------ ^^^^^^^^^^^^^^^^^^^ expected `[<u8 as Trait>::ASSOC]`, found `*b"\x01"`
| |
| expected due to this
|
= note: expected struct `TakesArr<[<u8 as Trait>::ASSOC]>`
found struct `TakesArr<*b"\x01">`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
Loading