From 7953e39df15a24693b0441321865aea7dba1f0e6 Mon Sep 17 00:00:00 2001 From: Chris Foster Date: Mon, 27 Jul 2020 14:03:22 +1000 Subject: [PATCH] Make Base.ifelse a generic function Allow user code to directly extend `Base.ifelse` rather than needing a special package for it. --- base/boot.jl | 4 ++-- base/compiler/abstractinterpretation.jl | 2 +- base/compiler/optimize.jl | 2 +- base/compiler/tfuncs.jl | 2 +- base/essentials.jl | 16 ++++++++++++++++ base/operators.jl | 16 ---------------- 6 files changed, 21 insertions(+), 21 deletions(-) diff --git a/base/boot.jl b/base/boot.jl index ee1370bf5dff1..86541c6da827b 100644 --- a/base/boot.jl +++ b/base/boot.jl @@ -192,8 +192,8 @@ export Expr, QuoteNode, LineNumberNode, GlobalRef, # object model functions fieldtype, getfield, setfield!, swapfield!, modifyfield!, replacefield!, - nfields, throw, tuple, ===, isdefined, eval, ifelse, - # sizeof # not exported, to avoid conflicting with Base.sizeof + nfields, throw, tuple, ===, isdefined, eval, + # ifelse, sizeof # not exported, to avoid conflicting with Base # type reflection <:, typeof, isa, typeassert, # method reflection diff --git a/base/compiler/abstractinterpretation.jl b/base/compiler/abstractinterpretation.jl index 4af4eb672bb6d..bb91bf15962e8 100644 --- a/base/compiler/abstractinterpretation.jl +++ b/base/compiler/abstractinterpretation.jl @@ -1039,7 +1039,7 @@ function abstract_call_builtin(interp::AbstractInterpreter, f::Builtin, fargs::U argtypes::Vector{Any}, sv::InferenceState, max_methods::Int) @nospecialize f la = length(argtypes) - if f === ifelse && fargs isa Vector{Any} && la == 4 + if f === Core.ifelse && fargs isa Vector{Any} && la == 4 cnd = argtypes[2] if isa(cnd, Conditional) newcnd = widenconditional(cnd) diff --git a/base/compiler/optimize.jl b/base/compiler/optimize.jl index 1898aa8b75778..2594c49815140 100644 --- a/base/compiler/optimize.jl +++ b/base/compiler/optimize.jl @@ -139,7 +139,7 @@ const _PURE_BUILTINS = Any[tuple, svec, ===, typeof, nfields] const _PURE_OR_ERROR_BUILTINS = [ fieldtype, apply_type, isa, UnionAll, getfield, arrayref, const_arrayref, isdefined, Core.sizeof, - Core.kwfunc, ifelse, Core._typevar, (<:) + Core.kwfunc, Core.ifelse, Core._typevar, (<:) ] const TOP_TUPLE = GlobalRef(Core, :tuple) diff --git a/base/compiler/tfuncs.jl b/base/compiler/tfuncs.jl index a43c9dd9afff1..2db230f425ef9 100644 --- a/base/compiler/tfuncs.jl +++ b/base/compiler/tfuncs.jl @@ -230,7 +230,7 @@ function ifelse_tfunc(@nospecialize(cnd), @nospecialize(x), @nospecialize(y)) end return tmerge(x, y) end -add_tfunc(ifelse, 3, 3, ifelse_tfunc, 1) +add_tfunc(Core.ifelse, 3, 3, ifelse_tfunc, 1) function egal_tfunc(@nospecialize(x), @nospecialize(y)) xx = widenconditional(x) diff --git a/base/essentials.jl b/base/essentials.jl index 5280252f1946a..cd4b618b5b0b1 100644 --- a/base/essentials.jl +++ b/base/essentials.jl @@ -477,6 +477,22 @@ Stacktrace: """ sizeof(x) = Core.sizeof(x) +""" + ifelse(condition::Bool, x, y) + +Return `x` if `condition` is `true`, otherwise return `y`. This differs from `?` or `if` in +that it is an ordinary function, so all the arguments are evaluated first. In some cases, +using `ifelse` instead of an `if` statement can eliminate the branch in generated code and +provide higher performance in tight loops. + +# Examples +```jldoctest +julia> ifelse(1 > 2, 1, 2) +2 +``` +""" +ifelse(condition::Bool, x, y) = Core.ifelse(condition, x, y) + # simple Array{Any} operations needed for bootstrap @eval setindex!(A::Array{Any}, @nospecialize(x), i::Int) = arrayset($(Expr(:boundscheck)), A, x, i) diff --git a/base/operators.jl b/base/operators.jl index 74cf3e95145a6..076105c6ac9b9 100644 --- a/base/operators.jl +++ b/base/operators.jl @@ -429,22 +429,6 @@ const ≥ = >= # which is more idiomatic: isless(x::Real, y::Real) = x ifelse(1 > 2, 1, 2) -2 -``` -""" -ifelse - """ cmp(x,y)