• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / 1649

26 Mar 2026 12:39AM UTC coverage: 77.623% (+0.8%) from 76.798%
1649

push

buildkite

web-flow
Make `SourceRef` contain `Ref{SourceFile}` instead of `SourceFile` (#61402)

64568 of 83181 relevant lines covered (77.62%)

23641174.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.25
/Compiler/src/methodtable.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
struct MethodLookupResult
4
    # Really Vector{Core.MethodMatch}, but it's easier to represent this as
5
    # and work with Vector{Any} on the C side.
6
    matches::Vector{Any}
835,899✔
7
    valid_worlds::WorldRange
8
    ambig::Bool
9
end
10
length(result::MethodLookupResult) = length(result.matches)
1,567,701✔
11
function iterate(result::MethodLookupResult, args...)
12
    r = iterate(result.matches, args...)
1,409,751✔
13
    r === nothing && return nothing
1,409,751✔
14
    match, state = r
441,686✔
15
    return (match::MethodMatch, state)
1,150,437✔
16
end
17
getindex(result::MethodLookupResult, idx::Int) = getindex(result.matches, idx)::MethodMatch
1,115,683✔
18

19
abstract type MethodTableView end
20

21
"""
22
    struct InternalMethodTable <: MethodTableView
23

24
A struct representing the state of the internal method table at a
25
particular world age.
26
"""
27
struct InternalMethodTable <: MethodTableView
28
    world::UInt
632,227✔
29
end
30

31
"""
32
    struct OverlayMethodTable <: MethodTableView
33

34
Overlays the internal method table such that specific queries can be redirected to an
35
external table, e.g., to override existing method.
36
"""
37
struct OverlayMethodTable <: MethodTableView
38
    world::UInt
12,471✔
39
    mt::MethodTable
40
end
41

42
struct MethodMatchKey
43
    sig # ::Type
44
    limit::Int
45
    MethodMatchKey(@nospecialize(sig), limit::Int) = new(sig, limit)
90,395✔
46
end
47

48
"""
49
    struct CachedMethodTable <: MethodTableView
50

51
Overlays another method table view with an additional local fast path cache that
52
can respond to repeated, identical queries faster than the original method table.
53
"""
54
struct CachedMethodTable{T<:MethodTableView} <: MethodTableView
55
    cache::IdDict{MethodMatchKey, Union{Nothing,MethodLookupResult}}
2,530✔
56
    table::T
57
end
58
CachedMethodTable(table::T) where T = CachedMethodTable{T}(IdDict{MethodMatchKey, Union{Nothing,MethodLookupResult}}(), table)
2,530✔
59

60
"""
61
    findall(sig::Type, view::MethodTableView; limit::Int=-1) ->
62
        matches::MethodLookupResult or nothing
63

64
Find all methods in the given method table `view` that are applicable to the given signature `sig`.
65
If no applicable methods are found, an empty result is returned.
66
If the number of applicable methods exceeded the specified `limit`, `nothing` is returned.
67
Note that the default setting `limit=-1` does not limit the number of applicable methods.
68
`overlayed` indicates if any of the matching methods comes from an overlayed method table.
69
"""
70
findall(@nospecialize(sig::Type), table::InternalMethodTable; limit::Int=-1) =
1,687,870✔
71
    _findall(sig, nothing, table.world, limit)
72

73
function findall(@nospecialize(sig::Type), table::OverlayMethodTable; limit::Int=-1)
12,384✔
74
    result = _findall(sig, table.mt, table.world, limit)
×
75
    result === nothing && return nothing
×
76
    nr = length(result)
×
77
    if nr ≥ 1 && result[nr].fully_covers
×
78
        # no need to fall back to the internal method table
79
        return result
×
80
    end
81
    # fall back to the internal method table
82
    fallback_result = _findall(sig, nothing, table.world, limit)
×
83
    fallback_result === nothing && return nothing
×
84
    # merge the fallback match results with the internal method table
85
    return MethodLookupResult(
×
86
        vcat(result.matches, fallback_result.matches),
87
        WorldRange(
88
            max(result.valid_worlds.min_world, fallback_result.valid_worlds.min_world),
89
            min(result.valid_worlds.max_world, fallback_result.valid_worlds.max_world)),
90
        result.ambig | fallback_result.ambig)
91
end
92

93
function _findall(@nospecialize(sig::Type), mt::Union{Nothing,MethodTable}, world::UInt, limit::Int)
94
    _min_val = RefValue{UInt}(typemin(UInt))
839,901✔
95
    _max_val = RefValue{UInt}(typemax(UInt))
839,901✔
96
    _ambig = RefValue{Int32}(0)
839,901✔
97
    ms = _methods_by_ftype(sig, mt, limit, world, false, _min_val, _max_val, _ambig)
839,901✔
98
    isa(ms, Vector) || return nothing
843,935✔
99
    return MethodLookupResult(ms, WorldRange(_min_val[], _max_val[]), _ambig[] != 0)
835,867✔
100
end
101

102
function findall(@nospecialize(sig::Type), table::CachedMethodTable; limit::Int=-1)
1,107,282✔
103
    if isconcretetype(sig)
228,601✔
104
        # as for concrete types, we cache result at on the next level
105
        return findall(sig, table.table; limit)
138,206✔
106
    end
107
    key = MethodMatchKey(sig, limit)
90,395✔
108
    if haskey(table.cache, key)
133,733✔
109
        return table.cache[key]
43,338✔
110
    else
111
        return table.cache[key] = findall(sig, table.table; limit)
47,057✔
112
    end
113
end
114

115
"""
116
    findsup(sig::Type, view::MethodTableView) ->
117
        (match::Union{MethodMatch,Nothing}, valid_worlds::WorldRange, overlayed::Bool)
118

119
Find the (unique) method such that `sig <: match.method.sig`, while being more
120
specific than any other method with the same property. In other words, find the method
121
which is the least upper bound (supremum) under the specificity/subtype relation of
122
the queried `sig`nature. If `sig` is concrete, this is equivalent to asking for the method
123
that will be called given arguments whose types match the given signature.
124
Note that this query is also used to implement `invoke`.
125

126
Such a matching method `match` doesn't necessarily exist.
127
It is possible that no method is an upper bound of `sig`, or
128
it is possible that among the upper bounds, there is no least element.
129
In both cases `nothing` is returned.
130

131
`overlayed` indicates if any of the matching methods comes from an overlayed method table.
132
"""
133
findsup(@nospecialize(sig::Type), table::InternalMethodTable) =
2,253✔
134
    _findsup(sig, nothing, table.world)
135

136
function findsup(@nospecialize(sig::Type), table::OverlayMethodTable)
×
137
    match, valid_worlds = _findsup(sig, table.mt, table.world)
51✔
138
    match !== nothing && return match, valid_worlds
51✔
139
    # fall back to the internal method table
140
    fallback_match, fallback_valid_worlds = _findsup(sig, nothing, table.world)
45✔
141
    return (
45✔
142
        fallback_match,
143
        WorldRange(
144
            max(valid_worlds.min_world, fallback_valid_worlds.min_world),
145
            min(valid_worlds.max_world, fallback_valid_worlds.max_world)))
146
end
147

148
function _findsup(@nospecialize(sig::Type), mt::Union{Nothing,MethodTable}, world::UInt)
169✔
149
    min_valid = RefValue{UInt}(typemin(UInt))
2,587✔
150
    max_valid = RefValue{UInt}(typemax(UInt))
2,587✔
151
    match = ccall(:jl_gf_invoke_lookup_worlds, Any, (Any, Any, UInt, Ptr{Csize_t}, Ptr{Csize_t}),
2,587✔
152
                   sig, mt, world, min_valid, max_valid)::Union{MethodMatch, Nothing}
153
    valid_worlds = WorldRange(min_valid[], max_valid[])
2,587✔
154
    return match, valid_worlds
2,193✔
155
end
156

157
# This query is not cached
158
findsup(@nospecialize(sig::Type), table::CachedMethodTable) = findsup(sig, table.table)
33✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc