Skip to content

Commit

Permalink
fix #27554, deprecate try without catch or finally
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Jun 14, 2018
1 parent 0658a11 commit 2a48787
Show file tree
Hide file tree
Showing 29 changed files with 42 additions and 22 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ Language changes
is deprecated. It will likely be reclaimed in a later version for passing keyword
arguments. Note this does not affect updating operators like `+=` ([#25631]).

* `try` blocks without `catch` or `finally` are no longer allowed. An explicit empty
`catch` block should be written instead ([#27554]).

Breaking changes
----------------

Expand Down
1 change: 1 addition & 0 deletions base/compiler/tfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,7 @@ function builtin_tfunction(@nospecialize(f), argtypes::Array{Any,1},
argvals = anymap(a -> a.val, argtypes)
try
return Const(f(argvals...))
catch
end
end
iidx = Int(reinterpret(Int32, f::IntrinsicFunction)) + 1
Expand Down
3 changes: 1 addition & 2 deletions base/docs/basedocs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,7 @@ end
`try`/`catch` statements also allow the `Exception` to be saved in a variable, e.g. `catch y`.
The `catch` clause is not strictly necessary; when omitted, the default return value is
[`nothing`](@ref). The power of the `try`/`catch` construct lies in the ability to unwind a deeply
The power of the `try`/`catch` construct lies in the ability to unwind a deeply
nested computation immediately to a much higher level in the stack of calling functions.
"""
kw"try", kw"catch"
Expand Down
1 change: 1 addition & 0 deletions base/download.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ else
Sys.which(checkcmd)
downloadcmd = checkcmd
break
catch
end
end
end
Expand Down
1 change: 1 addition & 0 deletions base/iostream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ function read(s::IOStream)
if pos > 0
sz -= pos
end
catch
end
b = StringVector(sz<=0 ? 1024 : sz)
nr = readbytes_all!(s, b, typemax(Int))
Expand Down
6 changes: 0 additions & 6 deletions doc/src/manual/control-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -790,12 +790,6 @@ catch
end
```

The `catch` clause is not strictly necessary; when omitted, the default return value is `nothing`.

```jldoctest
julia> try error() end # Returns nothing
```

The power of the `try/catch` construct lies in the ability to unwind a deeply nested computation
immediately to a much higher level in the stack of calling functions. There are situations where
no error has occurred, but the ability to unwind the stack and pass a value to a higher level
Expand Down
3 changes: 2 additions & 1 deletion src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -1505,7 +1505,8 @@
((eq? nxt 'end)
(list* 'try try-block (or catchv 'false)
;; default to empty catch block in `try ... end`
(or catchb (if finalb 'false '(block)))
(or catchb (if finalb 'false (begin (parser-depwarn s "try without catch or finally" "")
'(block))))
(if finalb (list finalb) '())))
((and (eq? nxt 'catch)
(not catchb))
Expand Down
1 change: 1 addition & 0 deletions stdlib/Distributed/src/clusterserialize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ function original_ex(s::ClusterSerializer, ex_str, remote_stktrace)
local pid_str = ""
try
pid_str = string(" from worker ", worker_id_from_socket(s.io))
catch
end

stk_str = remote_stktrace ? "Remote" : "Local"
Expand Down
1 change: 1 addition & 0 deletions stdlib/Distributed/test/distributed_exec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,7 @@ const get_num_threads = function() # anonymous so it will be serialized when cal
if Sys.isapple()
return tryparse(Cint, get(ENV, "VECLIB_MAXIMUM_THREADS", "1"))
end
catch
end

return nothing
Expand Down
6 changes: 3 additions & 3 deletions stdlib/InteractiveUtils/src/InteractiveUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ function versioninfo(io::IO=stdout; verbose::Bool=false, packages::Bool=false)
if verbose
lsb = ""
if Sys.islinux()
try lsb = readchomp(pipeline(`lsb_release -ds`, stderr=devnull)) end
try lsb = readchomp(pipeline(`lsb_release -ds`, stderr=devnull)); catch; end
end
if Sys.iswindows()
try lsb = strip(read(`$(ENV["COMSPEC"]) /c ver`, String)) end
try lsb = strip(read(`$(ENV["COMSPEC"]) /c ver`, String)); catch; end
end
if !isempty(lsb)
println(io, " ", lsb)
Expand All @@ -95,7 +95,7 @@ function versioninfo(io::IO=stdout; verbose::Bool=false, packages::Bool=false)

if verbose
println(io, " Memory: $(Sys.total_memory()/2^30) GB ($(Sys.free_memory()/2^20) MB free)")
try println(io, " Uptime: $(Sys.uptime()) sec") end
try println(io, " Uptime: $(Sys.uptime()) sec"); catch; end
print(io, " Load Avg: ")
Base.print_matrix(io, Sys.loadavg()')
println(io)
Expand Down
1 change: 1 addition & 0 deletions stdlib/LibGit2/src/LibGit2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ function checkout!(repo::GitRepo, commit::AbstractString = "";
head_name = string(GitHash(head_ref))
end
end
catch
end

# search for commit to get a commit object
Expand Down
1 change: 1 addition & 0 deletions stdlib/LibGit2/src/commit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ function commit(repo::GitRepo, msg::AbstractString;
if isempty(parent_ids)
try # if throws then HEAD not found -> empty repo
Base.push!(parent_ids, GitHash(repo, refname))
catch
end
end

Expand Down
2 changes: 1 addition & 1 deletion stdlib/LibGit2/src/config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ end

function get(c::GitConfig, name::AbstractString, default::T) where T
res = default
try res = get(T,c,name) end
try res = get(T,c,name); catch; end
return res
end

Expand Down
1 change: 1 addition & 0 deletions stdlib/LibGit2/test/libgit2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2694,6 +2694,7 @@ mktempdir() do dir
# In some environments, namely Macs, the hostname "macbook.local" is bound
# to the external address while "macbook" is bound to the loopback address.
pushfirst!(hostnames, replace(gethostname(), r"\..*$" => ""))
catch
end

loopback = ip"127.0.0.1"
Expand Down
1 change: 1 addition & 0 deletions stdlib/LinearAlgebra/src/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,7 @@ function factorize(A::StridedMatrix{T}) where T
if (herm & (T <: Complex)) | sym
try
return ldlt!(SymTridiagonal(diag(A), diag(A, -1)))
catch
end
end
return lu(Tridiagonal(diag(A, -1), diag(A), diag(A, 1)))
Expand Down
1 change: 1 addition & 0 deletions stdlib/OldPkg/src/entry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function add(pkg::AbstractString, vers::VersionSet)
try
LibGit2.fetch(repo)
outdated = LibGit2.isdiff(repo, "origin/$branch") ? (:yes) : (:no)
catch
end
end
else
Expand Down
1 change: 1 addition & 0 deletions stdlib/REPL/src/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2020,6 +2020,7 @@ AnyDict(
"^C" => (s,o...)->begin
try # raise the debugger if present
ccall(:jl_raise_debugger, Int, ())
catch
end
cancel_beep(s)
move_input_end(s)
Expand Down
1 change: 1 addition & 0 deletions stdlib/REPL/src/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ function run_frontend(repl::BasicREPL, backend::REPLBackendRef)
if isa(e,InterruptException)
try # raise the debugger if present
ccall(:jl_raise_debugger, Int, ())
catch
end
line = ""
interrupted = true
Expand Down
1 change: 1 addition & 0 deletions stdlib/Random/src/RNGs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ function make_seed()
seed = hash(seed, parse(UInt64,
read(pipeline(`ifconfig`, `sha1sum`), String)[1:40],
base = 16))
catch
end
return make_seed(seed)
end
Expand Down
1 change: 1 addition & 0 deletions stdlib/SHA/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ for f in sha_funcs
f(UInt32[0x23467, 0x324775])
warn("Non-UInt8 Arrays should fail")
nerrors += 1
catch
end
end

Expand Down
8 changes: 4 additions & 4 deletions stdlib/SparseArrays/test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1065,11 +1065,11 @@ end

A = Matrix{Int}(I, 0, 0)
S = sparse(A)
iA = try argmax(A) end
iS = try argmax(S) end
iA = try argmax(A); catch; end
iS = try argmax(S); catch; end
@test iA === iS === nothing
iA = try argmin(A) end
iS = try argmin(S) end
iA = try argmin(A); catch; end
iS = try argmin(S); catch; end
@test iA === iS === nothing
end

Expand Down
2 changes: 1 addition & 1 deletion test/char.jl
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ end
@test eof(io)
close(io)
end
finally
finally
rm(file, force=true)
end
end
Expand Down
1 change: 1 addition & 0 deletions test/compiler/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,7 @@ function h25579(g)
try
h = -1.25
error("continue at catch block")
catch
end
return t ? typeof(h) : typeof(h)
end
Expand Down
8 changes: 7 additions & 1 deletion test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ begin
global try_finally_glo_after = 1
end
global gothere = 1
catch
end
@test try_finally_loc_after == 0
@test try_finally_glo_after == 1
Expand Down Expand Up @@ -799,7 +800,7 @@ begin
@test retfinally() == 5
@test glo == 18

@test try error() end === nothing
@test try error(); catch; end === nothing
end

# issue #12806
Expand Down Expand Up @@ -1270,6 +1271,7 @@ let
function f()
try
return 1
catch
end
end
@test f() == 1
Expand Down Expand Up @@ -1658,6 +1660,7 @@ try
(function() end)(1)
# should throw an argument count error
@test false
catch
end

# issue #4526
Expand Down Expand Up @@ -1881,6 +1884,7 @@ try
# try running this code in a different context that triggers the codegen
# assertion `assert(isboxed || v.typ == typ)`.
f5142()
catch
end

primitive type Int5142b 8 end
Expand Down Expand Up @@ -2282,6 +2286,7 @@ let
# This can throw an error, but shouldn't segfault
try
issue7897!(sa, zeros(10))
catch
end
end

Expand Down Expand Up @@ -2605,6 +2610,7 @@ try
mutable struct Foo{T}
val::Bar{T}
end
catch
end
GC.gc()
redirect_stdout(OLD_STDOUT)
Expand Down
2 changes: 1 addition & 1 deletion test/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ end
@test c[1] == 1

c = [0]
ex = try retry(foo_error, check=(s,e)->(s,try e.http_status_code == "503" end != true))(c,2) catch e; e end
ex = try retry(foo_error, check=(s,e)->(s,try e.http_status_code == "503"; catch; end != true))(c,2) catch e; e end
@test typeof(ex) == ErrorException
@test ex.msg == "foo"
@test c[1] == 2
Expand Down
2 changes: 1 addition & 1 deletion test/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ end
open_streams = []
function cleanup()
for s_ in open_streams
try close(s_) end
try close(s_); catch; end
end
empty!(open_streams)
for tsk in tasks
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ cd(dirname(@__FILE__)) do
isa(e, InterruptException) || rethrow(e)
# If the test suite was merely interrupted, still print the
# summary, which can be useful to diagnose what's going on
foreach(task->try; schedule(task, InterruptException(); error=true); end, all_tasks)
foreach(task->try; schedule(task, InterruptException(); error=true); catch; end, all_tasks)
foreach(wait, all_tasks)
finally
if isa(stdin, Base.TTY)
Expand Down
1 change: 1 addition & 0 deletions test/spawn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ let r, t
t = @async begin
try
wait(r)
catch
end
p = run(`$sleepcmd 1`, wait=false); wait(p)
@test p.exitcode == 0
Expand Down
1 change: 1 addition & 0 deletions test/threads.jl
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ function test_load_and_lookup_18020(n)
ccall(:jl_load_and_lookup,
Ptr{Cvoid}, (Cstring, Cstring, Ref{Ptr{Cvoid}}),
"$i", :f, C_NULL)
catch
end
end
end
Expand Down

0 comments on commit 2a48787

Please sign in to comment.