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

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

Closed
mjbshaw opened this issue Oct 31, 2018 · 4 comments
Closed

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

mjbshaw opened this issue Oct 31, 2018 · 4 comments
Labels
I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

Comments

@mjbshaw
Copy link
Contributor

mjbshaw commented Oct 31, 2018

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).

@oli-obk
Copy link
Contributor

oli-obk commented Oct 31, 2018

cc @RalfJung

@RalfJung
Copy link
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
Copy link
Contributor Author

mjbshaw commented Nov 1, 2018

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
Copy link
Member

RalfJung commented Nov 1, 2018

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

@Centril Centril added the I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ label Nov 1, 2018
mjbshaw added a commit to mjbshaw/rust that referenced this issue Nov 2, 2018
kennytm added a commit to kennytm/rust that referenced this issue Nov 8, 2018
miri: accept extern types in structs if they are the only field

Fixes rust-lang#55541

Cc @oli-obk @eddyb rust-lang#43467
pietroalbini added a commit to pietroalbini/rust that referenced this issue Nov 12, 2018
miri: accept extern types in structs if they are the only field

Fixes rust-lang#55541

Cc @oli-obk @eddyb rust-lang#43467
pietroalbini added a commit to pietroalbini/rust that referenced this issue Nov 15, 2018
miri: accept extern types in structs if they are the only field

Fixes rust-lang#55541

Cc @oli-obk @eddyb rust-lang#43467
kennytm added a commit to kennytm/rust that referenced this issue Nov 17, 2018
miri: accept extern types in structs if they are the only field

Fixes rust-lang#55541

Cc @oli-obk @eddyb rust-lang#43467
bors added a commit that referenced this issue Nov 18, 2018
miri: accept extern types in structs if they are the only field

Fixes #55541

Cc @oli-obk @eddyb #43467
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
Projects
None yet
Development

No branches or pull requests

4 participants