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

Bind lambda in object initializer despite errors #75695

Merged
merged 8 commits into from
Nov 7, 2024

Conversation

jcouv
Copy link
Member

@jcouv jcouv commented Oct 31, 2024

Fixes #72571

The issue is that we get to BindAssignment but the LHS (produced in BindObjectInitializerMemberCommon) has the hasError flag set because the implicit receiver (a placeholder with the whole object creation as its syntax) has an error.
See hasErrors = boundMember.HasAnyErrors || implicitReceiver.HasAnyErrors; in BindObjectInitializerMemberCommon.

Note: I also tried the approach of attaching a narrower syntax node to the placeholder (commit 2), but I thought it's better to limit the impact of attached syntax on placeholders more generally.

@jcouv jcouv self-assigned this Oct 31, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged Issues and PRs which have not yet been triaged by a lead label Oct 31, 2024
@jcouv jcouv force-pushed the lambda-initializer branch from 6b79fe7 to e6796a3 Compare November 1, 2024 00:48
@jcouv jcouv marked this pull request as ready for review November 1, 2024 19:47
@jcouv jcouv requested a review from a team as a code owner November 1, 2024 19:47
@jcouv
Copy link
Member Author

jcouv commented Nov 5, 2024

@dotnet/roslyn-compiler for review. Thanks

{
return true;
}

// The syntax attached to placeholders should not limit usage of those placeholders
if (this is not BoundValuePlaceholderBase)
Copy link
Contributor

@AlekseyTs AlekseyTs Nov 5, 2024

Choose a reason for hiding this comment

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

if (this is not BoundValuePlaceholderBase

While I do not object to making this change, it feels like this is not a fix for the problem the PR is claiming to address. In order for SemanticModel to work properly, every syntax node that is normally bindable should be bound, regardless of errors, That means that the value returned by this helper shouldn't matter. The lambda should be bound even if this helper returns true. #Closed

Copy link
Contributor

Choose a reason for hiding this comment

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

We might suppress additional diagnostics based on what this helper returns, but we should not be suppressing the binding.

Copy link
Member Author

Choose a reason for hiding this comment

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

I can adjust the PR title if that's what you're suggesting. Like in another lambda-related PR last week, the question is whether we do BindToTypeForErrorRecovery (which results in poor lambda binding) or GenerateConversionForAssignment (which binds a conversion so results in better lambda information).
Maybe "Adjust lambda binding in object initializer despite errors"?

@AlekseyTs
Copy link
Contributor

Done with review pass (commit 4)

@jcouv jcouv marked this pull request as draft November 5, 2024 22:26
@@ -4180,10 +4180,10 @@ public static void Main()

var expectedDiagnostics = new[]
{
// file.cs(6,28): error CS8386: Invalid object creation
// (6,28): error CS8386: Invalid object creation
Copy link
Contributor

@AlekseyTs AlekseyTs Nov 6, 2024

Choose a reason for hiding this comment

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

// (6,28): error CS8386: Invalid object creation

It doesn't look like there are meaningful changes in this file. Consider reverting. #Closed

ILiteralOperation (OperationKind.Literal, Type: System.Int32, Constant: 0) (Syntax: '0')

Right:
IConversionOperation (TryCast: False, Unchecked) (OperationKind.Conversion, Type: System.Void, IsImplicit) (Syntax: '0')
Copy link
Contributor

@AlekseyTs AlekseyTs Nov 6, 2024

Choose a reason for hiding this comment

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

System.Void

Minor. Consider excluding void type as a meaningful target type for conversion. #Closed

Copy link
Member Author

Choose a reason for hiding this comment

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

It looks like we call GenerateConversionForAssignment with void in other scenarios too (VoidCall_ImplicitVoidConversion_DiscardAssignment which is a conditional operator, and CS0670ERR_FieldCantHaveVoidType01 which is a field initializer).
I'm leaning to leave as-is. But I'd also be okay to change and file an issue to align/tighten things up.

@@ -4082,25 +4082,22 @@ void M()
Statements (0)
Next (Regular) Block[B1]
Entering: {R1}

.locals {R1}
Copy link
Contributor

@AlekseyTs AlekseyTs Nov 6, 2024

Choose a reason for hiding this comment

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

It doesn't look like there are meaningful changes in this file. Consider reverting #Closed

Diagnostic(ErrorCode.ERR_SemicolonExpected, "").WithLocation(11, 34),
// (9,13): error CS1688: Cannot convert anonymous method block without a parameter list to delegate type 'OutParam' because it has one or more out parameters
// o = delegate // CS1688
Diagnostic(ErrorCode.ERR_CantConvAnonMethNoParams, "delegate").WithArguments("OutParam").WithLocation(9, 13));
Copy link
Contributor

@AlekseyTs AlekseyTs Nov 6, 2024

Choose a reason for hiding this comment

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

ERR_CantConvAnonMethNoParams

Let's make sure we have other tests covering this error. The name of the test seems to indicate that this error was the purpose for creating it. #Closed

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, we have a couple other tests that cover this error

@AlekseyTs
Copy link
Contributor

Done with review pass (commit 6)

@jcouv jcouv marked this pull request as ready for review November 6, 2024 16:56
@jcouv
Copy link
Member Author

jcouv commented Nov 6, 2024

@dotnet/roslyn-compiler for second review. Thanks

@jcouv jcouv requested a review from AlekseyTs November 6, 2024 21:44
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 8)

@jcouv jcouv merged commit 96b3071 into dotnet:main Nov 7, 2024
24 checks passed
@jcouv jcouv deleted the lambda-initializer branch November 7, 2024 03:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers 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.

IntelliSense loses the type of Action<T> parameter only while typing
4 participants