Skip to content

Commit

Permalink
Improve effects for Base.fieldindex (#50199)
Browse files Browse the repository at this point in the history
Split out the error path into a function with separate effects assumptions,
so that constant propagation on `err` can conclude that the `err=false` case
does not throw. Fixes #50198.
  • Loading branch information
Keno authored Jun 17, 2023
1 parent ba251e8 commit fd1cec2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
12 changes: 11 additions & 1 deletion base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -820,9 +820,19 @@ julia> Base.fieldindex(Foo, :z, false)
```
"""
function fieldindex(T::DataType, name::Symbol, err::Bool=true)
return err ? _fieldindex_maythrow(T, name) : _fieldindex_nothrow(T, name)
end

function _fieldindex_maythrow(T::DataType, name::Symbol)
@_foldable_meta
@noinline
return Int(ccall(:jl_field_index, Cint, (Any, Any, Cint), T, name, err)+1)
return Int(ccall(:jl_field_index, Cint, (Any, Any, Cint), T, name, true)+1)
end

function _fieldindex_nothrow(T::DataType, name::Symbol)
@_total_meta
@noinline
return Int(ccall(:jl_field_index, Cint, (Any, Any, Cint), T, name, false)+1)
end

function fieldindex(t::UnionAll, name::Symbol, err::Bool=true)
Expand Down
5 changes: 5 additions & 0 deletions test/compiler/effects.jl
Original file line number Diff line number Diff line change
Expand Up @@ -988,3 +988,8 @@ isassigned_effects(s) = isassigned(Ref(s))
@test fully_eliminated(; retval=true) do
isassigned_effects(:foo)
end

# Effects of Base.hasfield (#50198)
hf50198(s) = hasfield(typeof((;x=1, y=2)), s)
f50198() = (hf50198(Ref(:x)[]); nothing)
@test fully_eliminated(f50198)

2 comments on commit fd1cec2

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily package evaluation, I will reply here when finished:

@nanosoldier runtests(isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your package evaluation job has completed - possible new issues were detected.
A full report can be found here.

Please sign in to comment.