Skip to content
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

make &x sugar for RefValue(x) #27608

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ julia> Broadcast.broadcastable("hello") # Strings break convention of matching i
Base.RefValue{String}("hello")
```
"""
broadcastable(x::Union{Symbol,AbstractString,Function,UndefInitializer,Nothing,RoundingMode,Missing,Val}) = Ref(x)
broadcastable(x::Union{Symbol,AbstractString,Function,UndefInitializer,Nothing,RoundingMode,Missing,Val}) = &x
broadcastable(x::Ptr) = Ref{Ptr}(x) # Cannot use Ref(::Ptr) until ambiguous deprecation goes through
broadcastable(::Type{T}) where {T} = Ref{Type{T}}(T)
broadcastable(x::Union{AbstractArray,Number,Ref,Tuple,Broadcasted}) = x
Expand Down
2 changes: 1 addition & 1 deletion base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ end
@noinline function Broadcast.broadcastable(x)
depwarn("""
broadcast will default to iterating over its arguments in the future. Wrap arguments of
type `x::$(typeof(x))` with `Ref(x)` to ensure they broadcast as "scalar" elements.
type `x::$(typeof(x))` with `&x` to ensure they broadcast as "scalar" elements.
""", (:broadcast, :broadcast!))
return Ref{typeof(x)}(x)
end
Expand Down
6 changes: 3 additions & 3 deletions base/event.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ function yield(t::Task, @nospecialize x = nothing)
t.state == :runnable || error("schedule: Task not runnable")
t.result = x
enq_work(current_task())
return try_yieldto(ensure_rescheduled, Ref(t))
return try_yieldto(ensure_rescheduled, &t)
end

"""
Expand All @@ -179,7 +179,7 @@ or scheduling in any way. Its use is discouraged.
"""
function yieldto(t::Task, @nospecialize x = nothing)
t.result = x
return try_yieldto(identity, Ref(t))
return try_yieldto(identity, &t)
end

function try_yieldto(undo, reftask::Ref{Task})
Expand Down Expand Up @@ -237,7 +237,7 @@ end
return
end
t.state = :runnable
return Ref(t)
return &t
end

function wait()
Expand Down
2 changes: 1 addition & 1 deletion base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ unsafe_write(s::IO, p::Ptr, n::Integer) = unsafe_write(s, convert(Ptr{UInt8}, p)
write(s::IO, x::Ref{T}) where {T} = unsafe_write(s, x, Core.sizeof(T))
write(s::IO, x::Int8) = write(s, reinterpret(UInt8, x))
function write(s::IO, x::Union{Int16,UInt16,Int32,UInt32,Int64,UInt64,Int128,UInt128,Float16,Float32,Float64})
return write(s, Ref(x))
return write(s, &x)
end

write(s::IO, x::Bool) = write(s, UInt8(x))
Expand Down
4 changes: 2 additions & 2 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ const include_callbacks = Any[]
# used to optionally track dependencies when requiring a module:
const _concrete_dependencies = Pair{PkgId,UInt64}[] # these dependency versions are "set in stone", and the process should try to avoid invalidating them
const _require_dependencies = Any[] # a list of (mod, path, mtime) tuples that are the file dependencies of the module currently being precompiled
const _track_dependencies = Ref(false) # set this to true to track the list of file dependencies
const _track_dependencies = &false # set this to true to track the list of file dependencies
function _include_dependency(mod::Module, _path::AbstractString)
prev = source_path(nothing)
if prev === nothing
Expand Down Expand Up @@ -840,7 +840,7 @@ function __precompile__(isprecompilable::Bool=true)
end

# require always works in Main scope and loads files from node 1
const toplevel_load = Ref(true)
const toplevel_load = &true

"""
require(module::Symbol)
Expand Down
2 changes: 1 addition & 1 deletion base/logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ end

# Global log limiting mechanism for super fast but inflexible global log
# limiting.
const _min_enabled_level = Ref(Debug)
const _min_enabled_level = &Debug

# LogState - a concretely typed cache of data extracted from the logger, plus
# the logger itself.
Expand Down
2 changes: 1 addition & 1 deletion base/refpointer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ freeing by the garbage collector as long as the `Ref` itself is referenced.

In Julia, `Ref` objects are dereferenced (loaded or stored) with `[]`.

Creation of a `Ref` to a value `x` of type `T` is usually written `Ref(x)`.
Creation of a `Ref` to a value `x` of type `T` is usually written `&x` or `Ref(x)`.
Additionally, for creating interior pointers to containers (such as Array or Ptr),
it can be written `Ref(a, i)` for creating a reference to the `i`-th element of `a`.

Expand Down
2 changes: 1 addition & 1 deletion base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ function show_default(io::IO, @nospecialize(x))
end
else
print(io, "0x")
r = Ref(x)
r = &x
GC.@preserve r begin
p = unsafe_convert(Ptr{Cvoid}, r)
for i in (nb - 1):-1:0
Expand Down
2 changes: 1 addition & 1 deletion base/threadingconstructs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ on `threadid()`.
nthreads() = Int(unsafe_load(cglobal(:jl_n_threads, Cint)))

# Only read/written by the main thread
const in_threaded_loop = Ref(false)
const in_threaded_loop = &false

function _threadsfor(iter,lbody)
lidx = iter.args[1] # index
Expand Down
2 changes: 1 addition & 1 deletion base/version.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function print(io::IO, v::VersionNumber)
end
show(io::IO, v::VersionNumber) = print(io, "v\"", v, "\"")

Broadcast.broadcastable(v::VersionNumber) = Ref(v)
Broadcast.broadcastable(v::VersionNumber) = &v

const VERSION_REGEX = r"^
v? # prefix (optional)
Expand Down
2 changes: 1 addition & 1 deletion doc/src/manual/parallel-computing.md
Original file line number Diff line number Diff line change
Expand Up @@ -1480,7 +1480,7 @@ julia> using Base.Threads
julia> nthreads()
4

julia> acc = Ref(0)
julia> acc = &0
Base.RefValue{Int64}(0)

julia> @threads for i in 1:1000
Expand Down
25 changes: 7 additions & 18 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -959,12 +959,10 @@
(if (eq? ty 'Any)
(loop (if isseq F (cdr F)) (cdr A) stmts (list* a C) GC)
(let* ((g (make-ssavalue))
(isamp (and (pair? a) (eq? (car a) '&)))
(a (if isamp (cadr a) a))
(stmts (cons `(= ,g (call (top ,(if isamp 'ptr_arg_cconvert 'cconvert)) ,ty ,a)) stmts))
(ca `(call (top ,(if isamp 'ptr_arg_unsafe_convert 'unsafe_convert)) ,ty ,g)))
(stmts (cons `(= ,g (call (top cconvert) ,ty ,a)) stmts))
(ca `(call (top unsafe_convert) ,ty ,g)))
(loop (if isseq F (cdr F)) (cdr A) stmts
(list* (if isamp `(& ,ca) ca) C) (list* g GC))))))))
(list* ca C) (list* g GC))))))))

