Skip to content

Commit

Permalink
deprecate const on local variables, which used to be ignored.
Browse files Browse the repository at this point in the history
part of #5148
  • Loading branch information
JeffBezanson committed Aug 14, 2017
1 parent aecf426 commit cdd6da5
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 16 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ Language changes
(`need_to_handle_undef_sparam = Set{Any}(m.sig for m in Test.detect_unbound_args(Base, recursive=true))`)
is equal (`==`) to some known set (`expected = Set()`). ([#23117])

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

Breaking changes
----------------
Expand Down
3 changes: 2 additions & 1 deletion doc/src/manual/variables-and-scoping.md
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@ their values (or even their types) might change at almost any time. If a global
not change, adding a `const` declaration solves this performance problem.

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

Special top-level assignments, such as those performed by the `function` and `struct` keywords,
are constant by default.
Expand Down
15 changes: 9 additions & 6 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -3115,11 +3115,7 @@ f(x) = yt(x)
(if (vinfo:never-undef vi)
'(null)
`(newvar ,(cadr e))))))
((const)
(if (or (assq (cadr e) (car (lam:vinfo lam)))
(assq (cadr e) (cadr (lam:vinfo lam))))
'(null)
e))
((const) e)
((isdefined) ;; convert isdefined expr to function for closure converted variables
(let* ((sym (cadr e))
(vi (and (symbol? sym) (assq sym (car (lam:vinfo lam)))))
Expand Down Expand Up @@ -3661,7 +3657,14 @@ f(x) = yt(x)
((local-def) #f)
((local) #f)
((implicit-global) #f)
((const) (emit e))
((const)
(if (or (assq (cadr e) (car (lam:vinfo lam)))
(assq (cadr e) (cadr (lam:vinfo lam))))
(begin
(syntax-deprecation #f (string "`const` declaration on local variable" (linenode-string current-loc))
"")
'(null))
(emit e)))
((isdefined) (if tail (emit-return e) e))

;; top level expressions returning values
Expand Down
4 changes: 2 additions & 2 deletions test/TestHelpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ function open_fake_pty()
error("Unable to create a fake PTY in Windows")
end

const O_RDWR = Base.Filesystem.JL_O_RDWR
const O_NOCTTY = Base.Filesystem.JL_O_NOCTTY
O_RDWR = Base.Filesystem.JL_O_RDWR
O_NOCTTY = Base.Filesystem.JL_O_NOCTTY

fdm = ccall(:posix_openpt, Cint, (Cint,), O_RDWR|O_NOCTTY)
fdm == -1 && error("Failed to open PTY master")
Expand Down
3 changes: 2 additions & 1 deletion test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,8 @@ function const_implies_local()
x = 1
local y
let
const x = 0
# TODO: change back to `const` if that's ever allowed
local x = 0
y = x
end
x, y
Expand Down
2 changes: 1 addition & 1 deletion test/dates/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ end

# Issue: https://github.com/quinnj/TimeZones.jl/issues/19
let
const Zulu = String
Zulu = String

function Dates.tryparsenext(d::Dates.DatePart{'Z'}, str, i, len)
Dates.tryparsenext_word(str, i, len, Dates.min_width(d), Dates.max_width(d))
Expand Down
2 changes: 1 addition & 1 deletion test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ if Sys.iswindows()

let addr = cfunction(WeVirtualProtectThisToRWX, UInt64, (UInt64, UInt64))
addr = addr-(UInt64(addr)%4096)
const PAGE_EXECUTE_READWRITE = 0x40
PAGE_EXECUTE_READWRITE = 0x40
oldPerm = Ref{UInt32}()
err18083 = ccall(:VirtualProtect,stdcall,Cint,
(Ptr{Void}, Csize_t, UInt32, Ptr{UInt32}),
Expand Down
2 changes: 1 addition & 1 deletion test/repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ function AddCustomMode(repl, prompt)
hp.mode_mapping[:foobar] = foobar_mode
foobar_mode.hist = hp

const foobar_keymap = Dict{Any,Any}(
foobar_keymap = Dict{Any,Any}(
'<' => function (s,args...)
if isempty(s)
if !haskey(s.mode_state,foobar_mode)
Expand Down
2 changes: 1 addition & 1 deletion test/replcompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function temp_pkg_dir_noinit(fn::Function)
# Used in tests below to set up and tear down a sandboxed package directory
# Unlike the version in test/pkg.jl, this does not run Pkg.init so does not
# clone METADATA (only pkg and libgit2-online tests should need internet access)
const tmpdir = joinpath(tempdir(),randstring())
tmpdir = joinpath(tempdir(),randstring())
withenv("JULIA_PKGDIR" => tmpdir) do
@test !isdir(Pkg.dir())
try
Expand Down
4 changes: 2 additions & 2 deletions test/subtype.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1094,12 +1094,12 @@ f20103(::Type{TT20103{X,Y}},x::X,y::Y) where {X,Y} = 1
f20103(::Type{TT20103{X,X}},x::X) where {X} = 100
@test_broken typeintersect(Type{NTuple{N,E}} where E where N, Type{NTuple{N,E} where N} where E) == Union{} # use @testintersect once fixed
let ints = (Int, Int32, UInt, UInt32)
const Ints = Union{ints...}
Ints = Union{ints...}
vecs = []
for i = 2:4, t in ints
push!(vecs, NTuple{i, t})
end
const Vecs = Union{vecs...}
Vecs = Union{vecs...}
T = Type{Tuple{V, I}} where V <: Vecs where I <: Ints
@testintersect(T, T, T)
test(a::Type{Tuple{V, I}}) where {V <: Vecs, I <: Ints} = I
Expand Down

0 comments on commit cdd6da5

Please sign in to comment.