From 3f1328e06094a26e8ff7ebe475d3863b5f2f77fa Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Mon, 28 Apr 2025 16:03:43 +0000 Subject: [PATCH 1/2] use subtyping instead of structural inspection for ambiguity exclusions Should be slightly more robust, since subtyping already implements these queries with much more finesse than is going to be true of a re-implementation of the query here. --- src/ambiguities.jl | 52 +++++++++++++++------------------------- src/piracies.jl | 2 +- src/utils.jl | 5 ++-- test/test_ambiguities.jl | 7 +++++- test/test_getobj.jl | 20 ---------------- 5 files changed, 29 insertions(+), 57 deletions(-) delete mode 100644 test/test_getobj.jl diff --git a/src/ambiguities.jl b/src/ambiguities.jl index 00b26457..485b410f 100644 --- a/src/ambiguities.jl +++ b/src/ambiguities.jl @@ -154,37 +154,6 @@ function reprpkgid(pkg::PkgId) return "Base.PkgId(Base.UUID($(repr(uuid.value))), $(repr(name)))" end -struct _NoValue end - -function getobj(m::Method) - signature = Base.unwrap_unionall(m.sig) - ty = if is_kwcall(signature) - signature.parameters[3] - else - signature.parameters[1] - end - ty = Base.unwrap_unionall(ty) - if ty <: Function - try - return ty.instance # this should work for functions - catch - end - end - try - if ty.name.wrapper === Type - return ty.parameters[1] - else - return ty.name.wrapper - end - catch err - @error( - "Failed to obtain a function from `Method`.", - exception = (err, catch_backtrace()) - ) - end - return _NoValue() -end - function test_ambiguities_impl( packages::Vector{PkgId}, options::NamedTuple, @@ -196,9 +165,26 @@ function test_ambiguities_impl( ambiguities = detect_ambiguities(modules...; options...) if !isempty(exspecs) - exclude_objs = getobj.(exspecs) + exclude_ft = Any[getobj(spec) for spec in exspecs] + exclude_sig = Any[] + for ft in exclude_ft + if ft isa Type + push!(exclude_sig, Tuple{ft, Vararg}) + push!(exclude_sig, Tuple{Core.kwftype(ft), Any, ft, Vararg}) + ft = Type{<:ft} # alternatively, Type{ft} + else + ft = typeof(ft) + end + push!(exclude_sig, Tuple{ft, Vararg}) + push!(exclude_sig, Tuple{Core.kwftype(ft), Any, ft, Vararg}) + end ambiguities = filter(ambiguities) do (m1, m2) - getobj(m1) ∉ exclude_objs && getobj(m2) ∉ exclude_objs + for excl in exclude_sig + if m1.sig <: excl || m2.sig <: excl + return false + end + end + return true end end diff --git a/src/piracies.jl b/src/piracies.jl index 97c7c90a..317a8ac2 100644 --- a/src/piracies.jl +++ b/src/piracies.jl @@ -168,7 +168,7 @@ function is_pirate(meth::Method; treat_as_own = Union{Function,Type}[]) signature = Base.unwrap_unionall(meth.sig) function_type_index = 1 - if is_kwcall(signature) + if is_kwcall(meth.sig) # kwcall is a special case, since it is not a real function # but a wrapper around a function, the third parameter is the original # function, its positional arguments follow. diff --git a/src/utils.jl b/src/utils.jl index d869dca1..aaa38af4 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -55,8 +55,9 @@ function checked_repr(obj) return code end -function is_kwcall(signature::DataType) +function is_kwcall(signature::Type) @static if VERSION < v"1.9" + signature = Base.unwrap_unionall(signature)::DataType try length(signature.parameters) >= 3 || return false signature <: Tuple{Function,Any,Any,Vararg} || return false @@ -69,6 +70,6 @@ function is_kwcall(signature::DataType) return false end else - return signature.parameters[1] === typeof(Core.kwcall) + return signature <: Tuple{typeof(Core.kwcall), Any, Any, Vararg} end end diff --git a/test/test_ambiguities.jl b/test/test_ambiguities.jl index f9173b55..ae178cc9 100644 --- a/test/test_ambiguities.jl +++ b/test/test_ambiguities.jl @@ -30,6 +30,11 @@ include("preamble.jl") if broken @test_broken num_ambiguities_ == num_ambiguities else + if num_ambiguities_ != num_ambiguities + @show exclude + println(strout) + println(strerr) + end @test num_ambiguities_ == num_ambiguities end end @@ -50,7 +55,7 @@ include("preamble.jl") check_testcase([PkgWithAmbiguities.ConcreteType], total - num_ambs_ConcreteType) # exclude abstract supertype without callables and constructors - check_testcase([PkgWithAmbiguities.AbstractType], total) + check_testcase([PkgWithAmbiguities.AbstractType], total - num_ambs_SingletonType - num_ambs_ConcreteType) @static if VERSION >= v"1.3-" # for ambiguities between abstract and concrete type callables, only one needs to be excluded diff --git a/test/test_getobj.jl b/test/test_getobj.jl deleted file mode 100644 index 48acc11c..00000000 --- a/test/test_getobj.jl +++ /dev/null @@ -1,20 +0,0 @@ -module TestGetObj - -using Aqua: getobj -using Test - -module ModuleA -function f end -end - -module ModuleB -using ..ModuleA: ModuleA -ModuleA.f(::Int) = nothing -end - -@testset begin - m, = methods(ModuleA.f, Tuple{Int}) - @test getobj(m) === ModuleA.f -end - -end # module From 73643da58e724836a2a5f359549894f070c9f709 Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Mon, 5 May 2025 20:24:25 +0000 Subject: [PATCH 2/2] go back to doing some structural evaluation, but more carefully using types Even though that means there are cases that will be impossible to exclude, the author of Aqua confirmed that is intentional. --- src/ambiguities.jl | 62 +++++++++++++++++++--------------------- test/test_ambiguities.jl | 2 +- test/test_exclude.jl | 17 +++++++---- 3 files changed, 42 insertions(+), 39 deletions(-) diff --git a/src/ambiguities.jl b/src/ambiguities.jl index 485b410f..4070a5b3 100644 --- a/src/ambiguities.jl +++ b/src/ambiguities.jl @@ -29,30 +29,31 @@ test_ambiguities(packages; kwargs...) = _test_ambiguities(aspkgids(packages); kw const ExcludeSpec = Pair{Base.PkgId,String} -strnameof(x) = string(x) -strnameof(x::Type) = string(nameof(x)) - -rootmodule(x) = rootmodule(parentmodule(x)) +rootmodule(x::Type) = rootmodule(parentmodule(x)) rootmodule(m::Module) = Base.require(PkgId(m)) # this handles Base/Core well -normalize_exclude(x::Union{Type,Function}) = - Base.PkgId(rootmodule(x)) => join((fullname(parentmodule(x))..., strnameof(x)), ".") -normalize_exclude(::Any) = error("Only a function and type can be excluded.") +# get the Type associated with x +normalize_exclude_obj(@nospecialize x) = x isa Type ? x : typeof(x) + +function normalize_exclude(@nospecialize x) + x = normalize_exclude_obj(x) + return Base.PkgId(rootmodule(x)) => join((fullname(parentmodule(x))..., string(nameof(x))), ".") +end -function getobj((pkgid, name)::ExcludeSpec) +function getexclude((pkgid, name)::ExcludeSpec) nameparts = Symbol.(split(name, ".")) m = Base.require(pkgid) for name in nameparts m = getproperty(m, name) end - return m + return normalize_exclude_obj(m) end function normalize_and_check_exclude(exclude::AbstractVector) - exspecs = mapfoldl(normalize_exclude, push!, exclude, init = ExcludeSpec[]) - for (spec, obj) in zip(exspecs, exclude) - if getobj(spec) !== obj - error("Name `$(spec[2])` is resolved to a different object.") + exspecs = ExcludeSpec[normalize_exclude(exspec) for exspec in exclude] + for (i, exspec) in enumerate(exspecs) + if getexclude(exspec) != normalize_exclude_obj(exclude[i]) + error("Name `$(exspec[2])` is resolved to a different object.") end end return exspecs::Vector{ExcludeSpec} @@ -154,6 +155,20 @@ function reprpkgid(pkg::PkgId) return "Base.PkgId(Base.UUID($(repr(uuid.value))), $(repr(name)))" end +# try to extract the called function, or nothing if it is hard to analyze +function trygetft(m::Method) + sig = Base.unwrap_unionall(m.sig)::DataType + ft = sig.parameters[is_kwcall(sig) ? 3 : 1] + ft = Base.unwrap_unionall(ft) + if ft isa DataType && ft.name === Type.body.name + ft = Base.unwrap_unionall(ft.parameters[1]) + end + if ft isa DataType + return ft.name.wrapper + end + return nothing # cannot exclude signatures with Union +end + function test_ambiguities_impl( packages::Vector{PkgId}, options::NamedTuple, @@ -165,26 +180,9 @@ function test_ambiguities_impl( ambiguities = detect_ambiguities(modules...; options...) if !isempty(exspecs) - exclude_ft = Any[getobj(spec) for spec in exspecs] - exclude_sig = Any[] - for ft in exclude_ft - if ft isa Type - push!(exclude_sig, Tuple{ft, Vararg}) - push!(exclude_sig, Tuple{Core.kwftype(ft), Any, ft, Vararg}) - ft = Type{<:ft} # alternatively, Type{ft} - else - ft = typeof(ft) - end - push!(exclude_sig, Tuple{ft, Vararg}) - push!(exclude_sig, Tuple{Core.kwftype(ft), Any, ft, Vararg}) - end + exclude_ft = Any[getexclude(spec) for spec in exspecs] # vector of Type objects ambiguities = filter(ambiguities) do (m1, m2) - for excl in exclude_sig - if m1.sig <: excl || m2.sig <: excl - return false - end - end - return true + trygetft(m1) ∉ exclude_ft && trygetft(m2) ∉ exclude_ft end end diff --git a/test/test_ambiguities.jl b/test/test_ambiguities.jl index 83d2595f..720d1f6d 100644 --- a/test/test_ambiguities.jl +++ b/test/test_ambiguities.jl @@ -49,7 +49,7 @@ include("preamble.jl") check_testcase([PkgWithAmbiguities.ConcreteType], total - num_ambs_ConcreteType) # exclude abstract supertype without callables and constructors - check_testcase([PkgWithAmbiguities.AbstractType], total - num_ambs_SingletonType - num_ambs_ConcreteType) + check_testcase([PkgWithAmbiguities.AbstractType], total) # for ambiguities between abstract and concrete type callables, only one needs to be excluded check_testcase( diff --git a/test/test_exclude.jl b/test/test_exclude.jl index 6eca9659..4a8a5b18 100644 --- a/test/test_exclude.jl +++ b/test/test_exclude.jl @@ -2,7 +2,7 @@ module TestExclude include("preamble.jl") using Base: PkgId -using Aqua: getobj, normalize_exclude, normalize_and_check_exclude, rootmodule, reprexclude +using Aqua: getexclude, normalize_exclude, normalize_exclude_obj, normalize_and_check_exclude, rootmodule, reprexclude @assert parentmodule(Tuple) === Core @assert parentmodule(foldl) === Base @@ -11,27 +11,32 @@ using Aqua: getobj, normalize_exclude, normalize_and_check_exclude, rootmodule, @assert rootmodule(Broadcast.Broadcasted) === Base @testset "roundtrip" begin - @testset for x in [ + @testset for x in Any[ foldl Some Tuple Broadcast.Broadcasted + nothing + Any ] - @test getobj(normalize_exclude(x)) == x + @test getexclude(normalize_exclude(x)) === normalize_exclude_obj(x) end + @test_throws ErrorException normalize_and_check_exclude(Any[Pair{Int}]) @testset "$(repr(last(spec)))" for spec in [ - (PkgId(Base) => "Base.foldl") + (PkgId(Base) => "Base.#foldl") (PkgId(Base) => "Base.Some") (PkgId(Core) => "Core.Tuple") (PkgId(Base) => "Base.Broadcast.Broadcasted") + (PkgId(Core) => "Core.Nothing") + (PkgId(Core) => "Core.Any") ] - @test normalize_exclude(getobj(spec)) === spec + @test normalize_exclude(getexclude(spec)) === spec end end @testset "normalize_and_check_exclude" begin - @testset "$i" for (i, exclude) in enumerate([[foldl], [foldl, Some], [foldl, Tuple]]) + @testset "$i" for (i, exclude) in enumerate([Any[foldl], Any[foldl, Some], Any[foldl, Tuple]]) local specs @test (specs = normalize_and_check_exclude(exclude)) isa Vector @test Base.include_string(@__MODULE__, reprexclude(specs)) == specs