From a2f27efb7f21665603c3d78750674275b8cd2c77 Mon Sep 17 00:00:00 2001 From: simontreanor Date: Sat, 17 Oct 2026 22:49:25 +0100 Subject: [PATCH] =?UTF-8?q?diagnostics:=20the=20def=20hint=20suggests=20`l?= =?UTF-8?q?et=20greet=20=5F=20=3D=20=E2=80=A6`=20for=20a=20parameterless?= =?UTF-8?q?=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `def greet():` was answered with `let greet () = …`, which does not parse: Pyfun has no `()` parameter pattern, and a function that takes nothing is written `let greet _ = …` and called as `greet ()`. The test now also checks that every suggested spelling parses. --- src/parser/mod.rs | 5 +++-- tests/roundtrip.rs | 5 ++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 0c29474..270dd5a 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -400,8 +400,9 @@ impl Parser { Tok::Eq | Tok::Colon if depth == 0 => { let mut shape = vec!["let".to_string(), name.clone()]; if parenthesized && params.is_empty() { - // `def greet():` takes no arguments, which Pyfun spells `()`. - params.push("()".to_string()); + // `def greet():` takes no arguments: Pyfun gives it one it + // ignores (`let greet _ = …`), called as `greet ()`. + params.push("_".to_string()); } shape.extend(params); return Some(ParseError { diff --git a/tests/roundtrip.rs b/tests/roundtrip.rs index 653a20d..dd40c4c 100644 --- a/tests/roundtrip.rs +++ b/tests/roundtrip.rs @@ -781,7 +781,7 @@ fn def_is_answered_with_the_let_spelling() { "def area(w: int, h: int) -> int:\n w * h", "write `let area w h = …`", ), - ("def greet():\n print \"hi\"", "write `let greet () = …`"), + ("def greet():\n print \"hi\"", "write `let greet _ = …`"), ("let f x =\n def g y = y + 1\n g x", "write `let g y = …`"), ] { let err = parse(source).unwrap_err(); @@ -791,6 +791,9 @@ fn def_is_answered_with_the_let_spelling() { err.message() ); } + // Every suggested spelling parses. + assert!(parse("let area w h = w * h").is_ok()); + assert!(parse("let greet _ =\n print \"hi\"").is_ok()); // `def` is still an ordinary identifier: a function the program calls `def`. assert!(parse("let def x = x + 1\nprint (def 2)").is_ok()); }