Bug 159455 - Declared default for optional argument ignored when later argument specified by name.
Summary: Declared default for optional argument ignored when later argument specified ...
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
7.6.4.1 release
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: Macro-StarBasic
  Show dependency treegraph
 
Reported: 2024-01-30 14:54 UTC by Patrick Traill
Modified: 2024-02-21 13:39 UTC (History)
2 users (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Patrick Traill 2024-01-30 14:54:29 UTC
Description:
When I declare a default value for an Optional argument, but specify a later argument by name, the previous argument does not receive the specified default value. It looks as though it receives instead the default value for variables of the specified type.

This seems in obvious conflict with this statement in the help:

    = expression: Specify a default value for the argument, matching its declared type. Optional is necessary for each argument specifying a default value.

It seems pretty embarrassing, as fixing this could break existing macros.At a minimum, if it cannot be fixed cleanly, the Help quoted above should accurately describe the actual behaviour.

In the following code, the first argument receives the value False although the default was declared to be True:

I believe I have seen this in a number of versions, but currently I am seeing it in the version given under Other Information.

Steps to Reproduce:
1. Run the following code
Option Explicit

Option Compatible

' Demonstrate a bug in assigning values to optional parameters:
'	When an optional parameter is specified by name,
'	preceding unspecified optional parameters do not honour default values specified in the declaration.
Sub Bug_Default_Ignored ()
	Bug_Default_Ignored_Sub	(B := True)
End Sub

' Demonstrate a bug in assigning values to optional parameters, as above
Sub Bug_Default_Ignored_Sub _
( Optional A as Boolean = True _
, Optional B as Boolean = False _
)
	MsgBox ("A = " + A + ",  B = " + B)
End Sub


Actual Results:
Message saying
A = False,  B = True

Expected Results:
Message saying
A = True,  B = True


Reproducible: Always


User Profile Reset: No

Additional Info:
Version: 7.6.4.1 (X86_64) / LibreOffice Community
Build ID: e19e193f88cd6c0525a17fb7a176ed8e6a3e2aa1
CPU threads: 8; OS: Linux 5.3; UI render: default; VCL: kf5 (cairo+xcb)
Locale: en-GB (en_GB.UTF-8); UI: en-GB
Calc: threaded
Comment 1 Rafael Lima 2024-01-31 13:27:31 UTC
This is definitely a bug in the Basic interpreter, because the results depend on the position of the optional argument.

Consider the two following scenarios where I add a new argument (newArg) after and before "B" in your example:

SCENARIO 1 (new optional argument "newArg" after argument B)

Sub Bug_Default_Ignored ()
	Bug_Default_Ignored_Sub	(B := True)
End Sub

Sub Bug_Default_Ignored_Sub _
( Optional A as Boolean = True _
, Optional B as Boolean = False _
, Optional newArg as Integer = 10 _
)
	MsgBox ("A = " + A + ",  B = " + B + ", newArg = " + newArg)
End Sub

Running Bug_Default_Ignored will print "A = False,  B = True, newArg = 10" (notice that newArg = 10 which is correct)

SCENARIO 2 (new optional argument "newArg" before argument B)

Sub Bug_Default_Ignored ()
	Bug_Default_Ignored_Sub	(B := True)
End Sub

Sub Bug_Default_Ignored_Sub _
( Optional newArg as Integer = 10 _
, Optional A as Boolean = True _
, Optional B as Boolean = False _
)
	MsgBox ("A = " + A + ",  B = " + B + " newArg = " + newArg)
End Sub

Running Bug_Default_Ignored will print "A = False,  B = True, newArg = 0" (notice that newArg = 0 now).
Comment 2 Patrick Traill 2024-02-21 00:45:16 UTC
On a similar note, when a named parameter is passed, IsMissing for a preceding Optional parameter returns False although it is not supplied.
It is possible to make IsMissing return True by “explicitly omitting” the parameter, i.e. writing “,” in the appropriate place.

I suspect this is all a result of the same mechanism, so that this may be regarded as another aspect of the same bug.
Again, correcting this behaviour could break existing code.

This is demonstrated by the following test, in which the 3rd and 4th calls display the wrong behaviour.

Sub Bug_Optional_Not_IsMissing_2 ()
	Bug_Optional_Not_IsMissing_2_Sub	()
	Bug_Optional_Not_IsMissing_2_Sub	(,)
	Bug_Optional_Not_IsMissing_2_Sub	(B := True)
	Bug_Optional_Not_IsMissing_2_Sub	(B := False)
	Bug_Optional_Not_IsMissing_2_Sub	(, B := True)
	Bug_Optional_Not_IsMissing_2_Sub	(, B := False)
End Sub

' Demonstrate a bug in assigning values to optional parameters, as above
Sub Bug_Optional_Not_IsMissing_2_Sub _
( Optional A as Boolean _
, Optional B as Boolean = False _
)
	MsgBox ("IsMissing (A) = " + IsMissing (A) + ",  B = " + B)
End Sub
Comment 3 Rafael Lima 2024-02-21 11:50:30 UTC
> Again, correcting this behaviour could break existing code.

I'm not sure there's a high risk of breaking existing code, because the current behavior is a clear bug, since the argument assumes an unexpected value... it's more likely that someone else would have noticed this and reported as a bug (as you did).

So I'm in favor of fixing this issue.

@Andreas, what is your take on this bug? Any risks here?
Comment 4 Andreas Heinisch 2024-02-21 13:19:47 UTC
Fixing an issue may always result in breaking existing workflows or code: https://www.explainxkcd.com/wiki/index.php/1172:_Workflow

However, this should be fixed and imho is a duplicate of 143706
Comment 5 Patrick Traill 2024-02-21 13:23:13 UTC
Users should have no problems if they (or their macro programmers, if different):
* Do not use macros at all.
* Only use macros to record and replay UI interactions.
* Study the release notes carefully.
* Have well-run development teams that have set up regression tests for their macros and run them before deploying a new version of LibreOffice. (Atypical?)

Problems could mainly be experienced by users who:
* Have enough programming experience to find named arguments desirable.
* AND Write macros more as a sideline or hobby.
* AND Have produced a sufficiently large volume 
Also by users who:
* Use a spreadsheet written by a user such as the above.

I have no idea how widespread the use of macros is, so I cannot estimate how many are likely to be affected. My guess is that it is quite likely to affect a very small fraction of users; how small that fraction would be in absolute terms I do not know, but I guess that someone somewhere would get burned.

I should like to see this bug fixed too, but I wonder if anything can be done to mitigate the perhaps moderate risk. Obviously there should be a warning in the release notes. An option to enforce old behaviour or one to enable correct behaviour probably adds unwanted complication to the code base. It helps that it would first appear in an early release version used by risk-happy power users.
Comment 6 Patrick Traill 2024-02-21 13:39:03 UTC
(In reply to Andreas Heinisch from comment #4)
> Fixing an issue may always result in breaking existing workflows or code:
> https://www.explainxkcd.com/wiki/index.php/1172:_Workflow
> 
> However, this should be fixed and imho is a duplicate of 143706

I agree that this is essentially a duplicate. What this perhaps contributes is these regression test cases:
* If there is no default value, IsMissing should return True (and accessing the value throw an error).
* If there is a default value, it should be applied (especially if not the default for the type!).

Good XKCD!
(People abusing weird behaviour deserve what they get, I care more about those who accidentally depend on it.)