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

assertion failure with src/test/ui/pattern/const-pat-ice.rs #59996

Closed
matthiaskrgr opened this issue Apr 15, 2019 · 10 comments
Closed

assertion failure with src/test/ui/pattern/const-pat-ice.rs #59996

matthiaskrgr opened this issue Apr 15, 2019 · 10 comments
Assignees
Labels
A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html A-testsuite Area: The testsuite used to check the correctness of rustc C-bug Category: This is a bug. glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

matthiaskrgr commented Apr 15, 2019

// failure-status: 101
// rustc-env:RUST_BACKTRACE=0

// This is a repro test for an ICE in our pattern handling of constants.

const FOO: &&&u32 = &&&42;

fn main() {
    match unimplemented!() {
        &&&42 => {},
        FOO => {},
        _ => {},
    }
}

When checking with "cargo check", rustc hits an assertion failure:

    Checking crash v0.1.0 (/tmp/crash)
warning: unreachable arm
 --> src/main.rs:7:18
  |
7 |         &&&42 => {},
  |                  ^^
  |
  = note: #[warn(unreachable_code)] on by default

warning: unreachable pattern
 --> src/main.rs:8:9
  |
8 |         FOO => {},
  |         ^^^
  |
  = note: #[warn(unreachable_patterns)] on by default

