Skip to content

Render pattern types nicely in mir dumps #136176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 29, 2025
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
4 changes: 4 additions & 0 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1740,6 +1740,10 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
" as ",
)?;
}
ty::Pat(base_ty, pat) => {
self.pretty_print_const_scalar_int(int, *base_ty, print_ty)?;
p!(write(" is {pat:?}"));
}
// Nontrivial types with scalar bit representation
_ => {
let print = |this: &mut Self| {
Expand Down
15 changes: 15 additions & 0 deletions tests/mir-opt/pattern_types.main.PreCodegen.after.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// MIR for `main` after PreCodegen

fn main() -> () {
let mut _0: ();
scope 1 {
debug x => const 2_u32 is 1..=;
scope 2 {
debug y => const 0_u32 is 1..=;
Copy link
Contributor Author

@oli-obk oli-obk Jan 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is fun. We do render as transmute for other things that are invalid (e.g. chars that aren't valid chars, or bools that aren't 0 or 1), but it's harder to do this here without running validation on it... and I'm not sure all the extra logic for doing so would be worth it.

}
}

bb0: {
return;
}
}
12 changes: 12 additions & 0 deletions tests/mir-opt/pattern_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![feature(pattern_types)]
#![feature(pattern_type_macro)]

use std::pat::pattern_type;

// EMIT_MIR pattern_types.main.PreCodegen.after.mir
fn main() {
// CHECK: debug x => const 2_u32 is 1..=
let x: pattern_type!(u32 is 1..) = unsafe { std::mem::transmute(2) };
// CHECK: debug y => const 0_u32 is 1..=
let y: pattern_type!(u32 is 1..) = unsafe { std::mem::transmute(0) };
}
Loading