You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Allow macrocall in function def syntax
Goes with JuliaLang/julia#55040
* Update src/parser.jl
Co-authored-by: Claire Foster <aka.c42f@gmail.com>
* Test to cover function declaration with `var""` syntax
* Make `function (@f(x)) body end` an ambiguity error
This case is ambiguous as it might be either one of the following;
require the user to explicitly disambiguate between them
```
function (@f(x),)
body
end
function @f(x)
body
end
```
For the same reasons, `function ($f) body end` is also ambiguous.
Also fix parsing of `function (f(x),) end` to correctly emit a tuple.
---------
Co-authored-by: Claire Foster <aka.c42f@gmail.com>
@@ -2146,7 +2147,14 @@ function parse_function_signature(ps::ParseState, is_function::Bool)
2146
2147
# function (x,y) end ==> (function (tuple-p x y) (block))
2147
2148
# function (x=1) end ==> (function (tuple-p (= x 1)) (block))
2148
2149
# function (;x=1) end ==> (function (tuple-p (parameters (= x 1))) (block))
2150
+
# function (f(x),) end ==> (function (tuple-p (call f x)) (block))
2151
+
ambiguous_parens = opts.maybe_grouping_parens &&
2152
+
peek_behind(ps).kind inKSet"macrocall $"
2149
2153
emit(ps, mark, K"tuple", PARENS_FLAG)
2154
+
if ambiguous_parens
2155
+
# Got something like `(@f(x))`. Is it anon `(@f(x),)` or named sig `@f(x)` ??
2156
+
emit(ps, mark, K"error", error="Ambiguous signature. Add a trailing comma if this is a 1-argument anonymous function; remove parentheses if this is a macro call acting as function signature.")
2157
+
end
2150
2158
elseif is_empty_tuple
2151
2159
# Weird case which is consistent with parse_paren but will be
2152
2160
# rejected in lowering
@@ -2175,19 +2183,23 @@ function parse_function_signature(ps::ParseState, is_function::Bool)
Copy file name to clipboardExpand all lines: test/diagnostics.jl
+3Lines changed: 3 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -41,6 +41,9 @@ end
41
41
@testdiagnostic("\n+ (x, y)") ==
42
42
Diagnostic(3, 3, :error, "whitespace not allowed between prefix function call and argument list")
43
43
44
+
@testdiagnostic("function (\$f) body end") ==
45
+
Diagnostic(10, 13, :error, "Ambiguous signature. Add a trailing comma if this is a 1-argument anonymous function; remove parentheses if this is a macro call acting as function signature.")
46
+
44
47
@testdiagnostic("A.@B.x", only_first=true) ==
45
48
Diagnostic(3, 4, :error, "`@` must appear on first or last macro name component")
((v=v"1.12",), "function @callmemacro(a::T, b::T) where T <: Int64\n3\nend") =>"(function (where (macrocall-p @callmemacro (::-i a T) (::-i b T)) (<: T Int64)) (block 3))"
623
+
((v=v"1.12",), "function @callmemacro(a::Int, b::Int, c::Int)::Float64\n4\nend") =>"(function (::-i (macrocall-p @callmemacro (::-i a Int) (::-i b Int) (::-i c Int)) Float64) (block 4))"
0 commit comments