From 9a09a7075983765f986658294c52d90e71b9f887 Mon Sep 17 00:00:00 2001 From: Heejae Chang Date: Fri, 13 Oct 2017 16:51:46 -0700 Subject: [PATCH] remove unnecessary block from bound node for single line lambda (#22571) --- .../Portable/Binding/Binder_Lambda.vb | 10 +- .../Semantic/IOperation/IOperationTests.vb | 137 ++++++ ...rationTests_IDelegateCreationExpression.vb | 423 ++++++++---------- 3 files changed, 337 insertions(+), 233 deletions(-) diff --git a/src/Compilers/VisualBasic/Portable/Binding/Binder_Lambda.vb b/src/Compilers/VisualBasic/Portable/Binding/Binder_Lambda.vb index 05719842d7382..8e1ea7292e62a 100644 --- a/src/Compilers/VisualBasic/Portable/Binding/Binder_Lambda.vb +++ b/src/Compilers/VisualBasic/Portable/Binding/Binder_Lambda.vb @@ -478,8 +478,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic Dim singleLineLambdaSyntax = DirectCast(lambdaSyntax, SingleLineLambdaExpressionSyntax) Dim statement = DirectCast(singleLineLambdaSyntax.Body, StatementSyntax) - Dim boundStatement As BoundStatement - If statement.Kind = SyntaxKind.LocalDeclarationStatement Then ' A local declaration is not allowed in a single line lambda as the top level statement. Report the error here because it is legal ' to have a single line if which contains a local declaration. If the error reporting is done in BindStatement then all local @@ -487,19 +485,15 @@ Namespace Microsoft.CodeAnalysis.VisualBasic ' Bind local declaration, discard diagnostics Dim ignoredDiagnostics = DiagnosticBag.GetInstance() - boundStatement = bodyBinder.BindBlock(lambdaSyntax, singleLineLambdaSyntax.Statements, ignoredDiagnostics).MakeCompilerGenerated() + block = bodyBinder.BindBlock(lambdaSyntax, singleLineLambdaSyntax.Statements, ignoredDiagnostics).MakeCompilerGenerated() ignoredDiagnostics.Free() ' Generate a diagnostic and a bad statement node ReportDiagnostic(diagnostics, statement, ERRID.ERR_SubDisallowsStatement) - boundStatement = New BoundBadStatement(statement, ImmutableArray.Create(Of BoundNode)(boundStatement), hasErrors:=True) Else - boundStatement = bodyBinder.BindBlock(lambdaSyntax, singleLineLambdaSyntax.Statements, diagnostics).MakeCompilerGenerated() + block = bodyBinder.BindBlock(lambdaSyntax, singleLineLambdaSyntax.Statements, diagnostics).MakeCompilerGenerated() End If - block = New BoundBlock(lambdaSyntax, Nothing, ImmutableArray(Of LocalSymbol).Empty, - ImmutableArray.Create(boundStatement), boundStatement.HasErrors).MakeCompilerGenerated() - Case SyntaxKind.MultiLineFunctionLambdaExpression, SyntaxKind.MultiLineSubLambdaExpression diff --git a/src/Compilers/VisualBasic/Test/Semantic/IOperation/IOperationTests.vb b/src/Compilers/VisualBasic/Test/Semantic/IOperation/IOperationTests.vb index 5e677ee8288f4..02e008a5205b0 100644 --- a/src/Compilers/VisualBasic/Test/Semantic/IOperation/IOperationTests.vb +++ b/src/Compilers/VisualBasic/Test/Semantic/IOperation/IOperationTests.vb @@ -701,5 +701,142 @@ ICatchClause (Exception type: System.Exception) (OperationKind.CatchClause) (Syn VerifyOperationTreeAndDiagnosticsForTest(Of CatchBlockSyntax)(source, expectedOperationTree, expectedDiagnostics) End Sub + + + + Public Sub TestSubSingleLineLambda() + Dim source = .Value + + Dim expectedOperationTree = .Value + + Dim expectedDiagnostics = String.Empty + + VerifyOperationTreeAndDiagnosticsForTest(Of SingleLineLambdaExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) + End Sub + + + + Public Sub TestFunctionSingleLineLambda() + Dim source = .Value + + Dim expectedOperationTree = As System.Int32 + IReturnStatement (OperationKind.ReturnStatement, IsImplicit) (Syntax: '1') + ReturnedValue: + ILiteralExpression (OperationKind.LiteralExpression, Type: System.Int32, Constant: 1) (Syntax: '1') + ILabeledStatement (Label: exit) (OperationKind.LabeledStatement, IsImplicit) (Syntax: 'Function() 1') + Statement: + null + IReturnStatement (OperationKind.ReturnStatement, IsImplicit) (Syntax: 'Function() 1') + ReturnedValue: + ILocalReferenceExpression: (OperationKind.LocalReferenceExpression, Type: System.Int32, IsImplicit) (Syntax: 'Function() 1') +]]>.Value + + Dim expectedDiagnostics = String.Empty + + VerifyOperationTreeAndDiagnosticsForTest(Of SingleLineLambdaExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) + End Sub + + + + Public Sub TestFunctionMultiLineLambda() + Dim source = .Value + + Dim expectedOperationTree = As System.Int32 + IReturnStatement (OperationKind.ReturnStatement) (Syntax: 'Return 1') + ReturnedValue: + ILiteralExpression (OperationKind.LiteralExpression, Type: System.Int32, Constant: 1) (Syntax: '1') + ILabeledStatement (Label: exit) (OperationKind.LabeledStatement) (Syntax: 'End Function') + Statement: + null + IReturnStatement (OperationKind.ReturnStatement) (Syntax: 'End Function') + ReturnedValue: + ILocalReferenceExpression: (OperationKind.LocalReferenceExpression, Type: System.Int32) (Syntax: 'End Function') +]]>.Value + + Dim expectedDiagnostics = String.Empty + + VerifyOperationTreeAndDiagnosticsForTest(Of MultiLineLambdaExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) + End Sub + + + + Public Sub TestSubMultiLineLambda() + Dim source = .Value + + Dim expectedOperationTree = .Value + + Dim expectedDiagnostics = String.Empty + + VerifyOperationTreeAndDiagnosticsForTest(Of MultiLineLambdaExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics) + End Sub End Class End Namespace diff --git a/src/Compilers/VisualBasic/Test/Semantic/IOperation/IOperationTests_IDelegateCreationExpression.vb b/src/Compilers/VisualBasic/Test/Semantic/IOperation/IOperationTests_IDelegateCreationExpression.vb index 3255ec47260b8..ba0b250f55fa8 100644 --- a/src/Compilers/VisualBasic/Test/Semantic/IOperation/IOperationTests_IDelegateCreationExpression.vb +++ b/src/Compilers/VisualBasic/Test/Semantic/IOperation/IOperationTests_IDelegateCreationExpression.vb @@ -33,17 +33,16 @@ IVariableDeclarationStatement (1 declarations) (OperationKind.VariableDeclaratio Target: IAnonymousFunctionExpression (Symbol: Sub ()) (OperationKind.AnonymousFunctionExpression, Type: null) (Syntax: 'Sub() Conso ... iteLine("")') IBlockStatement (3 statements) (OperationKind.BlockStatement, IsImplicit) (Syntax: 'Sub() Conso ... iteLine("")') - IBlockStatement (1 statements) (OperationKind.BlockStatement, IsImplicit) (Syntax: 'Sub() Conso ... iteLine("")') - IExpressionStatement (OperationKind.ExpressionStatement) (Syntax: 'Console.WriteLine("")') - Expression: - IInvocationExpression (Sub System.Console.WriteLine(value As System.String)) (OperationKind.InvocationExpression, Type: System.Void) (Syntax: 'Console.WriteLine("")') - Instance Receiver: - null - Arguments(1): - IArgument (ArgumentKind.Explicit, Matching Parameter: value) (OperationKind.Argument) (Syntax: '""') - ILiteralExpression (OperationKind.LiteralExpression, Type: System.String, Constant: "") (Syntax: '""') - InConversion: CommonConversion (Exists: True, IsIdentity: True, IsNumeric: False, IsReference: False, IsUserDefined: False) (MethodSymbol: null) - OutConversion: CommonConversion (Exists: True, IsIdentity: True, IsNumeric: False, IsReference: False, IsUserDefined: False) (MethodSymbol: null) + IExpressionStatement (OperationKind.ExpressionStatement) (Syntax: 'Console.WriteLine("")') + Expression: + IInvocationExpression (Sub System.Console.WriteLine(value As System.String)) (OperationKind.InvocationExpression, Type: System.Void) (Syntax: 'Console.WriteLine("")') + Instance Receiver: + null + Arguments(1): + IArgument (ArgumentKind.Explicit, Matching Parameter: value) (OperationKind.Argument) (Syntax: '""') + ILiteralExpression (OperationKind.LiteralExpression, Type: System.String, Constant: "") (Syntax: '""') + InConversion: CommonConversion (Exists: True, IsIdentity: True, IsNumeric: False, IsReference: False, IsUserDefined: False) (MethodSymbol: null) + OutConversion: CommonConversion (Exists: True, IsIdentity: True, IsNumeric: False, IsReference: False, IsUserDefined: False) (MethodSymbol: null) ILabeledStatement (Label: exit) (OperationKind.LabeledStatement, IsImplicit) (Syntax: 'Sub() Conso ... iteLine("")') Statement: null @@ -72,17 +71,16 @@ End Module]]>.Value Dim expectedOperationTree =