Skip to content

Commit 116ee31

Browse files
committed
Remove boxed closure captures warned about by JET
* Use Ref to avoid triggering boxing of captures via closure variable assignment. * Use let blocks for temporary ParseState transitions to avoid some closures.
1 parent 24741b1 commit 116ee31

File tree

1 file changed

+49
-45
lines changed

1 file changed

+49
-45
lines changed

src/parser.jl

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ function normal_context(ps::ParseState)
5353
whitespace_newline=false)
5454
end
5555

56-
function with_space_sensitive(f::Function, ps::ParseState)
57-
f(ParseState(ps,
58-
space_sensitive=true,
59-
whitespace_newline=false))
56+
function with_space_sensitive(ps::ParseState)
57+
ParseState(ps,
58+
space_sensitive=true,
59+
whitespace_newline=false)
6060
end
6161

6262
# Convenient wrappers for ParseStream
@@ -1176,20 +1176,20 @@ function parse_unary_call(ps::ParseState)
11761176
mark_before_paren = position(ps)
11771177
bump(ps, TRIVIA_FLAG) # (
11781178
initial_semi = peek(ps) == K";"
1179-
is_call = false
1180-
is_block = false
1179+
is_call = Ref(false)
1180+
is_block = Ref(false)
11811181
parse_brackets(ps, K")") do had_commas, had_splat, num_semis, num_subexprs
1182-
is_call = had_commas || had_splat || initial_semi
1183-
is_block = !is_call && num_semis > 0
1182+
is_call[] = had_commas || had_splat || initial_semi
1183+
is_block[] = !is_call[] && num_semis > 0
11841184
bump_closing_token(ps, K")")
1185-
return (needs_parameters=is_call,
1186-
eq_is_kw_before_semi=is_call,
1187-
eq_is_kw_after_semi=is_call)
1185+
return (needs_parameters=is_call[],
1186+
eq_is_kw_before_semi=is_call[],
1187+
eq_is_kw_after_semi=is_call[])
11881188
end
11891189

11901190
# The precedence between unary + and any following infix ^ depends on
11911191
# whether the parens are a function call or not
1192-
if is_call
1192+
if is_call[]
11931193
if preceding_whitespace(t2)
11941194
# Whitespace not allowed before prefix function call bracket
11951195
# + (a,b) ==> (call + (error) a b)
@@ -1211,7 +1211,7 @@ function parse_unary_call(ps::ParseState)
12111211
parse_factor_with_initial_ex(ps, mark)
12121212
else
12131213
# Unary function calls with brackets as grouping, not an arglist
1214-
if is_block
1214+
if is_block[]
12151215
# +(a;b) ==> (call + (block a b))
12161216
emit(ps, mark_before_paren, K"block")
12171217
end
@@ -1400,7 +1400,7 @@ function parse_call_chain(ps::ParseState, mark, is_macrocall=false)
14001400
# a().@x y ==> (macrocall (error (. (call a) (quote x))) y)
14011401
# [@foo "x"] ==> (vect (macrocall @foo "x"))
14021402
finish_macroname(ps, mark, valid_macroname, macro_name_position)
1403-
with_space_sensitive(ps) do ps
1403+
let ps = with_space_sensitive(ps)
14041404
# Space separated macro arguments
14051405
# A.@foo a b ==> (macrocall (. A (quote @foo)) a b)
14061406
# @A.foo a b ==> (macrocall (. A (quote @foo)) a b)
@@ -1779,8 +1779,10 @@ function parse_resword(ps::ParseState)
17791779
bump(ps, TRIVIA_FLAG)
17801780
@check peek(ps) == K"type"
17811781
bump(ps, TRIVIA_FLAG)
1782-
with_space_sensitive(parse_subtype_spec, ps)
1783-
with_space_sensitive(parse_cond, ps)
1782+
let ps = with_space_sensitive(ps)
1783+
parse_subtype_spec(ps)
1784+
parse_cond(ps)
1785+
end
17841786
bump_semicolon_trivia(ps)
17851787
bump_closing_token(ps, K"end")
17861788
emit(ps, mark, K"primitive")
@@ -1975,7 +1977,7 @@ function parse_function(ps::ParseState)
19751977
word = peek(ps)
19761978
@check word in KSet`macro function`
19771979
is_function = word == K"function"
1978-
is_anon_func::Bool = false
1980+
is_anon_func = false
19791981
bump(ps, TRIVIA_FLAG)
19801982
bump_trivia(ps)
19811983

