Skip to content

Commit

Permalink
mark Symbol(::String) as :foldable method (JuliaLang#46014)
Browse files Browse the repository at this point in the history
  • Loading branch information
aviatesk committed Jul 13, 2022
1 parent 9815a8e commit 68ff835
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
19 changes: 15 additions & 4 deletions base/boot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,18 @@ convert(::Type{T}, x::T) where {T} = x
cconvert(::Type{T}, x) where {T} = convert(T, x)
unsafe_convert(::Type{T}, x::T) where {T} = x

_is_internal(__module__) = __module__ === Core
# can be used in place of `@assume_effects :foldable` (supposed to be used for bootstrapping)
macro _foldable_meta()
return _is_internal(__module__) && Expr(:meta, Expr(:purity,
#=:consistent=#true,
#=:effect_free=#true,
#=:nothrow=#false,
#=:terminates_globally=#true,
#=:terminates_locally=#false,
#=:notaskstate=#false))
end

const NTuple{N,T} = Tuple{Vararg{T,N}}

## primitive Array constructors
Expand Down Expand Up @@ -480,7 +492,6 @@ Array{T}(::UndefInitializer, d::NTuple{N,Int}) where {T,N} = Array{T,N}(undef, d
# empty vector constructor
Array{T,1}() where {T} = Array{T,1}(undef, 0)


(Array{T,N} where T)(x::AbstractArray{S,N}) where {S,N} = Array{S,N}(x)

Array(A::AbstractArray{T,N}) where {T,N} = Array{T,N}(A)
Expand All @@ -489,12 +500,12 @@ Array{T}(A::AbstractArray{S,N}) where {T,N,S} = Array{T,N}(A)
AbstractArray{T}(A::AbstractArray{S,N}) where {T,S,N} = AbstractArray{T,N}(A)

# primitive Symbol constructors
eval(Core, :(function Symbol(s::String)
$(Expr(:meta, :pure))
function Symbol(s::String)
@_foldable_meta
return ccall(:jl_symbol_n, Ref{Symbol}, (Ptr{UInt8}, Int),
ccall(:jl_string_ptr, Ptr{UInt8}, (Any,), s),
sizeof(s))
end))
end
function Symbol(a::Array{UInt8,1})
return ccall(:jl_symbol_n, Ref{Symbol}, (Ptr{UInt8}, Int),
ccall(:jl_array_ptr, Ptr{UInt8}, (Any,), a),
Expand Down
4 changes: 4 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7808,3 +7808,7 @@ end
import .Foo45350: x45350
f45350() = (global x45350 = 2)
@test_throws ErrorException f45350()

@testset "effect override on Symbol(::String)" begin
@test Core.Compiler.is_foldable(Base.infer_effects(Symbol, (String,)))
end

0 comments on commit 68ff835

Please sign in to comment.