Skip to content

[1.10] Function returns wrong value instead of throwing an error #53062

Closed
@vchuravy

Description

@vchuravy
julia> function f1(cond)
           x = Ref{Int}(0)
           if cond
               x[] = x
           else
               return -1
           end
       end
f1 (generic function with 1 method)

julia> @code_typed f1(true)
CodeInfo(
1 ─ %1 = %new(Base.RefValue{Int64}, 0)::Base.RefValue{Int64}
└──      goto #3 if not cond
2 ─      invoke Base.setindex!(%1::Base.RefValue{Int64}, %1::Base.RefValue{Int64})::Union{}
└──      unreachable
3 ─      return -1
) => Int64

julia> f1(true)
-1

julia> f1(false)
-1

julia> @code_llvm optimize=false f1(true)
;  @ REPL[1]:1 within `f1`
define i64 @julia_f1_305(i8 zeroext %0) #0 {
top:
  %1 = call {}*** @julia.get_pgcstack()
  %2 = bitcast {}*** %1 to {}**
  %current_task = getelementptr inbounds {}*, {}** %2, i64 -14
  %3 = bitcast {}** %current_task to i64*
  %world_age = getelementptr inbounds i64, i64* %3, i64 15
  ret i64 -1
}

julia> Base.code_ircode(f1, (Bool,))
1-element Vector{Any}:
2 1 ─ %1 = %new(Base.RefValue{Int64}, 0)::Base.RefValue{Int64}        │╻╷ Ref
3 └──      goto #3 if not _2                                          │  
4 2 ─      invoke Base.setindex!(%1::Base.RefValue{Int64}, %1::Base.RefValue{Int64})::Union{}
  └──      unreachable                                                │  
6 3 ─      return -1                                                  │  
   => Int64                                                   

This seems fixed on nightly and on 1.9, but not on 1.10

@aviatesk maybe you can take a look? I am very confused by the fact that code_ircode seems to give the right answer, but by the time we end up at LLVM someone replaced it all with a constant.

Metadata

Metadata

Assignees

Labels

correctness bug ⚠Bugs that are likely to lead to incorrect results in user code without throwing

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions