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

Const generics: Clippy: "expected bits of usize, got Const" #4775

Closed
andre-richter opened this issue Nov 5, 2019 · 6 comments · Fixed by #4780
Closed

Const generics: Clippy: "expected bits of usize, got Const" #4775

andre-richter opened this issue Nov 5, 2019 · 6 comments · Fixed by #4780
Labels
I-ICE Issue: Clippy panicked, giving an Internal Compilation Error (ICE) ❄️

Comments

@andre-richter
Copy link
Member

I ran into this error

error: internal compiler error: src/librustc/ty/sty.rs:2355: expected bits of usize, got Const {
    ty: usize,
    val: Param(
        NUM_SPECIAL_RANGES/#0,
    ),
}

thread 'rustc' panicked at 'Box<Any>', src/librustc_errors/lib.rs:931:9

Steps to repro:

  1. Install Rust nightly
  2. cargo install xbuild
  3. git clone git@github.com:rust-embedded/rust-raspi3-OS-tutorials.git
  4. cd rust-raspi3-OS-tutorials/11_virtual_memory
  5. make clippy

It might be about this code:

pub struct KernelVirtualLayout<const NUM_SPECIAL_RANGES: usize> {
    max_virt_addr_inclusive: usize,
    inner: [RangeDescriptor; NUM_SPECIAL_RANGES],
}

impl<const NUM_SPECIAL_RANGES: usize> KernelVirtualLayout<{ NUM_SPECIAL_RANGES }> {
    pub const fn new(max: usize, layout: [RangeDescriptor; NUM_SPECIAL_RANGES]) -> Self {
        Self {
            max_virt_addr_inclusive: max,
            inner: layout,
        }
    }

Meta

rustc 1.40.0-nightly (2477e2493 2019-11-04)
binary: rustc
commit-hash: 2477e2493e67527fc282c7239e019f7ebd513a1a
commit-date: 2019-11-04
host: x86_64-unknown-linux-gnu
release: 1.40.0-nightly
LLVM version: 9.0

Backtrace:

 RUST_BACKTRACE=1 make clippy
cargo xclippy --target=aarch64-unknown-none-softfloat --features bsp_rpi3
    Checking kernel v0.1.0 (/home/arichter/repos/rust-raspi3-OS-tutorials/11_virtual_memory)
error: internal compiler error: src/librustc/ty/sty.rs:2355: expected bits of usize, got Const {
    ty: usize,
    val: Param(
        NUM_SPECIAL_RANGES/#0,
    ),
}

thread 'rustc' panicked at 'Box<Any>', src/librustc_errors/lib.rs:931:9
stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.40/src/backtrace/libunwind.rs:88
   1: backtrace::backtrace::trace_unsynchronized
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.40/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print_fmt
             at src/libstd/sys_common/backtrace.rs:77
   3: <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt
             at src/libstd/sys_common/backtrace.rs:61
   4: core::fmt::write
             at src/libcore/fmt/mod.rs:1028
   5: std::io::Write::write_fmt
             at src/libstd/io/mod.rs:1412
   6: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:65
   7: std::sys_common::backtrace::print
             at src/libstd/sys_common/backtrace.rs:50
   8: std::panicking::default_hook::{{closure}}
             at src/libstd/panicking.rs:188
   9: std::panicking::default_hook
             at src/libstd/panicking.rs:205
  10: rustc_driver::report_ice
  11: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:468
  12: std::panicking::begin_panic
  13: rustc_errors::HandlerInner::bug
  14: rustc_errors::Handler::bug
  15: rustc::util::bug::opt_span_bug_fmt::{{closure}}
  16: rustc::ty::context::tls::with_opt::{{closure}}
  17: rustc::ty::context::tls::with_context_opt
  18: rustc::ty::context::tls::with_opt
  19: rustc::util::bug::opt_span_bug_fmt
  20: rustc::util::bug::bug_fmt
  21: rustc::ty::sty::Const::eval_bits::{{closure}}
  22: clippy_lints::loops::check_for_loop
  23: <clippy_lints::loops::Loops as rustc::lint::LateLintPass>::check_expr
  24: <rustc::lint::context::LateLintPassObjects as rustc::lint::LateLintPass>::check_expr
  25: <rustc::lint::context::LateContextAndPass<T> as rustc::hir::intravisit::Visitor>::visit_expr
  26: <rustc::lint::context::LateContextAndPass<T> as rustc::hir::intravisit::Visitor>::visit_expr
  27: rustc::hir::intravisit::walk_block
  28: rustc::hir::intravisit::walk_expr
  29: <rustc::lint::context::LateContextAndPass<T> as rustc::hir::intravisit::Visitor>::visit_expr
  30: <rustc::lint::context::LateContextAndPass<T> as rustc::hir::intravisit::Visitor>::visit_nested_body
  31: <rustc::lint::context::LateContextAndPass<T> as rustc::hir::intravisit::Visitor>::visit_fn
  32: rustc::hir::intravisit::walk_impl_item
  33: rustc::hir::intravisit::Visitor::visit_nested_impl_item
  34: rustc::hir::intravisit::walk_item
  35: rustc::hir::intravisit::Visitor::visit_nested_item
  36: rustc::hir::intravisit::walk_item
  37: rustc::hir::intravisit::Visitor::visit_nested_item
  38: rustc::hir::intravisit::walk_crate
  39: rustc::lint::context::late_lint_pass_crate
  40: rustc::lint::context::late_lint_crate
  41: rustc::util::common::time
  42: rustc::util::common::time
  43: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:78
  44: <std::panic::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
  45: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:78
  46: rustc_interface::passes::analysis::{{closure}}
  47: rustc_interface::passes::analysis
  48: rustc::ty::query::__query_compute::analysis
  49: rustc::dep_graph::graph::DepGraph::with_task_impl
  50: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
  51: rustc_interface::passes::BoxedGlobalCtxt::access::{{closure}}
  52: rustc_interface::passes::create_global_ctxt::{{closure}}
  53: rustc_interface::passes::BoxedGlobalCtxt::enter
  54: rustc_interface::interface::run_compiler_in_existing_thread_pool
  55: std::thread::local::LocalKey<T>::with
  56: scoped_tls::ScopedKey<T>::set
  57: syntax::with_globals
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.40.0-nightly (2477e2493 2019-11-04) running on x86_64-unknown-linux-gnu

note: compiler flags: -C debuginfo=2 -C incremental --crate-type bin

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to previous error

error: could not compile `kernel`.

To learn more, run the command again with --verbose.
Makefile:115: recipe for target 'clippy' failed
make: *** [clippy] Error 101
@Centril
Copy link
Contributor

Centril commented Nov 5, 2019

cc @rust-lang/clippy -- this could probably be transferred to Clippy?

@Manishearth Manishearth transferred this issue from rust-lang/rust Nov 6, 2019
@flip1995 flip1995 added the I-ICE Issue: Clippy panicked, giving an Internal Compilation Error (ICE) ❄️ label Nov 6, 2019
@flip1995
Copy link
Member

flip1995 commented Nov 6, 2019

Here is a minimal reproducer:

#![feature(const_generics)]
#![allow(incomplete_features)]

pub struct ArrayWrapper<const N: usize>([usize; N]);

impl<const N: usize> ArrayWrapper<{ N }> {
    pub fn ice(&self) {
        for i in self.0.iter() {
            println!("{}", i);
        }
    }
}

Playground

@flip1995
Copy link
Member

flip1995 commented Nov 6, 2019

Relevant (debug) backtrace for this:

