Skip to content

Commit fe7e7b7

Browse files
authored
Broaden check for eltypes to make sure we don't allow invalid stuff (#2756)
1 parent d2f8da2 commit fe7e7b7

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/array.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ function hasfieldcount(@nospecialize(dt))
1212
return true
1313
end
1414

15+
explain_nonisbits(@nospecialize(T), depth=0) = " "^depth * "$T is not a bitstype\n"
16+
1517
function explain_eltype(@nospecialize(T), depth=0; maxdepth=10)
1618
depth > maxdepth && return ""
1719

@@ -46,7 +48,8 @@ end
4648
# 3. bitstype unions (`Union{Int, Float32}`, etc)
4749
# these are stored contiguously and require a selector array (handled by us)
4850
@inline function check_eltype(name, T)
49-
if !Base.allocatedinline(T)
51+
eltype_is_invalid = !Base.allocatedinline(T) || (hasfieldcount(T) && any(!Base.allocatedinline, fieldtypes(T)))
52+
if eltype_is_invalid
5053
explanation = explain_eltype(T)
5154
error("""
5255
$name only supports element types that are allocated inline.

test/base/array.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,13 @@ mutable struct MyBadType
5555
a::Any
5656
end
5757
const MyBadType2 = Union{BigFloat, Float32}
58+
struct MyBadType3
59+
a::MyBadType2
60+
end
5861
@testset "Bad CuArray eltype" begin
5962
@test_throws ErrorException CuArray{MyBadType, 1}(undef, 64)
6063
@test_throws ErrorException CuArray{MyBadType2, 1}(undef, 64)
64+
@test_throws ErrorException CuArray{MyBadType3, 1}(undef, 64)
6165
end
6266

6367
@testset "synchronization" begin

0 commit comments

Comments
 (0)