Skip to content

Commit 4f1af7f

Browse files
authored
Allow opting out of PartialOpaque support via Expr(:opaque_closure, ...) (#55068)
This can be useful for when an OpaqueClosure is desired, e.g., as an invalidation barrier or when you know it passes through an inference barrier naturally (in my case, a mutable type). This is intentionally left undocumented for now.
1 parent cebfd7b commit 4f1af7f

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

base/opaque_closure.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ the argument type may be fixed length even if the function is variadic.
1818
This interface is experimental and subject to change or removal without notice.
1919
"""
2020
macro opaque(ex)
21-
esc(Expr(:opaque_closure, nothing, nothing, nothing, ex))
21+
esc(Expr(:opaque_closure, nothing, nothing, nothing, #= allow_partial =# true, ex))
2222
end
2323

2424
macro opaque(ty, ex)
@@ -34,7 +34,7 @@ macro opaque(ty, ex)
3434
end
3535
AT = (AT !== :_) ? AT : nothing
3636
RT = (RT !== :_) ? RT : nothing
37-
return esc(Expr(:opaque_closure, AT, RT, RT, ex))
37+
return esc(Expr(:opaque_closure, AT, RT, RT, #= allow_partial =# true, ex))
3838
end
3939

4040
# OpaqueClosure construction from pre-inferred CodeInfo/IRCode

src/julia-syntax.scm

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2435,7 +2435,8 @@
24352435
(let* ((argt (something (list (expand-forms (cadr e)) #f)))
24362436
(rt_lb (something (list (expand-forms (caddr e)) #f)))
24372437
(rt_ub (something (list (expand-forms (cadddr e)) #f)))
2438-
(F (caddddr e))
2438+
(allow-partial (caddddr e))
2439+
(F (cadddddr e))
24392440
(isva (let* ((arglist (function-arglist F))
24402441
(lastarg (and (pair? arglist) (last arglist))))
24412442
(if (and argt (any (lambda (arg)
@@ -2460,7 +2461,7 @@
24602461
(let* ((argtype (foldl (lambda (var ex) `(call (core UnionAll) ,var ,ex))
24612462
(expand-forms `(curly (core Tuple) ,@argtypes))
24622463
(reverse tvars))))
2463-
`(_opaque_closure ,(or argt argtype) ,rt_lb ,rt_ub ,isva ,(length argtypes) ,functionloc ,lam))))
2464+
`(_opaque_closure ,(or argt argtype) ,rt_lb ,rt_ub ,isva ,(length argtypes) ,allow-partial ,functionloc ,lam))))
24642465

24652466
'block
24662467
(lambda (e)
@@ -4028,7 +4029,8 @@ f(x) = yt(x)
40284029
((_opaque_closure)
40294030
(let* ((isva (car (cddddr e)))
40304031
(nargs (cadr (cddddr e)))
4031-
(functionloc (caddr (cddddr e)))
4032+
(allow-partial (caddr (cddddr e)))
4033+
(functionloc (cadddr (cddddr e)))
40324034
(lam2 (last e))
40334035
(vis (lam:vinfo lam2))
40344036
(cvs (map car (cadr vis))))
@@ -4040,7 +4042,7 @@ f(x) = yt(x)
40404042
v)))
40414043
cvs)))
40424044
`(new_opaque_closure
4043-
,(cadr e) ,(or (caddr e) '(call (core apply_type) (core Union))) ,(or (cadddr e) '(core Any)) (true)
4045+
,(cadr e) ,(or (caddr e) '(call (core apply_type) (core Union))) ,(or (cadddr e) '(core Any)) ,allow-partial
40444046
(opaque_closure_method (null) ,nargs ,isva ,functionloc ,(convert-lambda lam2 (car (lam:args lam2)) #f '() (symbol-to-idx-map cvs)))
40454047
,@var-exprs))))
40464048
((method)

test/precompile.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,6 +2012,13 @@ precompile_test_harness("Generated Opaque") do load_path
20122012
Expr(:opaque_closure_method, nothing, 0, false, lno, ci))
20132013
end
20142014
@assert oc_re_generated_no_partial()() === 1
2015+
@generated function oc_re_generated_no_partial_macro()
2016+
AT = nothing
2017+
RT = nothing
2018+
allow_partial = false # makes this legal to generate during pre-compile
2019+
return Expr(:opaque_closure, AT, RT, RT, allow_partial, :(()->const_int_barrier()))
2020+
end
2021+
@assert oc_re_generated_no_partial_macro()() === 1
20152022
end
20162023
""")
20172024
Base.compilecache(Base.PkgId("GeneratedOpaque"))

0 commit comments

Comments
 (0)