Skip to content
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

support type annotations on global variables #43671

Merged
merged 39 commits into from
Feb 8, 2022
Merged
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
f819538
add typing to global bindings
miguelraz Dec 17, 2021
ee478df
support type annotations on global variables
simeonschaub Dec 30, 2021
2846e34
rename to `_set_binding_type!`
simeonschaub Jan 5, 2022
b6e4587
try to fix analyzegc failure
simeonschaub Jan 5, 2022
a3e6c98
always add conversion step for globals
simeonschaub Jan 6, 2022
bc38f26
try to fix tests
simeonschaub Jan 6, 2022
1791c3c
fix inference test
simeonschaub Jan 6, 2022
c4161fd
fix analyzegc failures
simeonschaub Jan 7, 2022
28c8cba
allow `Core._get_binding_type` to be elided
simeonschaub Jan 8, 2022
646e2ab
clean up `julia-syntax.scm`
simeonschaub Jan 8, 2022
e0e0364
Merge remote-tracking branch 'origin/master' into sds/typedglobals
simeonschaub Jan 16, 2022
b28d542
add docs
simeonschaub Jan 16, 2022
b208fbf
add NEWS entry
simeonschaub Jan 16, 2022
d7e92f1
remove leading underscores in names
simeonschaub Jan 16, 2022
ff22fc0
fix docs
simeonschaub Jan 16, 2022
e93e694
just remove ref to `<:`
simeonschaub Jan 16, 2022
3656ddf
Merge remote-tracking branch 'origin/master' into sds/typedglobals
simeonschaub Jan 21, 2022
1d54432
try to make `ty` field atomic
simeonschaub Jan 22, 2022
ce66bbb
(de)serialize type field correctly
simeonschaub Jan 23, 2022
f2d1b48
Merge remote-tracking branch 'origin/master' into sds/typedglobals
simeonschaub Jan 23, 2022
931b7ba
fix serialization
simeonschaub Jan 24, 2022
e04c6be
address review comments
simeonschaub Jan 24, 2022
346220a
fix test, add test for different owner
simeonschaub Jan 24, 2022
8989e6e
try to fix distributed tests
simeonschaub Jan 24, 2022
de692a7
fix whitespace snafu
simeonschaub Jan 25, 2022
b36f3e2
Merge remote-tracking branch 'origin/master' into sds/typedglobals
simeonschaub Jan 25, 2022
edfadd3
refactor lowering a bit, fix unnecessary ssavalue
simeonschaub Jan 26, 2022
38d1b06
disallow type annotations in local scope
simeonschaub Feb 4, 2022
1504a97
Merge remote-tracking branch 'origin/master' into sds/typedglobals
simeonschaub Feb 4, 2022
160ee63
use nothing instead of NULL
simeonschaub Feb 4, 2022
a334a9b
address Jeff's review comment
simeonschaub Feb 7, 2022
efdb986
fix doctests
simeonschaub Feb 7, 2022
645f155
better error message in `head-to-text`
simeonschaub Feb 8, 2022
b442636
fix elision of `get_binding_type`
simeonschaub Feb 8, 2022
592c957
Merge remote-tracking branch 'origin/master' into sds/typedglobals
simeonschaub Feb 8, 2022
59bc7c0
fix tests
simeonschaub Feb 8, 2022
29afe48
update test for #33243
simeonschaub Feb 8, 2022
d265016
make test more robust
simeonschaub Feb 8, 2022
2183d33
use binding type in codegen?
simeonschaub Feb 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix test, add test for different owner
simeonschaub committed Jan 24, 2022

Verified

This commit was signed with the committer’s verified signature.
simeonschaub Simeon David Schaub
commit 346220ab457d91d06bc34366dac7075f37d06aa7
25 changes: 8 additions & 17 deletions doc/src/manual/variables-and-scoping.md
Original file line number Diff line number Diff line change
@@ -842,24 +842,15 @@ Stacktrace:
The type does not need to be concrete, but annotations with abstract types typically have little
performance benefit.

As for constants, the type of a global is not typically meant to be redefined. In interactive usage,
changing the annotation to a narrower type (the new type is a subtype of the previous type) is
allowed, but can result in unexpected behavior in code that has already been compiled:
Once a global has either been assigned to or its type has been set, the binding type is not allowed
to change:

```jldoctest
julia> global a::Integer
julia> g() = a
g (generic function with 1 method)
julia> Base.return_types(g)
1-element Vector{Any}:
Integer
julia> global a::Int
WARNING: redefinition of type of a. This may fail, cause incorrect answers, or produce other errors.
julia> x = 1
1
julia> Base.return_types(g)
1-element Vector{Any}:
Integer
julia> global x::Int
ERROR: cannot set type for global x. It already has a value or is already set to a different type.
Stacktrace:
[...]
```
47 changes: 34 additions & 13 deletions test/syntax.jl
Original file line number Diff line number Diff line change
@@ -2420,9 +2420,9 @@ end
@test_throws ParseError("extra token \"2\" after end of expression") Meta.parse("1#==# 2")

@test size([1#==#2#==#3]) == size([1 2 3])
@test size([1#==#2#==#3]) == size([1 2 3]) # tabs
@test size([1#==#2#==#3]) == size([1 2 3]) # tabs and spaces
@test size([1#==#2#==#3]) == size([1 2 3]) # tabs and spaces
@test size([1#==#2#==#3]) == size([1 2 3]) # tabs
@test size([1#==#2#==#3]) == size([1 2 3]) # tabs and spaces
@test size([1#==#2#==#3]) == size([1 2 3]) # tabs and spaces
@test [zeros(Int,2,2)#==#[1;2]
[3#==#4]#==#5] == [zeros(Int,2,2) [1; 2]
[3 4] 5 ] == [0 0 1
@@ -3088,19 +3088,19 @@ end

@test_throws ParseError Meta.parse("""
function checkUserAccess(u::User)
if u.accessLevel != "user\u202e \u2066# users are not allowed\u2069\u2066"
return true
end
return false
if u.accessLevel != "user\u202e \u2066# users are not allowed\u2069\u2066"
return true
end
return false
end
""")

@test_throws ParseError Meta.parse("""
function checkUserAccess(u::User)
#=\u202e \u2066if (u.isAdmin)\u2069 \u2066 begin admins only =#
return true
#= end admin only \u202e \u2066end\u2069 \u2066=#
return false
#=\u202e \u2066if (u.isAdmin)\u2069 \u2066 begin admins only =#
return true
#= end admin only \u202e \u2066end\u2069 \u2066=#
return false
end
""")

@@ -3169,11 +3169,12 @@ end
@test m.x === 2
@test Base.return_types(m.g, ()) == [Int]

@test Meta.isexpr(Meta.@lower(function f()
m = Module()
@test_throws ErrorException @eval m function f()
global x
x::Int = 1
x::Float64 = 2.
end), :error)
end

m = Module()
@test_throws ErrorException @eval m begin
@@ -3198,4 +3199,24 @@ end
x = 1
global x::Float64
end

m = Module()
@test_throws ErrorException @eval m begin
x = 1
global x::Int
end

m = Module()
@eval m module Foo
export bar
bar = 1
end
@eval m begin
using .Foo
bar::Float64 = 2
end
@test m.bar === 2.0
@test Core.get_binding_type(m, :bar) == Float64
@test m.Foo.bar === 1
@test Core.get_binding_type(m.Foo, :bar) == Any
end