From 2523d15dfc791231b960198c7b43fc11e2a9931e Mon Sep 17 00:00:00 2001 From: Jeff Bezanson <jeff.bezanson@gmail.com> Date: Wed, 4 Oct 2023 17:16:41 -0400 Subject: [PATCH] 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 f7e8f924f524b102ce8bb50e6662ca9b16e02c30) --- base/tuple.jl | 5 +++-- test/namedtuple.jl | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/base/tuple.jl b/base/tuple.jl index ae5de41586848..518641de1c143 100644 --- a/base/tuple.jl +++ b/base/tuple.jl @@ -606,14 +606,15 @@ 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 y === x && return true 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 eb3846c8cbffd..0e9648dbcb9c0 100644 --- a/test/namedtuple.jl +++ b/test/namedtuple.jl @@ -394,3 +394,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