# HG changeset patch # User Fernando Alvarruiz # Date 1670470648 18000 # Node ID a18897e4c7b5dae4d75f31dace35d228c7dee4ff # Parent beb7f91a0a972a97c26d19a40d9bf8d3b038fd7a classdef: call subsref method with correct nargout value (bug #48693) * /libinterp/octave-value/ov-classdef.cc (octave_classdef::subsref): Correct the nargout value for classdef subsref calls by checking the number of values the indexing argument addresses, using the same code used by the non-classdef subsref method. * /test/bug-48693/bug-48693.tst, bug48693.m, module.mk: Add simple test function files to verify subsref behavior. diff -r beb7f91a0a97 -r a18897e4c7b5 libinterp/octave-value/ov-classdef.cc --- a/libinterp/octave-value/ov-classdef.cc Wed Dec 07 14:26:59 2022 +0100 +++ b/libinterp/octave-value/ov-classdef.cc Wed Dec 07 22:37:28 2022 -0500 @@ -91,8 +91,29 @@ count++; args(0) = octave_value (this); + // Attempt to set up a proper value for nargout at least in the + // simple case where the cs-list-type expression - i.e., {} or + // ().x, is the leading one. + bool maybe_cs_list_query = (type[0] == '.' || type[0] == '{' + || (type.length () > 1 && type[0] == '(' + && type[1] == '.')); + + if (maybe_cs_list_query) + { + // Set up a proper nargout for the subsref call by calling numel. + octave_value_list tmp; + if (type[0] != '.') tmp = idx.front (); + nargout = xnumel (tmp); + } + retval = meth.execute (args, nargout, true, "subsref"); + // Since we're handling subsref, if the list has more than one + // element, return it as a comma-separated list so that we can + // pass it to the evaluator + if (retval.length () > 1) + retval = octave_value (retval); + return retval; } } diff -r beb7f91a0a97 -r a18897e4c7b5 test/bug-48693/bug-48693.tst --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/bug-48693/bug-48693.tst Wed Dec 07 22:37:28 2022 -0500 @@ -0,0 +1,3 @@ +%!test +%! x = bug48693(); +%! assert ({x{1:10}}, num2cell (zeros(1,10))) diff -r beb7f91a0a97 -r a18897e4c7b5 test/bug-48693/bug48693.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/bug-48693/bug48693.m Wed Dec 07 22:37:28 2022 -0500 @@ -0,0 +1,7 @@ +classdef bug48693 < handle + methods + function varargout = subsref (x, idx) + varargout = num2cell (zeros (size (idx(1).subs{1}))); + end + end +end diff -r beb7f91a0a97 -r a18897e4c7b5 test/bug-48693/module.mk --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/bug-48693/module.mk Wed Dec 07 22:37:28 2022 -0500 @@ -0,0 +1,5 @@ +bug_48693_TEST_FILES = \ + %reldir%/bug48693.m \ + %reldir%/bug-48693.tst + +TEST_FILES += $(bug_48693_TEST_FILES)