Skip to content

Commit 0cd4f09

Browse files
authored
fix #33270; stack overflow in named tuple ctor with Type{T} (#33303)
1 parent 9999ab9 commit 0cd4f09

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

base/namedtuple.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,13 @@ if nameof(@__MODULE__) === :Base
6969
Construct a named tuple with the given `names` (a tuple of Symbols) and field types `T`
7070
(a `Tuple` type) from a tuple of values.
7171
"""
72-
function NamedTuple{names,T}(args::Tuple) where {names, T <: Tuple}
72+
@eval function NamedTuple{names,T}(args::Tuple) where {names, T <: Tuple}
7373
if length(args) != length(names)
7474
throw(ArgumentError("Wrong number of arguments to named tuple constructor."))
7575
end
76-
NamedTuple{names,T}(T(args))
76+
# Note T(args) might not return something of type T; e.g.
77+
# Tuple{Type{Float64}}((Float64,)) returns a Tuple{DataType}
78+
$(Expr(:splatnew, :(NamedTuple{names,T}), :(T(args))))
7779
end
7880

7981
"""

test/namedtuple.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,9 @@ y = map(v -> (a=v.a, b=v.a + v.b), [(a=1, b=missing), (a=1, b=2)])
261261
@test merge((a=1, b=2), (b=3, c=4), (c=5,)) === (a=1, b=3, c=5)
262262
@test merge((a=1, b=2), (b=3, c=(d=1,)), (c=(d=2,),)) === (a=1, b=3, c=(d=2,))
263263
@test merge((a=1, b=2)) === (a=1, b=2)
264+
265+
# issue #33270
266+
let n = NamedTuple{(:T,), Tuple{Type{Float64}}}((Float64,))
267+
@test n isa NamedTuple{(:T,), Tuple{Type{Float64}}}
268+
@test n.T === Float64
269+
end

0 commit comments

Comments
 (0)