dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.74k stars 3.99k forks source link

[VBNET] Name resolution issue? #48307

Open AdamSpeight2008 opened 3 years ago

AdamSpeight2008 commented 3 years ago

Original Discoverer @VBAndCs in Link To Original Post

Steps to Reproduce:

Any code where the name of the variable (being assigned to), with the assignment expression having a property / method with the same name.

Example. Dim Length = From s In {"asdf"} Select s.Length

Actual Behavior

Property_Error

The following example works, Dim Length = "asdf".Length

Which narrows to suspects to LINQ Queries.

Expected Behavior: The name to be correctly resolved, as the Length is being qualified by the s

VBAndCs commented 3 years ago

Thanks @AdamSpeight2008 And it is also OK if we use the lambda form. This gives no error: Dim Length = "asdf".Select(Function(s As String) s.Length)

Note: I can't really tell when an issue belong to the lang, or Roslyn. I thought that this error originated from VB compiler, not something related to the editor and Roslyn.

VBAndCs commented 3 years ago

C# doesn't have this issue. This works fine:

 var Length = from s in new string[]{"asdf"}
                      select s.Length;
CyrusNajmabadi commented 3 years ago

I thought that this error originated from VB compiler, not something related to the editor and Roslyn.

Roslyn is the VB compiler.

AdamSpeight2008 commented 3 years ago

Note the code works as expected in VS2017

VBAndCs commented 3 years ago

May be it is more recent. I never faced this before. This is another case involving method params:

   Sub Foo(x As Integer, y As Integer)

        ' No issue
        Dim r = From n In {1, 2, 3}
                Select New With {.x = 1, .y = 2}

        ' ' No issue
        r = From p In {New Point(10, 20)}
            Select New With {.x = p.X, .y = p.Y}

        ' Issues in p.X and p.Y
        r = From p In {New Point(10, 20)}
            Select p.X, p.Y
    End Sub

Obviously, it is related to implicit anonymous type defined in the select, and the workaround is to explicitly define the anonymous type.

VBAndCs commented 3 years ago

Note the code works as expected in VS2017

This means they missed a test for this case, and should create it with the fix.

AdamSpeight2008 commented 3 years ago

I think i've seen the cause, but I can't find it now. One of the lookup option / result options changed.

AdamSpeight2008 commented 3 years ago

@CyrusNajmabadi Issue seem to be related to forming the Select part of the query, as Let works find.

Youssef1313 commented 3 years ago

Same issue as https://github.com/dotnet/roslyn/issues/15779?

@tmat Based on your comment in the issue above, I think you saw a reason why the program was invalid (I couldn't see that reason). Can you have a look on whether that reason applies to the code in this issue?

AdamSpeight2008 commented 3 years ago

@Youssef1313 It is the same issue as #15779. the variable named b is a different b to one being qualified as a member access on pair.b. So as we understand it, there is no shadowing,

The reason the I can't find the possible issue, is that if I recall correctly the affected line (and it's a single line) that it is in a kind of unrelated change (according to the pull request). Pull Request is something relatively minor like spelling error, or variable name change.

AdamSpeight2008 commented 3 years ago

Think this was the pull request where the option changed.