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

Fix InvalidCastException when initializing the properties of an undeclared class inside a lambda function #72480

Merged
merged 3 commits into from
Mar 14, 2024

Conversation

andrei-ungureanu-uipath
Copy link
Contributor

@andrei-ungureanu-uipath andrei-ungureanu-uipath commented Mar 11, 2024

Fixes #72456.

Building the following snippet in a VB project breaks the compilation:

Imports System
Imports System.Linq.Expressions
Module Program
    Public Function CreateExpression() As Expression(Of Func(Of Object))
        Return Function() (New UndeclaredClass() With {.Name = "testName"})
    End Function
End Module

There is a difference between VisualBasic and CSharp when getting diagnostics for the snippet above. The latter checks for the initializer type while the former forces a DirectCast which throws an InvalidCastException. This PR checks the type before forcing the cast similar to the CSharp approach.

Expected result

Error BC30002: Type 'UndeclaredClass' is not defined.

Actual result

Compilation breaks with the following trace:

System.InvalidCastException
  HResult=0x80004002
  Message=Unable to cast object of type 'Microsoft.CodeAnalysis.VisualBasic.BoundLiteral' to type 'Microsoft.CodeAnalysis.VisualBasic.BoundAssignmentOperator'.

This exception was originally thrown at this call stack:
Microsoft.CodeAnalysis.VisualBasic.dll!Microsoft.CodeAnalysis.VisualBasic.DiagnosticsPass.VisitObjectInitializerExpression(Microsoft.CodeAnalysis.VisualBasic.BoundObjectInitializerExpression node) Line 126	Basic
Microsoft.CodeAnalysis.VisualBasic.dll!Microsoft.CodeAnalysis.VisualBasic.BoundObjectInitializerExpression.Accept(Microsoft.CodeAnalysis.VisualBasic.BoundTreeVisitor visitor) Line 3792	Basic
Microsoft.CodeAnalysis.VisualBasic.dll!Microsoft.CodeAnalysis.VisualBasic.BoundTreeWalkerWithStackGuard.VisitExpressionWithoutStackGuard(Microsoft.CodeAnalysis.VisualBasic.BoundExpression node) Line 61	Basic
Microsoft.CodeAnalysis.VisualBasic.dll!Microsoft.CodeAnalysis.VisualBasic.BoundTreeVisitor.VisitExpressionWithStackGuard(Integer recursionDepth, Microsoft.CodeAnalysis.VisualBasic.BoundExpression node) Line 186	Basic
Microsoft.CodeAnalysis.VisualBasic.dll!Microsoft.CodeAnalysis.VisualBasic.BoundTreeWalkerWithStackGuard.Visit(Microsoft.CodeAnalysis.VisualBasic.BoundNode node) Line 50	Basic
Microsoft.CodeAnalysis.VisualBasic.dll!Microsoft.CodeAnalysis.VisualBasic.DiagnosticsPass.Visit(Microsoft.CodeAnalysis.VisualBasic.BoundNode node) Line 208	Basic
...

@andrei-ungureanu-uipath andrei-ungureanu-uipath requested a review from a team as a code owner March 11, 2024 11:52
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Mar 11, 2024
@dotnet-policy-service dotnet-policy-service bot added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label Mar 11, 2024
@andrei-ungureanu-uipath
Copy link
Contributor Author

@dotnet-policy-service agree

@AlekseyTs
Copy link
Contributor

Done with review pass (commit 1)

@jaredpar jaredpar added this to the 17.10 milestone Mar 12, 2024
Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (commit 2)

@AlekseyTs
Copy link
Contributor

@dotnet/roslyn-compiler For the second review

@AlekseyTs AlekseyTs merged commit 224f1a3 into dotnet:main Mar 14, 2024
24 checks passed
@dotnet-policy-service dotnet-policy-service bot modified the milestones: 17.10, Next Mar 14, 2024
@AlekseyTs
Copy link
Contributor

@andrei-ungureanu-uipath Thank you for the contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Community The pull request was submitted by a contributor who is not a Microsoft employee. untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

VisitObjectInitializerExpression throws InvalidCastException on invalid VisualBasic expression
5 participants