From 8ab4cacf75c3ea1b0c8e33e3fca479ccf8da24f6 Mon Sep 17 00:00:00 2001 From: Asuka Minato Date: Sat, 18 Jul 2026 11:42:04 +0900 Subject: [PATCH] fix --- pyrefly/lib/binding/binding.rs | 4 ++++ pyrefly/lib/binding/stmt.rs | 2 +- pyrefly/lib/test/named_tuple.rs | 12 ++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/pyrefly/lib/binding/binding.rs b/pyrefly/lib/binding/binding.rs index c61e8808a1..4164a534f1 100644 --- a/pyrefly/lib/binding/binding.rs +++ b/pyrefly/lib/binding/binding.rs @@ -902,6 +902,8 @@ pub enum Key { StmtExpr(TextRange), /// I am an expression that appears in a `with` context. ContextExpr(TextRange), + /// I am the context manager value for a `with` item without an assignment target. + ContextValue(TextRange), /// I am the result of joining several branches. Phi(Box<(Name, TextRange)>), /// I am the result of narrowing a type. The two ranges are the range at which the operation is @@ -960,6 +962,7 @@ impl Ranged for Key { Self::InvalidTarget(r) => *r, Self::StmtExpr(r) => *r, Self::ContextExpr(r) => *r, + Self::ContextValue(r) => *r, Self::Phi(x) => x.1, Self::Narrow(x) => x.1, Self::Anywhere(x) => x.1, @@ -990,6 +993,7 @@ impl DisplayWith for Key { Self::InvalidTarget(r) => write!(f, "Key::InvalidTarget({})", ctx.display(r)), Self::StmtExpr(r) => write!(f, "Key::StmtExpr({})", ctx.display(r)), Self::ContextExpr(r) => write!(f, "Key::ContextExpr({})", ctx.display(r)), + Self::ContextValue(r) => write!(f, "Key::ContextValue({})", ctx.display(r)), Self::Phi(x) => write!(f, "Key::Phi({} {})", x.0, ctx.display(&x.1)), Self::Narrow(x) => { write!( diff --git a/pyrefly/lib/binding/stmt.rs b/pyrefly/lib/binding/stmt.rs index b464186d66..e23dc5c739 100644 --- a/pyrefly/lib/binding/stmt.rs +++ b/pyrefly/lib/binding/stmt.rs @@ -1368,7 +1368,7 @@ impl<'a> BindingsBuilder<'a> { self.bind_target_no_expr(&mut opts, &make_binding); } else { self.insert_binding( - Key::Anon(item_range), + Key::ContextValue(item_range), Binding::ContextValue(None, context_idx, expr_range, kind), ); } diff --git a/pyrefly/lib/test/named_tuple.rs b/pyrefly/lib/test/named_tuple.rs index 9f27d0e092..d62ca1fe12 100644 --- a/pyrefly/lib/test/named_tuple.rs +++ b/pyrefly/lib/test/named_tuple.rs @@ -739,6 +739,18 @@ fn test_named_tuple_in_malformed_for_target() { ); } +// Regression test for https://github.com/facebook/pyrefly/issues/4177 +#[test] +fn test_named_tuple_in_malformed_with_item() { + // Keep the malformed source exact: adding inline expectations changes the unterminated string. + let _ = testcase_for_macro( + TestEnv::new(), + "from typing import NamedTuple\n\nwith NamedTuple('\n", + file!(), + line!(), + ); +} + testcase!( test_named_tuple_dynamic_fields, r#"