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
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/Compilers/CSharp/Portable/BoundTree/BoundNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,20 @@ public bool HasAnyErrors
{
get
{
// NOTE: check Syntax rather than WasCompilerGenerated because sequence points can have null syntax.
if (this.HasErrors || this.Syntax != null && this.Syntax.HasErrors)
if (this.HasErrors)
{
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"?

{
// NOTE: check Syntax rather than WasCompilerGenerated because sequence points can have null syntax.
if (this.Syntax != null && this.Syntax.HasErrors)
{
return true;
}
}
var expression = this as BoundExpression;
return expression?.Type?.IsErrorType() == true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using Roslyn.Test.Utilities;
using Xunit;
using Basic.Reference.Assemblies;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Linq;

namespace Microsoft.CodeAnalysis.CSharp.UnitTests.CodeGen
{
Expand Down Expand Up @@ -1815,6 +1817,43 @@ static string P(S? s)
S");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/72571")]
public void LambdaWithBindingErrorInInitializerOfTargetTypedNew()
{
var src = """
AddConfig(new()
{
A = a =>
{
a // 1
},
});

static void AddConfig(Config config) { }

class Config
{
public System.Action<A> A { get; set; }
}

class A
{
public string Property { get; set; } = "";
}
""";
var comp = CreateCompilation(src);
comp.VerifyEmitDiagnostics(
// (5,10): error CS1002: ; expected
// a // 1
Diagnostic(ErrorCode.ERR_SemicolonExpected, "").WithLocation(5, 10));

var tree = comp.SyntaxTrees.Single();
var model = comp.GetSemanticModel(tree);
var s = GetSyntax<IdentifierNameSyntax>(tree, "a");
Assert.Equal("A a", model.GetSymbolInfo(s).Symbol.ToTestDisplayString());
Assert.Equal(new string[] { }, model.GetSymbolInfo(s).CandidateSymbols.ToTestDisplayStrings());
}

#region Regression Tests

[WorkItem(544159, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/544159")]
Expand Down
20 changes: 10 additions & 10 deletions src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenRefLocalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4168,24 +4168,24 @@ public static void Main()
string expectedOperationTree = @"
IInvalidOperation (OperationKind.Invalid, Type: ?, IsInvalid) (Syntax: 'new ref[] { 1 }')
Children(1):
IObjectOrCollectionInitializerOperation (OperationKind.ObjectOrCollectionInitializer, Type: ?[]) (Syntax: '{ 1 }')
IObjectOrCollectionInitializerOperation (OperationKind.ObjectOrCollectionInitializer, Type: ?[], IsInvalid) (Syntax: '{ 1 }')
Initializers(1):
IInvalidOperation (OperationKind.Invalid, Type: ?, IsImplicit) (Syntax: '1')
Children(2):
IOperation: (OperationKind.None, Type: null, IsImplicit) (Syntax: '1')
Children(1):
IInstanceReferenceOperation (ReferenceKind: ImplicitReceiver) (OperationKind.InstanceReference, Type: ?[], IsInvalid, IsImplicit) (Syntax: 'ref[]')
ILiteralOperation (OperationKind.Literal, Type: System.Int32, Constant: 1) (Syntax: '1')
IInvalidOperation (OperationKind.Invalid, Type: ?, IsInvalid, IsImplicit) (Syntax: '1')
Children(1):
ILiteralOperation (OperationKind.Literal, Type: System.Int32, Constant: 1, IsInvalid) (Syntax: '1')
";

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

// _ = /*<bind>*/ new ref[] { 1 } /*</bind>*/ ;
Diagnostic(ErrorCode.ERR_InvalidObjectCreation, "ref[]").WithLocation(6, 28),
// file.cs(6,31): error CS1031: Type expected
// (6,31): error CS1031: Type expected
// _ = /*<bind>*/ new ref[] { 1 } /*</bind>*/ ;
Diagnostic(ErrorCode.ERR_TypeExpected, "[").WithLocation(6, 31)
Diagnostic(ErrorCode.ERR_TypeExpected, "[").WithLocation(6, 31),
// (6,36): error CS1061: '?[]' does not contain a definition for 'Add' and no accessible extension method 'Add' accepting a first argument of type '?[]' could be found (are you missing a using directive or an assembly reference?)
// _ = /*<bind>*/ new ref[] { 1 } /*</bind>*/ ;
Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "1").WithArguments("?[]", "Add").WithLocation(6, 36)
};

VerifyOperationTreeAndDiagnosticsForTest<ObjectCreationExpressionSyntax>(text, expectedOperationTree, expectedDiagnostics);
Expand Down
Loading