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

VB Collection with ? gets an error #23928

Closed
paul1956 opened this issue Dec 25, 2017 · 5 comments
Closed

VB Collection with ? gets an error #23928

paul1956 opened this issue Dec 25, 2017 · 5 comments

Comments

@paul1956
Copy link
Contributor

paul1956 commented Dec 25, 2017

Version Used:
VS 2017 15.5.2
Steps to Reproduce:

                   For Each a In node.AccessorList?.Accessors
                       ' Anything
                   Next
  1. With the above code I would expect no errors
  2. The problem is with the ?. before Accessors

Expected Behavior:
No Error
Actual Behavior:
Error BC32023 Expression is of type 'SyntaxList(Of AccessorDeclarationSyntax)?', which is not a collection type

@jcouv
Copy link
Member

jcouv commented Dec 26, 2017

@AnthonyDGreen Can you confirm the expected behavior for this?

@paul1956
Copy link
Contributor Author

FYI: In C# it works as expected.
'''
foreach (var a in node.AccessorList?.Accessors) {
}

@KathleenDollard
Copy link

@paul1956

I tried to repo this with the code below. Can you see why this is different and/or let me know which version of VB and .NET you're having this issue with.

Public Class Class1

    Public Sub Foo()

        Dim B As B = Nothing
        Dim x = B.A?.Bar

        For Each y In B.A?.Bar

        Next
    End Sub
End Class

Public Class B
    Public A As A
End Class

Public Class A

    Public Property Bar As IEnumerable(Of String)
End Class

@svick
Copy link
Contributor

svick commented May 1, 2018

The question here is whether foreach works on a nullable struct enumerable (SyntaxList(Of AccessorDeclarationSyntax)? is such type).

In C#:

using System.Collections.Generic;

class C
{
    void M(StructList? list)
    {
        foreach (var x in list) { }
    }
}

struct StructList
{
    public IEnumerator<int> GetEnumerator() => null;
}

In VB:

Imports System.Collections.Generic

Class C
    Sub M(list As StructList?)
        For Each x In list
    	Next
    End Sub
End Class

Structure StructList
    Public Function GetEnumerator() As IEnumerator(Of Integer)
        Return Nothing
    End Function
End Structure

Indeed, the C# code compiles, while the VB code does not.

Interestingly, the C# spec says that this code shouldn't compile, but Roslyn deliberately violates the spec:

// If collectionExprType is a nullable type, then use the underlying type and take the value (i.e. .Value) of collectionExpr.
// This behavior is not spec'd, but it's what Dev10 does.

I didn't check the VB spec, but I assume the behavior of the VB compiler is correct. So I think there is nothing to do here, unless you'd like to propose that VB changes its behavior (which would belong to the vblang repo) or that the C# spec should be changed to accommodate how the compiler currently behaves (which would belong to the csharplang repo).

@jaredpar
Copy link
Member

Closing as the compiler is behaving as designed for VB here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants