Skip to content

ICE with #![feature(trivial_bounds)]: SizeOf nullary MIR operator called for unsized type #6663

Open
@ghost

Description

I tried this code:

#![feature(trivial_bounds)]
#![warn(const_err)]

use core::mem::size_of;

struct Helper<T: ?Sized>(T);

trait Unsized<T: ?Sized> {
    const SIZE: usize = usize::MAX;
}

impl<T: ?Sized> Unsized<T> for Helper<T> {}

impl<T> Helper<T> {
    const SIZE: usize = size_of::<T>();
}

struct TrickClippy(str);

impl TrickClippy {
    fn trick_clippy() -> bool
    where
        Self: Sized,
    {
        Helper::<Self>::SIZE == Helper::<str>::SIZE
    }
}

rustc compiled the code successfully with only dead_code warnings, while Clippy gave me an additional const_err warning and an ICE (SizeOf nullary MIR operator called for unsized type TrickClippy):

warning: any use of this value will cause an error
   --> /rustup/home/toolchains/nightly-2021-01-28-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/mem/mod.rs:310:5
    |
310 |     intrinsics::size_of::<T>()
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^
    |     |
    |     size_of called on unsized type `TrickClippy`
    |     inside `std::mem::size_of::<TrickClippy>` at /rustup/home/toolchains/nightly-2021-01-28-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/mem/mod.rs:310:5
    |     inside `Helper::<TrickClippy>::SIZE` at <anon>:15:25
    | 
   ::: <anon>:15:5
    |
15  |     const SIZE: usize = size_of::<T>();
    |     -----------------------------------
    |
note: the lint level is defined here
   --> <anon>:2:9
    |
2   | #![warn(const_err)]
    |         ^^^^^^^^^

warning: 4 warnings emitted

error: internal compiler error: SizeOf nullary MIR operator called for unsized type TrickClippy
   --> /rustup/home/toolchains/nightly-2021-01-28-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/mem/mod.rs:310:5
    |
310 |     intrinsics::size_of::<T>()
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: delayed at compiler/rustc_mir/src/interpret/step.rs:269:35

thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', compiler/rustc_errors/src/lib.rs:974:13
Backtrace with -Z treat-err-as-bug
thread 'rustc' panicked at 'aborting due to `-Z treat-err-as-bug=1`', compiler/rustc_errors/src/lib.rs:1028:27
stack backtrace:
   0: std::panicking::begin_panic
   1: rustc_errors::HandlerInner::emit_diagnostic
   2: rustc_errors::HandlerInner::emit_diag_at_span
   3: rustc_errors::HandlerInner::span_bug
   4: rustc_errors::Handler::delay_span_bug
   5: rustc_mir::interpret::step::<impl rustc_mir::interpret::eval_context::InterpCx<M>>::eval_rvalue_into_place
   6: rustc_mir::interpret::step::<impl rustc_mir::interpret::eval_context::InterpCx<M>>::run
   7: rustc_mir::const_eval::eval_queries::eval_to_allocation_raw_provider
   8: rustc_query_impl::<impl rustc_query_system::query::config::QueryAccessors<rustc_query_impl::plumbing::QueryCtxt> for rustc_query_impl::queries::eval_to_allocation_raw>::compute
   9: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
  10: rustc_data_structures::stack::ensure_sufficient_stack
  11: rustc_query_system::query::plumbing::force_query_with_job
  12: rustc_query_system::query::plumbing::get_query_impl
  13: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::eval_to_allocation_raw
  14: rustc_mir::const_eval::eval_queries::eval_to_const_value_raw_provider
  15: rustc_query_impl::<impl rustc_query_system::query::config::QueryAccessors<rustc_query_impl::plumbing::QueryCtxt> for rustc_query_impl::queries::eval_to_const_value_raw>::compute
  16: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
  17: rustc_data_structures::stack::ensure_sufficient_stack
  18: rustc_query_system::query::plumbing::force_query_with_job
  19: rustc_query_system::query::plumbing::get_query_impl
  20: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::eval_to_const_value_raw
  21: rustc_middle::mir::interpret::queries::<impl rustc_middle::ty::context::TyCtxt>::const_eval_global_id
  22: rustc_middle::mir::interpret::queries::<impl rustc_middle::ty::context::TyCtxt>::const_eval_resolve
  23: clippy_utils::consts::ConstEvalLateContext::expr
  24: clippy_utils::hir_utils::HirEqInterExpr::eq_expr
  25: clippy_utils::hir_utils::eq_expr_value
  26: <clippy_lints::eq_op::EqOp as rustc_lint::passes::LateLintPass>::check_expr
  27: <rustc_lint::late::LateLintPassObjects as rustc_lint::passes::LateLintPass>::check_expr
  28: <rustc_lint::late::LateContextAndPass<T> as rustc_hir::intravisit::Visitor>::visit_expr
  29: <rustc_lint::late::LateContextAndPass<T> as rustc_hir::intravisit::Visitor>::visit_block
  30: <rustc_lint::late::LateContextAndPass<T> as rustc_hir::intravisit::Visitor>::visit_expr
  31: <rustc_lint::late::LateContextAndPass<T> as rustc_hir::intravisit::Visitor>::visit_nested_body
  32: <rustc_lint::late::LateContextAndPass<T> as rustc_hir::intravisit::Visitor>::visit_fn
  33: rustc_hir::intravisit::walk_impl_item
  34: rustc_hir::intravisit::Visitor::visit_nested_impl_item
  35: rustc_hir::intravisit::walk_impl_item_ref
  36: rustc_hir::intravisit::walk_item
  37: rustc_hir::intravisit::Visitor::visit_nested_item
  38: rustc_hir::intravisit::walk_mod
  39: <rustc_lint::late::LateContextAndPass<T> as rustc_hir::intravisit::Visitor>::visit_mod
  40: rustc_hir::intravisit::walk_crate
  41: rustc_lint::late::late_lint_pass_crate
  42: rustc_session::utils::<impl rustc_session::session::Session>::time
  43: std::panic::catch_unwind
  44: rustc_session::utils::<impl rustc_session::session::Session>::time
  45: rustc_interface::passes::analysis
  46: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
  47: rustc_data_structures::stack::ensure_sufficient_stack
  48: rustc_query_system::query::plumbing::force_query_with_job
  49: rustc_query_system::query::plumbing::get_query_impl
  50: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::analysis
  51: rustc_interface::passes::QueryContext::enter
  52: rustc_interface::queries::<impl rustc_interface::interface::Compiler>::enter
  53: rustc_span::with_source_map
  54: rustc_interface::interface::create_compiler_and_run
  55: rustc_span::with_session_globals
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

error: internal compiler error: unexpected panic

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.1.52 (3a5d45f 2021-03-09)

query stack during panic:
#0 [eval_to_allocation_raw] const-evaluating + checking `<impl at clippy-trivial-bounds-ice.rs:15:1: 17:2>::SIZE`
#1 [eval_to_const_value_raw] simplifying constant for the type system `<impl at clippy-trivial-bounds-ice.rs:15:1: 17:2>::SIZE`
#2 [analysis] running analysis passes on this crate
end of query stack

I believe the const_err warning was incorrect. rustc did not report it and core::intrinsics::size_of() has a Sized bound.

cc rust-lang/rust#48214

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-ICEIssue: Clippy panicked, giving an Internal Compilation Error (ICE) ❄️

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions