From cdd6da53e8ac907ab815e24197547d776ca08145 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Mon, 14 Aug 2017 16:19:48 -0400 Subject: [PATCH] deprecate `const` on local variables, which used to be ignored. part of #5148 --- NEWS.md | 3 +++ doc/src/manual/variables-and-scoping.md | 3 ++- src/julia-syntax.scm | 15 +++++++++------ test/TestHelpers.jl | 4 ++-- test/core.jl | 3 ++- test/dates/io.jl | 2 +- test/misc.jl | 2 +- test/repl.jl | 2 +- test/replcompletions.jl | 2 +- test/subtype.jl | 4 ++-- 10 files changed, 24 insertions(+), 16 deletions(-) diff --git a/NEWS.md b/NEWS.md index 02a4cb0beb31b..4f001ae743fec 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 ---------------- diff --git a/doc/src/manual/variables-and-scoping.md b/doc/src/manual/variables-and-scoping.md index f1df11b7ed5a3..22b1f2c8e7551 100644 --- a/doc/src/manual/variables-and-scoping.md +++ b/doc/src/manual/variables-and-scoping.md @@ -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. diff --git a/src/julia-syntax.scm b/src/julia-syntax.scm index e6481ffdefe2d..10b919e94798b 100644 --- a/src/julia-syntax.scm +++ b/src/julia-syntax.scm @@ -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))))) @@ -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 diff --git a/test/TestHelpers.jl b/test/TestHelpers.jl index 4f99773aa2a7a..0ee2501159c98 100644 --- a/test/TestHelpers.jl +++ b/test/TestHelpers.jl @@ -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") diff --git a/test/core.jl b/test/core.jl index 1faaba8622958..756831b4651ab 100644 --- a/test/core.jl +++ b/test/core.jl @@ -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 diff --git a/test/dates/io.jl b/test/dates/io.jl index c7523277e4535..55e01c6ab3a61 100644 --- a/test/dates/io.jl +++ b/test/dates/io.jl @@ -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)) diff --git a/test/misc.jl b/test/misc.jl index 094e8dc0c9328..840bb5ce56e00 100644 --- a/test/misc.jl +++ b/test/misc.jl @@ -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}), diff --git a/test/repl.jl b/test/repl.jl index 3c3fc9a1cbf6f..727bb33d4770c 100644 --- a/test/repl.jl +++ b/test/repl.jl @@ -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) diff --git a/test/replcompletions.jl b/test/replcompletions.jl index 6e4630aa52eaf..f47108f2dc10a 100644 --- a/test/replcompletions.jl +++ b/test/replcompletions.jl @@ -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 diff --git a/test/subtype.jl b/test/subtype.jl index 458843661792e..e18f4d6593f29 100644 --- a/test/subtype.jl +++ b/test/subtype.jl @@ -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