Skip to content

Commit

Permalink
Merge pull request #943 from microsoft/fix934
Browse files Browse the repository at this point in the history
Remove System.Drawing type references when the assembly isn't referenced
  • Loading branch information
AArnott authored May 24, 2023
2 parents 435c077 + f234e80 commit 73ae8da
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public Generator(string metadataLibraryPath, Docs? docs, GeneratorOptions option
AddSymbolIf(this.canCallCreateSpan, "canCallCreateSpan");
AddSymbolIf(this.canUseUnsafeAsRef, "canUseUnsafeAsRef");
AddSymbolIf(this.canUseUnsafeNullRef, "canUseUnsafeNullRef");
AddSymbolIf(compilation?.GetTypeByMetadataName("System.Drawing.Point") is not null, "canUseSystemDrawing");

if (extraSymbols.Count > 0)
{
Expand Down
4 changes: 4 additions & 0 deletions src/Microsoft.Windows.CsWin32/templates/RECT.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
partial struct RECT
{
#if canUseSystemDrawing
internal RECT(global::System.Drawing.Rectangle value) :
this(value.Left, value.Top, value.Right, value.Bottom) { }
internal RECT(global::System.Drawing.Point location, global::System.Drawing.Size size) :
this(location.X, location.Y, unchecked(location.X + size.Width), unchecked(location.Y + size.Height)) { }
#endif
internal RECT(int left, int top, int right, int bottom)
{
this.left = left;
Expand All @@ -19,8 +21,10 @@ internal static RECT FromXYWH(int x, int y, int width, int height) =>
internal readonly bool IsEmpty => this.left == 0 && this.top == 0 && this.right == 0 && this.bottom == 0;
internal readonly int X => this.left;
internal readonly int Y => this.top;
#if canUseSystemDrawing
internal readonly global::System.Drawing.Size Size => new global::System.Drawing.Size(this.Width, this.Height);
public static implicit operator global::System.Drawing.Rectangle(RECT value) => new global::System.Drawing.Rectangle(value.left, value.top, value.Width, value.Height);
public static implicit operator global::System.Drawing.RectangleF(RECT value) => new global::System.Drawing.RectangleF(value.left, value.top, value.Width, value.Height);
public static implicit operator RECT(global::System.Drawing.Rectangle value) => new RECT(value);
#endif
}
4 changes: 4 additions & 0 deletions src/Microsoft.Windows.CsWin32/templates/SIZE.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
partial struct SIZE
{
#if canUseSystemDrawing
internal SIZE(global::System.Drawing.Size value) : this(value.Width, value.Height) { }
#endif
internal SIZE(int width, int height)
{
this.cx = width;
Expand All @@ -10,6 +12,8 @@ internal SIZE(int width, int height)
internal readonly int Width => this.cx;
internal readonly int Height => this.cy;
internal readonly bool IsEmpty => this.cx == 0 && this.cy == 0;
#if canUseSystemDrawing
public static implicit operator global::System.Drawing.Size(SIZE value) => new global::System.Drawing.Size(value.cx, value.cy);
public static implicit operator SIZE(global::System.Drawing.Size value) => new SIZE(value);
#endif
}
18 changes: 18 additions & 0 deletions test/Microsoft.Windows.CsWin32.Tests/StructTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ public void PointReferencingStruct(string tfm)
this.AssertNoDiagnostics();
}

[Theory, CombinatorialData]
public void RECT_IncludesSystemDrawingWhenReferenced(bool referenceSystemDrawing)
{
this.compilation = this.starterCompilations["net472"];
if (!referenceSystemDrawing)
{
this.compilation = this.compilation.RemoveReferences(this.compilation.References.Where(r => r.Display?.EndsWith("System.Drawing.dll", StringComparison.OrdinalIgnoreCase) is true));
}

this.generator = this.CreateGenerator();
Assert.True(this.generator.TryGenerate("RECT", CancellationToken.None));
this.CollectGeneratedCode(this.generator);
this.AssertNoDiagnostics();

StructDeclarationSyntax rectStruct = (StructDeclarationSyntax)Assert.Single(this.FindGeneratedType("RECT"));
Assert.Equal(referenceSystemDrawing, rectStruct.Members.OfType<ConstructorDeclarationSyntax>().Any(ctor => ctor.ParameterList.Parameters.Any(p => p.Type?.ToString().Contains("System.Drawing.Rectangle") is true)));
}

[Fact]
public void CollidingStructNotGenerated()
{
Expand Down

0 comments on commit 73ae8da

Please sign in to comment.