From 0c5372f68b6d80cd59ebb6d5410633a1fdfe011c Mon Sep 17 00:00:00 2001 From: Keno Fischer Date: Sat, 15 Feb 2025 13:34:23 -0500 Subject: [PATCH] generated: Switch resolution module back to what it was before (#57419) This addresses post-commit review https://github.com/JuliaLang/julia/pull/57230#discussion_r1939750418. This change was left-over from before I decided to also change the type of the `source` argument (at which point `source.module` was unavailable in the function). This module was supposed to be the same, but it turns out that both the julia tests and several packages use this code manually and use different modules for the two places. Use the same one we used before (which is probably more correct anyway) to fix #57417 --- base/expr.jl | 2 +- test/staged.jl | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/base/expr.jl b/base/expr.jl index d71723ee26f1f..8ae394e122443 100644 --- a/base/expr.jl +++ b/base/expr.jl @@ -1694,6 +1694,6 @@ function (g::Core.GeneratedFunctionStub)(world::UInt, source::Method, @nospecial Expr(:meta, :pop_loc)))) spnames = g.spnames return generated_body_to_codeinfo(spnames === Core.svec() ? lam : Expr(Symbol("with-static-parameters"), lam, spnames...), - typename(typeof(g.gen)).module, + source.module, source.isva) end diff --git a/test/staged.jl b/test/staged.jl index 6811bc05a9b68..d416b0f9a22f0 100644 --- a/test/staged.jl +++ b/test/staged.jl @@ -449,3 +449,31 @@ end @test first(only(code_typed((Int,Int)) do x, y; @inline overdub54341(x, y); end)) isa Core.CodeInfo @test first(only(code_typed((Int,)) do x; @inline overdub54341(x, 1); end)) isa Core.CodeInfo @test_throws "Wrong number of arguments" overdub54341(1, 2, 3) + +# Test the module resolution scope of generated methods that are type constructors +module GeneratedScope57417 + using Test + import ..generate_lambda_ex + const x = 1 + struct Generator; end + @generated (::Generator)() = :x + f(x::Int) = 1 + module OtherModule + import ..f + const x = 2 + @generated f(::Float64) = :x + end + import .OtherModule: f + @test Generator()() == 1 + @test f(1.0) == 2 + + function g_generator(world::UInt, source::Method, _) + return generate_lambda_ex(world, source, (:g,), (), :(return x)) + end + + @eval function g() + $(Expr(:meta, :generated, g_generator)) + $(Expr(:meta, :generated_only)) + end + @test g() == 1 +end