(define (expand-function-def e) ;; handle function definitions
(define (just-arglist? ex)
Expand Down Expand Up @@ -2060,6 +2058,10 @@
'comparison
(lambda (e) (expand-forms (expand-compare-chain (cdr e))))

'&
(lambda (e)
(expand-forms `(call (top RefValue) ,(cadr e))))

'ref
(lambda (e)
(let ((args (cddr e)))
Expand Down Expand Up @@ -3552,14 +3554,6 @@ f(x) = yt(x)
((call new foreigncall cfunction)
(let* ((args
(cond ((eq? (car e) 'foreigncall)
(for-each (lambda (a)
(if (and (length= a 2) (eq? (car a) '&))
(deprecation-message
(string "Syntax `&argument`" (linenode-string current-loc)
" is deprecated. Remove the `&` and use a `Ref` argument "
"type instead.")
current-loc)))
(list-tail e 6))
;; NOTE: 2nd to 5th arguments of ccall must be left in place
;; the 1st should be compiled if an atom.
(append (if (or (atom? (cadr e))
Expand Down Expand Up @@ -3787,11 +3781,6 @@ f(x) = yt(x)
(loop (cdr actions)))))))
val)))

((&)
(if (or (not value) tail)
(error "misplaced \"&\" expression"))
`(& ,(compile (cadr e) break-labels value tail)))

((newvar)
;; avoid duplicate newvar nodes
(if (not (and (pair? code) (equal? (car code) e)))
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Dates/src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ end
(-)(x::AbstractRange{T}, y::AbstractRange{T}) where {T<:TimeType} = Vector(x) - Vector(y)

# Allow dates and times to broadcast as unwrapped scalars
Base.Broadcast.broadcastable(x::AbstractTime) = Ref(x)
Base.Broadcast.broadcastable(x::AbstractTime) = &x
2 changes: 1 addition & 1 deletion stdlib/Dates/src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ function Base.show(io::IO, df::DateFormat)
end
print(io, '"')
end
Base.Broadcast.broadcastable(x::DateFormat) = Ref(x)
Base.Broadcast.broadcastable(x::DateFormat) = &x

"""
dateformat"Y-m-d H:M:S"
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Distributed/src/clusterserialize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ end
ClusterSerializer(io::IO) = ClusterSerializer{typeof(io)}(io)

const object_numbers = WeakKeyDict()
const obj_number_salt = Ref(0)
const obj_number_salt = &0
function object_number(s::ClusterSerializer, @nospecialize(l))
global obj_number_salt, object_numbers
if haskey(object_numbers, l)
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Distributed/src/messages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

abstract type AbstractMsg end

const REF_ID = Ref(1)
const REF_ID = &1
next_ref_id() = (id = REF_ID[]; REF_ID[] = id+1; id)

struct RRID
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Distributed/test/distributed_exec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ let (p, p2) = filter!(p -> p != myid(), procs())
@test !remotecall_fetch(isdefined, p2, Main, :defined_on_p)
@test nothing === @everywhere [p, p] defined_on_p += 1
@test 3 === @everywhere p defined_on_p
let ref = Ref(0)
let ref = &0
@test nothing ===
@everywhere [myid(), p, myid(), myid(), p] begin
Test.@test Main === @__MODULE__
Expand Down
2 changes: 1 addition & 1 deletion stdlib/InteractiveUtils/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ end
@which get_A18434()(1, y=2)
@test counter18434 == 2

let _true = Ref(true), f, g, h
let _true = &true, f, g, h
@noinline f() = ccall((:time, "error_library_doesnt_exist\0"), Cvoid, ()) # some expression that throws an error in codegen
@noinline g() = _true[] ? 0 : h()
@noinline h() = (g(); f())
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LibGit2/src/blame.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function GitBlame(repo::GitRepo, path::AbstractString; options::BlameOptions=Bla
blame_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
@check ccall((:git_blame_file, :libgit2), Cint,
(Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Cstring, Ptr{BlameOptions}),
blame_ptr_ptr, repo.ptr, path, Ref(options))
blame_ptr_ptr, repo.ptr, path, &options)
return GitBlame(repo, blame_ptr_ptr[])
end

Expand Down
2 changes: 1 addition & 1 deletion stdlib/LibGit2/src/index.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ before the attempted merge, stage `1` is the changes which have been made locall
stages `2` and larger are for changes from other branches (for instance, in the case
of a multi-branch "octopus" merge, stages `2`, `3`, and `4` might be used).
"""
stage(ie::IndexEntry) = ccall((:git_index_entry_stage, :libgit2), Cint, (Ptr{IndexEntry},), Ref(ie))
stage(ie::IndexEntry) = ccall((:git_index_entry_stage, :libgit2), Cint, (Ptr{IndexEntry},), &ie)

function Base.show(io::IO, idx::GitIndex)
println(io, "GitIndex:\nRepository: ", repository(idx), "\nNumber of elements: ", count(idx))
Expand Down
4 changes: 2 additions & 2 deletions stdlib/LibGit2/src/merge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function GitAnnotated(repo::GitRepo, commit_id::GitHash)
ann_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
@check ccall((:git_annotated_commit_lookup, :libgit2), Cint,
(Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Ptr{GitHash}),
ann_ptr_ptr, repo.ptr, Ref(commit_id))
ann_ptr_ptr, repo.ptr, &commit_id)
return GitAnnotated(repo, ann_ptr_ptr[])
end

Expand Down Expand Up @@ -145,7 +145,7 @@ function merge!(repo::GitRepo, anns::Vector{GitAnnotated};
(Ptr{Cvoid}, Ptr{Ptr{Cvoid}}, Csize_t,
Ptr{MergeOptions}, Ptr{CheckoutOptions}),
repo.ptr, Base.map(x->x.ptr, anns), anns_size,
Ref(merge_opts), Ref(checkout_opts))
&merge_opts, &checkout_opts)
@info "Review and commit merged changes"
return true
end
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LibGit2/src/oid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ Base.hash(id::GitHash, h::UInt) = hash(id.val, h)
function Base.cmp(id1::GitHash, id2::GitHash)
Int(ccall((:git_oid_cmp, :libgit2), Cint,
(Ptr{GitHash}, Ptr{GitHash}),
Ref(id1), Ref(id2)))
&id1, &id2))
end
function Base.cmp(id1::GitShortHash, id2::GitShortHash)
# shortened hashes appear at the beginning of the order, i.e.
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LibGit2/src/rebase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function GitRebase(repo::GitRepo, branch::GitAnnotated, upstream::GitAnnotated;
(Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid},
Ptr{Cvoid}, Ptr{RebaseOptions}),
rebase_ptr_ptr, repo.ptr, branch.ptr, upstream.ptr,
onto === nothing ? C_NULL : onto.ptr, Ref(opts))
onto === nothing ? C_NULL : onto.ptr, &opts)
return GitRebase(repo, rebase_ptr_ptr[])
end

