From 55dbbdc75020e3ffead5359244c67a6103013ed2 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Fri, 15 May 2026 19:35:20 +0200 Subject: [PATCH] Fix UndefVarError in merge_nonunique_inputs warning interpolation The warning strings interpolated `$(u[i])` but `u` is not defined inside `merge_nonunique_inputs(sys)`. When the warning condition fires (overlapping non-zero entries between B/D columns merged under the same input name) the @warn would raise UndefVarError instead of issuing the documented warning. Use the local `inputnames[i]` instead, and add a regression test that constructs a system with overlapping B columns under a duplicated input name and asserts the warning is emitted. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/named_systems2.jl | 4 ++-- test/test_named_systems2.jl | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/named_systems2.jl b/src/named_systems2.jl index 2e71041e..631f6de2 100644 --- a/src/named_systems2.jl +++ b/src/named_systems2.jl @@ -610,8 +610,8 @@ function merge_nonunique_inputs(sys) # Check that the B-matrix entries are non-overlapping Bi = sys.B[:, inds] Di = sys.D[:, inds] - any(>(1), sum(.! iszero.(Bi), dims=2)) && @warn("Input names are not unique and the multiple B-matrix columns associated with the name $(u[i]) have a non-empty intersection of non-zero entries.") - any(>(1), sum(.! iszero.(Di), dims=2)) && @warn("Input names are not unique and the multiple D-matrix columns associated with the name $(u[i]) have a non-empty intersection of non-zero entries.") + any(>(1), sum(.! iszero.(Bi), dims=2)) && @warn("Input names are not unique and the multiple B-matrix columns associated with the name $(inputnames[i]) have a non-empty intersection of non-zero entries.") + any(>(1), sum(.! iszero.(Di), dims=2)) && @warn("Input names are not unique and the multiple D-matrix columns associated with the name $(inputnames[i]) have a non-empty intersection of non-zero entries.") B = copy(sys.B) D = copy(sys.D) B[:, inds[1]] = sum(Bi, dims=2) diff --git a/test/test_named_systems2.jl b/test/test_named_systems2.jl index fc55b537..6a4f84e4 100644 --- a/test/test_named_systems2.jl +++ b/test/test_named_systems2.jl @@ -543,4 +543,19 @@ isys = 2/s1 # Test https://github.com/JuliaControl/RobustAndOptimalControl.jl/issues/130 P = named_ss(ssrand(1,1,2, Ts=1.0)) C = named_ss(ssrand(1,1,2, Ts=1.0)) -@test gangoffour(P, C) isa NTuple{4, NamedStateSpace{Discrete{Float64}, StateSpace{Discrete{Float64}, Float64}}} \ No newline at end of file +@test gangoffour(P, C) isa NTuple{4, NamedStateSpace{Discrete{Float64}, StateSpace{Discrete{Float64}, Float64}}} + +## merge_nonunique_inputs: warning path must not crash on overlapping B/D columns +# Regression: previous code interpolated an undefined variable `u` and raised +# UndefVarError instead of issuing the documented warning. +let + A = -I(2) + B = [1.0 1.0; 0.0 1.0] # both columns nonzero in row 1 -> overlap + C = [1.0 0.0] + D = [0.0 0.0] + nsys = named_ss(ss(A, B, C, D); u=[:u_dup, :u_dup], y=:y, unique=false) + merged = @test_logs (:warn, r"B-matrix columns") RobustAndOptimalControl.merge_nonunique_inputs(nsys) + @test merged.nu == 1 + @test merged.u == [:u_dup] + @test merged.B == sum(B, dims=2) +end \ No newline at end of file