Skip to content

Commit cb907f3

Browse files
committed
Remove premature lowering of return without arguments
`return` can have zero arguments - indicate this with empty children, rather than with an invisible `K"nothing"` token.
1 parent f7c7488 commit cb907f3

File tree

6 files changed

+11
-8
lines changed

6 files changed

+11
-8
lines changed

src/expr.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ function _to_expr(node::SyntaxNode; iteration_spec=false, need_linenodes=true,
303303
args[1] = Expr(headsym, args[1].args...)
304304
headsym = :const
305305
end
306+
elseif headsym == :return && isempty(args)
307+
push!(args, nothing)
306308
end
307309
return Expr(headsym, args...)
308310
end

src/kinds.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ const _kind_names =
8383
"CmdString"
8484
"true"
8585
"false"
86-
"nothing" # A literal `nothing`
8786
"END_LITERAL"
8887

8988
"BEGIN_DELIMITERS"

src/parser.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,9 +1971,8 @@ function parse_resword(ps::ParseState)
19711971
bump(ps, TRIVIA_FLAG)
19721972
k = peek(ps)
19731973
if k == K"NewlineWs" || is_closing_token(ps, k)
1974-
# return\nx ==> (return nothing)
1975-
# return) ==> (return nothing)
1976-
bump_invisible(ps, K"nothing")
1974+
# return\nx ==> (return)
1975+
# return) ==> (return)
19771976
else
19781977
# return x ==> (return x)
19791978
# return x,y ==> (return (tuple x y))

src/syntax_tree.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ function SyntaxNode(source::SourceFile, raw::GreenNode{SyntaxHead}, position::In
107107
isempty(val_range) ?
108108
Symbol(untokenize(k)) : # synthetic invisible tokens
109109
Symbol(normalize_identifier(val_str))
110-
elseif k == K"nothing"
111-
nothing
112110
elseif k == K"error"
113111
ErrorVal()
114112
elseif k == K"MacroName"

test/expr.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,9 @@
302302
@test parse(Expr, "\n\"x\" f") ==
303303
Expr(:macrocall, GlobalRef(Core, Symbol("@doc")), LineNumberNode(2), "x", :f)
304304
end
305+
306+
@testset "return" begin
307+
@test parse(Expr, "return x") == Expr(:return, :x)
308+
@test parse(Expr, "return") == Expr(:return, nothing)
309+
end
305310
end

test/parser.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,8 @@ tests = [
475475
"struct A end" => "(struct false A (block))" => Expr(:struct, false, :A, Expr(:block))
476476
"struct try end" => "(struct false (error (try)) (block))"
477477
# return
478-
"return\nx" => "(return nothing)"
479-
"return)" => "(return nothing)"
478+
"return\nx" => "(return)"
479+
"return)" => "(return)"
480480
"return x" => "(return x)"
481481
"return x,y" => "(return (tuple x y))"
482482
# break/continue

0 commit comments

Comments
 (0)