Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exclude = ["crates/proc-macro-srv/proc-macro-test/imp"]
resolver = "2"

[workspace.package]
rust-version = "1.95"
rust-version = "1.94"
edition = "2024"
license = "MIT OR Apache-2.0"
authors = ["rust-analyzer team"]
Expand Down
19 changes: 18 additions & 1 deletion crates/parser/src/grammar/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,23 @@ fn expr_bp(
m: Option<Marker>,
r: Restrictions,
bp: u8,
) -> Option<(CompletedMarker, BlockLike)> {
if !p.check_recursion_limit() {

@ChayimFriedman2 ChayimFriedman2 Jun 3, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is, of course, not enough. Many functions in the parser recurse.

I'm not yet sure whether we want such thing, though.

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we want to fix this we should do what rustc does which is growing the stack on demand

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And there are also plenty of places besides parsing that will need that (although parsing is the most important).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea hir-def lowering does as well at the very least

if let Some(m) = m {
m.abandon(p);
}
return None;
}
let res = expr_bp_inner(p, m, r, bp);
p.decrease_recursion_limit();
res
}

fn expr_bp_inner(
p: &mut Parser<'_>,
m: Option<Marker>,
r: Restrictions,
bp: u8,
) -> Option<(CompletedMarker, BlockLike)> {
let m = m.unwrap_or_else(|| {
let m = p.start();
Expand Down Expand Up @@ -315,7 +332,7 @@ fn expr_bp(

// test binop_resets_statementness
// fn f() { v = {1}&2; }
expr_bp(p, None, Restrictions { prefer_stmt: false, ..r }, op_bp);
expr_bp_inner(p, None, Restrictions { prefer_stmt: false, ..r }, op_bp);
lhs = m.complete(p, if is_range { RANGE_EXPR } else { BIN_EXPR });
}
Some((lhs, BlockLike::NotBlock))
Expand Down
18 changes: 18 additions & 0 deletions crates/parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ pub(crate) struct Parser<'t> {
/// into this vec, keeping `Event` itself a flat 8-byte enum.
errors: Vec<String>,
steps: Cell<u32>,
depth: u32,
}

const PARSER_STEP_LIMIT: usize = if cfg!(debug_assertions) { 150_000 } else { 15_000_000 };
const PARSER_DEPTH_LIMIT: u32 = 256;

impl<'t> Parser<'t> {
pub(super) fn new(inp: &'t Input) -> Parser<'t> {
Expand All @@ -49,6 +51,7 @@ impl<'t> Parser<'t> {
events: Vec::with_capacity(2 * inp.len()),
errors: Vec::new(),
steps: Cell::new(0),
depth: 0,
}
}

Expand All @@ -75,6 +78,21 @@ impl<'t> Parser<'t> {
self.inp.kind(self.pos + n)
}

pub(crate) fn check_recursion_limit(&mut self) -> bool {
if self.depth > PARSER_DEPTH_LIMIT {
self.error("recursion limit exceeded");
return false;
}
self.depth += 1;
true
}

pub(crate) fn decrease_recursion_limit(&mut self) {
if self.depth > 0 {
self.depth -= 1;
}
}

/// Checks if the current token is `kind`.
pub(crate) fn at(&self, kind: SyntaxKind) -> bool {
self.nth_at(0, kind)
Expand Down
Loading
Loading