Skip to content

Commit 9437d22

Browse files
Avoid stack overflow in FindExprBySpan
1 parent 35f1109 commit 9437d22

3 files changed

Lines changed: 31 additions & 10 deletions

File tree

compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub mod suggestions;
88
use std::{fmt, iter};
99

1010
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
11+
use rustc_data_structures::stack::ensure_sufficient_stack;
1112
use rustc_data_structures::unord::UnordSet;
1213
use rustc_errors::{Applicability, Diag, E0038, E0276, MultiSpan, struct_span_code_err};
1314
use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
@@ -74,18 +75,20 @@ impl<'v> Visitor<'v> for FindExprBySpan<'v> {
7475
}
7576

7677
fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
77-
if self.span == ex.span {
78-
self.result = Some(ex);
79-
} else {
80-
if let hir::ExprKind::Closure(..) = ex.kind
81-
&& self.include_closures
82-
&& let closure_header_sp = self.span.with_hi(ex.span.hi())
83-
&& closure_header_sp == ex.span
84-
{
78+
ensure_sufficient_stack(|| {
79+
if self.span == ex.span {
8580
self.result = Some(ex);
81+
} else {
82+
if let hir::ExprKind::Closure(..) = ex.kind
83+
&& self.include_closures
84+
&& let closure_header_sp = self.span.with_hi(ex.span.hi())
85+
&& closure_header_sp == ex.span
86+
{
87+
self.result = Some(ex);
88+
}
89+
hir::intravisit::walk_expr(self, ex);
8690
}
87-
hir::intravisit::walk_expr(self, ex);
88-
}
91+
});
8992
}
9093

9194
fn visit_ty(&mut self, ty: &'v hir::Ty<'v, AmbigArg>) {

tests/ui/try-trait/issue-153583.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Regression test for #153583: deeply nested `?` operators should not overflow
2+
// the error-reporting visitor.
3+
4+
fn main() -> Result<(), ()> {
5+
0????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????; //~ ERROR the `?` operator can only be applied to values that implement `Try`
6+
Ok(())
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0277]: the `?` operator can only be applied to values that implement `Try`
2+
--> $DIR/issue-153583.rs:5:5
3+
|
4+
LL | 0???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????...
5+
| ^^ the `?` operator cannot be applied to type `{integer}`
6+
|
7+
= help: the nightly-only, unstable trait `Try` is not implemented for `{integer}`
8+
9+
error: aborting due to 1 previous error
10+
11+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)