From 68ff835b856471391a49c68636bcdd46063d5b9f Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki <40514306+aviatesk@users.noreply.github.com> Date: Thu, 14 Jul 2022 08:14:59 +0900 Subject: [PATCH] mark `Symbol(::String)` as `:foldable` method (#46014) --- base/boot.jl | 19 +++++++++++++++---- test/core.jl | 4 ++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/base/boot.jl b/base/boot.jl index 057767db295fe..ddf82b9823453 100644 --- a/base/boot.jl +++ b/base/boot.jl @@ -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 @@ -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) @@ -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), diff --git a/test/core.jl b/test/core.jl index ddd501412955a..dfb7414e034a8 100644 --- a/test/core.jl +++ b/test/core.jl @@ -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