Skip to content

type assertion doesn't help type inference within method body, causing run time dispatch #55043

@nsajko

Description

@nsajko

Reproducer:

using JET

struct MyStruct
    alist::Union{Nothing,Vector{Int}}
end

function f(m)
    a = m.alist
    a::Vector{Int}
    a[1]   
end


report_opt(f, Tuple{MyStruct})  # run time dispatch

This is with Julia v1.11, as JET doesn't work on nightly Julia:

julia> versioninfo()
Julia Version 1.11.0-beta2
Commit edb3c92d6a6 (2024-05-29 09:37 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × AMD Ryzen 3 5300U with Radeon Graphics
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, znver2)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)

If this:

    a::Vector{Int}
    a[1]

... is changed to:

    (a::Vector{Int})[1]

... then there's no run time dispatch!

Metadata

Metadata

Assignees

No one assigned

    Labels

    compiler:inferenceType inferenceduplicateIndicates similar issues or pull requeststypes and dispatchTypes, subtyping and method dispatch

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions