Skip to content

Commit

Permalink
Merge pull request #1288 from microsoft/fix1205
Browse files Browse the repository at this point in the history
Avoid using `UnscopedRefAttribute` on downlevel *compilers*
  • Loading branch information
AArnott authored Sep 27, 2024
2 parents 7327da2 + d679d95 commit 748f605
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Microsoft.Windows.CsWin32/Generator.Features.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Microsoft.Windows.CsWin32;

public partial class Generator
{
private readonly bool canUseUnscopedRef;
private readonly bool canUseSpan;
private readonly bool canCallCreateSpan;
private readonly bool canUseUnsafeAsRef;
Expand Down
6 changes: 6 additions & 0 deletions src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public Generator(string metadataLibraryPath, Docs? docs, GeneratorOptions option
this.parseOptions = parseOptions;
this.volatileCode = new(this.committedCode);

// UnscopedRefAttribute may be emitted to work on downlevel *runtimes*, but we can't use it
// on downlevel *compilers*. Only .NET 8+ SDK compilers support it. Since we cannot detect
// compiler version, we use language version instead.
this.canUseUnscopedRef = this.parseOptions?.LanguageVersion >= (LanguageVersion)1100; // C# 11.0

this.canUseSpan = this.compilation?.GetTypeByMetadataName(typeof(Span<>).FullName) is not null;
this.canCallCreateSpan = this.compilation?.GetTypeByMetadataName(typeof(MemoryMarshal).FullName)?.GetMembers("CreateSpan").Any() is true;
this.canUseUnsafeAsRef = this.compilation?.GetTypeByMetadataName(typeof(Unsafe).FullName)?.GetMembers("Add").Any() is true;
Expand Down Expand Up @@ -117,6 +122,7 @@ public Generator(string metadataLibraryPath, Docs? docs, GeneratorOptions option
AddSymbolIf(this.canUseUnsafeNullRef, "canUseUnsafeNullRef");
AddSymbolIf(compilation?.GetTypeByMetadataName("System.Drawing.Point") is not null, "canUseSystemDrawing");
AddSymbolIf(this.IsFeatureAvailable(Feature.InterfaceStaticMembers), "canUseInterfaceStaticMembers");
AddSymbolIf(this.canUseUnscopedRef, "canUseUnscopedRef");

if (extraSymbols.Count > 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
{
internal T e0;

#if canUseUnscopedRef

#if canUseUnsafeAdd
internal ref T this[int index]
{
Expand Down Expand Up @@ -30,4 +32,6 @@ internal Span<T> AsSpan(int length)
#endif
}
#endif

#endif
}

0 comments on commit 748f605

Please sign in to comment.