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 cdd6da5

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

10 files changed

+24
-16
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
----------------

‎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

+9-6
Original file line numberDiff line numberDiff line change
@@ -3115,11 +3115,7 @@ f(x) = yt(x)
31153115
(if (vinfo:never-undef vi)
31163116
'(null)
31173117
`(newvar ,(cadr e))))))
3118-
((const)
3119-
(if (or (assq (cadr e) (car (lam:vinfo lam)))
3120-
(assq (cadr e) (cadr (lam:vinfo lam))))
3121-
'(null)
3122-
e))
3118+
((const) e)
31233119
((isdefined) ;; convert isdefined expr to function for closure converted variables
31243120
(let* ((sym (cadr e))
31253121
(vi (and (symbol? sym) (assq sym (car (lam:vinfo lam)))))
@@ -3661,7 +3657,14 @@ f(x) = yt(x)
36613657
((local-def) #f)
36623658
((local) #f)
36633659
((implicit-global) #f)
3664-
((const) (emit e))
3660+
((const)
3661+
(if (or (assq (cadr e) (car (lam:vinfo lam)))
3662+
(assq (cadr e) (cadr (lam:vinfo lam))))
3663+
(begin
3664+
(syntax-deprecation #f (string "`const` declaration on local variable" (linenode-string current-loc))
3665+
"")
3666+
'(null))
3667+
(emit e)))
36653668
((isdefined) (if tail (emit-return e) e))
36663669

36673670
;; 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

+2-1
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

‎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/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.