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() {
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