Skip to content

Commit 8a59be6

Browse files
authored
avoid Pair constructor being excessively specialized by Core.Compiler (#46684)
1 parent ad19f2f commit 8a59be6

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

base/Base.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,6 @@ include("options.jl")
107107
include("promotion.jl")
108108
include("tuple.jl")
109109
include("expr.jl")
110-
Pair{A, B}(@nospecialize(a), @nospecialize(b)) where {A, B} = (@inline; Pair{A, B}(convert(A, a)::A, convert(B, b)::B))
111-
#Pair{Any, B}(@nospecialize(a::Any), b) where {B} = (@inline; Pair{Any, B}(a, Base.convert(B, b)::B))
112-
#Pair{A, Any}(a, @nospecialize(b::Any)) where {A} = (@inline; Pair{A, Any}(Base.convert(A, a)::A, b))
113110
include("pair.jl")
114111
include("traits.jl")
115112
include("range.jl")
@@ -125,6 +122,13 @@ include("pointer.jl")
125122
include("refvalue.jl")
126123
include("refpointer.jl")
127124

125+
# now replace the Pair constructor (relevant for NamedTuples) with one that calls our Base.convert
126+
delete_method(which(Pair{Any,Any}, (Any, Any)))
127+
@eval function (P::Type{Pair{A, B}})(@nospecialize(a), @nospecialize(b)) where {A, B}
128+
@inline
129+
return $(Expr(:new, :P, :(convert(A, a)), :(convert(B, b))))
130+
end
131+
128132
# The REPL stdlib hooks into Base using this Ref
129133
const REPL_MODULE_REF = Ref{Module}()
130134

@@ -429,6 +433,7 @@ end
429433
for m in methods(include)
430434
delete_method(m)
431435
end
436+
432437
# These functions are duplicated in client.jl/include(::String) for
433438
# nicer stacktraces. Modifications here have to be backported there
434439
include(mod::Module, _path::AbstractString) = _include(identity, mod, _path)

base/boot.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,10 @@ struct Pair{A, B}
838838
# but also mark the whole function with `@inline` to ensure we will inline it whenever possible
839839
# (even if `convert(::Type{A}, a::A)` for some reason was expensive)
840840
Pair(a, b) = new{typeof(a), typeof(b)}(a, b)
841-
Pair{A, B}(a::A, b::B) where {A, B} = new(a, b)
842-
Pair{Any, Any}(@nospecialize(a::Any), @nospecialize(b::Any)) = new(a, b)
841+
function Pair{A, B}(@nospecialize(a), @nospecialize(b)) where {A, B}
842+
@inline
843+
return new(a::A, b::B)
844+
end
843845
end
844846

845847
ccall(:jl_set_istopmod, Cvoid, (Any, Bool), Core, true)

0 commit comments

Comments
 (0)