Skip to content

Commit

Permalink
Bind lambda in initializer despite errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouv committed Nov 1, 2024
1 parent 6200694 commit e6796a3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
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)
{
// 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
39 changes: 39 additions & 0 deletions src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenExprLambdaTests.cs
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

0 comments on commit e6796a3

Please sign in to comment.