Skip to content

Commit c0b6ffa

Browse files
committed
Auto merge of #114990 - Zoxc:else-if-overflow, r=cjgillot
Fix a stack overflow with long else if chains This fixes stack overflows when running the `issue-74564-if-expr-stack-overflow.rs` test with the parallel compiler.
2 parents ff55fa3 + 1e87ef6 commit c0b6ffa

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

Diff for: compiler/rustc_ast/src/mut_visit.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::tokenstream::*;
1313
use crate::{ast::*, StaticItem};
1414

1515
use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
16+
use rustc_data_structures::stack::ensure_sufficient_stack;
1617
use rustc_data_structures::sync::Lrc;
1718
use rustc_span::source_map::Spanned;
1819
use rustc_span::symbol::Ident;
@@ -1369,7 +1370,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
13691370
ExprKind::If(cond, tr, fl) => {
13701371
vis.visit_expr(cond);
13711372
vis.visit_block(tr);
1372-
visit_opt(fl, |fl| vis.visit_expr(fl));
1373+
visit_opt(fl, |fl| ensure_sufficient_stack(|| vis.visit_expr(fl)));
13731374
}
13741375
ExprKind::While(cond, body, label) => {
13751376
vis.visit_expr(cond);

Diff for: compiler/rustc_parse/src/parser/expr.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use rustc_ast::{AnonConst, BinOp, BinOpKind, FnDecl, FnRetTy, MacCall, Param, Ty
2222
use rustc_ast::{Arm, Async, BlockCheckMode, Expr, ExprKind, Label, Movability, RangeLimits};
2323
use rustc_ast::{ClosureBinder, MetaItemLit, StmtKind};
2424
use rustc_ast_pretty::pprust;
25+
use rustc_data_structures::stack::ensure_sufficient_stack;
2526
use rustc_errors::{
2627
AddToDiagnostic, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, IntoDiagnostic,
2728
PResult, StashKey,
@@ -2489,7 +2490,7 @@ impl<'a> Parser<'a> {
24892490
let else_span = self.prev_token.span; // `else`
24902491
let attrs = self.parse_outer_attributes()?; // For recovery.
24912492
let expr = if self.eat_keyword(kw::If) {
2492-
self.parse_expr_if()?
2493+
ensure_sufficient_stack(|| self.parse_expr_if())?
24932494
} else if self.check(&TokenKind::OpenDelim(Delimiter::Brace)) {
24942495
self.parse_simple_block()?
24952496
} else {

0 commit comments

Comments
 (0)