Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions base/char.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ represents a valid Unicode character.
"""
Char

@constprop :aggressive (::Type{T})(x::Number) where {T<:AbstractChar} = T(UInt32(x))
@constprop :aggressive (::Type{T})(x::Number) where {T<:AbstractChar} = T(UInt32(x)::UInt32)
@constprop :aggressive AbstractChar(x::Number) = Char(x)
@constprop :aggressive (::Type{T})(x::AbstractChar) where {T<:Union{Number,AbstractChar}} = T(codepoint(x))
@constprop :aggressive (::Type{T})(x::AbstractChar) where {T<:Union{Int32,Int64}} = codepoint(x) % T
Expand Down Expand Up @@ -225,9 +225,9 @@ hash(x::Char, h::UInt) =
hash_finalizer(((bitcast(UInt32, x) + UInt64(0xd4d64234)) << 32) ⊻ UInt64(h)) % UInt

# fallbacks:
isless(x::AbstractChar, y::AbstractChar) = isless(Char(x), Char(y))
==(x::AbstractChar, y::AbstractChar) = Char(x) == Char(y)
hash(x::AbstractChar, h::UInt) = hash(Char(x), h)
isless(x::AbstractChar, y::AbstractChar) = isless(Char(x)::Char, Char(y)::Char)
==(x::AbstractChar, y::AbstractChar) = Char(x)::Char == Char(y)::Char
hash(x::AbstractChar, h::UInt) = hash(Char(x)::Char, h)
widen(::Type{T}) where {T<:AbstractChar} = T

@inline -(x::AbstractChar, y::AbstractChar) = Int(x) - Int(y)
Expand Down Expand Up @@ -257,7 +257,7 @@ end
# (Packages may implement other IO subtypes to specify different encodings.)
# In contrast, `write(io, c)` outputs a `c` in an encoding determined by typeof(c).
print(io::IO, c::Char) = (write(io, c); nothing)
print(io::IO, c::AbstractChar) = print(io, Char(c)) # fallback: convert to output UTF-8
print(io::IO, c::AbstractChar) = print(io, Char(c)::Char) # fallback: convert to output UTF-8

const hex_chars = UInt8['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
Expand Down
6 changes: 3 additions & 3 deletions base/filesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ function isexecutable(path::String)
X_OK = 0x01
return ccall(:jl_fs_access, Cint, (Cstring, Cint), path, X_OK) == 0
end
isexecutable(path::AbstractString) = isexecutable(String(path))
isexecutable(path::AbstractString) = isexecutable(String(path)::String)

"""
isreadable(path::String)
Expand All @@ -417,7 +417,7 @@ function isreadable(path::String)
R_OK = 0x04
return ccall(:jl_fs_access, Cint, (Cstring, Cint), path, R_OK) == 0
end
isreadable(path::AbstractString) = isreadable(String(path))
isreadable(path::AbstractString) = isreadable(String(path)::String)

"""
iswritable(path::String)
Expand All @@ -444,7 +444,7 @@ function iswritable(path::String)
W_OK = 0x02
return ccall(:jl_fs_access, Cint, (Cstring, Cint), path, W_OK) == 0
end
iswritable(path::AbstractString) = iswritable(String(path))
iswritable(path::AbstractString) = iswritable(String(path)::String)


end
2 changes: 1 addition & 1 deletion base/iobuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ end
return sizeof(UInt8)
end

readbytes!(io::GenericIOBuffer, b::MutableDenseArrayType{UInt8}, nb=length(b)) = readbytes!(io, b, Int(nb))
readbytes!(io::GenericIOBuffer, b::MutableDenseArrayType{UInt8}, nb=length(b)) = readbytes!(io, b, Int(nb)::Int)

function readbytes!(io::GenericIOBuffer, b::MutableDenseArrayType{UInt8}, nb::Int)
io.readable || _throw_not_readable()
Expand Down
2 changes: 1 addition & 1 deletion base/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ function tryparse_internal(::Type{Float32}, s::SubString{String}, startpos::Int,
(Ptr{UInt8},Csize_t,Csize_t), s.string, s.offset+startpos-1, endpos-startpos+1)
hasvalue ? val : nothing
end
tryparse(::Type{T}, s::AbstractString) where {T<:Union{Float32,Float64}} = tryparse(T, String(s))
tryparse(::Type{T}, s::AbstractString) where {T<:Union{Float32,Float64}} = tryparse(T, String(s)::String)
tryparse(::Type{Float16}, s::AbstractString) =
convert(Union{Float16, Nothing}, tryparse(Float32, s))
tryparse_internal(::Type{Float16}, s::AbstractString, startpos::Int, endpos::Int) =
Expand Down
8 changes: 4 additions & 4 deletions base/path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ julia> splitpath("/home/myuser/example.jl")
"example.jl"
```
"""
splitpath(p::AbstractString) = splitpath(String(p))
splitpath(p::AbstractString) = splitpath(String(p)::String)

function splitpath(p::String)
drive, p = splitdrive(p)
Expand Down Expand Up @@ -608,10 +608,10 @@ function relpath(path::String, startpath::String = ".")
return isempty(relpath_) ? curdir : relpath_
end
relpath(path::AbstractString, startpath::AbstractString) =
relpath(String(path), String(startpath))
relpath(String(path)::String, String(startpath)::String)

for f in (:isdirpath, :splitdir, :splitdrive, :splitext, :normpath, :abspath)
@eval $f(path::AbstractString) = $f(String(path))
@eval $f(path::AbstractString) = $f(String(path)::String)
end

# RFC3986 Section 2.1
Expand Down Expand Up @@ -665,4 +665,4 @@ else
end
end

uripath(path::AbstractString) = uripath(String(path))
uripath(path::AbstractString) = uripath(String(path)::String)
2 changes: 1 addition & 1 deletion base/stat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ function ispath(path::String)
end
return r == 0
end
ispath(path::AbstractString) = ispath(String(path))
ispath(path::AbstractString) = ispath(String(path)::String)

"""
isfifo(path)::Bool
Expand Down
18 changes: 9 additions & 9 deletions base/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ Vector{UInt8}(s::AbstractString) = unsafe_wrap(Vector{UInt8}, String(s))
Array{UInt8}(s::AbstractString) = unsafe_wrap(Vector{UInt8}, String(s))
Vector{T}(s::AbstractString) where {T<:AbstractChar} = collect(T, s)

Symbol(s::AbstractString) = Symbol(String(s))
Symbol(s::AbstractString) = Symbol(String(s)::String)
Symbol(x...) = Symbol(string(x...))

convert(::Type{T}, s::T) where {T<:AbstractString} = s
Expand Down Expand Up @@ -364,7 +364,7 @@ isless(a::Symbol, b::Symbol) = cmp(a, b) < 0

# hashing

hash(s::AbstractString, h::UInt) = hash(String(s), h)
hash(s::AbstractString, h::UInt) = hash(String(s)::String, h)

## character index arithmetic ##

Expand Down Expand Up @@ -412,7 +412,7 @@ function length(s::AbstractString, i::Int, j::Int)
end

@propagate_inbounds length(s::AbstractString, i::Integer, j::Integer) =
length(s, Int(i), Int(j))
length(s, Int(i)::Int, Int(j)::Int)

"""
thisind(s::AbstractString, i::Integer)::Int
Expand Down Expand Up @@ -446,7 +446,7 @@ ERROR: BoundsError: attempt to access 2-codeunit String at index [-1]
[...]
```
"""
thisind(s::AbstractString, i::Integer) = thisind(s, Int(i))
thisind(s::AbstractString, i::Integer) = thisind(s, Int(i)::Int)

function thisind(s::AbstractString, i::Int)
z = ncodeunits(s)::Int + 1
Expand Down Expand Up @@ -502,8 +502,8 @@ julia> prevind("α", 2, 3)
-1
```
"""
prevind(s::AbstractString, i::Integer, n::Integer) = prevind(s, Int(i), Int(n))
prevind(s::AbstractString, i::Integer) = prevind(s, Int(i))
prevind(s::AbstractString, i::Integer, n::Integer) = prevind(s, Int(i)::Int, Int(n)::Int)
prevind(s::AbstractString, i::Integer) = prevind(s, Int(i)::Int)
prevind(s::AbstractString, i::Int) = prevind(s, i, 1)

function prevind(s::AbstractString, i::Int, n::Int)
Expand Down Expand Up @@ -561,8 +561,8 @@ julia> nextind("α", 1, 2)
4
```
"""
nextind(s::AbstractString, i::Integer, n::Integer) = nextind(s, Int(i), Int(n))
nextind(s::AbstractString, i::Integer) = nextind(s, Int(i))
nextind(s::AbstractString, i::Integer, n::Integer) = nextind(s, Int(i)::Int, Int(n)::Int)
nextind(s::AbstractString, i::Integer) = nextind(s, Int(i)::Int)
nextind(s::AbstractString, i::Int) = nextind(s, i, 1)

function nextind(s::AbstractString, i::Int, n::Int)
Expand Down Expand Up @@ -757,7 +757,7 @@ julia> repeat("ha", 3)
"hahaha"
```
"""
repeat(s::AbstractString, r::Integer) = repeat(String(s), r)
repeat(s::AbstractString, r::Integer) = repeat(String(s)::String, r)

"""
^(s::Union{AbstractString,AbstractChar}, n::Integer)::AbstractString
Expand Down
2 changes: 1 addition & 1 deletion base/strings/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ pointer(s::String, i::Integer) = pointer(s) + Int(i)::Int - 1
ncodeunits(s::String) = Core.sizeof(s)
codeunit(s::String) = UInt8

codeunit(s::String, i::Integer) = codeunit(s, Int(i))
codeunit(s::String, i::Integer) = codeunit(s, Int(i)::Int)
@assume_effects :foldable @inline function codeunit(s::String, i::Int)
@boundscheck checkbounds(s, i)
b = GC.@preserve s unsafe_load(pointer(s, i))
Expand Down
2 changes: 1 addition & 1 deletion base/strings/substring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ end

@propagate_inbounds SubString(s::T, i::Int, j::Int) where {T<:AbstractString} = SubString{T}(s, i, j)
@propagate_inbounds SubString(s::T, i::Int, j::Int, v::Val{:noshift}) where {T<:AbstractString} = SubString{T}(s, i, j, v)
@propagate_inbounds SubString(s::AbstractString, i::Integer, j::Integer=lastindex(s)) = SubString(s, Int(i), Int(j))
@propagate_inbounds SubString(s::AbstractString, i::Integer, j::Integer=lastindex(s)) = SubString(s, Int(i)::Int, Int(j)::Int)
@propagate_inbounds SubString(s::AbstractString, r::AbstractUnitRange{<:Integer}) = SubString(s, first(r), last(r))

@propagate_inbounds function SubString(s::SubString, i::Int, j::Int)
Expand Down
2 changes: 1 addition & 1 deletion base/strings/unicode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ const _julia_charmap = Dict{UInt32,UInt32}(
0x210F => 0x0127, # hbar -> small letter h with stroke (#48870)
)

utf8proc_map(s::AbstractString, flags::Integer, chartransform::F = identity) where F = utf8proc_map(String(s), flags, chartransform)
utf8proc_map(s::AbstractString, flags::Integer, chartransform::F = identity) where F = utf8proc_map(String(s)::String, flags, chartransform)

# Documented in Unicode module
function normalize(
Expand Down
6 changes: 3 additions & 3 deletions base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function Base.startswith(io::IO, prefix::Union{String,SubString{String}})
reset(io)
return s == codeunits(prefix)
end
Base.startswith(io::IO, prefix::AbstractString) = startswith(io, String(prefix))
Base.startswith(io::IO, prefix::AbstractString) = startswith(io, String(prefix)::String)

function endswith(a::Union{String, SubString{String}},
b::Union{String, SubString{String}})
Expand Down Expand Up @@ -1216,7 +1216,7 @@ function hex2bytes!(dest::AbstractArray{UInt8}, itr)
return dest
end

@inline number_from_hex(c::AbstractChar) = number_from_hex(Char(c))
@inline number_from_hex(c::AbstractChar) = number_from_hex(Char(c)::Char)
@inline number_from_hex(c::Char) = number_from_hex(UInt8(c))
@inline function number_from_hex(c::UInt8)
UInt8('0') <= c <= UInt8('9') && return c - UInt8('0')
Expand Down Expand Up @@ -1299,7 +1299,7 @@ julia> ascii("abcdefgh")
"abcdefgh"
```
"""
ascii(x::AbstractString) = ascii(String(x))
ascii(x::AbstractString) = ascii(String(x)::String)

Base.rest(s::Union{String,SubString{String}}, i=1) = SubString(s, i)
function Base.rest(s::AbstractString, st...)
Expand Down
2 changes: 1 addition & 1 deletion base/sysinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ function which(program_name::String)
# If we couldn't find anything, don't return anything
nothing
end
which(program_name::AbstractString) = which(String(program_name))
which(program_name::AbstractString) = which(String(program_name)::String)

"""
Sys.username()::String
Expand Down
3 changes: 2 additions & 1 deletion test/choosetests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const TESTNAMES = [
"channels", "iostream", "secretbuffer", "specificity",
"reinterpretarray", "syntax", "corelogging", "missing", "asyncmap",
"smallarrayshrink", "opaque_closure", "filesystem", "download",
"scopedvalues", "compileall", "rebinding"
"scopedvalues", "compileall", "rebinding",
"faulty_constructor_method_should_not_cause_stack_overflows"
]

const INTERNET_REQUIRED_LIST = [
Expand Down
54 changes: 54 additions & 0 deletions test/faulty_constructor_method_should_not_cause_stack_overflows.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
for (typ, sup) in (
(:Char, :AbstractChar),
(:String, :AbstractString),
(:Int, :Integer),
(:UInt32, :Integer),
)
fau = Symbol("Faulty", typ)
@eval struct $fau <: $sup end
@eval function Base.$typ(x::$fau) x end
end

using Test
using Unicode: Unicode

@testset "faulty constructor method for new type should not cause stack overflows" begin
exc = Union{TypeError,MethodError}
@testset let x = FaultyChar()
@test_throws exc isless(x, x)
@test_throws exc x == x
@test_throws exc hash(x, UInt(3))
@test_throws exc print(devnull, x)
@test_throws exc hex2bytes!(Vector{UInt8}(undef, 1), (x, x))
end
@testset let x = FaultyString()
@test_throws exc hash(x, UInt(3))
@test_throws exc repeat(x, 3)
@test_throws exc startswith(devnull, x)
@test_throws exc relpath(x, x)
@test_throws exc tryparse(Float32, x)
@test_throws exc tryparse(Float64, x)
for f in (
Symbol, ascii, splitpath, isdirpath, splitdir, splitdrive, splitext, normpath, abspath, ispath,
Base.Filesystem.uripath,
Sys.which, Sys.isexecutable, Sys.isreadable, Sys.iswritable,
Unicode.normalize,
)
@test_throws exc f(x)
end
end
@testset let x = FaultyInt()
@test_throws exc readbytes!(IOBuffer(), Vector{UInt8}(undef, 0), x)
@test_throws exc length("", x, x)
@test_throws exc thisind("", x)
@test_throws exc prevind("", x)
@test_throws exc prevind("", x, x)
@test_throws exc nextind("", x)
@test_throws exc nextind("", x, x)
@test_throws exc codeunit("", x)
@test_throws exc SubString("", x, x)
end
@testset let x = FaultyUInt32()
@test_throws exc Char(x)
end
end