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 regression of LambdaSymbol.GetHashCode #58247

Merged
merged 9 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 8 additions & 13 deletions src/Compilers/CSharp/Portable/Symbols/Source/LambdaSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal sealed class LambdaSymbol : SourceMethodSymbolWithAttributes
private readonly Binder _binder;
private readonly Symbol _containingSymbol;
private readonly MessageID _messageID;
private readonly SyntaxNode _syntax;
private readonly ImmutableArray<ParameterSymbol> _parameters;
private RefKind _refKind;
private TypeWithAnnotations _returnType;
Expand Down Expand Up @@ -49,6 +50,7 @@ public LambdaSymbol(
_binder = binder;
_containingSymbol = containingSymbol;
_messageID = unboundLambda.Data.MessageID;
_syntax = unboundLambda.Syntax;
if (!unboundLambda.HasExplicitReturnType(out _refKind, out _returnType))
{
_refKind = refKind;
Expand Down Expand Up @@ -217,7 +219,7 @@ public override ImmutableArray<Location> Locations
{
get
{
return ImmutableArray.Create<Location>(Syntax.Location);
return ImmutableArray.Create<Location>(_syntax.Location);
}
}

Expand All @@ -229,7 +231,7 @@ internal Location DiagnosticLocation
{
get
{
return Syntax switch
return _syntax switch
{
AnonymousMethodExpressionSyntax syntax => syntax.DelegateKeyword.GetLocation(),
LambdaExpressionSyntax syntax => syntax.ArrowToken.GetLocation(),
Expand All @@ -238,7 +240,7 @@ internal Location DiagnosticLocation
}
}

private bool HasExplicitReturnType => Syntax is ParenthesizedLambdaExpressionSyntax { ReturnType: not null };
private bool HasExplicitReturnType => _syntax is ParenthesizedLambdaExpressionSyntax { ReturnType: not null };

public override ImmutableArray<SyntaxReference> DeclaringSyntaxReferences
{
Expand All @@ -263,15 +265,13 @@ public override bool IsExtensionMethod
get { return false; }
}

internal SyntaxNode Syntax => syntaxReferenceOpt.GetSyntax();

internal override Binder SignatureBinder => _binder;

internal override Binder ParameterBinder => new WithLambdaParametersBinder(this, _binder);

internal override OneOrMany<SyntaxList<AttributeListSyntax>> GetAttributeDeclarations()
{
return Syntax is LambdaExpressionSyntax lambdaSyntax ?
return _syntax is LambdaExpressionSyntax lambdaSyntax ?
OneOrMany.Create(lambdaSyntax.AttributeLists) :
default;
}
Expand Down Expand Up @@ -369,22 +369,17 @@ public sealed override bool Equals(Symbol symbol, TypeCompareKind compareKind)
if ((object)this == symbol) return true;

return symbol is LambdaSymbol lambda
&& areEqual(lambda.syntaxReferenceOpt, syntaxReferenceOpt)
&& lambda._syntax == _syntax
&& lambda._refKind == _refKind
&& TypeSymbol.Equals(lambda.ReturnType, this.ReturnType, compareKind)
&& ParameterTypesWithAnnotations.SequenceEqual(lambda.ParameterTypesWithAnnotations, compareKind,
(p1, p2, compareKind) => p1.Equals(p2, compareKind))
&& lambda.ContainingSymbol.Equals(ContainingSymbol, compareKind);

static bool areEqual(SyntaxReference a, SyntaxReference b)
{
return (object)a.SyntaxTree == b.SyntaxTree && a.Span == b.Span;
}
}

public override int GetHashCode()
{
return syntaxReferenceOpt.GetHashCode();
return _syntax.GetHashCode();
}

public override bool IsImplicitlyDeclared
Expand Down
58 changes: 58 additions & 0 deletions src/Compilers/CSharp/Test/Symbol/Symbols/SymbolEqualityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

#nullable disable

using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.Operations;
using Roslyn.Test.Utilities;
using Xunit;

Expand Down Expand Up @@ -911,6 +913,62 @@ public static void M(A<T> t)
);
}

[Fact]
[WorkItem(58226, "https://github.com/dotnet/roslyn/issues/58226")]
public void LambdaSymbol()
{
var source =
@"
#nullable enable

using System;
using System.Linq;

M1(args => string.Join("" "", args.Select(a => a!.ToString())));
void M1<TResult>(Func<object?[], TResult>? f) { }
";
var comp = CreateCompilation(source);
comp.VerifyDiagnostics();

var syntaxTree = comp.SyntaxTrees[0];
var root = syntaxTree.GetRoot();

var lambdaSyntax = root.DescendantNodes().OfType<SimpleLambdaExpressionSyntax>().First();
var semanticModel1 = comp.GetSemanticModel(syntaxTree);
var semanticModel2 = comp.GetSemanticModel(syntaxTree);

var lambdaSymbol = (IMethodSymbol)semanticModel1.GetSymbolInfo(lambdaSyntax).Symbol;
var p1 = lambdaSymbol.Parameters.Single();

var p2 = semanticModel2.GetDeclaredSymbol(lambdaSyntax.Parameter);
VerifyEquality(p1, p2, expectedIncludeNullability: true);
}

[Fact]
[WorkItem(58226, "https://github.com/dotnet/roslyn/issues/58226")]
public void LambdaSymbol_02()
{
var source =
@"class Program
{
static void Main()
{
var q = from i in new int[] { 4, 5 } where /*pos*/
}
}";
var comp = CreateCompilation(source);
var syntaxTree = comp.SyntaxTrees[0];
var model = comp.GetSemanticModel(syntaxTree);
var syntaxNode = syntaxTree.GetRoot().DescendantNodes().
OfType<QueryExpressionSyntax>().Single();
var operation = model.GetOperation(syntaxNode);
var lambdas = operation.Descendants().OfType<AnonymousFunctionOperation>().
Select(op => op.Symbol.GetSymbol<LambdaSymbol>()).ToImmutableArray();
Assert.Equal(2, lambdas.Length);
Assert.Equal(lambdas[0].SyntaxRef.Span, lambdas[1].SyntaxRef.Span);
Assert.NotEqual(lambdas[0], lambdas[1]);
}
Youssef1313 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

@Youssef1313, it looks like a couple of namespaces are missing:

using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Operations;

Copy link
Member Author

Choose a reason for hiding this comment

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

That's what happens when I edit via GitHub directly 😄


private void VerifyEquality(ISymbol symbol1, ISymbol symbol2, bool expectedIncludeNullability)
{
// Symbol.Equals
Expand Down