runtime icon indicating copy to clipboard operation
runtime copied to clipboard

LibraryImportGenerator, .NET 7: possible error in the 'Equals' method implementation

Open VasilievSerg opened this issue 4 years ago • 11 comments

Description

Code:

public bool Equals(IncrementalStubGenerationContext? other)
{
  return other is not null
               && StubEnvironment.AreCompilationSettingsEqual(Environment, other.Environment)
               && SignatureContext.Equals(other.SignatureContext)
               && ContainingSyntaxContext.Equals(other.ContainingSyntaxContext)
               && StubMethodSyntaxTemplate.Equals(other.StubMethodSyntaxTemplate)
               && LibraryImportData.Equals(other.LibraryImportData)
               && DiagnosticLocation.Equals(DiagnosticLocation)               // <=
               && ForwardedAttributes.SequenceEqual(other.ForwardedAttributes, (IEqualityComparer<AttributeSyntax>)SyntaxEquivalentComparer.Instance)
               && GeneratorFactoryKey.Equals(other.GeneratorFactoryKey)
               && Diagnostics.SequenceEqual(other.Diagnostics);
            }

Link to the sources.

Looks suspicious that DiagnosticLocation compares to itself.

Reproduction Steps

Expected behavior

Actual behavior

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

VasilievSerg avatar Nov 10 '22 06:11 VasilievSerg

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

ghost avatar Nov 10 '22 06:11 ghost

Tagging subscribers to this area: @dotnet/interop-contrib See info in area-owners.md if you want to be subscribed.

Issue Details

Description

Code:

public bool Equals(IncrementalStubGenerationContext? other)
{
  return other is not null
               && StubEnvironment.AreCompilationSettingsEqual(Environment, other.Environment)
               && SignatureContext.Equals(other.SignatureContext)
               && ContainingSyntaxContext.Equals(other.ContainingSyntaxContext)
               && StubMethodSyntaxTemplate.Equals(other.StubMethodSyntaxTemplate)
               && LibraryImportData.Equals(other.LibraryImportData)
               && DiagnosticLocation.Equals(DiagnosticLocation)               // <=
               && ForwardedAttributes.SequenceEqual(other.ForwardedAttributes, (IEqualityComparer<AttributeSyntax>)SyntaxEquivalentComparer.Instance)
               && GeneratorFactoryKey.Equals(other.GeneratorFactoryKey)
               && Diagnostics.SequenceEqual(other.Diagnostics);
            }

Link to the sources.

Looks suspicious that DiagnosticLocation compares to itself.

Reproduction Steps

Expected behavior

Actual behavior

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Author: VasilievSerg
Assignees: -
Labels:

area-System.Runtime.InteropServices, untriaged

Milestone: -

ghost avatar Nov 10 '22 09:11 ghost

@VasilievSerg Agreed, this does look odd. Care to submit a PR for adding the other.?

@jkoritzinsky Are we missing a test for this or is this one of those checks that doesn't matter often enough in practice?

AaronRobinsonMSFT avatar Nov 10 '22 15:11 AaronRobinsonMSFT

I fixed this in .NET 8 and found that we did have a test validating the behavior. Unfortunately, the test is wrong in .NET 7 and didn't catch it.

The fix and the updated test were part of https://github.com/dotnet/runtime/pull/76474/files#diff-e5ffd09434c9daa4ab5234b83ae383b344c4c20d15a93216b93d358226a10586

In particular, the updates to the AppendingUnrelatedSource_DoesNotRegenerateSource test capture the result of fixing this bug.

jkoritzinsky avatar Nov 10 '22 19:11 jkoritzinsky

Updating the milestone to .NET 7.0.X as this was already fixed in .NET 8.

jkoritzinsky avatar Nov 10 '22 19:11 jkoritzinsky

I found two more similar places:

  • JSImportGenerator.cs (link);
  • JSExportGenerator.cs (link).

VasilievSerg avatar Nov 14 '22 13:11 VasilievSerg

The fix for those was also included in #76474 for .NET 8. It might be worthwhile backporting these fixes to .NET 7 (or all of #76474, which I mentioned in that PR).

jkoritzinsky avatar Nov 14 '22 19:11 jkoritzinsky

@jkoritzinsky What is the fallout for this? How are customers impacted by this bug?

AaronRobinsonMSFT avatar Nov 14 '22 20:11 AaronRobinsonMSFT

Diagnostics from the source generator would be reported in the wrong location if the user changes the file above the LibraryImport. This can affect both the IDE experience and an inner-loop command line build experience as the compiler server also has the incremental generator support enabled.

jkoritzinsky avatar Nov 14 '22 20:11 jkoritzinsky

I see. The referenced PR isn't too big. We can try porting this now or wait until we hear from people. Since it doesn't impact the actual generated code, I don't have a strong opinion either way.

AaronRobinsonMSFT avatar Nov 14 '22 20:11 AaronRobinsonMSFT

This doesn't see to be actionable any longer. Closing.

AaronRobinsonMSFT avatar May 14 '24 15:05 AaronRobinsonMSFT