If you pass a Variant holding an array to a Function normally you may access that array by its parameter name, as expected, regardless of array dimension count. However, if you ReDim that parameter name with different dimension count even in an excluded code branch, BEFORE attempting to access the array, the access to the array under the passed dimension count will not compile. The example below will not compile. Sub StartBug() Dim A(1 To 2, 1 To 2) FuncBug(A) End Sub Function FuncBug(A As Variant) If False Then ReDim A(1 To 3) Else A(1,1) = 42 End If End Function This next example below will compile. Sub StartBug() Dim A(1 To 2, 1 To 2) FuncBug(A) End Sub Function FuncBug(A As Variant) If True Then A(1,1) = 42 Else ReDim A(1 To 3) End If End Function Simply put, the compiler seems--I mean seems, not is--naive when protecting against dimension count errors. There would seem to be a heartier workaround than ordering of the branches by Dimming and ReDimming an array locally to FuncBug then assigning that back to A before exit, but this bug might relate to Bug 133811 and seems worth consideration.
just a guess: should we prepare stack-thingy that holds dimensions for each block , pop and reset to that dimensions here https://opengrok.libreoffice.org/xref/core/basic/source/comp/loops.cxx?r=ab9b67bb#63 and before Parse() here? https://opengrok.libreoffice.org/xref/core/basic/source/comp/loops.cxx?r=ab9b67bb#74
and similar fix may be needed for single-line redim and for other branching syntax such as Switch-case. Another thing I'm just a bit concerned is "Go To" Statement.
just a guess: Sub Main() Dim X() As Integer A: Redim X(1) Goto C: ' Probably While runtime gotes to C, Parser goes to B, not C. B: X(1,1) = 3 Exit Sub C: Redim X(1,1) Goto B: End Sub
Looking at the following Sub StartUp1() Dim Passed(1 To 2) As Variant FuncBug1(Passed) End Sub Function FuncBug1(Passed() As Variant) Dim LocalDimmed(1 To 2) As Variant 'LocalDimmed(1, 1) = 42 'Will not compile for a locally Dimmed array Passed(1, 1) = 42 'Compiles and works, ignoring 2nd index MsgBox Passed(1, 1, 404, "Hi, how are you?") 'Shows 42 End Function we see--as a fun side note--that passed arrays may not even generate a runtime error with mismatched dimensions. This is harmless because when accessed with too many indices the code works, but when accessed with too few indices the runtime generates an 'Index out of defined range' error. Quirkiness aside, this leads to the question: Should an array passed to a function even be checked for dimension count at all at compile time if that makes no claims about operation at runtime? I get that the scoping issue would still exist for locally Dimmed arrays, but I am just wondering about the philosophy of issuing compile-time errors on things that can't really be checked statically anyway.
@Andreas, @Mike, I thought you might be interested in this issue