Skip to content

Commit 5be65b9

Browse files
committed
Fix macro hygiene when calling the macro in the same module.
Closes #14893
1 parent 17a293a commit 5be65b9

File tree

6 files changed

+36
-16
lines changed

6 files changed

+36
-16
lines changed

base/linalg/factorization.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ transpose(F::Factorization) = error("transpose not implemented for $(typeof(F))"
99
ctranspose(F::Factorization) = error("ctranspose not implemented for $(typeof(F))")
1010

1111
macro assertposdef(A, info)
12-
:(($info)==0 ? $A : throw(PosDefException($info)))
12+
:($(esc(info)) == 0 ? $(esc(A)) : throw(PosDefException($(esc(info)))))
1313
end
1414

1515
macro assertnonsingular(A, info)
16-
:(($info)==0 ? $A : throw(SingularException($info)))
16+
:($(esc(info)) == 0 ? $(esc(A)) : throw(SingularException($(esc(info)))))
1717
end
1818

1919

base/sparse/cholmod_h.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,5 @@ type CHOLMODException <: Exception
7575
end
7676

7777
macro isok(A)
78-
:($A == TRUE || throw(CHOLMODException("")))
78+
:($(esc(A)) == TRUE || throw(CHOLMODException("")))
7979
end

base/sparse/umfpack.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function umferror(status::Integer)
5454
end
5555

5656
macro isok(A)
57-
:(umferror($A))
57+
:(umferror($(esc(A))))
5858
end
5959

6060
# check the size of SuiteSparse_long

src/ast.c

+9-8
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,15 @@ value_t fl_invoke_julia_macro(fl_context_t *fl_ctx, value_t *args, uint32_t narg
168168
fl_gc_handle(fl_ctx, &scm);
169169
value_t scmresult;
170170
jl_module_t *defmod = jl_gf_mtable(f)->module;
171-
if (defmod == NULL || defmod == jl_current_module) {
172-
scmresult = fl_cons(fl_ctx, scm, fl_ctx->F);
173-
}
174-
else {
175-
value_t opaque = cvalue(fl_ctx, jl_ast_ctx(fl_ctx)->jvtype, sizeof(void*));
176-
*(jl_value_t**)cv_data((cvalue_t*)ptr(opaque)) = (jl_value_t*)defmod;
177-
scmresult = fl_cons(fl_ctx, scm, opaque);
178-
}
171+
assert(defmod);
172+
/* if (defmod == NULL) { */
173+
/* scmresult = fl_cons(fl_ctx, scm, fl_ctx->F); */
174+
/* } */
175+
/* else { */
176+
value_t opaque = cvalue(fl_ctx, jl_ast_ctx(fl_ctx)->jvtype, sizeof(void*));
177+
*(jl_value_t**)cv_data((cvalue_t*)ptr(opaque)) = (jl_value_t*)defmod;
178+
scmresult = fl_cons(fl_ctx, scm, opaque);
179+
/* } */
179180
fl_free_gc_handles(fl_ctx, 1);
180181

181182
JL_GC_POP();

src/macroexpand.scm

+1-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@
421421
(error (cadr form)))
422422
(let ((form (car form))
423423
(m (cdr form)))
424-
;; m is the macro's def module, or #f if def env === use env
424+
;; m is the macro's def module
425425
(rename-symbolic-labels
426426
(julia-expand-macros
427427
(resolve-expansion-vars form m))))))

test/core.jl

+22-3
Original file line numberDiff line numberDiff line change
@@ -3320,7 +3320,7 @@ end
33203320

33213321
# issue 13855
33223322
macro m13855()
3323-
Expr(:localize, :(() -> x))
3323+
Expr(:localize, :(() -> $(esc(:x))))
33243324
end
33253325
@noinline function foo13855(x)
33263326
@m13855()
@@ -3464,11 +3464,11 @@ module TestMacroGlobalFunction
34643464
macro makefn(f,g)
34653465
quote
34663466
global $(f)
3467-
function $(f)(x)
3467+
function $(esc(f))(x)
34683468
x+1
34693469
end
34703470
global $(g)
3471-
$(g)(x) = x+2
3471+
$(esc(g))(x) = x+2
34723472
end
34733473
end
34743474
@makefn ff gg
@@ -3757,3 +3757,22 @@ function f15809()
37573757
end
37583758
f15809()
37593759
@test g15809(2) === Int
3760+
3761+
# issue
3762+
module M14893
3763+
x = 14893
3764+
macro m14893()
3765+
:x
3766+
end
3767+
function f14893()
3768+
x = 1
3769+
@m14893
3770+
end
3771+
end
3772+
function f14893()
3773+
x = 2
3774+
M14893.@m14893
3775+
end
3776+
3777+
@test f14893() == 14893
3778+
@test M14893.f14893() == 14893

0 commit comments

Comments
 (0)