thread 'rustc' panicked at 'assertion failed: rows.iter().all(|r| r.len() == v.len())', src/librustc_mir/hair/pattern/_match.rs:1069:5
stack backtrace:
   0:     0x7f2eaa92bd73 - std::sys::unix::backtrace::tracing::imp::unwind_backtrace::he0ba5a87706fdb06
                               at src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:39
   1:     0x7f2eaa923bfb - std::sys_common::backtrace::_print::h0f3483ebe5e1af88
                               at src/libstd/sys_common/backtrace.rs:71
   2:     0x7f2eaa927fd6 - std::panicking::default_hook::{{closure}}::haadf77e5fbe3559e
                               at src/libstd/sys_common/backtrace.rs:59
                               at src/libstd/panicking.rs:197
   3:     0x7f2eaa927d69 - std::panicking::default_hook::hdaa11accde9a3f58
                               at src/libstd/panicking.rs:211
   4:     0x7f2ea86c51f0 - rustc::util::common::panic_hook::h4cbb878598297ef8
   5:     0x7f2eaa9287c8 - std::panicking::rust_panic_with_hook::hf72148e2f6bd5661
                               at src/libstd/panicking.rs:478
   6:     0x7f2ea8de2cf4 - std::panicking::begin_panic::h2007827ee00b90b2
   7:     0x7f2ea8e093c4 - rustc_mir::hair::pattern::_match::is_useful::hd7ed9e7213150024
   8:     0x7f2ea8e0ab6d - rustc_mir::hair::pattern::_match::is_useful_specialized::hb8a1598a65e3b271
   9:     0x7f2ea91e7f54 - <core::iter::adapters::Map<I,F> as core::iter::traits::iterator::Iterator>::try_fold::h3fcdca02ea14ec38
  10:     0x7f2ea8e0a605 - rustc_mir::hair::pattern::_match::is_useful::hd7ed9e7213150024
  11:     0x7f2ea8e0ab6d - rustc_mir::hair::pattern::_match::is_useful_specialized::hb8a1598a65e3b271
  12:     0x7f2ea91e7f54 - <core::iter::adapters::Map<I,F> as core::iter::traits::iterator::Iterator>::try_fold::h3fcdca02ea14ec38
  13:     0x7f2ea8e0a605 - rustc_mir::hair::pattern::_match::is_useful::hd7ed9e7213150024
  14:     0x7f2ea918ce06 - rustc_mir::hair::pattern::check_match::check_arms::h6c99f59ecc84ce94
  15:     0x7f2ea8e059ab - rustc_mir::hair::pattern::_match::MatchCheckCtxt::create_and_enter::h1b1560cb14c299f7
  16:     0x7f2ea918c3d7 - <rustc_mir::hair::pattern::check_match::MatchVisitor as rustc::hir::intravisit::Visitor>::visit_expr::haf5104fa4e978225
  17:     0x7f2ea918c16b - <rustc_mir::hair::pattern::check_match::MatchVisitor as rustc::hir::intravisit::Visitor>::visit_expr::haf5104fa4e978225
  18:     0x7f2ea918c561 - <rustc_mir::hair::pattern::check_match::MatchVisitor as rustc::hir::intravisit::Visitor>::visit_body::hb0e552a09c406625
  19:     0x7f2ea918bf6c - rustc_mir::hair::pattern::check_match::check_match::h0531531b2ba4c51b
  20:     0x7f2eaa3b25aa - rustc::ty::query::__query_compute::check_match::hc106e74c584eb3c9
  21:     0x7f2eaa30173c - rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::check_match>::compute::hb98edf93d3a6e54c
  22:     0x7f2eaa2fa24a - rustc::dep_graph::graph::DepGraph::with_task_impl::h638c28c96ca61a0c
  23:     0x7f2eaa316fe7 - rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query::hacd07eac3642d837
  24:     0x7f2eaa301658 - rustc::ty::<impl rustc::ty::context::TyCtxt>::par_body_owners::heecedce46dfbac9a
  25:     0x7f2eaa2f50b7 - rustc::util::common::time::h85c150ce80df33f3
  26:     0x7f2eaa9394a9 - __rust_maybe_catch_panic
                               at src/libpanic_unwind/lib.rs:87
  27:     0x7f2eaa2ffe31 - rustc_interface::passes::analysis::{{closure}}::h43f87ca6908e783e
  28:     0x7f2eaa2f4468 - rustc::util::common::time::h61b8a0bb51f98662
  29:     0x7f2eaa34d14e - rustc_interface::passes::analysis::h2a8459513ab94adc
  30:     0x7f2eaac07682 - rustc::ty::query::__query_compute::analysis::hfdfdefc95b72369b
  31:     0x7f2eaac39d88 - rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::analysis>::compute::hc5444c8ee5e312dc
  32:     0x7f2eaac16a72 - rustc::dep_graph::graph::DepGraph::with_task_impl::h6ae66b24bed5ec4f
  33:     0x7f2eaac3ac4d - rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query::h146c2a97853525ef
  34:     0x7f2eaabfe859 - rustc_interface::passes::BoxedGlobalCtxt::access::{{closure}}::hc5bcf3e705fb931f
  35:     0x7f2eaa38fb04 - rustc_interface::passes::create_global_ctxt::{{closure}}::h77440ef2a630b6be
  36:     0x7f2eaac01028 - rustc_interface::interface::run_compiler_in_existing_thread_pool::h46bba02ee59f142b
  37:     0x7f2eaabe1b63 - std::thread::local::LocalKey<T>::with::h34ffb00bfd367860
  38:     0x7f2eaac46c84 - scoped_tls::ScopedKey<T>::set::h7d0ef554ad6a2f92
  39:     0x7f2eaac77d4f - syntax::with_globals::h8119039844d87a66
  40:     0x7f2eaabe3357 - std::sys_common::backtrace::__rust_begin_short_backtrace::h04eb124b36b3f4b3
  41:     0x7f2eaa9394a9 - __rust_maybe_catch_panic
                               at src/libpanic_unwind/lib.rs:87
  42:     0x7f2eaac02b58 - core::ops::function::FnOnce::call_once{{vtable.shim}}::h62a25d2cc7f5b35b
  43:     0x7f2eaa90aefe - <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once::h9112a52938383736
                               at /rustc/3de0106789468b211bcc3a25c09c0cf07119186d/src/liballoc/boxed.rs:702
  44:     0x7f2eaa93820f - std::sys::unix::thread::Thread::new::thread_start::h858b049c594cf60d
                               at /rustc/3de0106789468b211bcc3a25c09c0cf07119186d/src/liballoc/boxed.rs:702
                               at src/libstd/sys_common/thread.rs:14
                               at src/libstd/sys/unix/thread.rs:80
  45:     0x7f2eaa85ea9c - start_thread
  46:     0x7f2eaa77fb22 - clone
  47:                0x0 - <unknown>
