From 9940f023b3ca163f295c51d5959a13de289eb615 Mon Sep 17 00:00:00 2001 From: Cody Tapscott Date: Wed, 21 Feb 2024 04:46:08 -0500 Subject: [PATCH] Adjust `getsrc` to return MethodInstance as well I'm not 100% sure about the lookup here, but this at least seems to get the nightly tests back into the state they were in before the upstream CodeInstance change. --- TypedSyntax/src/node.jl | 19 ++++++++++----- TypedSyntax/test/runtests.jl | 45 ++++++++++++++++++------------------ 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/TypedSyntax/src/node.jl b/TypedSyntax/src/node.jl index 0d7db70f..4bc534f5 100644 --- a/TypedSyntax/src/node.jl +++ b/TypedSyntax/src/node.jl @@ -26,8 +26,8 @@ const no_default_value = NoDefaultValue() # where `mappings[i]` corresponds to the list of nodes matching `(src::CodeInfo).code[i]`. function tsn_and_mappings(@nospecialize(f), @nospecialize(t); kwargs...) m = which(f, t) - src, rt = getsrc(f, t) - tsn_and_mappings(m, src, rt; kwargs...) + src, rt, mi = getsrc(f, t) + tsn_and_mappings(mi, src, rt; kwargs...) end function tsn_and_mappings(mi::MethodInstance, src::CodeInfo, @nospecialize(rt); warn::Bool=true, strip_macros::Bool=false, kwargs...) @@ -65,8 +65,8 @@ function TypedSyntaxNode(mi::MethodInstance; kwargs...) tsn_and_mappings(mi, src, rt; kwargs...)[1] end -TypedSyntaxNode(rootnode::SyntaxNode, src::CodeInfo, Δline::Integer=0) = - TypedSyntaxNode(rootnode, src, map_ssas_to_source(src, rootnode, Δline)...) +TypedSyntaxNode(rootnode::SyntaxNode, src::CodeInfo, mi::MethodInstance, Δline::Integer=0) = + TypedSyntaxNode(rootnode, src, map_ssas_to_source(src, mi, rootnode, Δline)...) function TypedSyntaxNode(rootnode::SyntaxNode, src::CodeInfo, mappings, symtyps) # There may be ambiguous assignments back to the source; preserve just the unambiguous ones @@ -307,7 +307,13 @@ end function getsrc(@nospecialize(f), @nospecialize(t)) srcrts = code_typed(f, t; debuginfo=:source, optimize=false) - return only(srcrts) + src, rt = only(srcrts) + if hasfield(typeof(src), :parent) + mi = src.parent + else + mi = Base.method_instance(f, t) + end + return src, rt, mi end function getsrc(mi::MethodInstance) @@ -315,7 +321,8 @@ function getsrc(mi::MethodInstance) isempty(cis) && error("no applicable type-inferred code found for ", mi) length(cis) == 1 || error("got $(length(cis)) possible type-inferred results for ", mi, ", you may need a more specialized signature") - return cis[1]::Pair{CodeInfo} + src::CodeInfo, rt = cis[1] + return src, rt end function is_function_def(node) # this is not `Base.is_function_def` diff --git a/TypedSyntax/test/runtests.jl b/TypedSyntax/test/runtests.jl index 52bf2d7e..a3bb6270 100644 --- a/TypedSyntax/test/runtests.jl +++ b/TypedSyntax/test/runtests.jl @@ -15,8 +15,8 @@ include("test_module.jl") """ rootnode = JuliaSyntax.parsestmt(SyntaxNode, st; filename="TSN1.jl") TSN.eval(Expr(rootnode)) - src, _ = getsrc(TSN.f, (Float32, Int, Float64)) - tsn = TypedSyntaxNode(rootnode, src) + src, _, mi = getsrc(TSN.f, (Float32, Int, Float64)) + tsn = TypedSyntaxNode(rootnode, src, mi) sig, body = children(tsn) @test children(sig)[2].typ === Float32 @test children(sig)[3].typ === Int @@ -33,8 +33,8 @@ include("test_module.jl") """ rootnode = JuliaSyntax.parsestmt(SyntaxNode, st; filename="TSN2.jl") TSN.eval(Expr(rootnode)) - src, _ = getsrc(TSN.g, (Int16, Int16, Int32)) - tsn = TypedSyntaxNode(rootnode, src) + src, _, mi = getsrc(TSN.g, (Int16, Int16, Int32)) + tsn = TypedSyntaxNode(rootnode, src, mi) sig, body = children(tsn) @test length(children(sig)) == 4 @test children(body)[2].typ === Int32 @@ -46,8 +46,8 @@ include("test_module.jl") st = "math(x) = x + sin(x + π / 4)" rootnode = JuliaSyntax.parsestmt(SyntaxNode, st; filename="TSN2.jl") TSN.eval(Expr(rootnode)) - src, _ = getsrc(TSN.math, (Int,)) - tsn = TypedSyntaxNode(rootnode, src) + src, _, mi = getsrc(TSN.math, (Int,)) + tsn = TypedSyntaxNode(rootnode, src, mi) sig, body = children(tsn) @test has_name_typ(child(body, 1), :x, Int) @test has_name_typ(child(body, 3, 2, 1), :x, Int) @@ -70,8 +70,8 @@ include("test_module.jl") st = "math2(x) = sin(x) + sin(x)" rootnode = JuliaSyntax.parsestmt(SyntaxNode, st; filename="TSN2.jl") TSN.eval(Expr(rootnode)) - src, _ = getsrc(TSN.math2, (Int,)) - tsn = TypedSyntaxNode(rootnode, src) + src, _, mi = getsrc(TSN.math2, (Int,)) + tsn = TypedSyntaxNode(rootnode, src, mi) sig, body = children(tsn) @test body.typ === Float64 @test_broken child(body, 1).typ === Float64 @@ -91,8 +91,8 @@ include("test_module.jl") ) rootnode = JuliaSyntax.parsestmt(SyntaxNode, st; filename="TSN3.jl") TSN.eval(Expr(rootnode)) - src, _ = getsrc(TSN.firstfirst, (Vector{Vector{Real}},)) - tsn = TypedSyntaxNode(rootnode, src) + src, _, mi = getsrc(TSN.firstfirst, (Vector{Vector{Real}},)) + tsn = TypedSyntaxNode(rootnode, src, mi) sig, body = children(tsn) @test child(body, idxsinner...).typ === nothing @test child(body, idxsouter...).typ === Vector{Real} @@ -150,8 +150,8 @@ include("test_module.jl") """ rootnode = JuliaSyntax.parsestmt(SyntaxNode, st; filename="TSN4.jl") TSN.eval(Expr(rootnode)) - src, rt = getsrc(TSN.setlist!, (Vector{Vector{Float32}}, Vector{Vector{UInt8}}, Int, Int)) - tsn = TypedSyntaxNode(rootnode, src) + src, rt, mi = getsrc(TSN.setlist!, (Vector{Vector{Float32}}, Vector{Vector{UInt8}}, Int, Int)) + tsn = TypedSyntaxNode(rootnode, src, mi) sig, body = children(tsn) nodelist = child(body, 1, 2, 1, 1) # `listget` @test sourcetext(nodelist) == "listget" && nodelist.typ === Vector{Vector{UInt8}} @@ -175,8 +175,8 @@ include("test_module.jl") """ rootnode = JuliaSyntax.parsestmt(SyntaxNode, st; filename="TSN5.jl") TSN.eval(Expr(rootnode)) - src, rt = getsrc(TSN.callfindmin, (Vector{Float64},)) - tsn = TypedSyntaxNode(rootnode, src) + src, rt, mi = getsrc(TSN.callfindmin, (Vector{Float64},)) + tsn = TypedSyntaxNode(rootnode, src, mi) sig, body = children(tsn) t = child(body, 1, 1) @test kind(t) == K"tuple" @@ -280,18 +280,18 @@ include("test_module.jl") """ rootnode = JuliaSyntax.parsestmt(SyntaxNode, st; filename="TSN6.jl") TSN.eval(Expr(rootnode)) - src, rt = getsrc(TSN.avoidzero, (Int,)) + src, rt, mi = getsrc(TSN.avoidzero, (Int,)) # src looks like this: # %1 = Main.TSN.:(var"#avoidzero#6")(true, #self#, x)::Float64 # return %1 # Consequently there is nothing to match, but at least we shouldn't error - tsn = TypedSyntaxNode(rootnode, src) + tsn = TypedSyntaxNode(rootnode, src, mi) @test isa(tsn, TypedSyntaxNode) @test rt === Float64 # Try the kwbodyfunc m = which(TSN.avoidzero, (Int,)) - src, rt = getsrc(Base.bodyfunction(m), (Bool, typeof(TSN.avoidzero), Int,)) - tsn = TypedSyntaxNode(rootnode, src) + src, rt, mi = getsrc(Base.bodyfunction(m), (Bool, typeof(TSN.avoidzero), Int,)) + tsn = TypedSyntaxNode(rootnode, src, mi) sig, body = children(tsn) isz = child(body, 2, 1, 1) @test kind(isz) == K"call" && child(isz, 1).val == :iszero @@ -520,8 +520,8 @@ include("test_module.jl") @test_broken body.typ == Int # Construction from MethodInstance - src, rt = TypedSyntax.getsrc(TSN.myoftype, (Float64, Int)) - tsn = TypedSyntaxNode(src.parent) + src, rt, mi = TypedSyntax.getsrc(TSN.myoftype, (Float64, Int)) + tsn = TypedSyntaxNode(mi) sig, body = children(tsn) node = child(body, 1) @test node.typ === Type{Float64} @@ -643,10 +643,9 @@ include("test_module.jl") @test isa(tsnc, TypedSyntaxNode) # issue 487 - m = which(TSN.f487, (Int,)) - src, rt = getsrc(TSN.f487, (Int,)) + src, rt, mi = getsrc(TSN.f487, (Int,)) rt = Core.Const(1) - tsn, _ = TypedSyntax.tsn_and_mappings(m, src, rt) + tsn, _ = TypedSyntax.tsn_and_mappings(mi, src, rt) @test_nowarn str = sprint(tsn; context=:color=>false) do io, obj printstyled(io, obj; hide_type_stable=false) end