Expand Down
4 changes: 2 additions & 2 deletions stdlib/LibGit2/src/reference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function GitReference(repo::GitRepo, obj_oid::GitHash, refname::AbstractString =
ref_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
@check ccall((:git_reference_create, :libgit2), Cint,
(Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Ptr{UInt8}, Ptr{GitHash}, Cint, Cstring),
ref_ptr_ptr, repo.ptr, refname, Ref(obj_oid), Cint(force),
ref_ptr_ptr, repo.ptr, refname, &obj_oid, Cint(force),
isempty(msg) ? C_NULL : msg)
return GitReference(repo, ref_ptr_ptr[])
end
Expand Down Expand Up @@ -307,7 +307,7 @@ function target!(ref::GitReference, new_oid::GitHash; msg::AbstractString="")
ref_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
@check ccall((:git_reference_set_target, :libgit2), Cint,
(Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Ptr{GitHash}, Cstring),
ref_ptr_ptr, ref.ptr, Ref(new_oid), isempty(msg) ? C_NULL : msg)
ref_ptr_ptr, ref.ptr, &new_oid, isempty(msg) ? C_NULL : msg)
return GitReference(ref.owner, ref_ptr_ptr[])
end

Expand Down
4 changes: 2 additions & 2 deletions stdlib/LibGit2/src/remote.jl
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ function fetch(rmt::GitRemote, refspecs::Vector{<:AbstractString};
msg = "libgit2.fetch: $msg"
@check ccall((:git_remote_fetch, :libgit2), Cint,
(Ptr{Cvoid}, Ptr{StrArrayStruct}, Ptr{FetchOptions}, Cstring),
rmt.ptr, isempty(refspecs) ? C_NULL : refspecs, Ref(options), msg)
rmt.ptr, isempty(refspecs) ? C_NULL : refspecs, &options, msg)
end

"""
Expand All @@ -309,7 +309,7 @@ function push(rmt::GitRemote, refspecs::Vector{<:AbstractString};
force::Bool = false, options::PushOptions = PushOptions())
@check ccall((:git_remote_push, :libgit2), Cint,
(Ptr{Cvoid}, Ptr{StrArrayStruct}, Ptr{PushOptions}),
rmt.ptr, isempty(refspecs) ? C_NULL : refspecs, Ref(options))
rmt.ptr, isempty(refspecs) ? C_NULL : refspecs, &options)
end

"""
Expand Down
Loading