Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ebceab8

Browse files
committedAug 15, 2017
deprecate const on local variables, which used to be ignored.
part of #5148
1 parent aecf426 commit ebceab8

23 files changed

+89
-75
lines changed
 

‎NEWS.md

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ Language changes
6363
(`need_to_handle_undef_sparam = Set{Any}(m.sig for m in Test.detect_unbound_args(Base, recursive=true))`)
6464
is equal (`==`) to some known set (`expected = Set()`). ([#23117])
6565

66+
* `const` declarations on local variables were previously ignored. They now give a
67+
warning, so that this syntax can be disallowed or given a new meaning in a
68+
future version ([#5148]).
6669

6770
Breaking changes
6871
----------------

‎base/complex.jl

+7-7
Original file line numberDiff line numberDiff line change
@@ -499,9 +499,9 @@ julia> rad2deg(angle(-1 - im))
499499
angle(z::Complex) = atan2(imag(z), real(z))
500500

501501
function log(z::Complex{T}) where T<:AbstractFloat
502-
const T1::T = 1.25
503-
const T2::T = 3
504-
const ln2::T = log(convert(T,2)) #0.6931471805599453
502+
T1::T = 1.25
503+
T2::T = 3
504+
ln2::T = log(convert(T,2)) #0.6931471805599453
505505
x, y = reim(z)
506506
ρ, k = ssqs(x,y)
507507
ax = abs(x)
@@ -835,7 +835,7 @@ function cosh(z::Complex)
835835
end
836836

837837
function tanh(z::Complex{T}) where T<:AbstractFloat
838-
const Ω = prevfloat(typemax(T))
838+
Ω = prevfloat(typemax(T))
839839
ξ, η = reim(z)
840840
if isnan(ξ) && η==0 return Complex(ξ, η) end
841841
if 4*abs(ξ) > asinh(Ω) #Overflow?
@@ -880,9 +880,9 @@ function acosh(z::Complex)
880880
end
881881

882882
function atanh(z::Complex{T}) where T<:AbstractFloat
883-
const Ω = prevfloat(typemax(T))
884-
const θ = sqrt(Ω)/4
885-
const ρ = 1/θ
883+
Ω = prevfloat(typemax(T))
884+
θ = sqrt(Ω)/4
885+
ρ = 1/θ
886886
x, y = reim(z)
887887
ax = abs(x)
888888
ay = abs(y)

‎base/libc.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ if Sys.iswindows()
3939
function dup(src::WindowsRawSocket)
4040
new_handle = Ref{Ptr{Void}}(-1)
4141
my_process = ccall(:GetCurrentProcess, stdcall, Ptr{Void}, ())
42-
const DUPLICATE_SAME_ACCESS = 0x2
42+
DUPLICATE_SAME_ACCESS = 0x2
4343
status = ccall(:DuplicateHandle, stdcall, Int32,
4444
(Ptr{Void}, Ptr{Void}, Ptr{Void}, Ptr{Ptr{Void}}, UInt32, Int32, UInt32),
4545
my_process, src.handle, my_process, new_handle, 0, false, DUPLICATE_SAME_ACCESS)
@@ -290,10 +290,10 @@ if Sys.iswindows()
290290
GetLastError() = ccall(:GetLastError, stdcall, UInt32, ())
291291

292292
function FormatMessage(e=GetLastError())
293-
const FORMAT_MESSAGE_ALLOCATE_BUFFER = UInt32(0x100)
294-
const FORMAT_MESSAGE_FROM_SYSTEM = UInt32(0x1000)
295-
const FORMAT_MESSAGE_IGNORE_INSERTS = UInt32(0x200)
296-
const FORMAT_MESSAGE_MAX_WIDTH_MASK = UInt32(0xFF)
293+
FORMAT_MESSAGE_ALLOCATE_BUFFER = UInt32(0x100)
294+
FORMAT_MESSAGE_FROM_SYSTEM = UInt32(0x1000)
295+
FORMAT_MESSAGE_IGNORE_INSERTS = UInt32(0x200)
296+
FORMAT_MESSAGE_MAX_WIDTH_MASK = UInt32(0xFF)
297297
lpMsgBuf = Ref{Ptr{UInt16}}()
298298
lpMsgBuf[] = 0
299299
len = ccall(:FormatMessageW, stdcall, UInt32, (Cint, Ptr{Void}, Cint, Cint, Ptr{Ptr{UInt16}}, Cint, Ptr{Void}),

‎base/libdl.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ function dllist()
234234
dynamic_libraries = Vector{AbstractString}(0)
235235

236236
@static if Sys.islinux()
237-
const callback = cfunction(dl_phdr_info_callback, Cint,
238-
Tuple{Ref{dl_phdr_info}, Csize_t, Ref{Vector{AbstractString}}})
237+
callback = cfunction(dl_phdr_info_callback, Cint,
238+
Tuple{Ref{dl_phdr_info}, Csize_t, Ref{Vector{AbstractString}}})
239239
ccall(:dl_iterate_phdr, Cint, (Ptr{Void}, Ref{Vector{AbstractString}}), callback, dynamic_libraries)
240240
end
241241

@@ -254,8 +254,8 @@ function dllist()
254254
end
255255

256256
@static if Sys.isbsd() && !Sys.isapple()
257-
const callback = cfunction(dl_phdr_info_callback, Cint,
258-
Tuple{Ref{dl_phdr_info}, Csize_t, Ref{Vector{AbstractString}}})
257+
callback = cfunction(dl_phdr_info_callback, Cint,
258+
Tuple{Ref{dl_phdr_info}, Csize_t, Ref{Vector{AbstractString}}})
259259
ccall(:dl_iterate_phdr, Cint, (Ptr{Void}, Ref{Vector{AbstractString}}), callback, dynamic_libraries)
260260
shift!(dynamic_libraries)
261261
end

‎base/loading.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ elseif Sys.isapple()
4646
isfile(path) || return false
4747
path_basename = String(basename(path))
4848
local casepreserved_basename
49-
const header_size = 12
49+
header_size = 12
5050
buf = Vector{UInt8}(length(path_basename) + header_size + 1)
5151
while true
5252
ret = ccall(:getattrlist, Cint,

‎base/math.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -758,9 +758,9 @@ end
758758
@inline literal_pow(::typeof(^), x::Float16, ::Val{p}) where {p} = Float16(literal_pow(^,Float32(x),Val(p)))
759759

760760
function angle_restrict_symm(theta)
761-
const P1 = 4 * 7.8539812564849853515625e-01
762-
const P2 = 4 * 3.7748947079307981766760e-08
763-
const P3 = 4 * 2.6951514290790594840552e-15
761+
P1 = 4 * 7.8539812564849853515625e-01
762+
P2 = 4 * 3.7748947079307981766760e-08
763+
P3 = 4 * 2.6951514290790594840552e-15
764764

765765
y = 2*floor(theta/(2*pi))
766766
r = ((theta - y*P1) - y*P2) - y*P3

‎base/regex.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,10 @@ function _write_capture(io, re, group)
251251
end
252252

253253
function _replace(io, repl_s::SubstitutionString, str, r, re)
254-
const SUB_CHAR = '\\'
255-
const GROUP_CHAR = 'g'
256-
const LBRACKET = '<'
257-
const RBRACKET = '>'
254+
SUB_CHAR = '\\'
255+
GROUP_CHAR = 'g'
256+
LBRACKET = '<'
257+
RBRACKET = '>'
258258
repl = repl_s.string
259259
i = start(repl)
260260
e = endof(repl)

‎base/repl/REPL.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ function setup_interface(
807807
extra_repl_keymap = [extra_repl_keymap]
808808
end
809809

810-
const repl_keymap = AnyDict(
810+
repl_keymap = AnyDict(
811811
';' => function (s,o...)
812812
if isempty(s) || position(LineEdit.buffer(s)) == 0
813813
buf = copy(LineEdit.buffer(s))

‎base/repl/REPLCompletions.jl

+8-7
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,15 @@ function complete_symbol(sym, ffunc)
8282
suggestions
8383
end
8484

85+
const sorted_keywords = [
86+
"abstract type", "baremodule", "begin", "break", "catch", "ccall",
87+
"const", "continue", "do", "else", "elseif", "end", "export", "false",
88+
"finally", "for", "function", "global", "if", "import",
89+
"importall", "let", "local", "macro", "module", "mutable struct",
90+
"primitive type", "quote", "return", "struct",
91+
"true", "try", "using", "while"]
92+
8593
function complete_keyword(s::String)
86-
const sorted_keywords = [
87-
"abstract type", "baremodule", "begin", "break", "catch", "ccall",
88-
"const", "continue", "do", "else", "elseif", "end", "export", "false",
89-
"finally", "for", "function", "global", "if", "import",
90-
"importall", "let", "local", "macro", "module", "mutable struct",
91-
"primitive type", "quote", "return", "struct",
92-
"true", "try", "using", "while"]
9394
r = searchsorted(sorted_keywords, s)
9495
i = first(r)
9596
n = length(sorted_keywords)

‎base/sparse/sparsematrix.jl

+6-6
Original file line numberDiff line numberDiff line change
@@ -2042,8 +2042,8 @@ function getindex_I_sorted(A::SparseMatrixCSC{Tv,Ti}, I::AbstractVector, J::Abst
20422042
end
20432043

20442044
function getindex_I_sorted_bsearch_A(A::SparseMatrixCSC{Tv,Ti}, I::AbstractVector, J::AbstractVector) where {Tv,Ti}
2045-
const nI = length(I)
2046-
const nJ = length(J)
2045+
nI = length(I)
2046+
nJ = length(J)
20472047

20482048
colptrA = A.colptr; rowvalA = A.rowval; nzvalA = A.nzval
20492049
colptrS = Vector{Ti}(nJ+1)
@@ -2101,8 +2101,8 @@ function getindex_I_sorted_bsearch_A(A::SparseMatrixCSC{Tv,Ti}, I::AbstractVecto
21012101
end
21022102

21032103
function getindex_I_sorted_linear(A::SparseMatrixCSC{Tv,Ti}, I::AbstractVector, J::AbstractVector) where {Tv,Ti}
2104-
const nI = length(I)
2105-
const nJ = length(J)
2104+
nI = length(I)
2105+
nJ = length(J)
21062106

21072107
colptrA = A.colptr; rowvalA = A.rowval; nzvalA = A.nzval
21082108
colptrS = Vector{Ti}(nJ+1)
@@ -2160,8 +2160,8 @@ function getindex_I_sorted_linear(A::SparseMatrixCSC{Tv,Ti}, I::AbstractVector,
21602160
end
21612161

21622162
function getindex_I_sorted_bsearch_I(A::SparseMatrixCSC{Tv,Ti}, I::AbstractVector, J::AbstractVector) where {Tv,Ti}
2163-
const nI = length(I)
2164-
const nJ = length(J)
2163+
nI = length(I)
2164+
nJ = length(J)
21652165

21662166
colptrA = A.colptr; rowvalA = A.rowval; nzvalA = A.nzval
21672167
colptrS = Vector{Ti}(nJ+1)

‎doc/src/manual/variables-and-scoping.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,8 @@ their values (or even their types) might change at almost any time. If a global
475475
not change, adding a `const` declaration solves this performance problem.
476476

477477
Local constants are quite different. The compiler is able to determine automatically when a local
478-
variable is constant, so local constant declarations are not necessary, and are currently just ignored.
478+
variable is constant, so local constant declarations are not necessary, and in fact are currently
479+
not supported.
479480

480481
Special top-level assignments, such as those performed by the `function` and `struct` keywords,
481482
are constant by default.

‎src/julia-syntax.scm

+12-4
Original file line numberDiff line numberDiff line change
@@ -1320,7 +1320,7 @@
13201320
(if (null? params)
13211321
(error (string "empty type parameter list in \"" (deparse `(= (curly ,name) ,type-ex)) "\"")))
13221322
`(block
1323-
(const ,name)
1323+
(const-if-global ,name)
13241324
,(expand-forms
13251325
`(= ,name (where ,type-ex ,@params)))))
13261326
(expand-forms
@@ -3115,11 +3115,12 @@ f(x) = yt(x)
31153115
(if (vinfo:never-undef vi)
31163116
'(null)
31173117
`(newvar ,(cadr e))))))
3118-
((const)
3118+
((const) e)
3119+
((const-if-global)
31193120
(if (or (assq (cadr e) (car (lam:vinfo lam)))
31203121
(assq (cadr e) (cadr (lam:vinfo lam))))
31213122
'(null)
3122-
e))
3123+
`(const ,(cadr e))))
31233124
((isdefined) ;; convert isdefined expr to function for closure converted variables
31243125
(let* ((sym (cadr e))
31253126
(vi (and (symbol? sym) (assq sym (car (lam:vinfo lam)))))
@@ -3661,7 +3662,14 @@ f(x) = yt(x)
36613662
((local-def) #f)
36623663
((local) #f)
36633664
((implicit-global) #f)
3664-
((const) (emit e))
3665+
((const)
3666+
(if (or (assq (cadr e) (car (lam:vinfo lam)))
3667+
(assq (cadr e) (cadr (lam:vinfo lam))))
3668+
(begin
3669+
(syntax-deprecation #f (string "`const` declaration on local variable" (linenode-string current-loc))
3670+
"")
3671+
'(null))
3672+
(emit e)))
36653673
((isdefined) (if tail (emit-return e) e))
36663674

36673675
;; top level expressions returning values

‎test/TestHelpers.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ function open_fake_pty()
2424
error("Unable to create a fake PTY in Windows")
2525
end
2626

27-
const O_RDWR = Base.Filesystem.JL_O_RDWR
28-
const O_NOCTTY = Base.Filesystem.JL_O_NOCTTY
27+
O_RDWR = Base.Filesystem.JL_O_RDWR
28+
O_NOCTTY = Base.Filesystem.JL_O_NOCTTY
2929

3030
fdm = ccall(:posix_openpt, Cint, (Cint,), O_RDWR|O_NOCTTY)
3131
fdm == -1 && error("Failed to open PTY master")

‎test/core.jl

+7-6
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,8 @@ function const_implies_local()
438438
x = 1
439439
local y
440440
let
441-
const x = 0
441+
# TODO: change back to `const` if that's ever allowed
442+
local x = 0
442443
y = x
443444
end
444445
x, y
@@ -2055,11 +2056,11 @@ end
20552056

20562057
# issue #8798
20572058
let
2058-
const npy_typestrs = Dict("b1"=>Bool,
2059-
"i1"=>Int8, "u1"=>UInt8,
2060-
"i2"=>Int16, "u2"=>UInt16,
2061-
"i4"=>Int32, "u4"=>UInt32,
2062-
"i8"=>Int64, "u8"=>UInt64)
2059+
npy_typestrs = Dict("b1"=>Bool,
2060+
"i1"=>Int8, "u1"=>UInt8,
2061+
"i2"=>Int16, "u2"=>UInt16,
2062+
"i4"=>Int32, "u4"=>UInt32,
2063+
"i8"=>Int64, "u8"=>UInt64)
20632064
sizeof_lookup() = sizeof(npy_typestrs["i8"])
20642065
@test sizeof_lookup() == 8
20652066
end

‎test/dates/io.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ end
378378

379379
# Issue: https://github.com/quinnj/TimeZones.jl/issues/19
380380
let
381-
const Zulu = String
381+
Zulu = String
382382

383383
function Dates.tryparsenext(d::Dates.DatePart{'Z'}, str, i, len)
384384
Dates.tryparsenext_word(str, i, len, Dates.min_width(d), Dates.max_width(d))

‎test/misc.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ if Sys.iswindows()
576576

577577
let addr = cfunction(WeVirtualProtectThisToRWX, UInt64, (UInt64, UInt64))
578578
addr = addr-(UInt64(addr)%4096)
579-
const PAGE_EXECUTE_READWRITE = 0x40
579+
PAGE_EXECUTE_READWRITE = 0x40
580580
oldPerm = Ref{UInt32}()
581581
err18083 = ccall(:VirtualProtect,stdcall,Cint,
582582
(Ptr{Void}, Csize_t, UInt32, Ptr{UInt32}),

‎test/perf/shootout/binary_trees.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ function loop_depths(d, min_depth, max_depth)
4444
end
4545

4646
function binary_trees(N::Int=10)
47-
const min_depth = 4
48-
const max_depth = N
49-
const stretch_depth = max_depth + 1
47+
min_depth = 4
48+
max_depth = N
49+
stretch_depth = max_depth + 1
5050

5151
# create and check stretch tree
5252
let c = check(make(0, stretch_depth))

‎test/perf/threads/lbm3d/lbm3d.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ end
119119
precompile(calc_equi!, (Array{Float64,4}, Array{Float64,4}, Array{Float64,3}, Array{Float64,3}, Array{Float64,3}, Array{Float64,2}, Array{Float64,3}, Array{Float64,3}, Array{Float64,3}, Array{Float64,3}, Int64, Int64, Int64, Float64))
120120

121121
function lbm3d(n)
122-
const nx = n
123-
const ny = nx
124-
const nz = nx
125-
const omega = 1.0
126-
const density = 1.0
122+
nx = n
123+
ny = nx
124+
nz = nx
125+
omega = 1.0
126+
density = 1.0
127127

128128
# Implementation note: setting nchunk to nthreads() is a hack
129129
# to simulate the previous implementation's use of parallel regions.

‎test/perf/threads/stockcorr/pstockcorr.jl

+8-8
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ end
6161
# Threaded version
6262
function pstockcorr(n)
6363
## Correlated asset information
64-
const CurrentPrice = [78. 102.] # Initial Prices of the two stocks
65-
const Corr = [1. 0.4; 0.4 1.] # Correlation Matrix
66-
const T = 500 # Number of days to simulate = 2years = 500days
67-
const dt = 1/250 # Time step (1year = 250days)
68-
const Div=[0.01 0.01] # Dividend
69-
const Vol=[0.2 0.3] # Volatility
64+
CurrentPrice = [78. 102.] # Initial Prices of the two stocks
65+
Corr = [1. 0.4; 0.4 1.] # Correlation Matrix
66+
T = 500 # Number of days to simulate = 2years = 500days
67+
dt = 1/250 # Time step (1year = 250days)
68+
Div=[0.01 0.01] # Dividend
69+
Vol=[0.2 0.3] # Volatility
7070

7171
## Market Information
72-
const r = 0.03 # Risk-free rate
72+
r = 0.03 # Risk-free rate
7373

7474
## Define storages
7575
SimulPriceA = zeros(T,n) # Simulated Price of Asset A
@@ -78,7 +78,7 @@ function pstockcorr(n)
7878
SimulPriceB[1,:] = CurrentPrice[2]
7979

8080
## Generating the paths of stock prices by Geometric Brownian Motion
81-
const UpperTriangle = full(chol(Corr)) # UpperTriangle Matrix by Cholesky decomposition
81+
UpperTriangle = full(chol(Corr)) # UpperTriangle Matrix by Cholesky decomposition
8282

8383
# Optimization: pre-allocate these for performance
8484
# NOTE: the new GC will hopefully fix this, but currently GC time

‎test/pkg.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ temp_pkg_dir() do
480480
nothingtodomsg) Pkg.update("Example")
481481

482482
metadata_dir = Pkg.dir("METADATA")
483-
const old_commit = "313bfaafa301e82d40574a778720e893c559a7e2"
483+
old_commit = "313bfaafa301e82d40574a778720e893c559a7e2"
484484

485485
# Force a METADATA rollback to an old version, so that we will install some
486486
# outdated versions of some packages and then update some of those
@@ -525,7 +525,7 @@ temp_pkg_dir() do
525525
Pkg.rm(package) # Remove package if installed
526526

527527
metadata_dir = Pkg.dir("METADATA")
528-
const old_commit = "83ff7116e51fc9cdbd7e67affbd344b9f5c9dbf2"
528+
old_commit = "83ff7116e51fc9cdbd7e67affbd344b9f5c9dbf2"
529529

530530
# Reset METADATA to the second to last update of Example.jl
531531
LibGit2.with(LibGit2.GitRepo, metadata_dir) do repo

‎test/repl.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ function AddCustomMode(repl, prompt)
232232
hp.mode_mapping[:foobar] = foobar_mode
233233
foobar_mode.hist = hp
234234

235-
const foobar_keymap = Dict{Any,Any}(
235+
foobar_keymap = Dict{Any,Any}(
236236
'<' => function (s,args...)
237237
if isempty(s)
238238
if !haskey(s.mode_state,foobar_mode)

‎test/replcompletions.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function temp_pkg_dir_noinit(fn::Function)
7676
# Used in tests below to set up and tear down a sandboxed package directory
7777
# Unlike the version in test/pkg.jl, this does not run Pkg.init so does not
7878
# clone METADATA (only pkg and libgit2-online tests should need internet access)
79-
const tmpdir = joinpath(tempdir(),randstring())
79+
tmpdir = joinpath(tempdir(),randstring())
8080
withenv("JULIA_PKGDIR" => tmpdir) do
8181
@test !isdir(Pkg.dir())
8282
try

‎test/subtype.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -1094,12 +1094,12 @@ f20103(::Type{TT20103{X,Y}},x::X,y::Y) where {X,Y} = 1
10941094
f20103(::Type{TT20103{X,X}},x::X) where {X} = 100
10951095
@test_broken typeintersect(Type{NTuple{N,E}} where E where N, Type{NTuple{N,E} where N} where E) == Union{} # use @testintersect once fixed
10961096
let ints = (Int, Int32, UInt, UInt32)
1097-
const Ints = Union{ints...}
1097+
Ints = Union{ints...}
10981098
vecs = []
10991099
for i = 2:4, t in ints
11001100
push!(vecs, NTuple{i, t})
11011101
end
1102-
const Vecs = Union{vecs...}
1102+
Vecs = Union{vecs...}
11031103
T = Type{Tuple{V, I}} where V <: Vecs where I <: Ints
11041104
@testintersect(T, T, T)
11051105
test(a::Type{Tuple{V, I}}) where {V <: Vecs, I <: Ints} = I

0 commit comments

Comments
 (0)
Please sign in to comment.