Skip to content

ICE while using TaggedPtr' compile-time evaluations. #124400

Closed

Description

@Alonely0

I encountered an ICE while playing with tagged_pointer's TaggedPtr that has to do with its comptime assertions. I have not been able to reproduce the ICE when inlining the constant value in which the checks are performed. Perhaps it is related with the fact that originally there was a crate boundary? I have an inkling const blocks' merger might be to blame.

ICE file

Code

use tagged_pointer::TaggedPtr; // version = 0.2.7
struct Foo;
TaggedPtr::<_, 2>::new(NonNull::from(&Foo), 0x1);

Meta

rustc --version --verbose:

rustc 1.79.0-nightly (3a36386dc 2024-04-25)
binary: rustc
commit-hash: 3a36386dc1075018dc7ca2640a2656adb31a61fe
commit-date: 2024-04-25
host: x86_64-unknown-linux-gnu
release: 1.79.0-nightly
LLVM version: 18.1.4

Error output

error[E0080]: evaluation of `tagged_pointer::<impl tagged_pointer::ptr::PtrImpl<engine::layout::tests::test_iter::Foo, 2>>::ASSERT` failed
   --> /home/guillem/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tagged-pointer-0.2.7/src/lib.rs:161:9
    |
161 |         ["alignment of `T` must be at least `1 << BITS`"][!b as usize];
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ index out of bounds: the length is 1 but the index is 1

error: internal compiler error: compiler/rustc_middle/src/mir/mod.rs:705:17: Couldn't evaluate constant const tagged_pointer::<impl tagged_pointer::ptr::PtrImpl<T, BITS>>::ASSERT in mono Instance { def: Item(DefId(25:58 ~ tagged_pointer[1ee3]::{impl#0}::assert)), args: [engine::layout::tests::test_iter::Foo, 2_usize] }

thread 'rustc' panicked at compiler/rustc_middle/src/mir/mod.rs:705:17:
Box<dyn Any>
Backtrace

stack backtrace:
   0: std::panicking::begin_panic::<rustc_errors::ExplicitBug>
   1: <rustc_errors::diagnostic::BugAbort as rustc_errors::diagnostic::EmissionGuarantee>::emit_producing_guarantee
   2: rustc_middle::util::bug::opt_span_bug_fmt::<rustc_span::span_encoding::Span>::{closure#0}
   3: rustc_middle::ty::context::tls::with_opt::<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, !>::{closure#0}
   4: rustc_middle::ty::context::tls::with_context_opt::<rustc_middle::ty::context::tls::with_opt<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
   5: rustc_middle::util::bug::bug_fmt
   6: rustc_monomorphize::collector::collect_items_rec::{closure#0}
   7: rustc_monomorphize::collector::collect_items_rec
   8: rustc_monomorphize::collector::collect_items_rec
   9: rustc_monomorphize::collector::collect_items_rec
  10: rustc_monomorphize::collector::collect_items_rec
  11: rustc_monomorphize::collector::collect_items_rec
  12: rustc_monomorphize::collector::collect_items_rec
  13: rustc_monomorphize::partitioning::collect_and_partition_mono_items
      [... omitted 2 frames ...]
  14: rustc_codegen_ssa::base::codegen_crate::<rustc_codegen_llvm::LlvmCodegenBackend>
  15: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::CodegenBackend>::codegen_crate
  16: rustc_interface::passes::start_codegen
  17: <rustc_interface::queries::Queries>::codegen_and_build_linker
  18: rustc_interface::interface::run_compiler::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}

Activity

  1. added
    C-bug
    I-ICE
    T-compiler
    on Apr 26, 2024
  2. added
    needs-triage
    on Apr 26, 2024
  3. added
    E-needs-mcve
    on Apr 26, 2024
  4. Noratrieb commented on Apr 26, 2024

    @Noratrieb
    Member

    It would be useful to have a minimal example that doesn't involve the entire tagged_ptr crate.

  5. removed
    needs-triage
    on Apr 26, 2024
  6. lukas-code commented on Apr 26, 2024

    @lukas-code
    Contributor

    Reduced: playground, needs cargo build

    struct Bar<const BITS: usize>;
    
    impl<const BITS: usize> Bar<BITS> {
        const ASSERT: bool = {
            let b = std::convert::identity(1);
            ["oops"][b];
            true
        };
    
        fn assert() {
            let val = Self::ASSERT;
            if val {
                std::convert::identity(val);
            }
        }
    }
    
    
    fn main() {
        Bar::<0>::assert();
    }

    This bisects to #123272 cc @saethlin

    @rustbot label A-monomorphization S-has-mcve -E-needs-mcve

  7. added
    A-monomorphization
    S-has-mcve
    and removed
    E-needs-mcve
    on Apr 26, 2024
  8. self-assigned this
    on Apr 26, 2024
  9. saethlin commented on Apr 27, 2024

    @saethlin
    Member

    ye olde "compiler must not crash on invalid code" problem

  10. saethlin commented on Apr 27, 2024

    @saethlin
    Member

    PR with a fix is up. Thanks for catching this before the bug hit beta @Alonely0!

  11. Alonely0 commented on Apr 27, 2024

    @Alonely0
    Author

    PR with a fix is up. Thanks for catching this before the bug hit beta @Alonely0!

    No, thanks to you all for not only fixing it, but running this amazing project every day. I hope I have more time in the future so that I can spend it here and be a part of this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-monomorphizationC-bugI-ICES-has-mcveT-compiler

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions