Skip to content

ICE when creating a static with an extern type field #55541

Closed
@mjbshaw

Description

@mjbshaw
Contributor

The following code used to work, but no longer does on the latest nightly:

#![feature(extern_types, const_transmute)]

extern "C" {
  pub type ExternType;
}
unsafe impl Sync for ExternType {}

#[repr(transparent)]
pub struct Wrapper(ExternType);

static MAGIC_FFI_STATIC: u8 = 42;

pub static MAGIC_FFI_REF: &'static Wrapper = unsafe {
  std::mem::transmute(&MAGIC_FFI_STATIC)
};

It causes an ICE when attempting to compile it.

Backtrace:
thread 'main' panicked at 'Fields cannot be extern types', libcore/option.rs:1008:5
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
   1: std::sys_common::backtrace::_print
   2: std::panicking::default_hook::{{closure}}
   3: std::panicking::default_hook
   4: rustc::util::common::panic_hook
   5: std::panicking::rust_panic_with_hook
   6: std::panicking::continue_panic_fmt
   7: rust_begin_unwind
   8: core::panicking::panic_fmt
   9: core::option::expect_failed
  10: <rustc_mir::interpret::eval_context::EvalContext<'a, 'mir, 'tcx, M>>::size_and_align_of
  11: rustc_mir::interpret::validity::<impl rustc_mir::interpret::eval_context::EvalContext<'a, 'mir, 'tcx, M>>::validate_primitive_type
  12: rustc_mir::interpret::validity::<impl rustc_mir::interpret::eval_context::EvalContext<'a, 'mir, 'tcx, M>>::validate_operand
  13: rustc_mir::const_eval::const_eval_provider
  14: rustc::ty::query::__query_compute::const_eval
  15: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors<'tcx> for rustc::ty::query::queries::const_eval<'tcx>>::compute
  16: rustc::dep_graph::graph::DepGraph::with_task_impl
  17: rustc::ty::context::tls::with_related_context
  18: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::force_query_with_job
  19: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::get_query
  20: rustc::ty::query::<impl rustc::ty::context::TyCtxt<'a, 'tcx, 'lcx>>::const_eval
  21: rustc_mir::const_eval::const_eval_provider
  22: rustc::ty::query::__query_compute::const_eval
  23: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors<'tcx> for rustc::ty::query::queries::const_eval<'tcx>>::compute
  24: rustc::dep_graph::graph::DepGraph::with_task_impl
  25: rustc::ty::context::tls::with_related_context
  26: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::force_query_with_job
  27: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::get_query
  28: rustc::ty::query::<impl rustc::ty::context::TyCtxt<'a, 'tcx, 'lcx>>::const_eval
  29: rustc_lint::builtin::check_const
  30: <rustc_lint::register_builtins::BuiltinCombinedLateLintPass as rustc::lint::LateLintPass<'a, 'tcx>>::check_item
  31: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_item
  32: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_mod
  33: rustc::hir::intravisit::walk_crate
  34: rustc::lint::context::check_crate
  35: rustc::util::common::time
  36: rustc::ty::context::tls::enter_context
  37: <std::thread::local::LocalKey<T>>::with
  38: rustc::ty::context::TyCtxt::create_and_enter
  39: rustc_driver::driver::compile_input
  40: rustc_driver::run_compiler_with_pool
  41: rustc_driver::driver::spawn_thread_pool
  42: rustc_driver::run_compiler
  43: <scoped_tls::ScopedKey<T>>::set
  44: syntax::with_globals
  45: __rust_maybe_catch_panic
  46: rustc_driver::run
  47: rustc_driver::main
  48: std::rt::lang_start::{{closure}}
  49: std::panicking::try::do_call
  50: __rust_maybe_catch_panic
  51: std::rt::lang_start_internal
  52: main
query stack during panic:
#0 [const_eval] const-evaluating + checking `MAGIC_FFI_REF`
#1 [const_eval] const-evaluating + checking `MAGIC_FFI_REF`
end of query stack

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/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.31.0-nightly (1cf82fd9c 2018-10-30) running on x86_64-apple-darwin

note: compiler flags: --crate-type rlib

The problem appears to be the Wrapper type. If you modify MAGIC_FFI_REF to have the type &'static ExternType then it compiles okay. It only fails with the type &'static Wrapper.

My rust version: rustc 1.31.0-nightly (1cf82fd9c 2018-10-30).

Activity

oli-obk

oli-obk commented on Oct 31, 2018

@oli-obk
Contributor
RalfJung

RalfJung commented on Oct 31, 2018

@RalfJung
Member

Wasn't the conclusion from the extern type RFC that we cannot have such fields because we cannot compute their offsets without knowing the alignment? I am surprised this struct definition is even accepted.

mjbshaw

mjbshaw commented on Nov 1, 2018

@mjbshaw
ContributorAuthor

I would expect and hope that Rust permits #[repr(transparent)] newtype wrappers for extern type. Reading through the extern_types tracking issue I see there are valid concerns regarding offsets and alignments for extern type fields, but if the struct is #[repr(transparent)] and only contains zero-sized types (i.e. PhantomData) plus a single extern type, I don't see why it should be disallowed.

RalfJung

RalfJung commented on Nov 1, 2018

@RalfJung
Member

So what exactly are the current rules for fields of dynamically unsized types?

Every other field being zero-sized is likely not enough, but if it also has alignment 1 then I can see how that works. I'd just like to know what the rules are so we can implement them in miri.

Cc @plietar

added
I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
on Nov 1, 2018
added a commit that references this issue on Nov 8, 2018

Rollup merge of rust-lang#55672 - RalfJung:miri-extern-types, r=eddyb

6b93f19
added a commit that references this issue on Nov 12, 2018

Rollup merge of rust-lang#55672 - RalfJung:miri-extern-types, r=eddyb

1a61f26

9 remaining items

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-ICEIssue: The compiler 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

        Participants

        @RalfJung@oli-obk@Centril@mjbshaw

        Issue actions

          ICE when creating a static with an extern type field · Issue #55541 · rust-lang/rust