Skip to content

Commit 3819238

Browse files
authored
replace MethodInfoKey Pair alias with struct to avoid type piracy (#149)
Changes `MethodInfoKey` from a `Pair` type alias to a proper struct, avoiding type piracy that would be introduced by defining `Pair{...}(::Method)` conversion methods. The struct implements `Base.iterate` to maintain compatibility with destructuring patterns. This is a breaking change as it changes the representation of method info keys, which packages like LCU and Revise depend heavily on. So we unfotunately need to bump the major version from 2 to 3. As an alternative, removing `Pair{...}(::Method)` would also be possible, but that would also break LCU and Revise, so I'm taking this approach which appears to be a better and moer complete fix.
1 parent c5463b6 commit 3819238

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "CodeTracking"
22
uuid = "da1fd8a2-8d9e-5ec2-8556-3022fb5608a2"
33
authors = ["Tim Holy <[email protected]>"]
4-
version = "2.0.2"
4+
version = "3.0.0"
55

66
[deps]
77
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

src/CodeTracking.jl

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,27 @@ include("utils.jl")
2929

3030
# These values get populated by Revise
3131

32-
# `method_info[mt => sig]` is either:
32+
# `method_info[MethodInfoKey(mt, sig)]` is either:
3333
# - `missing`, to indicate that the method cannot be located
3434
# - a list of `(lnn,ex)` pairs. In almost all cases there will be just one of these,
3535
# but "mistakes" in moving methods from one file to another can result in more than
3636
# one definition. The last pair in the list is the currently-active definition.
3737

38-
const MethodInfoKey = Pair{Union{Nothing, MethodTable}, Type}
38+
struct MethodInfoKey
39+
mt::Union{Nothing, MethodTable}
40+
sig::Type
41+
MethodInfoKey(mt::Union{Nothing, MethodTable}, @nospecialize(sig::Type)) = new(mt, sig)
42+
end
43+
44+
function Base.iterate(mt_sig::MethodInfoKey, s::Union{Int,Nothing}=nothing)
45+
if s === nothing
46+
return mt_sig.mt, 1
47+
elseif s == 1
48+
return mt_sig.sig, 2
49+
else
50+
return nothing
51+
end
52+
end
3953

4054
const method_info = IdDict{MethodInfoKey,Union{Missing,Vector{Tuple{LineNumberNode,Expr}}}}()
4155

0 commit comments

Comments
 (0)