diff --git a/Compiler/src/tfuncs.jl b/Compiler/src/tfuncs.jl index f39601c1e729d..f3ce3b010a345 100644 --- a/Compiler/src/tfuncs.jl +++ b/Compiler/src/tfuncs.jl @@ -450,6 +450,10 @@ end return Const(true) end end + # datatype_fieldcount is what `fieldcount` uses internally + # and returns nothing (!==0) for non-definite field counts. + elseif datatype_fieldcount(a1) === 0 + return Const(false) end elseif isa(a1, Union) # Results can only be `Const` or `Bool` diff --git a/Compiler/test/inference.jl b/Compiler/test/inference.jl index 249a614fccc5b..5b6b9f1d4749e 100644 --- a/Compiler/test/inference.jl +++ b/Compiler/test/inference.jl @@ -1210,6 +1210,7 @@ let isdefined_tfunc(@nospecialize xs...) = @test isdefined_tfunc(Union{UnionIsdefinedA,UnionIsdefinedB}, Const(:x)) === Const(true) @test isdefined_tfunc(Union{UnionIsdefinedA,UnionIsdefinedB}, Const(:y)) === Const(false) @test isdefined_tfunc(Union{UnionIsdefinedA,Nothing}, Const(:x)) === Bool + @test isdefined_tfunc(Nothing, Any) === Const(false) end # https://github.com/aviatesk/JET.jl/issues/379 diff --git a/base/runtime_internals.jl b/base/runtime_internals.jl index 0599cc0b6ec36..f9ed8b0d55c32 100644 --- a/base/runtime_internals.jl +++ b/base/runtime_internals.jl @@ -1116,7 +1116,7 @@ function datatype_fieldcount(t::DataType) return length(names) end if types isa DataType && types <: Tuple - return fieldcount(types) + return datatype_fieldcount(types) end return nothing elseif isabstracttype(t)