8000 Too eager reporting of failed typeassert · Issue #704 · aviatesk/JET.jl · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Too eager reporting of failed typeassert #704
Open
@jakobnissen

Description

@jakobnissen

MWE:

julia> @report_call empty!(Dict{String,Union{String, UInt}}())
═════ 1 possible error found ═════
┌ empty!(h::Dict{String, Union{UInt64, String}}) @ Base ./dict.jl:225
│┌ _unsetindex!(A::Memory{Union{UInt64, String}}, i::Int64) @ Base ./genericmemory.jl:77
││┌ _unsetindex!(A::MemoryRef{Union{UInt64, String}}) @ Base ./genericmemory.jl:88
│││ invalid builtin function call: typeassert(T::Type{Union{UInt64, String}}, DataType)
││└────────────────────

This happens due to this code in Base (slightly modified):

arrayelem = datatype_arrayelem(MemT)
isbits = 0; isboxed = 1; isunion = 2
arrayelem == isbits && datatype_pointerfree(T::DataType) && return A

where

  1. The function datatype_arrayelem is a C function that returns 0, 1, or 2 depending on how a given Memory{T} subtype stores its elements.
  2. In the error case, the memory being passed in is the Memory{String, UInt} in the dict. Therefore, the C function will return 2
  3. The line arrayelem == isbits && datatype_pointerfree(T::DataType) && return A will never execute. However, the compiler does not know this.
  4. The issue occurs because JET, at compile time, knows that T::DataType will fail, since T === Union{String, UInt}.

The core problem is that JET will flag typeasserts it knows at compile time will fail. However, we typically use typeasserts precisely when we have type information which the compiler doesn't.
In this case, we know a priori that arrayelem == isunion.

Possible solutions:

  1. Make JET less eager to report typeasserts as errors, unless the compiler can prove that the typeassert is going to be executed with the wrong type
  2. Add a mechanism in Base to disable type checking (that's probably not a good idea)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0