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

Trying to pattern-match field 1 of Box causes an ICE #82772

Closed
CAD97 opened this issue Mar 4, 2021 · 5 comments · Fixed by #82789
Closed

Trying to pattern-match field 1 of Box causes an ICE #82772

CAD97 opened this issue Mar 4, 2021 · 5 comments · Fixed by #82789
Labels
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 regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@CAD97
Copy link
Contributor

CAD97 commented Mar 4, 2021

Code

fn main() {
    let Box { 1: _, .. }: Box<()>;
}

[playground]

Meta

rustc --version --verbose:

1.52.0-nightly

(2021-03-01 4f20caa6258d4c74ce6b)

Error output

thread 'rustc' panicked at 'index out of bounds: the len is 1 but the index is 1', compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs:1346:21
Backtrace

stack backtrace:
   0: rust_begin_unwind
             at /rustc/4f20caa6258d4c74ce6b316fd347e3efe81cf557/library/std/src/panicking.rs:493:5
   1: core::panicking::panic_fmt
             at /rustc/4f20caa6258d4c74ce6b316fd347e3efe81cf557/library/core/src/panicking.rs:92:14
   2: core::panicking::panic_bounds_check
             at /rustc/4f20caa6258d4c74ce6b316fd347e3efe81cf557/library/core/src/panicking.rs:69:5
   3: rustc_mir_build::thir::pattern::deconstruct_pat::Fields::replace_with_pattern_arguments
   4: rustc_mir_build::thir::pattern::usefulness::PatStack::pop_head_constructor
   5: rustc_mir_build::thir::pattern::usefulness::is_useful
   6: <core::iter::adapters::copied::Copied<I> as core::iter::traits::iterator::Iterator>::fold
   7: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter::SpecFromIter<T,I>>::from_iter
   8: rustc_mir_build::thir::pattern::usefulness::compute_match_usefulness
   9: rustc_mir_build::thir::pattern::check_match::MatchVisitor::check_irrefutable
  10: <rustc_mir_build::thir::pattern::check_match::MatchVisitor as rustc_hir::intravisit::Visitor>::visit_local
  11: rustc_hir::intravisit::walk_block
  12: <rustc_mir_build::thir::pattern::check_match::MatchVisitor as rustc_hir::intravisit::Visitor>::visit_expr
  13: rustc_mir_build::thir::pattern::check_match::check_match
  14: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
  15: rustc_data_structures::stack::ensure_sufficient_stack
  16: rustc_query_system::query::plumbing::force_query_with_job
  17: rustc_query_system::query::plumbing::get_query_impl
  18: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::check_match
  19: rustc_middle::ty::<impl rustc_middle::ty::context::TyCtxt>::par_body_owners
  20: rustc_session::utils::<impl rustc_session::session::Session>::time
  21: rustc_interface::passes::analysis
  22: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
  23: rustc_data_structures::stack::ensure_sufficient_stack
  24: rustc_query_system::query::plumbing::force_query_with_job
  25: rustc_query_system::query::plumbing::get_query_impl
  26: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::analysis
  27: rustc_interface::passes::QueryContext::enter
  28: rustc_interface::queries::<impl rustc_interface::interface::Compiler>::enter
  29: rustc_span::with_source_map
  30: rustc_interface::interface::create_compiler_and_run
  31: scoped_tls::ScopedKey<T>::set
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

query stack during panic:
#0 [check_match] match-checking `main`
#1 [analysis] running analysis passes on this crate
end of query stack

@CAD97 CAD97 added 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 Mar 4, 2021
@CAD97
Copy link
Contributor Author

CAD97 commented Mar 4, 2021

Seems to be the indicated function:

/// Overrides some of the fields with the provided patterns. This is used when a pattern
/// defines some fields but not all, for example `Foo { field1: Some(_), .. }`: here we start
/// with a `Fields` that is just one wildcard per field of the `Foo` struct, and override the
/// entry corresponding to `field1` with the pattern `Some(_)`. This is also used for slice
/// patterns for the same reason.
fn replace_fields_indexed(
&self,
new_pats: impl IntoIterator<Item = (usize, &'p Pat<'tcx>)>,
) -> Self {
let mut fields = self.clone();
if let Fields::Slice(pats) = fields {
fields = Fields::Vec(pats.iter().collect());
}
match &mut fields {
Fields::Vec(pats) => {
for (i, pat) in new_pats {
pats[i] = pat
}
}
Fields::Filtered { fields, .. } => {
for (i, pat) in new_pats {
if let FilteredField::Kept(p) = &mut fields[i] {
*p = pat
}
}
}
Fields::Slice(_) => unreachable!(),
}
fields
}

@estebank estebank added the regression-from-stable-to-stable Performance or correctness regression from one stable version to another. label Mar 4, 2021
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Mar 4, 2021
@estebank
Copy link
Contributor

estebank commented Mar 4, 2021

1.49 didn't ICE.

@CAD97 CAD97 changed the title Trying to access field 1 of Box causes an ICE Trying to pattern-match field 1 of Box causes an ICE Mar 4, 2021
@apiraino
Copy link
Contributor

apiraino commented Mar 5, 2021

Assigning P-medium as discussed as part of the Prioritization Working Group procedure and removing I-prioritize.

@rustbot label -I-prioritize +P-medium

@rustbot rustbot added P-medium Medium priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Mar 5, 2021
fanninpm added a commit to fanninpm/glacier that referenced this issue Mar 5, 2021
@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Mar 6, 2021
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Mar 13, 2021
Get with field index from pattern slice instead of directly indexing

Closes rust-lang#82772
r? `@estebank`

rust-lang#82789 (comment)
> `@estebank` So the real cause is we only generate single pattern for Box here
https://github.com/csmoe/rust/blob/615b03aeaa8ce9819de7828740ab3cd7def4fa76/compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs#L1130-L1132
But in the replacing function, it tries to index on the 1-length pattern slice with field 1, thus out of bounds.
https://github.com/csmoe/rust/blob/615b03aeaa8ce9819de7828740ab3cd7def4fa76/compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs#L1346
@bors bors closed this as completed in 0d9a6ed Mar 14, 2021
@Nadrieril
Copy link
Member

Oh this is ugly x) Reading the PR, the problem is that there are two ways to match a Box: via a box-pattern or via Box { .. }. But we can't know which will be used based on the type only. Current code assumes box-patterns, hence the error. The PR works because luckily all the fields of Box are private.
If you tried pattern-matching within the module that defines Box, you'd get crazy results like (actually wouldn't even typecheck):

match Box::new(true) {
    Box(true, _) => {}
    box true => {} //~ ERROR unreachable pattern
	_ => {}
}

match Box::new(true) {
    box true => {}
	box false => {}
	Box(_, _) => {} //~ ERROR unreachable pattern
}

We'll need to figure out a real solution when we implement generalized Deref patterns...

@estebank
Copy link
Contributor

@Nadrieril in that case we should open a new ticket for that work, tagged something like F-deref_patterns or something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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 regression-from-stable-to-stable Performance or correctness regression from one stable version to another. 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.

6 participants