Skip to content

Commit f19a5bf

Browse files
Merge pull request #3170 from ven-k/vkb/fix-implicit-extend
fix: unpack components of the base sys in implicit extend
2 parents 2a1024a + dddd698 commit f19a5bf

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

src/systems/model_parsing.jl

+11-11
Original file line numberDiff line numberDiff line change
@@ -772,18 +772,17 @@ function Base.names(model::Model)
772772
map(first, get(model.structure, :components, EMPTY_VoVoSYMBOL))))
773773
end
774774

775-
function _parse_extend!(ext, a, b, dict, expr, kwargs, vars, additional_args)
775+
function _parse_extend!(ext, a, b, dict, expr, kwargs, vars, implicit_arglist)
776776
extend_args!(a, b, dict, expr, kwargs)
777777

778-
# `additional_args` doubles as a flag to check the mode of `@extend`. It is
778+
# `implicit_arglist` doubles as a flag to check the mode of `@extend`. It is
779779
# `nothing` for explicit destructuring.
780780
# The following block modifies the arguments of both base and higher systems
781781
# for the implicit extend statements.
782-
if additional_args !== nothing
782+
if implicit_arglist !== nothing
783783
b.args = [b.args[1]]
784-
allvars = [additional_args.args..., vars.args...]
785784
push!(b.args, Expr(:parameters))
786-
for var in allvars
785+
for var in implicit_arglist.args
787786
push!(b.args[end].args, var)
788787
if !haskey(dict[:kwargs], var)
789788
push!(dict[:kwargs], var => Dict(:value => NO_VALUE))
@@ -814,8 +813,8 @@ function parse_extend!(exprs, ext, dict, mod, body, kwargs)
814813
end
815814
a, b = b.args
816815
# This doubles as a flag to identify the mode of `@extend`
817-
additional_args = nothing
818-
_parse_extend!(ext, a, b, dict, expr, kwargs, vars, additional_args)
816+
implicit_arglist = nothing
817+
_parse_extend!(ext, a, b, dict, expr, kwargs, vars, implicit_arglist)
819818
else
820819
error("When explicitly destructing in `@extend` please use the syntax: `@extend a, b = oneport = OnePort()`.")
821820
end
@@ -825,11 +824,12 @@ function parse_extend!(exprs, ext, dict, mod, body, kwargs)
825824
b = body
826825
if (model = getproperty(mod, b.args[1])) isa Model
827826
vars = Expr(:tuple)
828-
append!(vars.args, _arguments(model))
829-
additional_args = Expr(:tuple)
830-
append!(additional_args.args,
827+
append!(vars.args, names(model))
828+
implicit_arglist = Expr(:tuple)
829+
append!(implicit_arglist.args, _arguments(model))
830+
append!(implicit_arglist.args,
831831
keys(get(model.structure, :structural_parameters, EMPTY_DICT)))
832-
_parse_extend!(ext, a, b, dict, expr, kwargs, vars, additional_args)
832+
_parse_extend!(ext, a, b, dict, expr, kwargs, vars, implicit_arglist)
833833
else
834834
error("Cannot infer the exact `Model` that `@extend $(body)` refers." *
835835
" Please specify the names that it brings into scope by:" *

test/model_parsing.jl

+26
Original file line numberDiff line numberDiff line change
@@ -932,3 +932,29 @@ end
932932
@test getdefault(main_sys.p2) == 12
933933
@test getdefault(main_sys.v1) == 13
934934
end
935+
936+
@mtkmodel InnerModel begin
937+
@parameters begin
938+
p
939+
end
940+
end
941+
942+
@mtkmodel MidModel begin
943+
@components begin
944+
inmodel = InnerModel()
945+
end
946+
end
947+
948+
@mtkmodel OuterModel begin
949+
@extend MidModel()
950+
@equations begin
951+
inmodel.p ~ 0
952+
end
953+
end
954+
955+
# The base system is fetched from the module while extending implicitly. This
956+
# way of defining fails when defined inside the `@testset`. So, it is moved out.
957+
@testset "Test unpacking of components in implicit extend" begin
958+
@named out = OuterModel()
959+
@test OuterModel.structure[:extend][1] == [:inmodel]
960+
end

0 commit comments

Comments
 (0)