@@ -2005,13 +2007,15 @@ function parse_function(ps::ParseState)
20052007
#
20062008
# The flisp parser disambiguates this case quite differently,
20072009
# producing less consistent syntax for anonymous functions.
2010+
is_anon_func_ = Ref(is_anon_func)
20082011
parse_brackets(ps, K")") do _, _, _, _
20092012
bump_closing_token(ps, K")")
2010-
is_anon_func = peek(ps) != K"("
2011-
return (needs_parameters = is_anon_func,
2012-
eq_is_kw_before_semi = is_anon_func,
2013-
eq_is_kw_after_semi = is_anon_func)
2013+
is_anon_func_[] = peek(ps) != K"("
2014+
return (needs_parameters = is_anon_func_[],
2015+
eq_is_kw_before_semi = is_anon_func_[],
2016+
eq_is_kw_after_semi = is_anon_func_[])
20142017
end
2018+
is_anon_func = is_anon_func_[]
20152019
if is_anon_func
20162020
# function (x) body end ==> (function (tuple x) (block body))
20172021
# function (x,y) end ==> (function (tuple x y) (block))
@@ -2209,8 +2213,8 @@ function parse_macro_name(ps::ParseState)
22092213
# @! x ==> (macrocall @! x)
22102214
# @.. x ==> (macrocall @.. x)
22112215
# @$ x ==> (macrocall @$ x)
2212-
with_space_sensitive(ps) do ps1
2213-
parse_atom(ps1, false)
2216+
let ps = with_space_sensitive(ps)
2217+
parse_atom(ps, false)
22142218
end
22152219
end
22162220
end
@@ -2263,13 +2267,14 @@ function parse_imports(ps::ParseState)
22632267
# import A, y ==> (import (. A) (. y))
22642268
# import A: x, y ==> (import (: (. A) (. x) (. y)))
22652269
# import A: +, == ==> (import (: (. A) (. +) (. ==)))
2266-
parse_comma_separated(ps, ps1->parse_import(ps1, word, has_import_prefix))
2270+
has_import_prefix_ = has_import_prefix
2271+
parse_comma_separated(ps, ps1->parse_import(ps1, word, has_import_prefix_))
22672272
if peek(ps) == K":"
22682273
# Error recovery
22692274
# import A: x, B: y ==> (import (: (. A) (. x) (. B) (error-t (. y))))
22702275
emark = position(ps)
22712276
bump(ps, TRIVIA_FLAG)
2272-
parse_comma_separated(ps, ps1->parse_import(ps1, word, has_import_prefix))
2277+
parse_comma_separated(ps, ps1->parse_import(ps1, word, has_import_prefix_))
22732278
emit(ps, emark, K"error", TRIVIA_FLAG,
22742279
error="`:` can only be used when importing a single module. Split imports into multiple lines")
22752280
end
@@ -2436,7 +2441,7 @@ function parse_iteration_spec(ps::ParseState)
24362441
mark = position(ps)
24372442
k = peek(ps)
24382443
# Handle `outer` contextual keyword
2439-
with_space_sensitive(parse_pipe_lt, ps)
2444+
parse_pipe_lt(with_space_sensitive(ps))
24402445
if peek_behind(ps).orig_kind == K"outer"
24412446
if peek_skip_newline_in_gen(ps) in KSet`= in ∈`
24422447
# Not outer keyword
@@ -2465,19 +2470,18 @@ end
24652470

24662471
# flisp: parse-space-separated-exprs
24672472
function parse_space_separated_exprs(ps::ParseState)
2468-
with_space_sensitive(ps) do ps
2469-
n_sep = 0
2470-
while true
2471-
k = peek(ps)
2472-
if is_closing_token(ps, k) || k == K"NewlineWs" ||
2473-
(ps.for_generator && k == K"for")
2474-
break
2475-
end
2476-
parse_eq(ps)
2477-
n_sep += 1
2473+
ps = with_space_sensitive(ps)
2474+
n_sep = 0
2475+
while true
2476+
k = peek(ps)
2477+
if is_closing_token(ps, k) || k == K"NewlineWs" ||
2478+
(ps.for_generator && k == K"for")
2479+
break
24782480
end
2479-
return n_sep
2481+
parse_eq(ps)
2482+
n_sep += 1
24802483
end
2484+
return n_sep
24812485
end
24822486

24832487
# like parse-arglist, but with `for` parsed as a generator
@@ -2858,18 +2862,18 @@ function parse_paren(ps::ParseState, check_identifiers=true)
28582862
# Deal with all other cases of tuple or block syntax via the generic
28592863
# parse_brackets
28602864
initial_semi = peek(ps) == K";"
2861-
is_tuple = false
2862-
is_block = false
2865+
is_tuple = Ref(false)
2866+
is_block = Ref(false)
28632867
parse_brackets(ps, K")") do had_commas, had_splat, num_semis, num_subexprs
2864-
is_tuple = had_commas || (had_splat && num_semis >= 1) ||
2868+
is_tuple[] = had_commas || (had_splat && num_semis >= 1) ||
28652869
(initial_semi && (num_semis == 1 || num_subexprs > 0))
2866-
is_block = num_semis > 0
2870+
is_block[] = num_semis > 0
28672871
bump_closing_token(ps, K")")
2868-
return (needs_parameters=is_tuple,
2872+
return (needs_parameters=is_tuple[],
28692873
eq_is_kw_before_semi=false,
2870-
eq_is_kw_after_semi=is_tuple)
2874+
eq_is_kw_after_semi=is_tuple[])
28712875
end
2872-
if is_tuple
2876+
if is_tuple[]
28732877
# Tuple syntax with commas
28742878
# (x,) ==> (tuple x)
28752879
# (x,y) ==> (tuple x y)
@@ -2886,7 +2890,7 @@ function parse_paren(ps::ParseState, check_identifiers=true)
28862890
# (a; b; c,d) ==> (tuple a (parameters b (parameters c d)))
28872891
# (a=1, b=2; c=3) ==> (tuple (= a 1) (= b 2) (parameters (kw c 3)))
28882892
emit(ps, mark, K"tuple")
2889-
elseif is_block
2893+
elseif is_block[]
28902894
# Blocks
28912895
# (;;) ==> (block)
28922896
# (a=1;) ==> (block (= a 1))

0 commit comments

Comments
 (0)