From c5d093395cee48f44418607c8b8f3d2aaf986cb1 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 29 Nov 2023 19:00:50 -0500 Subject: [PATCH] Avoid off-by-one error in with-item named expressions --- crates/ruff_python_parser/src/python.lalrpop | 2 +- crates/ruff_python_parser/src/python.rs | 4 ++-- ...n_parser__parser__tests__parenthesized_with_statement.snap | 2 +- .../ruff_python_parser__parser__tests__with_statement.snap | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/ruff_python_parser/src/python.lalrpop b/crates/ruff_python_parser/src/python.lalrpop index 67c49a7b644ab..98ac90fc73e65 100644 --- a/crates/ruff_python_parser/src/python.lalrpop +++ b/crates/ruff_python_parser/src/python.lalrpop @@ -1026,7 +1026,7 @@ WithItems: Vec = { // ``` // In this case, the `(` and `)` are part of the `with` statement. // The same applies to `yield` and `yield from`. - let item = if matches!(item.context_expr, ast::Expr::NamedExpr(_) | ast::Expr::Yield(_) | ast::Expr::YieldFrom(_)) { + let item = if item.optional_vars.is_none() && matches!(item.context_expr, ast::Expr::NamedExpr(_) | ast::Expr::Yield(_) | ast::Expr::YieldFrom(_)) { ast::WithItem { range: item.range().add_start(TextSize::new(1)).sub_end(TextSize::new(1)), context_expr: item.context_expr, diff --git a/crates/ruff_python_parser/src/python.rs b/crates/ruff_python_parser/src/python.rs index 3452ede4e59d8..3b2174bc69456 100644 --- a/crates/ruff_python_parser/src/python.rs +++ b/crates/ruff_python_parser/src/python.rs @@ -1,5 +1,5 @@ // auto-generated: "lalrpop 0.20.0" -// sha3: e999c9c9ca8fe5a29655244aa995b8cf4e639f0bda95099d8f2a395bc06b6408 +// sha3: c7c0b9368fa05f7d2fc1d06a665ff4232555f276a1d9569afdbc86d0905b3a2a use ruff_text_size::{Ranged, TextLen, TextRange, TextSize}; use ruff_python_ast::{self as ast, Int, IpyEscapeKind}; use crate::{ @@ -35374,7 +35374,7 @@ fn __action159< // ``` // In this case, the `(` and `)` are part of the `with` statement. // The same applies to `yield` and `yield from`. - let item = if matches!(item.context_expr, ast::Expr::NamedExpr(_) | ast::Expr::Yield(_) | ast::Expr::YieldFrom(_)) { + let item = if item.optional_vars.is_none() && matches!(item.context_expr, ast::Expr::NamedExpr(_) | ast::Expr::Yield(_) | ast::Expr::YieldFrom(_)) { ast::WithItem { range: item.range().add_start(TextSize::new(1)).sub_end(TextSize::new(1)), context_expr: item.context_expr, diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parenthesized_with_statement.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parenthesized_with_statement.snap index 8707ba33d6e21..0e703191724d8 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parenthesized_with_statement.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parenthesized_with_statement.snap @@ -347,7 +347,7 @@ expression: "parse_suite(source, \"\").unwrap()" is_async: false, items: [ WithItem { - range: 184..195, + range: 183..196, context_expr: NamedExpr( ExprNamedExpr { range: 184..190, diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__with_statement.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__with_statement.snap index de4e64d775aa1..dec15e10e878d 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__with_statement.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__with_statement.snap @@ -782,7 +782,7 @@ expression: "parse_suite(source, \"\").unwrap()" is_async: false, items: [ WithItem { - range: 382..393, + range: 381..394, context_expr: NamedExpr( ExprNamedExpr { range: 382..388,