Skip to content

Commit 5e4f571

Browse files
committed
bpart changes: struct desugaring
Changes from JuliaLang/julia#57253 (bpart: Fully switch to partitioned semantics). This fixes one failing test and realigns struct desugaring to match lisp for now. Also changed: the expected result of redefining a primitive type (now allowed).
1 parent 58a5487 commit 5e4f571

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

src/desugaring.jl

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3883,6 +3883,9 @@ function expand_struct_def(ctx, ex, docs)
38833883
min_initialized = minimum((_constructor_min_initalized(e) for e in inner_defs),
38843884
init=length(field_names))
38853885
newtype_var = ssavar(ctx, ex, "struct_type")
3886+
hasprev = ssavar(ctx, ex, "hasprev")
3887+
prev = ssavar(ctx, ex, "prev")
3888+
newdef = ssavar(ctx, ex, "newdef")
38863889
layer = new_scope_layer(ctx, struct_name)
38873890
global_struct_name = adopt_scope(struct_name, layer)
38883891
if !isempty(typevar_names)
@@ -3949,7 +3952,6 @@ function expand_struct_def(ctx, ex, docs)
39493952
# Needed for later constdecl to work, though plain global form may be removed soon.
39503953
[K"global" global_struct_name]
39513954
[K"block"
3952-
[K"global" global_struct_name]
39533955
[K"local" struct_name]
39543956
[K"always_defined" struct_name]
39553957
typevar_stmts...
@@ -3968,40 +3970,37 @@ function expand_struct_def(ctx, ex, docs)
39683970
]
39693971
[K"=" struct_name newtype_var]
39703972
[K"call"(supertype) "_setsuper!"::K"core" newtype_var supertype]
3971-
[K"if"
3972-
[K"call" "isdefinedglobal"::K"core"
3973-
ctx.mod::K"Value"
3974-
struct_name=>K"Symbol"
3975-
false::K"Bool"]
3976-
[K"if"
3977-
[K"call" "_equiv_typedef"::K"core" global_struct_name newtype_var]
3978-
[K"block"
3979-
# If this is compatible with an old definition, use
3980-
# the existing type object and throw away the new
3981-
# type
3982-
[K"=" struct_name global_struct_name]
3983-
if !isempty(typevar_names)
3984-
# And resassign the typevar_names - these may be
3985-
# referenced in the definition of the field
3986-
# types below
3987-
[K"="
3988-
[K"tuple" typevar_names...]
3989-
prev_typevars
3990-
]
3991-
end
3992-
]
3973+
[K"=" hasprev
3974+
[K"&&" [K"call" "isdefinedglobal"::K"core"
3975+
ctx.mod::K"Value"
3976+
struct_name=>K"Symbol"
3977+
false::K"Bool"]
3978+
[K"call" "_equiv_typedef"::K"core" global_struct_name newtype_var]
3979+
]]
3980+
[K"=" prev [K"if" hasprev global_struct_name false::K"Bool"]]
3981+
[K"if" hasprev
3982+
[K"block"
3983+
# if this is compatible with an old definition, use the old parameters, but the
3984+
# new object. This will fail to capture recursive cases, but the call to typebody!
3985+
# below is permitted to choose either type definition to put into the binding table
3986+
if !isempty(typevar_names)
3987+
# And resassign the typevar_names - these may be
3988+
# referenced in the definition of the field
3989+
# types below
3990+
[K"=" [K"tuple" typevar_names...] prev_typevars]
3991+
end
39933992
]
39943993
]
3995-
[K"call"(type_body)
3996-
"_typebody!"::K"core"
3997-
# TODO: if there is a previous compatible definition, re-use params. See #57253
3998-
false::K"Bool"
3999-
newtype_var
4000-
[K"call" "svec"::K"core" insert_struct_shim(ctx, field_types, struct_name)...]
4001-
]
3994+
[K"=" newdef
3995+
[K"call"(type_body)
3996+
"_typebody!"::K"core"
3997+
prev
3998+
newtype_var
3999+
[K"call" "svec"::K"core" insert_struct_shim(ctx, field_types, struct_name)...]
4000+
]]
40024001
[K"constdecl"
40034002
global_struct_name
4004-
newtype_var
4003+
newdef
40054004
]
40064005
[K"latestworld"]
40074006
# Default constructors

test/typedefs.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,9 @@ end
255255
JuliaLowering.include_string(test_mod, "struct X36104; x::Int; end")
256256
@test fieldtypes(test_mod.X36104) == (Int,)
257257
JuliaLowering.include_string(test_mod, "primitive type P36104 8 end")
258-
@test_throws ErrorException("invalid redefinition of constant TestMod.P36104") #=
259-
=# JuliaLowering.include_string(test_mod, "primitive type P36104 16 end")
258+
JuliaLowering.include_string(test_mod, "const orig_P36104 = P36104")
259+
JuliaLowering.include_string(test_mod, "primitive type P36104 16 end")
260+
@test test_mod.P36104 !== test_mod.orig_P36104
260261

261262
# Struct with outer constructor where one typevar is constrained by the other
262263
# See https://github.com/JuliaLang/julia/issues/27269)

0 commit comments

Comments
 (0)