diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs index bda0c4fa2c6f..69afa4f20b3e 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs @@ -8,6 +8,7 @@ use std::{fmt, iter}; use rustc_data_structures::fx::{FxIndexMap, FxIndexSet}; +use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_data_structures::unord::UnordSet; use rustc_errors::{Applicability, Diag, E0038, E0276, MultiSpan, struct_span_code_err}; use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId}; @@ -74,18 +75,20 @@ fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { } fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) { - if self.span == ex.span { - self.result = Some(ex); - } else { - if let hir::ExprKind::Closure(..) = ex.kind - && self.include_closures - && let closure_header_sp = self.span.with_hi(ex.span.hi()) - && closure_header_sp == ex.span - { + ensure_sufficient_stack(|| { + if self.span == ex.span { self.result = Some(ex); + } else { + if let hir::ExprKind::Closure(..) = ex.kind + && self.include_closures + && let closure_header_sp = self.span.with_hi(ex.span.hi()) + && closure_header_sp == ex.span + { + self.result = Some(ex); + } + hir::intravisit::walk_expr(self, ex); } - hir::intravisit::walk_expr(self, ex); - } + }); } fn visit_ty(&mut self, ty: &'v hir::Ty<'v, AmbigArg>) { diff --git a/tests/ui/try-trait/deep-try-chain-issue-153583.rs b/tests/ui/try-trait/deep-try-chain-issue-153583.rs new file mode 100644 index 000000000000..cc00866fa329 --- /dev/null +++ b/tests/ui/try-trait/deep-try-chain-issue-153583.rs @@ -0,0 +1,8 @@ +// Regression test for #153583: deeply nested `?` operators should not overflow +// the error-reporting visitor. +// ignore-tidy-linelength + +fn main() -> Result<(), ()> { + 0????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????; //~ ERROR the `?` operator can only be applied to values that implement `Try` + Ok(()) +} diff --git a/tests/ui/try-trait/deep-try-chain-issue-153583.stderr b/tests/ui/try-trait/deep-try-chain-issue-153583.stderr new file mode 100644 index 000000000000..d3794136202b --- /dev/null +++ b/tests/ui/try-trait/deep-try-chain-issue-153583.stderr @@ -0,0 +1,11 @@ +error[E0277]: the `?` operator can only be applied to values that implement `Try` + --> $DIR/deep-try-chain-issue-153583.rs:6:5 + | +LL | 0???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????... + | ^^ the `?` operator cannot be applied to type `{integer}` + | + = help: the nightly-only, unstable trait `Try` is not implemented for `{integer}` + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0277`.