-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix error message when using const
inside a function, require the LHS to be global, and prohibit "const x[] = 1"
#57470
Conversation
0f5a7aa
to
5a09ce3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Minor nit with this error though:
julia> const (x{T},) = T
ERROR: syntax: `const` left hand side "SSAValue(81)" contains non-variables around REPL[13]:1
Argh, this is actually a regression. 1.11.3:
This:
I'll need to think of a way to handle this. |
Also update some tests that used @test with a quote containing `const`, which cannot be spliced into a function.
33184ba
to
b45c8e0
Compare
Also update some tests that used @test with a quote containing `const`, which cannot be spliced into a function.
b45c8e0
to
a4f7d00
Compare
Also update some tests that used @test with a quote containing `const`, which cannot be spliced into a function.
a4f7d00
to
ce76dbf
Compare
This is a note to myself to not squash 9fe9bbe so I am not git blame'd for all of the assignment lowering code. |
@nanosoldier |
Linking #25642 |
The package evaluation job you requested has completed - possible new issues were detected. Report summary❗ Packages that crashed1 packages crashed only on the current version.
13 packages crashed on the previous version too. ✖ Packages that failed24 packages failed only on the current version.
1109 packages failed on the previous version too. ✔ Packages that passed tests27 packages passed tests only on the current version.
5330 packages passed tests on the previous version too. ~ Packages that at least loaded7 packages successfully loaded only on the current version.
2926 packages successfully loaded on the previous version too. ➖ Packages that were skipped altogether899 packages were skipped on the previous version too. |
Also update some tests that used @test with a quote containing `const`, which cannot be spliced into a function.
ce76dbf
to
14c14f1
Compare
Also update some tests that used @test with a quote containing `const`, which cannot be spliced into a function.
14c14f1
to
cbce6cd
Compare
Also update some tests that used @test with a quote containing `const`, which cannot be spliced into a function.
Modifies the handling of the 3-argument "lowered const" so `const x = y` participates in scope resolution again (JuliaLang#56613); we treat (const x y) as an assignment form, producing the "unsupported `const` declaration on local variable" error if the usual scope rules would not make x global. At the top level, these should succeed: const x = 1 begin const x = 1 end let const global x = 1 end Each of the following should fail: # ERROR: syntax: unsupported `const` declaration on local variable around REPL[1]:2 let const x = 1 end # ERROR: syntax: unsupported `const` declaration on local variable around REPL[1]:2 function f() const x = 1 end This also amkes the use of `const global` inside a function body produce the original, specific error message rather than a generic "World age increment not at top level" error (JuliaLang#57334): # ERROR: syntax: `global const` declaration not allowed inside function around REPL[1]:2 function f() global const x = 1 end Issue JuliaLang#57334 revealed we silently accept `const` expressions writing to variables. These now fail with a new error message: a = [1, 2] # ERROR: syntax: cannot declare "a[1]" `const` around REPL[1]:2 const a[1] = 3 The bulk of these changes track const-ness through the functions called by expand-assignment, since each of the three types of LHS is affect differently: - ordinary variables become `const`, producing error if not global - `X{T} = ...` forces X to be `const`, rather than the defaulting to being implicitly const only if in global scope. - `f() = ...` ignores `const`.
Fixes JuliaLang#57574 Co-authored-by: Em Chu <[email protected]>
cbce6cd
to
64672f5
Compare
# @test splices into a function, where const cannot appear | ||
@test Core.eval(@__MODULE__, :(const _::CantConvert = 1)) == 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems misleading, since there are no functions created by @test
, on master for example:
julia> let z = 2
const z = 1
z
end
2
julia> z
1
julia> let z = 2
global z = 1
z
end
ERROR: syntax: variable "z" declared both local and global
Stacktrace:
[1] top-level scope
@ REPL[3]:1
Though I find this whole test highly unsatisfying since declaring const _
is fairly surprising to mean no-op, while declaring global _
is a side-effect-ful operation. Although not actually relevant to this PR since it is existing behaviors.
julia> Meta.@lower global _ = 1
:($(Expr(:thunk, CodeInfo(
1 ─ global _
│ $(Expr(:latestworld))
└── return 1
))))
julia> names(Main)
4-element Vector{Symbol}:
:Base
:Core
:Main
:_
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Woops. I think I made that comment after causing the test to fail because of a bug, then I misinterpreted the output of @macroexpand
.
…HS to be global, and prohibit "const x[] = 1" (#57470) The changes to `const` resulted in confusing error messages when it was used inside a function (#57334). On 1.11.3: ``` julia> function f() const x = 1 end ERROR: syntax: unsupported `const` declaration on local variable around REPL[1]:2 Stacktrace: [1] top-level scope @ REPL[1]:1 ``` On nightly: ``` julia> function f() const x = 1 end ERROR: syntax: World age increment not at top level Stacktrace: [1] top-level scope @ REPL[1]:1 ``` In prior versions, we also accepted confused expressions like: ``` x = Ref(1) const x[] = 1 ``` This change adds a new error messages explicitly prohibiting `const` where the left hand side is not declaring variables: ``` ERROR: syntax: `const` left hand side "x[]" contains non-variables around REPL[2]:1 Stacktrace: [1] top-level scope @ REPL[2]:1 ``` Finally, #54773 made `const` stop participating in scope resolution (the left hand side was always taken to be in global scope). Some expressions that were prohibited started being accepted: In 1.11.3: ``` julia> let const x = 1 end ERROR: syntax: unsupported `const` declaration on local variable around REPL[1]:2 Stacktrace: [1] top-level scope @ REPL[1]:1 ``` Nightly: ``` julia> let const x = 1 end 1 ``` This change rejects `const` unless the variable would be in global scope (`global const` would be required in the example), so we don't lose the ability to make `const` in local scope meaningful later. (cherry picked from commit fb01f91)
The changes to
const
resulted in confusing error messages when it was usedinside a function (#57334). On 1.11.3:
On nightly:
In prior versions, we also accepted confused expressions like:
This change adds a new error messages explicitly prohibiting
const
where theleft hand side is not declaring variables:
Finally, #54773 made
const
stop participating in scope resolution (the lefthand side was always taken to be in global scope). Some expressions that were
prohibited started being accepted:
In 1.11.3:
Nightly:
This change rejects
const
unless the variable would be in global scope(
global const
would be required in the example), so we don't lose theability to make
const
in local scope meaningful later.