Skip to content

Commit 454cc5f

Browse files
committed
Auto merge of rust-lang#91164 - Badel2:usefulness-stack-overflow, r=davidtwco
Fix stack overflow in `usefulness.rs` Fix rust-lang#88747 Applied the suggestion from `@nbdd0121,` not sure if this has any drawbacks. The first call to `ensure_sufficient_stack` is not needed to fix the test case, but I added it to be safe.
2 parents 1e79d79 + 6955afe commit 454cc5f

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

compiler/rustc_mir_build/src/thir/pattern/usefulness.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ use super::deconstruct_pat::{Constructor, DeconstructedPat, Fields, SplitWildcar
289289
use rustc_data_structures::captures::Captures;
290290

291291
use rustc_arena::TypedArena;
292+
use rustc_data_structures::stack::ensure_sufficient_stack;
292293
use rustc_hir::def_id::DefId;
293294
use rustc_hir::HirId;
294295
use rustc_middle::ty::{self, Ty, TyCtxt};
@@ -808,8 +809,9 @@ fn is_useful<'p, 'tcx>(
808809
// We try each or-pattern branch in turn.
809810
let mut matrix = matrix.clone();
810811
for v in v.expand_or_pat() {
811-
let usefulness =
812-
is_useful(cx, &matrix, &v, witness_preference, hir_id, is_under_guard, false);
812+
let usefulness = ensure_sufficient_stack(|| {
813+
is_useful(cx, &matrix, &v, witness_preference, hir_id, is_under_guard, false)
814+
});
813815
ret.extend(usefulness);
814816
// If pattern has a guard don't add it to the matrix.
815817
if !is_under_guard {
@@ -840,8 +842,9 @@ fn is_useful<'p, 'tcx>(
840842
// We cache the result of `Fields::wildcards` because it is used a lot.
841843
let spec_matrix = start_matrix.specialize_constructor(pcx, &ctor);
842844
let v = v.pop_head_constructor(cx, &ctor);
843-
let usefulness =
844-
is_useful(cx, &spec_matrix, &v, witness_preference, hir_id, is_under_guard, false);
845+
let usefulness = ensure_sufficient_stack(|| {
846+
is_useful(cx, &spec_matrix, &v, witness_preference, hir_id, is_under_guard, false)
847+
});
845848
let usefulness = usefulness.apply_constructor(pcx, start_matrix, &ctor);
846849

847850
// When all the conditions are met we have a match with a `non_exhaustive` enum
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// check-pass: this used to be a stack overflow because of recursion in `usefulness.rs`
2+
3+
macro_rules! long_tuple_arg {
4+
([$($t:tt)*]#$($h:tt)*) => {
5+
long_tuple_arg!{[$($t)*$($t)*]$($h)*}
6+
};
7+
([$([$t:tt $y:tt])*]) => {
8+
pub fn _f(($($t,)*): ($($y,)*)) {}
9+
}
10+
}
11+
12+
long_tuple_arg!{[[_ u8]]########## ###}
13+
14+
fn main() {}

0 commit comments

Comments
 (0)