Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Line labels with code after them are not recognized as line labels. #2636

Closed
SystemsModelling opened this issue Feb 4, 2017 · 2 comments
Closed
Labels
bug Identifies work items for known bugs parse-tree-processing

Comments

@SystemsModelling
Copy link

Two warnings about use of Again: label

(1)
Variable 'Again' is used but not assigned
doubleclick points to white space left of "Dim Fname"
Function MacGetSaveAsFilenameExcel(MyInitialFilename As String, FileExtension As String)
'Ron de Bruin, 03-April-2015
'Custom function for the Mac to save the activeworkbook in the format you want.
'If FileExtension = "" you can save in the following formats : xls, xlsx, xlsm, xlsb
'You can also set FileExtension to the extension you want like "xlsx" for example
Dim FName As Variant
Dim FileFormatValue As Long
Dim TestIfOpen As Workbook
Dim FileExtGetSaveAsFilename As String

736 Again: FName = False

Maybe it's the double label? Line number 736 AND a Goto target?
(2)
delete 736 and redo Code Inspection
Now get
Variable 'Again' is never assigned

same pointing to white space

@comintern
Copy link
Contributor

For reference, the code is from this link, and the problem spot is here:

    Dim FileExtGetSaveAsFilename As String

Again:         FName = False
    'Call VBA GetSaveAsFilename

It appears that RD is treating the : marker in the label as a statement separator instead of a label marker. TBH I had no idea that you could put code on the same line as a label (that should probably be an inspection itself, because the syntax on that line is completely ambiguous at a glance.

Good find!

@comintern comintern added bug Identifies work items for known bugs parse-tree-processing labels Feb 4, 2017
@comintern comintern changed the title Odd Label: handling Line labels with code after them are not recognized as line labels. Feb 4, 2017
@comintern
Copy link
Contributor

This is a bug in the grammar, not in the resolver. The token Again is a match for LExprContext and the token is being consumed as a SimpleNameExprContext.

The problem is that the label rule is...

identifierStatementLabel : unrestrictedIdentifier whiteSpace? COLON;

...but the issue seems to be that there is nothing that anchors identifierStatementLabel to the beginning of the line. This is related to #1873, and it's possible that adding a "beginning of line" rule would be the direction to go here. The tricky part is even a BOL is ambiguous because it can't be preceded with a line continuation. For example, in this code Bar: is a label:

Sub Foo()
    Dim x As Long, y As Long, result As Long
Bar: result = x + y
    Debug.Print result
End Sub

Sub Bar()
    Debug.Print "Bar called"
End Sub

In this code, Bar: is (obviously) not:

Sub Foo()
    Dim x As Long, y As Long, result As Long
    Call _
Bar: result = x + y
    Debug.Print result
End Sub

Sub Bar()
    Debug.Print "Bar called"
End Sub

Also worth noting is that VBA has some problems with this too:

'In a UserForm with a Label named Label:
Private Sub UserForm_Click()
    Dim Label As Label
    Set Label = Me.Label
Label:
    Label = "Foo"
    If Label <> "Bar" Then
        GoTo Label    '<--Runtime error 13 - "Type mismatch"
    End If
End Sub

MDoerner added a commit to MDoerner/Rubberduck that referenced this issue May 10, 2017
@MDoerner MDoerner mentioned this issue May 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Identifies work items for known bugs parse-tree-processing
Projects
None yet
Development

No branches or pull requests

3 participants