Skip to content

Commit

Permalink
apicompat: treat IntPtr as nint and UIntPtr as nuint (#27601)
Browse files Browse the repository at this point in the history
Previously, ApiCompat would consider `IntPtr` and `nint` (as well as `UIntPtr` and `nuint`) as different types, emitting diagnostics where there shouldn't have been. This change updates the display string used by the SymbolComparer to replace all instances of `IntPtr` with `nint` and `UIntPtr` with `nuint`.

Fixes #25566
  • Loading branch information
smasher164 authored Aug 31, 2022
1 parent a05f394 commit f19c3d7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ private static SymbolDisplayFormat GetSymbolDisplayFormat()
return format.WithParameterOptions(format.ParameterOptions & ~SymbolDisplayParameterOptions.IncludeParamsRefOut);
}

internal static string ToComparisonDisplayString(this ISymbol symbol) => symbol.ToDisplayString(Format);
internal static string ToComparisonDisplayString(this ISymbol symbol) =>
symbol.ToDisplayString(Format)
.Replace("System.IntPtr", "nint") // Treat IntPtr and nint as the same
.Replace("System.UIntPtr", "nuint"); // Treat UIntPtr and nuint as the same

internal static IEnumerable<ITypeSymbol> GetAllBaseTypes(this ITypeSymbol type)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,5 +466,39 @@ private First() { }
};
Assert.Equal(expected, differences);
}

[Fact]
public void NumericPtrNotFlagged()
{
string leftSyntax = @"
namespace CompatTests
{
using System;
public class First
{
public void F(IntPtr p) {}
public void G(UIntPtr p) {}
}
}
";
string rightSyntax = @"
namespace CompatTests
{
public class First
{
public void F(nint p) {}
public void G(nuint p) {}
}
}
";
IAssemblySymbol left = SymbolFactory.GetAssemblyFromSyntax(leftSyntax);
IAssemblySymbol right = SymbolFactory.GetAssemblyFromSyntax(rightSyntax);
ApiComparer differ = new(s_ruleFactory);

IEnumerable<CompatDifference> differences = differ.GetDifferences(left, right);

Assert.Empty(differences);
}
}
}

0 comments on commit f19c3d7

Please sign in to comment.