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

Compiler panic when using repr(packed) associated constant in a match arm #50356

Closed
Dillonu opened this issue May 1, 2018 · 2 comments · Fixed by #50393
Closed

Compiler panic when using repr(packed) associated constant in a match arm #50356

Dillonu opened this issue May 1, 2018 · 2 comments · Fixed by #50393
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Dillonu
Copy link

Dillonu commented May 1, 2018

I recently updated my nightly toolchain for rust, and compiled a project I was working on. Previously I was on the nightly-2018-04-19-x86_64-pc-windows-msvc version, and updated to the nightly-2018-04-29-x86_64-pc-windows-msvc version. I encountered a compiler error when dealing with match statements and Associated Constants, centered around alignment issues of the associated constant.

After some testing, I managed to create a simple example with the code below:

#[derive(PartialEq, Eq)] // Needed for pattern matching
#[repr(packed)] // Commenting out this line removes the error
pub struct Num(u64);

impl Num {
    const ZERO: Self = Num(0); // Error points to this line as the alignment issue
}

fn decrement(a: Num) -> Num {
    match a {
        // Matching Num against Num::Zero with repr(packed) causes alignment error:
        Num::ZERO => Num::ZERO, // Commenting out this line removes the error
        a => Num(a.0 - 1)
    }
}

fn main() {
}

cargo build:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: ConstEvalErr { span: src\main.rs:6:5: 6:31, kind: Miri(EvalError { kind: AlignmentCheckFailed { required: 8, has: 1 }, backtrace: None }, []) }', libcore\result.rs:945:5
stack backtrace:
   0: <std::collections::hash::map::DefaultHasher as core::fmt::Debug>::fmt
   1: <std::sys::windows::os_str::Buf as std::sys_common::IntoInner<std::sys_common::wtf8::Wtf8Buf>>::into_inner
   2: std::panicking::take_hook
   3: std::panicking::take_hook
   4: rustc::ty::structural_impls::<impl rustc::ty::context::Lift<'tcx> for rustc::middle::const_val::ErrKind<'a>>::lift_to_tcx
   5: std::panicking::rust_panic_with_hook
   6: std::panicking::begin_panic_fmt
   7: rust_begin_unwind
   8: core::panicking::panic_fmt
   9: <rustc_mir::transform::inline::CallSite<'tcx> as core::fmt::Debug>::fmt
  10: rustc_mir::hair::pattern::PatternContext::lower_pattern
  11: <unknown>
  12: rustc_mir::hair::pattern::PatternContext::lower_pattern
  13: rustc_mir::hair::pattern::PatternContext::lower_pattern
  14: rustc_mir::hair::pattern::PatternContext::lower_pattern
  15: <rustc_mir::transform::inline::CallSite<'tcx> as core::fmt::Debug>::fmt
  16: <unknown>
  17: <unknown>
  18: <rustc_mir::hair::pattern::check_match::MatchVisitor<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_expr
  19: <rustc_mir::hair::pattern::check_match::MatchVisitor<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_expr
  20: <rustc_mir::monomorphize::collector::MonoItemCollectionMode as core::fmt::Debug>::fmt
  21: rustc_mir::hair::pattern::check_match::check_crate
  22: rustc::ty::context::tls::track_diagnostic
  23: rustc::dep_graph::graph::DepGraph::assert_ignored
  24: rustc::ty::context::tls::track_diagnostic
  25: rustc::ty::maps::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::try_mark_green_and_read
  26: rustc::ty::maps::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::try_mark_green_and_read
  27: rustc::ty::maps::<impl rustc::ty::context::TyCtxt<'a, 'tcx, 'lcx>>::check_match
  28: <rustc_mir::transform::inline::CallSite<'tcx> as core::fmt::Debug>::fmt
  29: <rustc_mir::monomorphize::collector::MonoItemCollectionMode as core::fmt::Debug>::fmt
  30: rustc_mir::hair::pattern::check_match::check_crate
  31: <env_logger::filter::inner::Filter as core::fmt::Display>::fmt
  32: <env_logger::filter::inner::Filter as core::fmt::Display>::fmt
  33: <unknown>
  34: rustc_driver::driver::compile_input
  35: rustc_driver::run_compiler
  36: rustc_driver::target_features::add_configuration
  37: <unknown>
  38: rustc_driver::target_features::add_configuration
  39: _rust_maybe_catch_panic
  40: rustc_driver::profile::trace::write_style
  41: rustc_driver::main
  42: <unknown>
  43: std::panicking::update_panic_count
  44: _rust_maybe_catch_panic
  45: std::rt::lang_start_internal
  46: <unknown>
  47: <unknown>
  48: BaseThreadInitThunk
  49: RtlUserThreadStart
query stack during panic:
#0 [check_match] processing `decrement`
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.27.0-nightly (79252ff4e 2018-04-29) running on x86_64-pc-windows-msvc

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

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

error: Could not compile `error-example`.

To learn more, run the command again with --verbose.

Meta

rustc --version --verbose:

rustc 1.27.0-nightly (79252ff4e 2018-04-29)
binary: rustc
commit-hash: 79252ff4e25d82f9fe856cb66f127b79cdace163
commit-date: 2018-04-29
host: x86_64-pc-windows-msvc
release: 1.27.0-nightly
LLVM version: 6.0

I've also tested and encountered this error on the following versions:

  • nightly-2018-04-27-x86_64-pc-windows-msvc
  • nightly-2018-04-28-x86_64-pc-windows-msvc
  • nightly-2018-04-29-x86_64-pc-windows-msvc
  • nightly-2018-04-30-x86_64-pc-windows-msvc
@oli-obk oli-obk added I-nominated regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. P-high High priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. regression-from-stable-to-beta Performance or correctness regression from stable to beta. A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) and removed I-nominated P-high High priority regression-from-stable-to-beta Performance or correctness regression from stable to beta. regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. labels May 1, 2018
@oli-obk
Copy link
Contributor

oli-obk commented May 1, 2018

Sounds like some alignment check is getting the alignment from the wrong side of an assignment.

I hope it wasn't introduced in a beta backported bugfix, we need to check that.

@oli-obk
Copy link
Contributor

oli-obk commented May 2, 2018

Ok, It's just in nightly, not in beta! I'll try to bisect

kennytm added a commit to kennytm/rust that referenced this issue May 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants