diff --git a/src/Compilers/VisualBasic/Portable/BoundTree/Statement.vb b/src/Compilers/VisualBasic/Portable/BoundTree/Statement.vb index 301f6dd1a1f1a11a8ec61f50a2723d0c9061b080..8f7a2ac2cd8ea08fb0adece939e2ee12bb80ea4e 100644 --- a/src/Compilers/VisualBasic/Portable/BoundTree/Statement.vb +++ b/src/Compilers/VisualBasic/Portable/BoundTree/Statement.vb @@ -1461,7 +1461,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic Return s_variablesMappings.GetValue( Me, Function(BoundUsing) - Dim usingStatementSyntax = Syntax.ChildNodes().OfType(Of UsingStatementSyntax).SingleOrDefault() + If (BoundUsing.ResourceList.IsDefault) Then + Return Nothing + End If + Dim usingStatementSyntax = DirectCast(Syntax.ChildNodes()(0), UsingStatementSyntax) Return New Variables(BoundUsing.ResourceList.As(Of IVariableDeclaration), usingStatementSyntax) End Function) End Get diff --git a/src/Compilers/VisualBasic/Test/Semantic/BasicCompilerSemanticTest.vbproj b/src/Compilers/VisualBasic/Test/Semantic/BasicCompilerSemanticTest.vbproj index c2ef6593119e53351bae019d68fa28194e43a74e..6ec72efa6966bc1224150d648bbbfb17312eaea8 100644 --- a/src/Compilers/VisualBasic/Test/Semantic/BasicCompilerSemanticTest.vbproj +++ b/src/Compilers/VisualBasic/Test/Semantic/BasicCompilerSemanticTest.vbproj @@ -128,6 +128,7 @@ + True diff --git a/src/Compilers/VisualBasic/Test/Semantic/IOperation/IOperationTests.vb b/src/Compilers/VisualBasic/Test/Semantic/IOperation/IOperationTests.vb index 8bcbc745ad024679de044c2e850be16f72e71a93..0f924bfc835f3b6f575c398fd52e309251252872 100644 --- a/src/Compilers/VisualBasic/Test/Semantic/IOperation/IOperationTests.vb +++ b/src/Compilers/VisualBasic/Test/Semantic/IOperation/IOperationTests.vb @@ -352,106 +352,5 @@ BC30581: 'AddressOf' expression cannot be converted to 'Integer' because 'Intege IObjectCreationExpression (Constructor: Sub System.Guid..ctor()) (OperationKind.ObjectCreationExpression, Type: System.Guid) (Syntax: 'New System.Guid()') IOperation: (OperationKind.None) (Syntax: 'AddressOf Main')") End Sub - - - - Public Sub UsingDeclarationSyntaxNotNull() - Dim source = - - - - - - Dim comp = CreateCompilationWithMscorlibAndVBRuntime(source, parseOptions:=TestOptions.RegularWithIOperationFeature) - Dim tree = comp.SyntaxTrees.Single() - Dim node = tree.GetRoot().DescendantNodes().OfType(Of UsingBlockSyntax).Single() - Dim op = DirectCast(comp.GetSemanticModel(tree).GetOperationInternal(node), IUsingStatement) - - Assert.NotNull(op.Declaration.Syntax) - Assert.Equal("Using D1 as New C1()", op.Declaration.Syntax.ToString()) - End Sub - - - - Public Sub UsingDeclarationIncompleteUsingNotNullSyntax() - Dim source = - - - - - - Dim comp = CreateCompilationWithMscorlibAndVBRuntime(source, parseOptions:=TestOptions.RegularWithIOperationFeature) - Dim tree = comp.SyntaxTrees.Single() - Dim node = tree.GetRoot().DescendantNodes().OfType(Of UsingBlockSyntax).Single() - Dim op = DirectCast(comp.GetSemanticModel(tree).GetOperationInternal(node), IUsingStatement) - - Assert.NotNull(op.Declaration.Syntax) - Assert.Equal("Using", op.Declaration.Syntax.ToString()) - End Sub - - - Public Sub UsingDeclarationExistingVariableNonNullSyntax() - ' TODO: This will need to be removed when https://github.com/dotnet/roslyn/issues/19887 is fixed. - ' The docs for IUsingStatement.Declaration state that it should be returning null when the - ' using statement does not declare any variables. However, it currently does not. When that is - ' fixed, this test should be updated to correctly reflect that assertion. - - Dim source = - - - - - - Dim comp = CreateCompilationWithMscorlibAndVBRuntime(source, parseOptions:=TestOptions.RegularWithIOperationFeature) - Dim tree = comp.SyntaxTrees.Single() - Dim node = tree.GetRoot().DescendantNodes().OfType(Of UsingBlockSyntax).Single() - Dim op = DirectCast(comp.GetSemanticModel(tree).GetOperationInternal(node), IUsingStatement) - - Assert.NotNull(op.Declaration.Syntax) - Assert.Equal("Using x", op.Declaration.Syntax.ToString()) - End Sub End Class End Namespace diff --git a/src/Compilers/VisualBasic/Test/Semantic/IOperation/IOperationTests_IUsingStatement.vb b/src/Compilers/VisualBasic/Test/Semantic/IOperation/IOperationTests_IUsingStatement.vb new file mode 100644 index 0000000000000000000000000000000000000000..484f2d856761ce4205ffcdfa17a60351309e7317 --- /dev/null +++ b/src/Compilers/VisualBasic/Test/Semantic/IOperation/IOperationTests_IUsingStatement.vb @@ -0,0 +1,115 @@ +Imports Microsoft.CodeAnalysis.Semantics +Imports Microsoft.CodeAnalysis.VisualBasic.Syntax +Imports Roslyn.Test.Utilities + +Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests.Semantics + Partial Public Class IOperationTests + Inherits SemanticModelTestBase + + + + Public Sub UsingDeclarationSyntaxNotNull() + Dim source = + + + + + + Dim comp = CreateCompilationWithMscorlibAndVBRuntime(source, parseOptions:=TestOptions.RegularWithIOperationFeature) + CompilationUtils.AssertNoDiagnostics(comp) + + Dim tree = comp.SyntaxTrees.Single() + Dim node = tree.GetRoot().DescendantNodes().OfType(Of UsingBlockSyntax).Single() + Dim op = DirectCast(comp.GetSemanticModel(tree).GetOperationInternal(node), IUsingStatement) + + Assert.NotNull(op.Declaration.Syntax) + Assert.Same(node.UsingStatement, op.Declaration.Syntax) + End Sub + + + + Public Sub UsingDeclarationIncompleteUsingNullDeclaration() + Dim source = + + + + + + Dim comp = CreateCompilationWithMscorlibAndVBRuntime(source, parseOptions:=TestOptions.RegularWithIOperationFeature) + CompilationUtils.AssertTheseDiagnostics(comp, + +BC30201: Expression expected. + Using + ~ + ) + + Dim tree = comp.SyntaxTrees.Single() + Dim node = tree.GetRoot().DescendantNodes().OfType(Of UsingBlockSyntax).Single() + Dim op = DirectCast(comp.GetSemanticModel(tree).GetOperationInternal(node), IUsingStatement) + + Assert.Null(op.Declaration) + End Sub + + + + Public Sub UsingDeclarationExistingVariableNullDeclaration() + Dim source = + + + + + + Dim comp = CreateCompilationWithMscorlibAndVBRuntime(source, parseOptions:=TestOptions.RegularWithIOperationFeature) + CompilationUtils.AssertNoDiagnostics(comp) + + Dim tree = comp.SyntaxTrees.Single() + Dim node = tree.GetRoot().DescendantNodes().OfType(Of UsingBlockSyntax).Single() + Dim op = DirectCast(comp.GetSemanticModel(tree).GetOperationInternal(node), IUsingStatement) + + Assert.Null(op.Declaration) + End Sub + End Class +End Namespace