From fda85b3f4a58d55364b0691445f93bed4ecd0b9d Mon Sep 17 00:00:00 2001 From: Neven Sajko Date: Wed, 27 Aug 2025 19:48:46 +0200 Subject: [PATCH] `getindex(::Array, ::Colon)`: typeassert `::Int` to help type inference Fix a regression where abstract return type inference of `getindex(::Array, ::Colon)` used to produce `Any` after Julia v1.11. --- base/array.jl | 2 +- test/arrayops.jl | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/base/array.jl b/base/array.jl index d62436ca30497..33024aef07a89 100644 --- a/base/array.jl +++ b/base/array.jl @@ -954,7 +954,7 @@ end getindex(a::Array, r::AbstractUnitRange{Bool}) = getindex(a, to_index(r)) function getindex(A::Array, c::Colon) - lI = length(A) + lI = length(A)::Int X = similar(A, lI) if lI > 0 unsafe_copyto!(X, 1, A, 1, lI) diff --git a/test/arrayops.jl b/test/arrayops.jl index f9c592e672973..e1588263e3f55 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -221,6 +221,12 @@ end end end +@testset "return type inference of `getindex(::Array, ::Colon)`" begin + f = a -> a[:] + @test Vector === Base.infer_return_type(f, Tuple{Array}) + @test Vector{Float32} === Base.infer_return_type(f, Tuple{Array{Float32}}) +end + struct Z26163 <: AbstractArray{Int,0}; end Base.size(::Z26163) = () Base.getindex(::Z26163) = 0