Skip to content
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

type_name depends on -Zverbose flag #94187

Closed
tmiasko opened this issue Feb 20, 2022 · 6 comments · Fixed by #103764
Closed

type_name depends on -Zverbose flag #94187

tmiasko opened this issue Feb 20, 2022 · 6 comments · Fixed by #103764
Labels
A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate.

Comments

@tmiasko
Copy link
Contributor

tmiasko commented Feb 20, 2022

#![feature(adt_const_params)]
#![feature(const_type_name)]
#![allow(incomplete_features)]

pub trait True {}

pub struct Equal<const A: &'static str, const B: &'static str>;

impl<const A: &'static str> True for Equal<{A}, {A}> {}

pub fn assert<T: True>(_: T) {}

fn main() {
    assert(Equal::<{std::any::type_name::<[u32; 0]>()}, "[u32; 0]">);
}
$ rustc t.rs
$ rustc t.rs -Zverbose
error[E0277]: the trait bound `Equal<Const(Value(Slice { data: Allocation { bytes: [91, 117, 51, 50, 59, 32, 67, 111, 110, 115, 116, 32, 123, 32, 116, 121, 58, 32, 117, 115, 105, 122, 101, 44, 32, 118, 97, 108, 58, 32, 86, 97, 108, 117, 101, 40, 83, 99, 97, 108, 97, 114, 40, 48, 120, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 41, 41, 32, 125, 93], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [18446744073709551615, 3], len: Size { raw: 66 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 66 }): &ReStatic str), Const(Value(Slice { data: Allocation { bytes: [91, 117, 51, 50, 59, 32, 48, 93], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [255], len: Size { raw: 8 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 8 }): &ReStatic str)>: True` is not satisfied
  --> t.rs:14:12
   |
14 |     assert(Equal::<{std::any::type_name::<[u32; 0]>()}, "[u32; 0]">);
   |     ------ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `True` is not implemented for `Equal<Const(Value(Slice { data: Allocation { bytes: [91, 117, 51, 50, 59, 32, 67, 111, 110, 115, 116, 32, 123, 32, 116, 121, 58, 32, 117, 115, 105, 122, 101, 44, 32, 118, 97, 108, 58, 32, 86, 97, 108, 117, 101, 40, 83, 99, 97, 108, 97, 114, 40, 48, 120, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 41, 41, 32, 125, 93], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [18446744073709551615, 3], len: Size { raw: 66 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 66 }): &ReStatic str), Const(Value(Slice { data: Allocation { bytes: [91, 117, 51, 50, 59, 32, 48, 93], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [255], len: Size { raw: 8 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 8 }): &ReStatic str)>`
   |     |
   |     required by a bound introduced by this call
   |
   = help: the following implementations were found:
             <Equal<Const(Param(A/#0): &ReStatic str), Const(Param(A/#0): &ReStatic str)> as True>
note: required by a bound in `assert`
  --> t.rs:11:18
   |
11 | pub fn assert<T: True>(_: T) {}
   |                  ^^^^ required by this bound in `assert`

error: aborting due to previous error

cc #63084

@tmiasko tmiasko added C-bug Category: This is a bug. A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. labels Feb 20, 2022
@RalfJung
Copy link
Member

Cc @rust-lang/wg-const-eval strange type-level equality quirk?

@RalfJung RalfJung added the A-const-generics Area: const generics (parameters and arguments) label Feb 20, 2022
@tmiasko
Copy link
Contributor Author

tmiasko commented Feb 20, 2022

pretty_print_const used by type_name implementation depends on -Zverbose:

fn pretty_print_const(
mut self,
ct: ty::Const<'tcx>,
print_ty: bool,
) -> Result<Self::Const, Self::Error> {
define_scoped_cx!(self);
if self.tcx().sess.verbose() {
p!(write("Const({:?}: {:?})", ct.val(), ct.ty()));
return Ok(self);
}

@oli-obk
Copy link
Contributor

oli-obk commented Feb 20, 2022

That's not really a const eval issue. This likely also happens for any lifetimes or impl trait types or anything else affected by the verbose flag.

Not sure we want to fix this, relying on a specific name is not something that should be done afaik. You should only use type names for printing to users

@tmiasko
Copy link
Contributor Author

tmiasko commented Feb 20, 2022

-Zverbose is an untracked option:

  • Producing different query output based on its value is incompatible with incremental compilation.
  • Its value could vary between crates which makes type system inconsistent.

@lcnr
Copy link
Contributor

lcnr commented Feb 21, 2022

jup, should fix that 😅 type_name should always not be verbose i think

@oli-obk oli-obk added the E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. label Oct 18, 2022
@oli-obk
Copy link
Contributor

oli-obk commented Oct 18, 2022

As a first step we should probably just remove the verbose flag check from rendering constants. That looks just useless even for compiler debugging.

But to fix this properly we'll likely need to create a way to tell the pretty printer whether to use verbose mode or not, and entirely avoid using verbosity checks within it.

@oli-obk oli-obk added the E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. label Oct 18, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Oct 26, 2022
…iler-errors

Removed verbose printing from the `PrettyPrinter` when printing constants

Partially solves rust-lang#94187 by completing the first step described in [this comment](rust-lang#94187 (comment)).
notriddle added a commit to notriddle/rust that referenced this issue Oct 30, 2022
…mpiler-errors

All verbosity checks in `PrettyPrinter` now go through `PrettyPrinter::should_print_verbose`

Follow-up to rust-lang#103428. That pr only partially fixed rust-lang#94187. In some cases (like closures) `std::any::type_name` was still producing a different output when `-Zverbose` was enabled.

This pr fixes those cases and adds a new function `PrettyPrinter::should_print_verbose`. This function should always be used over `self.tcx().sess.verbose()` inside a `impl PrettyPrinter`.

Maybe closes rust-lang#94187 now.

r? `@compiler-errors`
@bors bors closed this as completed in e6ffd96 Oct 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate.
Projects
None yet
4 participants