query stack during panic:
#0 [check_match] processing `main::main`
#1 [analysis] running analysis passes on this crate
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.35.0-nightly (3de010678 2019-04-11) running on x86_64-unknown-linux-gnu

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

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

error: Could not compile `crash`.

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

cc rust-lang/rust-clippy#3142

@jonas-schievink jonas-schievink added A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html A-testsuite Area: The testsuite used to check the correctness of rustc C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 15, 2019
@varkor
Copy link
Member

varkor commented Apr 15, 2019

I haven't looked into this yet, but it could be related to #53708.

@Centril
Copy link
Contributor

Centril commented Apr 15, 2019

Reproduces all the way back to 1.26.0 at least (tested with godbolt).

@pnkfelix
Copy link
Member

pnkfelix commented Apr 18, 2019

triage: P-high since its an ICE. Leaving nominated because I want someone to talk me out of downgrading to P-medium based on it being an ancient bug.

@pnkfelix pnkfelix added the P-high High priority label Apr 18, 2019
@pnkfelix
Copy link
Member

retriage: P-medium. Assigning to self so that I do not lose track of it.

@pnkfelix pnkfelix added P-medium Medium priority and removed I-nominated P-high High priority labels May 16, 2019
@pnkfelix pnkfelix self-assigned this May 16, 2019
@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Oct 15, 2019
@matthiaskrgr
Copy link
Member Author

Bug is still present as of bbf1372

@matthiaskrgr
Copy link
Member Author

Bug is still present with rustc 1.43.0-nightly (436494b8f 2020-02-22)

@Centril
Copy link
Contributor

Centril commented Mar 10, 2020

cc @Nadrieril

@Nadrieril
Copy link
Member

First I've reduced a bit more:

const FOO: &&&() = &&&();

fn main() {
    match &&&() {
        FOO => {},
        &&&() => {},
    }
}

I think I see where the problem comes from, but I don't know how to fix it.
On this line:

kind: box PatKind::Deref {
subpattern: Pat {
ty: rty,
span: pat.span,
kind: box PatKind::Constant {
value: Const::from_value(
self.tcx,
self.fold_const_value_deref(*val, rty, crty),
rty,
),
},
},

we essentially say "if we have a constant pattern and the constant is a reference to something, then we convert it to being a pattern of the form &CONST. Not that this is not done recursively.
From the stack trace, the crash happens when trying to check if the second pattern is useful wrt the first. What would usually happen in a case like:

match &&&&0 {
    &&&&0 => {},
    &&&&1 => {},
}

is that we remove one layer of & at a time, until we get to compare the integers. Here however, the first pattern has one & but the second has three ! So when we try to remove the second layer of & something bad happens, because we didn't realize that the first pattern had more references hidden in the constant.
The solution would be to have as many layers of PatKind::Deref as are present in the const value, instead of just one. Maybe fixing the lack of recursion I mention above would do the trick ? I have no idea how constant references work.

Now what that surprises me most here is that I expected the crash to come from that line:

// FIXME(oli-obk): this is reachable for `const FOO: &&&u32 = &&&42;` being used
_ => bug!("cannot deref {:#?}, {} -> {}", val, crty, rty),
given the comment there. So I'll ping @oli-obk, hoping you understand what's happening with constants behind references.

@oli-obk
Copy link
Contributor

oli-obk commented Mar 10, 2020

Basically I never got around to implementing actual dereferencing of constants. It requires reading from a constant just like fn const_field accesses a field. Though I don't remember what the blockers for that were

@Alexendoo
Copy link
Member

Fixed by #70743

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html A-testsuite Area: The testsuite used to check the correctness of rustc C-bug Category: This is a bug. glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

9 participants