From 0f1696e71daa275b7236ca860f9b8993e80c6ab2 Mon Sep 17 00:00:00 2001 From: Neven Sajko Date: Wed, 27 Aug 2025 20:37:13 +0200 Subject: [PATCH] `sizeof(::Array)`: typeassert `::Int` to help abstract type inference Fix a regression where abstract return type inference of `sizeof(::Array)` used to produce `Any` after Julia v1.11. --- base/array.jl | 2 +- test/arrayops.jl | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/base/array.jl b/base/array.jl index d62436ca30497..00fb5b38dd196 100644 --- a/base/array.jl +++ b/base/array.jl @@ -232,7 +232,7 @@ function elsize(::Type{Ptr{T}}) where T end elsize(::Type{Union{}}, slurp...) = 0 -sizeof(a::Array) = length(a) * elsize(typeof(a)) # n.b. this ignores bitsunion bytes, as a historical fact +sizeof(a::Array) = length(a)::Int * elsize(typeof(a)) # n.b. this ignores bitsunion bytes, as a historical fact function isassigned(a::Array, i::Int...) @inline diff --git a/test/arrayops.jl b/test/arrayops.jl index f9c592e672973..556c44ceab010 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -536,6 +536,10 @@ end @test_throws BoundsError badpop() end +@testset "return type inference of `sizeof(::Array)`" begin + @test isconcretetype(Base.infer_return_type(sizeof, Tuple{Array})) +end + @testset "concatenation" begin @test isequal([fill(1.,2,2) fill(2.,2,1)], [1. 1 2; 1 1 2]) @test isequal([fill(1.,2,2); fill(2.,1,2)], [1. 1; 1 1; 2 2])