Skip to content

Commit

Permalink
Fix macro hygiene when calling the macro in the same module.
Browse files Browse the repository at this point in the history
Closes #14893
  • Loading branch information
yuyichao committed Apr 12, 2016
1 parent 17a293a commit 5be65b9
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
4 changes: 2 additions & 2 deletions base/linalg/factorization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ transpose(F::Factorization) = error("transpose not implemented for $(typeof(F))"
ctranspose(F::Factorization) = error("ctranspose not implemented for $(typeof(F))")

macro assertposdef(A, info)
:(($info)==0 ? $A : throw(PosDefException($info)))
:($(esc(info)) == 0 ? $(esc(A)) : throw(PosDefException($(esc(info)))))
end

macro assertnonsingular(A, info)
:(($info)==0 ? $A : throw(SingularException($info)))
:($(esc(info)) == 0 ? $(esc(A)) : throw(SingularException($(esc(info)))))
end


Expand Down
2 changes: 1 addition & 1 deletion base/sparse/cholmod_h.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ type CHOLMODException <: Exception
end

macro isok(A)
:($A == TRUE || throw(CHOLMODException("")))
:($(esc(A)) == TRUE || throw(CHOLMODException("")))
end
2 changes: 1 addition & 1 deletion base/sparse/umfpack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function umferror(status::Integer)
end

macro isok(A)
:(umferror($A))
:(umferror($(esc(A))))
end

# check the size of SuiteSparse_long
Expand Down
17 changes: 9 additions & 8 deletions src/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,15 @@ value_t fl_invoke_julia_macro(fl_context_t *fl_ctx, value_t *args, uint32_t narg
fl_gc_handle(fl_ctx, &scm);
value_t scmresult;
jl_module_t *defmod = jl_gf_mtable(f)->module;
if (defmod == NULL || defmod == jl_current_module) {
scmresult = fl_cons(fl_ctx, scm, fl_ctx->F);
}
else {
value_t opaque = cvalue(fl_ctx, jl_ast_ctx(fl_ctx)->jvtype, sizeof(void*));
*(jl_value_t**)cv_data((cvalue_t*)ptr(opaque)) = (jl_value_t*)defmod;
scmresult = fl_cons(fl_ctx, scm, opaque);
}
assert(defmod);
/* if (defmod == NULL) { */
/* scmresult = fl_cons(fl_ctx, scm, fl_ctx->F); */
/* } */
/* else { */
value_t opaque = cvalue(fl_ctx, jl_ast_ctx(fl_ctx)->jvtype, sizeof(void*));
*(jl_value_t**)cv_data((cvalue_t*)ptr(opaque)) = (jl_value_t*)defmod;
scmresult = fl_cons(fl_ctx, scm, opaque);
/* } */
fl_free_gc_handles(fl_ctx, 1);

JL_GC_POP();
Expand Down
2 changes: 1 addition & 1 deletion src/macroexpand.scm
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@
(error (cadr form)))
(let ((form (car form))
(m (cdr form)))
;; m is the macro's def module, or #f if def env === use env
;; m is the macro's def module
(rename-symbolic-labels
(julia-expand-macros
(resolve-expansion-vars form m))))))
Expand Down
25 changes: 22 additions & 3 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3320,7 +3320,7 @@ end

# issue 13855
macro m13855()
Expr(:localize, :(() -> x))
Expr(:localize, :(() -> $(esc(:x))))
end
@noinline function foo13855(x)
@m13855()
Expand Down Expand Up @@ -3464,11 +3464,11 @@ module TestMacroGlobalFunction
macro makefn(f,g)
quote
global $(f)
function $(f)(x)
function $(esc(f))(x)
x+1
end
global $(g)
$(g)(x) = x+2
$(esc(g))(x) = x+2
end
end
@makefn ff gg
Expand Down Expand Up @@ -3757,3 +3757,22 @@ function f15809()
end
f15809()
@test g15809(2) === Int

# issue
module M14893
x = 14893
macro m14893()
:x
end
function f14893()
x = 1
@m14893
end
end
function f14893()
x = 2
M14893.@m14893
end

@test f14893() == 14893
@test M14893.f14893() == 14893

0 comments on commit 5be65b9

Please sign in to comment.