diff options
| author | bors <bors@rust-lang.org> | 2021-11-26 13:42:35 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2021-11-26 13:42:35 +0000 |
| commit | 454cc5fb86be180b3ec1138b6f2b480fbf3f1388 (patch) | |
| tree | 618d3a042df21961cf8987774e019c8f1b3dbbe4 | |
| parent | 1e79d79dac11ce121b972fbaa0db9b0d1756b991 (diff) | |
| parent | 6955afe8fd16d51b2c617d69f89a9eac5b7fd150 (diff) | |
| download | rust-454cc5fb86be180b3ec1138b6f2b480fbf3f1388.tar.gz rust-454cc5fb86be180b3ec1138b6f2b480fbf3f1388.zip | |
Auto merge of #91164 - Badel2:usefulness-stack-overflow, r=davidtwco
Fix stack overflow in `usefulness.rs` Fix #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.
| -rw-r--r-- | compiler/rustc_mir_build/src/thir/pattern/usefulness.rs | 11 | ||||
| -rw-r--r-- | src/test/ui/pattern/usefulness/issue-88747.rs | 14 |
2 files changed, 21 insertions, 4 deletions
diff --git a/compiler/rustc_mir_build/src/thir/pattern/usefulness.rs b/compiler/rustc_mir_build/src/thir/pattern/usefulness.rs index 3ed434131b2..286473c38e3 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/usefulness.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/usefulness.rs @@ -289,6 +289,7 @@ use super::deconstruct_pat::{Constructor, DeconstructedPat, Fields, SplitWildcar use rustc_data_structures::captures::Captures; use rustc_arena::TypedArena; +use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_hir::def_id::DefId; use rustc_hir::HirId; use rustc_middle::ty::{self, Ty, TyCtxt}; @@ -808,8 +809,9 @@ fn is_useful<'p, 'tcx>( // We try each or-pattern branch in turn. let mut matrix = matrix.clone(); for v in v.expand_or_pat() { - let usefulness = - is_useful(cx, &matrix, &v, witness_preference, hir_id, is_under_guard, false); + let usefulness = ensure_sufficient_stack(|| { + is_useful(cx, &matrix, &v, witness_preference, hir_id, is_under_guard, false) + }); ret.extend(usefulness); // If pattern has a guard don't add it to the matrix. if !is_under_guard { @@ -840,8 +842,9 @@ fn is_useful<'p, 'tcx>( // We cache the result of `Fields::wildcards` because it is used a lot. let spec_matrix = start_matrix.specialize_constructor(pcx, &ctor); let v = v.pop_head_constructor(cx, &ctor); - let usefulness = - is_useful(cx, &spec_matrix, &v, witness_preference, hir_id, is_under_guard, false); + let usefulness = ensure_sufficient_stack(|| { + is_useful(cx, &spec_matrix, &v, witness_preference, hir_id, is_under_guard, false) + }); let usefulness = usefulness.apply_constructor(pcx, start_matrix, &ctor); // When all the conditions are met we have a match with a `non_exhaustive` enum diff --git a/src/test/ui/pattern/usefulness/issue-88747.rs b/src/test/ui/pattern/usefulness/issue-88747.rs new file mode 100644 index 00000000000..948c99f9ce9 --- /dev/null +++ b/src/test/ui/pattern/usefulness/issue-88747.rs @@ -0,0 +1,14 @@ +// check-pass: this used to be a stack overflow because of recursion in `usefulness.rs` + +macro_rules! long_tuple_arg { + ([$($t:tt)*]#$($h:tt)*) => { + long_tuple_arg!{[$($t)*$($t)*]$($h)*} + }; + ([$([$t:tt $y:tt])*]) => { + pub fn _f(($($t,)*): ($($y,)*)) {} + } +} + +long_tuple_arg!{[[_ u8]]########## ###} + +fn main() {} |
