diff --git a/base/tuple.jl b/base/tuple.jl index 9c03274f190bf..339aafac171b1 100644 --- a/base/tuple.jl +++ b/base/tuple.jl @@ -596,7 +596,7 @@ any(x::Tuple{Bool, Bool}) = x[1]|x[2] any(x::Tuple{Bool, Bool, Bool}) = x[1]|x[2]|x[3] # a version of `in` esp. for NamedTuple, to make it pure, and not compiled for each tuple length -function sym_in(x::Symbol, @nospecialize itr::Tuple{Vararg{Symbol}}) +function sym_in(x::Symbol, itr::Tuple{Vararg{Symbol}}) @noinline @_total_meta for y in itr @@ -604,7 +604,7 @@ function sym_in(x::Symbol, @nospecialize itr::Tuple{Vararg{Symbol}}) end return false end -in(x::Symbol, @nospecialize itr::Tuple{Vararg{Symbol}}) = sym_in(x, itr) +in(x::Symbol, itr::Tuple{Vararg{Symbol}}) = sym_in(x, itr) """ diff --git a/test/namedtuple.jl b/test/namedtuple.jl index babc61f267cc9..20737d68db1bb 100644 --- a/test/namedtuple.jl +++ b/test/namedtuple.jl @@ -406,3 +406,23 @@ let a = Base.NamedTuple{(:a, :b), Tuple{Any, Any}}((1, 2)), b = Base.NamedTuple{ @test typeof(Base.merge(a, b)) == Base.NamedTuple{(:a, :b), Tuple{Any, Float64}} @test typeof(Base.structdiff(a, b)) == Base.NamedTuple{(:a,), Tuple{Any}} end + +function mergewith51009(combine, a::NamedTuple{an}, b::NamedTuple{bn}) where {an, bn} + names = Base.merge_names(an, bn) + NamedTuple{names}(ntuple(Val{nfields(names)}()) do i + n = getfield(names, i) + if Base.sym_in(n, an) + if Base.sym_in(n, bn) + combine(getfield(a, n), getfield(b, n)) + else + getfield(a, n) + end + else + getfield(b, n) + end + end) +end +let c = (a=1, b=2), + d = (b=3, c=(d=1,)) + @test @inferred(mergewith51009((x,y)->y, c, d)) === (a = 1, b = 3, c = (d = 1,)) +end diff --git a/test/tuple.jl b/test/tuple.jl index 8ff311928b6b8..b806667fd9d0a 100644 --- a/test/tuple.jl +++ b/test/tuple.jl @@ -763,6 +763,12 @@ g42457(a, b) = Base.isequal(a, b) ? 1 : 2.0 # issue #46049: setindex(::Tuple) regression @inferred Base.setindex((1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16), 42, 1) +# issue #50562 +f50562(r) = in(:i_backward, r[]) +r50562 = Ref((:b_back, :foofakgka, :i_backw)) +f50562(r50562) +@test @allocated(f50562(r50562)) == 0 + # issue #47326 function fun1_47326(args...) head..., tail = args