Skip to content

Better pretty printing for const raw pointers #65859

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

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
10 changes: 9 additions & 1 deletion src/librustc/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,15 @@ pub trait PrettyPrinter<'tcx>:
},
(ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Char) =>
p!(write("{:?}", ::std::char::from_u32(data as u32).unwrap())),
(ConstValue::Scalar(_), ty::RawPtr(_)) => p!(write("{{pointer}}")),
(ConstValue::Scalar(value), ty::RawPtr(_)) => {
match value {
Copy link
Member

Choose a reason for hiding this comment

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

So this is a "fallback" case now, right? We already have a big case for references above. Unfortunately it is not immediately clear what distinguishes these cases.

Could you add some comments both at the if let ty::Ref above and at the match arm here describing why we do the matches we do in the order they are, and which various cases of references each conditional covers?

Copy link
Author

Choose a reason for hiding this comment

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

I don't know the history of that big if let ty::Ref statement or why it looks the way it does internally, so I don't feel comfortable documenting it. But I added a comment specifying that it needs to happen before the general case.

Copy link
Member

Choose a reason for hiding this comment

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

From what I can see, the point of that is to print the content of the slice/array when possible for types &[u8], [u8; N] and &str. Do you agree? If yes, please write that. Right now you only mention &str but that is not the only type handled here.

The fallback then is that for all other references and raw pointers, we just print the pointer value, not the thing it points to.

Scalar::Raw { data, size } => {
p!(write("0x{:01$x} : ", data, size as usize * 2));
}
_ => p!(write("{{pointer}} : "))
};
p!(print(ct.ty));
}
(ConstValue::Scalar(Scalar::Ptr(ptr)), ty::FnPtr(_)) => {
let instance = {
let alloc_map = self.tcx().alloc_map.lock();
Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/const-generics/raw-ptr-const-param.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// normalize-stderr-32bit: "0x" -> "$$PREFIX"
// normalize-stderr-64bit: "0x00000000" -> "$$PREFIX"

#![feature(const_generics, const_compare_raw_pointers)]
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash

Expand All @@ -6,4 +9,6 @@ struct Const<const P: *const u32>;
fn main() {
let _: Const<{15 as *const _}> = Const::<{10 as *const _}>; //~ mismatched types
let _: Const<{10 as *const _}> = Const::<{10 as *const _}>;

let _: Const<{10 as *const _}> = Const::<{&8_u32 as *const _}>; //~ mismatched types
}
21 changes: 15 additions & 6 deletions src/test/ui/const-generics/raw-ptr-const-param.stderr
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
--> $DIR/raw-ptr-const-param.rs:1:12
--> $DIR/raw-ptr-const-param.rs:4:12
|
LL | #![feature(const_generics, const_compare_raw_pointers)]
| ^^^^^^^^^^^^^^
|
= note: `#[warn(incomplete_features)]` on by default

error[E0308]: mismatched types
--> $DIR/raw-ptr-const-param.rs:7:38
--> $DIR/raw-ptr-const-param.rs:10:38
|
LL | let _: Const<{15 as *const _}> = Const::<{10 as *const _}>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `{pointer}`, found `{pointer}`
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `$PREFIX0000000f : *const u32`, found `$PREFIX0000000a : *const u32`
|
= note: expected type `Const<{pointer}>`
found type `Const<{pointer}>`
= note: expected type `Const<$PREFIX0000000f : *const u32>`
found type `Const<$PREFIX0000000a : *const u32>`

error: aborting due to previous error
error[E0308]: mismatched types
--> $DIR/raw-ptr-const-param.rs:13:38
|
LL | let _: Const<{10 as *const _}> = Const::<{&8_u32 as *const _}>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `$PREFIX0000000a : *const u32`, found `{pointer} : *const u32`
|
= note: expected type `Const<$PREFIX0000000a : *const u32>`
found type `Const<{pointer} : *const u32>`

error: aborting due to 2 previous errors

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