  21: rustc::ty::sty::Const::eval_bits::{{closure}}
             at /rustc/3f0e16473de5ec010f44290a8c3ea1d90e0ad7a2/src/librustc/macros.rs:44
  22: core::option::Option<T>::unwrap_or_else
             at /rustc/3f0e16473de5ec010f44290a8c3ea1d90e0ad7a2/src/libcore/option.rs:419
  23: rustc::ty::sty::Const::eval_bits
             at /rustc/3f0e16473de5ec010f44290a8c3ea1d90e0ad7a2/src/librustc/ty/sty.rs:2354
  24: rustc::ty::sty::Const::eval_usize
             at /rustc/3f0e16473de5ec010f44290a8c3ea1d90e0ad7a2/src/librustc/ty/sty.rs:2360
  25: clippy_lints::loops::is_iterable_array
             at clippy_lints/src/loops.rs:1996

It tries to call eval_usize on a generic parameter. This is currently not supported. See
https://github.com/rust-lang/rust/blob/3f0e16473de5ec010f44290a8c3ea1d90e0ad7a2/src/librustc/mir/interpret/value.rs#L69-L80

and the ConstValue documentation


I think just bailing out, when eval_usize fails is the best thing to do here right now.

flip1995 added a commit to flip1995/rust-clippy that referenced this issue Nov 6, 2019
flip1995 added a commit to flip1995/rust-clippy that referenced this issue Nov 6, 2019
@flip1995 flip1995 mentioned this issue Nov 6, 2019
@bors bors closed this as completed in 338f5e6 Nov 11, 2019
@c410-f3r
Copy link
Contributor

Issue still happens in this repository.

error: internal compiler error: src/librustc/ty/sty.rs:2522: expected bits of usize, got Const {
    ty: usize,
    val: Param(
        N/#1,
    ),
}

thread 'rustc' panicked at 'Box<Any>', src/librustc_errors/lib.rs:881:9
stack backtrace:
   0:     0x7f8276766644 - backtrace::backtrace::libunwind::trace::h9d5fa1647efe1ab1
                               at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.44/src/backtrace/libunwind.rs:86
   1:     0x7f8276766644 - backtrace::backtrace::trace_unsynchronized::h86fb9c5f1eca30ef
                               at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.44/src/backtrace/mod.rs:66
   2:     0x7f8276766644 - std::sys_common::backtrace::_print_fmt::he9ba33a8eb3eb9ee
                               at src/libstd/sys_common/backtrace.rs:78
   3:     0x7f8276766644 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h5b9c1af25ae7c717
                               at src/libstd/sys_common/backtrace.rs:59
   4:     0x7f827679f7ec - core::fmt::write::h307eb561be2a404c
                               at src/libcore/fmt/mod.rs:1052
   5:     0x7f8276757fa7 - std::io::Write::write_fmt::h9f10c0557c95103f
                               at src/libstd/io/mod.rs:1428
   6:     0x7f827676b435 - std::sys_common::backtrace::_print::h9ba9ae783f57eedf
                               at src/libstd/sys_common/backtrace.rs:62
   7:     0x7f827676b435 - std::sys_common::backtrace::print::hfc2586ace23d36ac
                               at src/libstd/sys_common/backtrace.rs:49
   8:     0x7f827676b435 - std::panicking::default_hook::{{closure}}::hae11ef594f739717
                               at src/libstd/panicking.rs:204
   9:     0x7f827676b176 - std::panicking::default_hook::h91a2179e24328404
                               at src/libstd/panicking.rs:224
  10:     0x563c96f7b591 - clippy_driver::report_clippy_ice::h00b78117f8199052
  11:     0x7f827676bb65 - std::panicking::rust_panic_with_hook::h5394bdfc184d7cc7
                               at src/libstd/panicking.rs:474
  12:     0x7f82795865d3 - std::panicking::begin_panic::h586c0171fbdab168
  13:     0x7f82795c2a10 - rustc_errors::HandlerInner::bug::hca42a38f4a7f5b9a
  14:     0x7f82795c1860 - rustc_errors::Handler::bug::he56a55254aefe7ac
  15:     0x7f82792b2d29 - rustc::util::bug::opt_span_bug_fmt::{{closure}}::hcf8a9328b7d9651c
  16:     0x7f82792a90bb - rustc::ty::context::tls::with_opt::{{closure}}::hc97cc9e8bd8ae5d8
  17:     0x7f82792a9062 - rustc::ty::context::tls::with_opt::hc754df500ae2375b
  18:     0x7f82792b2c38 - rustc::util::bug::opt_span_bug_fmt::h7906b53d89b4c2e1
  19:     0x7f82792b2ba2 - rustc::util::bug::bug_fmt::hd3ba947be3f6a8df
  20:     0x563c96f9f47f - rustc::ty::sty::Const::eval_bits::{{closure}}::he6bca075ade2f14a
  21:     0x563c96fb1f47 - <clippy_lints::indexing_slicing::IndexingSlicing as rustc_lint::passes::LateLintPass>::check_expr::hc5e15fc7d2e672c0
  22:     0x7f8278973e43 - <rustc_lint::late::LateLintPassObjects as rustc_lint::passes::LateLintPass>::check_expr::hf6d54270d843a380
  23:     0x7f827702c6e2 - <rustc_lint::late::LateContextAndPass<T> as rustc_hir::intravisit::Visitor>::visit_expr::h9f4179d7d857c919
  24:     0x7f8276fc6686 - rustc_hir::intravisit::walk_expr::he754354f137a537e
  25:     0x7f827702c6ed - <rustc_lint::late::LateContextAndPass<T> as rustc_hir::intravisit::Visitor>::visit_expr::h9f4179d7d857c919
  26:     0x7f8276fc6882 - rustc_hir::intravisit::walk_expr::he754354f137a537e
  27:     0x7f827702c6ed - <rustc_lint::late::LateContextAndPass<T> as rustc_hir::intravisit::Visitor>::visit_expr::h9f4179d7d857c919
  28:     0x7f8276fc4bf4 - rustc_hir::intravisit::walk_fn::h3ff39dabd4ec61b9
  29:     0x7f827702d8a8 - <rustc_lint::late::LateContextAndPass<T> as rustc_hir::intravisit::Visitor>::visit_fn::hbb0b96ca91285ee7
  30:     0x7f8276fc226b - rustc_hir::intravisit::walk_impl_item::h598229146e9414df
  31:     0x7f827702ed5a - rustc_hir::intravisit::Visitor::visit_nested_impl_item::hdf4d97b76cbfda60
  32:     0x7f8276fc79d0 - rustc_hir::intravisit::walk_item::h44e80ad5b6277e08
  33:     0x7f827702e470 - rustc_hir::intravisit::Visitor::visit_nested_item::h290b2b9ac183f311
  34:     0x7f8276fc706e - rustc_hir::intravisit::walk_item::h44e80ad5b6277e08
  35:     0x7f827702e470 - rustc_hir::intravisit::Visitor::visit_nested_item::h290b2b9ac183f311
  36:     0x7f8276fc126e - rustc_hir::intravisit::walk_crate::h68de15a1d07fe82c
  37:     0x7f827701fecd - rustc_lint::late::late_lint_pass_crate::he7676702f03bb31a
  38:     0x7f827701f6eb - rustc_lint::late::late_lint_crate::h303b0802dd8c9a09
  39:     0x7f8276fde60b - rustc_data_structures::sync::join::hf5650324bb2ed613
  40:     0x7f8277020c9b - std::panicking::try::do_call::he8ffa9e7e95783d2
  41:     0x7f827677d457 - __rust_maybe_catch_panic
                               at src/libpanic_unwind/lib.rs:86
  42:     0x7f8276fd9413 - <std::panic::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::hf6c4f0787ce2bd29
  43:     0x7f827677d457 - __rust_maybe_catch_panic
                               at src/libpanic_unwind/lib.rs:86
  44:     0x7f8276fdc60a - rustc_session::utils::<impl rustc_session::session::Session>::time::h7249262b0ade9d94
  45:     0x7f827705aa36 - rustc_interface::passes::analysis::h63a5c50615a3546c
  46:     0x7f8276df5616 - rustc::ty::query::__query_compute::analysis::h7f7df2982d90698a
  47:     0x7f8276e5cc21 - rustc::dep_graph::graph::DepGraph::with_task_impl::h3547b6b631c468ed
  48:     0x7f8276e327d0 - rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query::hc2385fc085b423d4
  49:     0x7f8276e6c577 - rustc::ty::context::tls::enter_global::h8350ae2f23d40602
  50:     0x7f8276e3ed97 - rustc_interface::interface::run_compiler_in_existing_thread_pool::hc7ccb7d92618cd77
  51:     0x7f8276df0394 - std::sys_common::backtrace::__rust_begin_short_backtrace::h4e648e17f1d26b8c
  52:     0x7f827677d457 - __rust_maybe_catch_panic
                               at src/libpanic_unwind/lib.rs:86
  53:     0x7f8276e40796 - core::ops::function::FnOnce::call_once{{vtable.shim}}::h34b4c1a65012f088
  54:     0x7f827674832f - <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once::h558ce43155c9780d
                               at /rustc/436494b8f8008b600d64b3951f63c2bb0ea81673/src/liballoc/boxed.rs:1017
  55:     0x7f827677bd60 - <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once::hb6d9c1adc82281fd
                               at /rustc/436494b8f8008b600d64b3951f63c2bb0ea81673/src/liballoc/boxed.rs:1017
  56:     0x7f827677bd60 - std::sys_common::thread::start_thread::hcd78bb6c3dec167d
                               at src/libstd/sys_common/thread.rs:13
  57:     0x7f827677bd60 - std::sys::unix::thread::Thread::new::thread_start::h1c16950397532ea6
                               at src/libstd/sys/unix/thread.rs:80
  58:     0x7f82766ce669 - start_thread
  59:     0x7f82765dc323 - clone
  60:                0x0 - <unknown>

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust-clippy/issues/new

note: Clippy version: clippy 0.0.212 (8fbb23f 2020-02-21)

query stack during panic:
#0 [analysis] running analysis passes on this crate
end of query stack

@flip1995
Copy link
Member

That is another ICE. Can you open a separate issue?

@c410-f3r
Copy link
Contributor

Sure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-ICE Issue: Clippy panicked, giving an Internal Compilation Error (ICE) ❄️
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants