Skip to content

Commit 2523d15

Browse files
JeffBezansonKristofferC
authored andcommitted
fix annotations on sym_in (#51573)
This seems to be the right combination of annotations to fix both #50562 and an inference regression in PropertyDicts.jl on the 1.10 release branch. When backported the `@noinline` should be restored for 1.10. (cherry picked from commit f7e8f92)
1 parent b3898c3 commit 2523d15

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

base/tuple.jl

+3-2
Original file line numberDiff line numberDiff line change
@@ -606,14 +606,15 @@ any(x::Tuple{Bool, Bool}) = x[1]|x[2]
606606
any(x::Tuple{Bool, Bool, Bool}) = x[1]|x[2]|x[3]
607607

608608
# a version of `in` esp. for NamedTuple, to make it pure, and not compiled for each tuple length
609-
function sym_in(x::Symbol, @nospecialize itr::Tuple{Vararg{Symbol}})
609+
function sym_in(x::Symbol, itr::Tuple{Vararg{Symbol}})
610+
@noinline
610611
@_total_meta
611612
for y in itr
612613
y === x && return true
613614
end
614615
return false
615616
end
616-
in(x::Symbol, @nospecialize itr::Tuple{Vararg{Symbol}}) = sym_in(x, itr)
617+
in(x::Symbol, itr::Tuple{Vararg{Symbol}}) = sym_in(x, itr)
617618

618619

619620
"""

test/namedtuple.jl

+20
Original file line numberDiff line numberDiff line change
@@ -394,3 +394,23 @@ let a = Base.NamedTuple{(:a, :b), Tuple{Any, Any}}((1, 2)), b = Base.NamedTuple{
394394
@test typeof(Base.merge(a, b)) == Base.NamedTuple{(:a, :b), Tuple{Any, Float64}}
395395
@test typeof(Base.structdiff(a, b)) == Base.NamedTuple{(:a,), Tuple{Any}}
396396
end
397+
398+
function mergewith51009(combine, a::NamedTuple{an}, b::NamedTuple{bn}) where {an, bn}
399+
names = Base.merge_names(an, bn)
400+
NamedTuple{names}(ntuple(Val{nfields(names)}()) do i
401+
n = getfield(names, i)
402+
if Base.sym_in(n, an)
403+
if Base.sym_in(n, bn)
404+
combine(getfield(a, n), getfield(b, n))
405+
else
406+
getfield(a, n)
407+
end
408+
else
409+
getfield(b, n)
410+
end
411+
end)
412+
end
413+
let c = (a=1, b=2),
414+
d = (b=3, c=(d=1,))
415+
@test @inferred(mergewith51009((x,y)->y, c, d)) === (a = 1, b = 3, c = (d = 1,))
416+
end

0 commit comments

Comments
 (0)