Bug 132064 - "With" statement likely evaluates its argument on each unqualified member access
Summary: "With" statement likely evaluates its argument on each unqualified member access
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
Inherited From OOo
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
: 99554 (view as bug list)
Depends on:
Blocks: Macro-StarBasic
  Show dependency treegraph
 
Reported: 2020-04-12 12:27 UTC by Mike Kaganski
Modified: 2024-03-17 03:14 UTC (History)
4 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 Mike Kaganski 2020-04-12 12:27:29 UTC
Look at this BASIC code:

> sub TestRootAccess1
>   GlobalScope.BasicLibraries.LoadLibrary("Tools")
>   Dim KeyWriter As Object
>   KeyWriter = GetRegistryKeyContent("org.openoffice.Office.Calc/Input", True)
>   KeyWriter.setPropertyValue("ExpandReference", True)
>   KeyWriter.commitChanges()
>   ' Use *different* object to read the setting
>   MsgBox GetRegistryKeyContent("org.openoffice.Office.Calc/Input").getByName("ExpandReference")
> end sub

This code will show "True", irrespective of previous setting value (which means it works as expected).

Now modify that like this:

> sub TestRootAccess2
>   GlobalScope.BasicLibraries.LoadLibrary("Tools")
>   With GetRegistryKeyContent("org.openoffice.Office.Calc/Input", True)
>     .setPropertyValue("ExpandReference", False)
>     .commitChanges()
>   End With
>   ' Use *different* object to read the setting
>   MsgBox GetRegistryKeyContent("org.openoffice.Office.Calc/Input").getByName("ExpandReference")
> end sub

Now this code will not actually change the setting; e.g., if TestRootAccess2 run after the previous TestRootAccess1, the result of TestRootAccess2 will be True, not the expected False.

The problem seems to be that the With statement does not evaluate its argument (GetRegistryKeyContent("org.openoffice.Office.Calc/Input", True)) at entry to use it later at each member access, but evaluates it each time anew when it accesses the members of the object using unqualified ".member" syntax. That means that commitChanges() is executed for a different object than setPropertyValue(), and the changes are not committed.

The help for With statement [1] mentions "single object":

> Use With and End With if you have several properties or methods for a single object.

VBA help [2] also says about that:

> Executes a series of statements on a single object or a user-defined type.

VB help [3] goes even further:

> objectExpression Required. An expression that evaluates to an object. The expression may be arbitrarily complex and is evaluated only once.

So the implementation of With looks inconsistent, unexpected, inefficient (in the best case) and wrong.

[1] https://help.libreoffice.org/6.4/en-US/text/sbasic/shared/03090411.html
[2] https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/with-statement
[3] https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/with-end-with-statement

Tested with Version: 6.4.3.1 (x64)
Build ID: 4d2b2b47cca498fed6abf712a36d0788901091eb
CPU threads: 12; OS: Windows 10.0 Build 18363; UI render: default; VCL: win; 
Locale: en-US (ru_RU); UI-Language: en-US
Calc: threaded
Comment 1 Mike Kaganski 2022-03-17 06:24:47 UTC
Already repro with OOo 2.2.0
Comment 2 Mike Kaganski 2022-03-17 07:02:01 UTC
*** Bug 99554 has been marked as a duplicate of this bug. ***
Comment 3 QA Administrators 2024-03-17 03:14:16 UTC
Dear Mike Kaganski,

To make sure we're focusing on the bugs that affect our users today, LibreOffice QA is asking bug reporters and confirmers to retest open, confirmed bugs which have not been touched for over a year.

There have been thousands of bug fixes and commits since anyone checked on this bug report. During that time, it's possible that the bug has been fixed, or the details of the problem have changed. We'd really appreciate your help in getting confirmation that the bug is still present.

If you have time, please do the following:

Test to see if the bug is still present with the latest version of LibreOffice from https://www.libreoffice.org/download/

If the bug is present, please leave a comment that includes the information from Help - About LibreOffice.
 
If the bug is NOT present, please set the bug's Status field to RESOLVED-WORKSFORME and leave a comment that includes the information from Help - About LibreOffice.

Please DO NOT

Update the version field
Reply via email (please reply directly on the bug tracker)
Set the bug's Status field to RESOLVED - FIXED (this status has a particular meaning that is not 
appropriate in this case)


If you want to do more to help you can test to see if your issue is a REGRESSION. To do so:
1. Download and install oldest version of LibreOffice (usually 3.3 unless your bug pertains to a feature added after 3.3) from https://downloadarchive.documentfoundation.org/libreoffice/old/

2. Test your bug
3. Leave a comment with your results.
4a. If the bug was present with 3.3 - set version to 'inherited from OOo';
4b. If the bug was not present in 3.3 - add 'regression' to keyword


Feel free to come ask questions or to say hello in our QA chat: https://web.libera.chat/?settings=#libreoffice-qa

Thank you for helping us make LibreOffice even better for everyone!

Warm Regards,
QA Team

MassPing-UntouchedBug