Skip to content

test that various interface callables behave well #58829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions base/strings/search.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,18 @@ struct RvCharPosIter{S}
last_char_byte::UInt8
end

IteratorSize(s::Type{<:Union{FwCharPosIter, RvCharPosIter}}) = SizeUnknown()
eltype(::Type{<:Union{FwCharPosIter, RvCharPosIter}}) = Int
@nospecializeinfer function IteratorSize(@nospecialize unused::Type{<:FwCharPosIter})
SizeUnknown()
end
@nospecializeinfer function eltype(@nospecialize unused::Type{<:FwCharPosIter})
Int
end
@nospecializeinfer function IteratorSize(@nospecialize unused::Type{<:RvCharPosIter})
SizeUnknown()
end
@nospecializeinfer function eltype(@nospecialize unused::Type{<:RvCharPosIter})
Int
end

function RvCharPosIter(s::Union{String, SubString{String}}, c::AbstractChar)
char = Char(c)::Char
Expand Down
2 changes: 1 addition & 1 deletion test/choosetests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const STDLIBS = filter!(x -> isfile(joinpath(STDLIB_DIR, x, "src", "$(x).jl")),

const TESTNAMES = [
"subarray", "core", "compiler", "compiler_extras", "worlds", "atomics",
"keywordargs", "numbers", "subtype",
"keywordargs", "numbers", "subtype", "interface_callables",
"char", "strings", "triplequote", "unicode", "intrinsics",
"dict", "hashing", "iobuffer", "staged", "offsetarray",
"arrayops", "tuple", "reduce", "reducedim", "abstractarray",
Expand Down
72 changes: 72 additions & 0 deletions test/interface_callables.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Test

module ExampleTypes end

@testset "interface callables" begin
Base.@nospecializeinfer function max_methods_of(@nospecialize callable::Any)
raw = Base._stable_typeof(callable).name.max_methods
if iszero(raw)
3 # default value, TODO: how to avoid hardcoding this?
else
Int(raw)
end
end
"""
interface_callables::Dict{DataType, Vector{Any}}

In each key-value pair:

* the key is the direct supertype of the newly-defined type

* the values are the applicable interface callables
"""
interface_callables = let
nums = Any[widen, zero, one, oneunit]
keys_and_values = Any[
(Any => Any[eltype, Base.IteratorSize, Base.IteratorEltype]),
(DenseVector{Float32} => Any[Base.elsize]),
(Real => nums),
(AbstractFloat => nums),
(Integer => nums),
]
Dict{DataType, Vector{Any}}(keys_and_values)
end
interface_callables_supertypes = collect(DataType, keys(interface_callables))
function example_type_name(i::Int, j::Int)
n = string('_', i, '_', j)
Symbol(n)
end
function tests(interface_callables, interface_callables_supertypes)
for (i, supertype) ∈ enumerate(interface_callables_supertypes)
for (j, callable) ∈ enumerate(interface_callables[supertype])
typ = getproperty(ExampleTypes, example_type_name(i, j))
method_match_length = length(methods(callable, Tuple{Type{<:typ}}))
# `max_methods` is high-enough for good inference
@test method_match_length ≤ max_methods_of(callable)
# don't match an excessive number of methods for the newly-defined type
@test method_match_length ≤ 2
end
end
end
for (i, supertype) ∈ enumerate(interface_callables_supertypes) # define types
for (j, _) ∈ enumerate(interface_callables[supertype])
type_name = example_type_name(i, j)
@eval ExampleTypes struct $type_name{X} <: $supertype
x::X
end
end
end
@testset "first pass: without any new method defined for the new type" begin
tests(interface_callables, interface_callables_supertypes)
end
for (i, supertype) ∈ enumerate(interface_callables_supertypes) # define methods for new types
for (j, callable) ∈ enumerate(interface_callables[supertype])
callable_name = nameof(callable)
type_name = example_type_name(i, j)
@eval ExampleTypes function Base.$callable_name(::Type{<:$type_name}) end
end
end
@testset "second pass: with a new method defined for the new type" begin
tests(interface_callables, interface_callables_supertypes)
end
end