Skip to content

Empty NamedTuple definitions for get to appease JET #58214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

quinnj
Copy link
Member

@quinnj quinnj commented Apr 24, 2025

Without these, we end up with JET errors like:

┌ get(f::StructUtils.var"#31#33", nt::@NamedTuple{}, key::Int64) @ Base ./namedtuple.jl:389
 │ invalid builtin function call: Base.getfield(nt::@NamedTuple{}, key::Int64)

I'm no expert in JET, and not sure if there are other simpler ways to fix this kind of thing, but it didn't seem like any assertion would work (can't assert that a NamedTuple has at least 1 key). Or because the isdefined on NamedTuple doesn't provide enough guarantee that nt is then definitely not empty? cc: @aviatesk

Without these, we end up with JET errors like:

```
┌ get(f::StructUtils.var"#31#33", nt::@NamedTuple{}, key::Int64) @ Base ./namedtuple.jl:389
│ invalid builtin function call: Base.getfield(nt::@NamedTuple{}, key::Int64)
```
@JeffBezanson
Copy link
Member

It seems like this should be fixed by improving analysis precision; we should know the getfield is not reachable. Also JET should have (I think it already has?) a no-false-positives mode, where it only gives errors like this if it can prove they are reachable.

@vtjnash
Copy link
Member

vtjnash commented Apr 24, 2025

It is perhaps just a general inference precision problem, that we return Bool instead of Const(false) when the object has no fields

julia> code_typed((Int,)) do x; isdefined(nothing, x); end
1-element Vector{Any}:
 CodeInfo(
    @ REPL[4]:1 within `#14`
1 ─ %1 =   builtin Main.isdefined(Main.nothing, x)::Bool
└──      return %1
) => Bool

@oscardssmith
Copy link
Member

@vtjnash yeah looks like #58220 should fix it.

@quinnj
Copy link
Member Author

quinnj commented Apr 25, 2025

Closing in favor of #58220

@quinnj quinnj closed this Apr 25, 2025
@giordano giordano deleted the jq-getfield-empty-nt branch May 2, 2025 22:04
oscardssmith added a commit that referenced this pull request May 3, 2025
alternate to #58214.

---------

Co-authored-by: Jeff Bezanson <[email protected]>
aviatesk pushed a commit that referenced this pull request May 3, 2025
alternate to #58214.

---------

Co-authored-by: Jeff Bezanson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants