Skip to content

Commit

Permalink
Add some type-asserts and argtypes
Browse files Browse the repository at this point in the history
The main concern here is the specification of types in the REPL code
required changes to the tests. However, since these are testing
internal functions that does not seem to be too serious a concern.
  • Loading branch information
timholy committed Aug 23, 2020
1 parent 151ab86 commit 61b581d
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 47 deletions.
4 changes: 2 additions & 2 deletions base/Enums.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ julia> instances(Fruit)
(apple, orange, kiwi)
```
"""
macro enum(T, syms...)
macro enum(T::Union{Symbol,Expr}, syms...)
if isempty(syms)
throw(ArgumentError("no arguments given for Enum $T"))
end
Expand All @@ -131,7 +131,7 @@ macro enum(T, syms...)
elseif !isa(T, Symbol)
throw(ArgumentError("invalid type expression for enum $T"))
end
values = basetype[]
values = basetype[]::Vector
seen = Set{Symbol}()
namemap = Dict{basetype,Symbol}()
lo = hi = 0
Expand Down
6 changes: 3 additions & 3 deletions base/cartesian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ function inlineanonymous(ex::Expr, val)
if !isa(ex.args[1], Symbol)
throw(ArgumentError("not a single-argument anonymous function"))
end
sym = ex.args[1]
ex = ex.args[2]
sym = ex.args[1]::Symbol
ex = ex.args[2]::Expr
exout = lreplace(ex, sym, val)
exout = poplinenum(exout)
exprresolve(exout)
Expand All @@ -262,7 +262,7 @@ struct LReplace{S<:AbstractString}
end
LReplace(sym::Symbol, val::Integer) = LReplace(sym, string(sym), val)

lreplace(ex, sym::Symbol, val) = lreplace!(copy(ex), LReplace(sym, val))
lreplace(ex::Expr, sym::Symbol, val) = lreplace!(copy(ex), LReplace(sym, val))

function lreplace!(sym::Symbol, r::LReplace)
sym == r.pat_sym && return r.val
Expand Down
4 changes: 2 additions & 2 deletions base/deepcopy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ function deepcopy_internal(x::Array, stackdict::IdDict)
_deepcopy_array_t(x, eltype(x), stackdict)
end

function _deepcopy_array_t(@nospecialize(x), T, stackdict::IdDict)
function _deepcopy_array_t(@nospecialize(x::Array), T, stackdict::IdDict)
if isbitstype(T)
return (stackdict[x]=copy(x))
end
dest = similar(x)
stackdict[x] = dest
for i = 1:(length(x)::Int)
for i = 1:length(x)
if ccall(:jl_array_isassigned, Cint, (Any, Csize_t), x, i-1) != 0
xi = ccall(:jl_arrayref, Any, (Any, Csize_t), x, i-1)
if !isbits(xi)
Expand Down
4 changes: 2 additions & 2 deletions base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ function showerror(io::IO, ex::MethodError)
# ex.args is a tuple type if it was thrown from `invoke` and is
# a tuple of the arguments otherwise.
is_arg_types = isa(ex.args, DataType)
arg_types = is_arg_types ? ex.args : typesof(ex.args...)
arg_types = (is_arg_types ? ex.args : typesof(ex.args...))::DataType
f = ex.f
meth = methods_including_ambiguous(f, arg_types)
if length(meth) > 1
Expand Down Expand Up @@ -781,7 +781,7 @@ function show_backtrace(io::IO, t::Vector)

try invokelatest(update_stackframes_callback[], filtered) catch end
# process_backtrace returns a Vector{Tuple{Frame, Int}}
frames = (first.(filtered))::Vector{StackFrame}
frames = map(x->first(x)::StackFrame, filtered)
show_full_backtrace(io, frames; print_linebreaks = stacktrace_linebreaks())
return
end
Expand Down
4 changes: 2 additions & 2 deletions base/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ function copy(c::CodeInfo)
cnew.slotnames = copy(cnew.slotnames)
cnew.slotflags = copy(cnew.slotflags)
cnew.codelocs = copy(cnew.codelocs)
cnew.linetable = copy(cnew.linetable)
cnew.linetable = copy(cnew.linetable::Union{Vector{Any},Vector{Core.LineInfoNode}})
cnew.ssaflags = copy(cnew.ssaflags)
cnew.edges = cnew.edges === nothing ? nothing : copy(cnew.edges)
cnew.edges = cnew.edges === nothing ? nothing : copy(cnew.edges::Vector)
ssavaluetypes = cnew.ssavaluetypes
ssavaluetypes isa Vector{Any} && (cnew.ssavaluetypes = copy(ssavaluetypes))
return cnew
Expand Down
6 changes: 3 additions & 3 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ julia> fieldnames(Rational)
```
"""
fieldnames(t::DataType) = (fieldcount(t); # error check to make sure type is specific enough
(_fieldnames(t)...,))
(_fieldnames(t)...,))::Tuple{Vararg{Symbol}}
fieldnames(t::UnionAll) = fieldnames(unwrap_unionall(t))
fieldnames(::Core.TypeofBottom) =
throw(ArgumentError("The empty type does not have field names since it does not have instances."))
Expand Down Expand Up @@ -871,8 +871,8 @@ function methods_including_ambiguous(@nospecialize(f), @nospecialize(t))
max = UInt[typemax(UInt)]
has_ambig = Int32[0]
ms = _methods_by_ftype(tt, -1, world, true, min, max, has_ambig)
ms === false && return false
return MethodList(Method[m.method for m in ms], typeof(f).name.mt)
isa(ms, Bool) && return ms
return MethodList(Method[(m::Core.MethodMatch).method for m in ms], typeof(f).name.mt)
end

function methods(@nospecialize(f),
Expand Down
2 changes: 1 addition & 1 deletion base/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ to the standard libraries before running the tests.
If a seed is provided via the keyword argument, it is used to seed the
global RNG in the context where the tests are run; otherwise the seed is chosen randomly.
"""
function runtests(tests = ["all"]; ncores = ceil(Int, Sys.CPU_THREADS / 2),
function runtests(tests = ["all"]; ncores::Int = ceil(Int, Sys.CPU_THREADS::Int / 2),
exit_on_error::Bool=false,
revise::Bool=false,
seed::Union{BitInteger,Nothing}=nothing)
Expand Down
5 changes: 3 additions & 2 deletions base/views.jl
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,10 @@ function _views(ex::Expr)
# splat the corresponding temp var.
I = similar(i, Any)
for k = 1:length(i)
if Meta.isexpr(lhs.args[k+1], :...)
argk1 = lhs.args[k+1]
if Meta.isexpr(argk1, :...)
I[k] = Expr(:..., i[k])
lhs.args[k+1] = lhs.args[k+1].args[1] # unsplat
lhs.args[k+1] = (argk1::Expr).args[1] # unsplat
else
I[k] = i[k]
end
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Distributed/src/managers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ function connect(manager::ClusterManager, pid::Int, config::WorkerConfig)
end

function connect_w2w(pid::Int, config::WorkerConfig)
(rhost, rport) = notnothing(config.connect_at)::Tuple{AbstractString, Int}
(rhost, rport) = notnothing(config.connect_at)::Tuple{String, Int}
config.host = rhost
config.port = rport
(s, bind_addr) = connect_to_worker(rhost, rport)
Expand Down
24 changes: 12 additions & 12 deletions stdlib/REPL/src/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ mutable struct Prompt <: TextInterface
prompt_prefix::Union{String,Function}
# Same as prefix except after the prompt
prompt_suffix::Union{String,Function}
keymap_dict::Dict{Char}
keymap_dict::Dict{Char,Any}
repl::Union{AbstractREPL,Nothing}
complete::CompletionProvider
on_enter::Function
Expand Down Expand Up @@ -1315,7 +1315,7 @@ end

const wildcard = '\U10f7ff' # "Private Use" Char

normalize_key(key::AbstractChar) = string(key)
normalize_key(key::Char) = string(key)
normalize_key(key::Union{Int,UInt8}) = normalize_key(Char(key))
function normalize_key(key::AbstractString)
wildcard in key && error("Matching '\U10f7ff' not supported.")
Expand Down Expand Up @@ -1349,7 +1349,7 @@ function normalize_key(key::AbstractString)
return String(take!(buf))
end

function normalize_keys(keymap::Dict)
function normalize_keys(keymap::Union{Dict{Char,Any},AnyDict})
ret = Dict{Any,Any}()
for (k,v) in keymap
normalized = normalize_key(k)
Expand Down Expand Up @@ -1464,7 +1464,7 @@ end
#

# deep merge where target has higher precedence
function keymap_merge!(target::Dict, source::Dict)
function keymap_merge!(target::Dict{Char,Any}, source::Union{Dict{Char,Any},AnyDict})
for k in keys(source)
if !haskey(target, k)
target[k] = source[k]
Expand All @@ -1477,7 +1477,7 @@ function keymap_merge!(target::Dict, source::Dict)
end

fixup_keymaps!(d, l, s, sk) = nothing
function fixup_keymaps!(dict::Dict, level, s, subkeymap)
function fixup_keymaps!(dict::Dict{Char,Any}, level, s, subkeymap)
if level > 0
for d in values(dict)
fixup_keymaps!(d, level-1, s, subkeymap)
Expand All @@ -1494,7 +1494,7 @@ function fixup_keymaps!(dict::Dict, level, s, subkeymap)
nothing
end

function add_specialisations(dict, subdict, level)
function add_specialisations(dict::Dict{Char,Any}, subdict, level)
default_branch = subdict[wildcard]
if isa(default_branch, Dict)
# Go through all the keymaps in the default branch
Expand All @@ -1507,7 +1507,7 @@ function add_specialisations(dict, subdict, level)
end

postprocess!(others) = nothing
function postprocess!(dict::Dict)
function postprocess!(dict::Dict{Char,Any})
# needs to be done first for every branch
if haskey(dict, wildcard)
add_specialisations(dict, dict, 1)
Expand All @@ -1518,7 +1518,7 @@ function postprocess!(dict::Dict)
end
end

function getEntry(keymap,key)
function getEntry(keymap::Dict{Char,Any},key::Union{String,Char})
v = keymap
for c in key
if !haskey(v,c)
Expand All @@ -1531,7 +1531,7 @@ end

# `target` is the total keymap being built up, already being a nested tree of Dicts.
# source is the keymap specified by the user (with normalized keys)
function keymap_merge(target,source)
function keymap_merge(target::Dict{Char,Any}, source::Dict)
ret = copy(target)
direct_keys = filter(p -> isa(p.second, Union{Function, KeyAlias, Nothing}), source)
# first direct entries
Expand All @@ -1543,7 +1543,7 @@ function keymap_merge(target,source)
# We first resolve redirects in the source
value = source[key]
visited = Vector{Any}()
while isa(value, Union{AbstractChar,AbstractString})
while isa(value, Union{Char,String})
value = normalize_key(value)
if value in visited
error("Eager redirection cycle detected for key " * escape_string(key))
Expand All @@ -1555,7 +1555,7 @@ function keymap_merge(target,source)
value = source[value]
end

if isa(value, Union{AbstractChar,AbstractString})
if isa(value, Union{Char,String})
value = getEntry(ret, value)
if value === nothing
error("Could not find redirected value " * escape_string(source[key]))
Expand Down Expand Up @@ -1589,7 +1589,7 @@ function validate_keymap(keymap)
end
end

function keymap(keymaps::Array{<:Dict})
function keymap(keymaps::Union{Vector{AnyDict},Vector{Dict{Char,Any}}})
# keymaps is a vector of prioritized keymaps, with highest priority first
ret = keymap_unify(map(normalize_keys, reverse(keymaps)))
validate_keymap(ret)
Expand Down
2 changes: 1 addition & 1 deletion stdlib/REPL/src/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ mutable struct REPLHistoryProvider <: HistoryProvider
mode_mapping::Dict{Symbol,Prompt}
modes::Vector{Symbol}
end
REPLHistoryProvider(mode_mapping) =
REPLHistoryProvider(mode_mapping::Dict{Symbol}) =
REPLHistoryProvider(String[], nothing, 0, 0, -1, IOBuffer(),
nothing, mode_mapping, UInt8[])

Expand Down
32 changes: 16 additions & 16 deletions stdlib/REPL/test/lineedit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@ end

a_foo = 0

const foo_keymap = Dict(
const foo_keymap = Dict{Char,Any}(
'a' => (o...)->(global a_foo; a_foo += 1)
)

b_foo = 0

const foo2_keymap = Dict(
const foo2_keymap = Dict{Char,Any}(
'b' => (o...)->(global b_foo; b_foo += 1)
)

a_bar = 0
b_bar = 0

const bar_keymap = Dict(
const bar_keymap = Dict{Char,Any}(
'a' => (o...)->(global a_bar; a_bar += 1),
'b' => (o...)->(global b_bar; b_bar += 1)
)
Expand All @@ -84,7 +84,7 @@ run_test(test3_dict,IOBuffer("aab"))
@test b_bar == 1

# Multiple spellings in the same keymap
const test_keymap_1 = Dict(
const test_keymap_1 = Dict{Any,Any}(
"^C" => (o...)->1,
"\\C-C" => (o...)->2
)
Expand All @@ -93,11 +93,11 @@ const test_keymap_1 = Dict(

a_foo = a_bar = 0

const test_keymap_2 = Dict(
const test_keymap_2 = Dict{Any,Any}(
"abc" => (o...)->(global a_foo = 1)
)

const test_keymap_3 = Dict(
const test_keymap_3 = Dict{Any,Any}(
"a" => (o...)->(global a_foo = 2),
"bc" => (o...)->(global a_bar = 3)
)
Expand All @@ -119,13 +119,13 @@ end

a_foo = 0

const test_keymap_4 = Dict(
const test_keymap_4 = Dict{Any,Any}(
"a" => (o...)->(global a_foo = 1),
"b" => "a",
"c" => (o...)->(global a_foo = 2),
)

const test_keymap_5 = Dict(
const test_keymap_5 = Dict{Any,Any}(
"a" => (o...)->(global a_foo = 3),
"d" => "c"
)
Expand All @@ -143,7 +143,7 @@ end

# Eager redirection with cycles

const test_cycle = Dict(
const test_cycle = Dict{Any,Any}(
"a" => "b",
"b" => "a"
)
Expand All @@ -152,15 +152,15 @@ const test_cycle = Dict(

# Lazy redirection with Cycles

const level1 = Dict(
const level1 = Dict{Any,Any}(
"a" => LineEdit.KeyAlias("b")
)

const level2a = Dict(
const level2a = Dict{Any,Any}(
"b" => "a"
)

const level2b = Dict(
const level2b = Dict{Any,Any}(
"b" => LineEdit.KeyAlias("a")
)

Expand All @@ -171,13 +171,13 @@ const level2b = Dict(

a_foo = 0

const test_keymap_6 = Dict(
const test_keymap_6 = Dict{Any,Any}(
"a" => (o...)->(global a_foo = 1),
"b" => LineEdit.KeyAlias("a"),
"c" => (o...)->(global a_foo = 2),
)

const test_keymap_7 = Dict(
const test_keymap_7 = Dict{Any,Any}(
"a" => (o...)->(global a_foo = 3),
"d" => "c"
)
Expand All @@ -200,7 +200,7 @@ global path1 = 0
global path2 = 0
global path3 = 0

const test_keymap_8 = Dict(
const test_keymap_8 = Dict{Any,Any}(
"**" => (o...)->(global path1 += 1),
"ab" => (o...)->(global path2 += 1),
"cd" => (o...)->(global path3 += 1),
Expand All @@ -223,7 +223,7 @@ end
global path1 = 0
global path2 = 0

const test_keymap_9 = Dict(
const test_keymap_9 = Dict{Any,Any}(
"***" => (o...)->(global path1 += 1),
"*a*" => (o...)->(global path2 += 1)
)
Expand Down

0 comments on commit 61b581d

Please sign in to comment.