Skip to content

Commit e4b47ac

Browse files
committed
Add generic copy stagedfunction for all types
It is a no-op for all immutable types, and tries to call the default constructor for mutable types. Supersedes #9246; see that issue for some discussion of this
1 parent dd03b5e commit e4b47ac

File tree

5 files changed

+10
-8
lines changed

5 files changed

+10
-8
lines changed

base/expr.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ copy(e::Expr) = (n = Expr(e.head);
3131
n.args = astcopy(e.args);
3232
n.typ = e.typ;
3333
n)
34-
copy(s::SymbolNode) = SymbolNode(s.name, s.typ)
35-
copy(n::GetfieldNode) = GetfieldNode(n.value, n.name, n.typ)
3634

3735
# copy parts of an AST that the compiler mutates
3836
astcopy(x::Union(SymbolNode,GetfieldNode,Expr)) = copy(x)

base/linalg/uniformscaling.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ ndims(J::UniformScaling) = 2
1212
getindex(J::UniformScaling, i::Integer,j::Integer) = ifelse(i==j,J.λ,zero(J.λ))
1313

1414
show(io::IO, J::UniformScaling) = print(io, "$(typeof(J))\n$(J.λ)*I")
15-
copy(J::UniformScaling) = UniformScaling(J.λ)
1615

1716
transpose(J::UniformScaling) = J
1817
ctranspose(J::UniformScaling) = UniformScaling(conj(J.λ))

base/operators.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,15 @@ widen{T<:Number}(x::T) = convert(widen(T), x)
170170

171171
sizeof(x) = Core.sizeof(x)
172172

173-
# copying immutable things
174-
copy(x::Union(Symbol,Number,AbstractString,Function,Tuple,LambdaStaticData,
175-
TopNode,QuoteNode,DataType,UnionType)) = x
173+
# copying immutable things is a no-op, mutable things create a new object with the same contents
174+
stagedfunction Base.copy(x)
175+
if !x.mutable
176+
:x
177+
else
178+
args = map(fld->:(x.$fld), x.names)
179+
Expr(:call, x, args...)
180+
end
181+
end
176182

177183
# function pipelining
178184
|>(x, f::Callable) = f(x)

base/process.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ end
100100

101101
immutable DevNullStream <: AsyncStream end
102102
const DevNull = DevNullStream()
103-
copy(::DevNullStream) = DevNull
104103
uvhandle(::DevNullStream) = C_NULL
105104
uvhandle(x::Ptr) = x
106105
uvtype(::Ptr) = UV_STREAM

base/range.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ maximum(r::Range) = isempty(r) ? error("range must be non-empty") : max(first(r
228228
ctranspose(r::Range) = [x for _=1, x=r]
229229
transpose(r::Range) = r'
230230

231-
# Ranges are immutable
231+
# Ranges are immutable, but the AbstractArray copy method doesn't know that
232232
copy(r::Range) = r
233233

234234

0 commit comments

Comments
 (0)