From 65440fed56ef25430274d100754eaa74ff99dc22 Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Thu, 7 Dec 2023 19:04:14 +0530 Subject: [PATCH] fix: canonicalize all positional args in `@named` - So far, only the 1st positional-arg was canonicalized. - Add relevant tests. --- src/systems/abstractsystem.jl | 2 +- test/direct.jl | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 59d1f23406..36a3d1a124 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -964,7 +964,7 @@ function _named(name, call, runtime = false) if length(call.args) >= 2 && call.args[2] isa Expr # canonicalize to use `:parameters` if call.args[2].head === :kw - call.args[2] = Expr(:parameters, Expr(:kw, call.args[2].args...)) + call = Expr(call.head, call.args[1], Expr(:parameters, call.args[2:end]...)) has_kw = true elseif call.args[2].head === :parameters has_kw = true diff --git a/test/direct.jl b/test/direct.jl index fde52fb2b4..fd0db24d38 100644 --- a/test/direct.jl +++ b/test/direct.jl @@ -2,6 +2,8 @@ using ModelingToolkit, StaticArrays, LinearAlgebra, SparseArrays using DiffEqBase using Test +using ModelingToolkit: getdefault, getmetadata, SymScope + canonequal(a, b) = isequal(simplify(a), simplify(b)) # Calculus @@ -271,3 +273,24 @@ end @parameters x [misc = "wow"] @test SymbolicUtils.getmetadata(Symbolics.unwrap(x), ModelingToolkit.VariableMisc, nothing) == "wow" + +# Scope of defaults in the systems generated by @named +@mtkmodel MoreThanOneArg begin + @variables begin + x(t) + y(t) + z(t) + end +end + +@parameters begin + l + m + n +end + +@named model = MoreThanOneArg(x = l, y = m, z = n) + +@test getmetadata(getdefault(model.x), SymScope) == ParentScope(LocalScope()) +@test getmetadata(getdefault(model.y), SymScope) == ParentScope(LocalScope()) +@test getmetadata(getdefault(model.z), SymScope) == ParentScope(LocalScope())