From 842a5f7711361c6a2ae4c8420e056aaf3da5473d Mon Sep 17 00:00:00 2001 From: jnm2 Date: Thu, 23 Sep 2021 13:35:39 -0400 Subject: [PATCH] Check whether Span<> is available before using it to unblock net35 tests --- src/Microsoft.Windows.CsWin32/Generator.cs | 23 +++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.Windows.CsWin32/Generator.cs b/src/Microsoft.Windows.CsWin32/Generator.cs index 5667e120..7f360c87 100644 --- a/src/Microsoft.Windows.CsWin32/Generator.cs +++ b/src/Microsoft.Windows.CsWin32/Generator.cs @@ -291,6 +291,7 @@ public class Generator : IDisposable private readonly GeneratorOptions options; private readonly CSharpCompilation? compilation; private readonly CSharpParseOptions? parseOptions; + private readonly bool canUseSpan; private readonly bool canCallCreateSpan; private readonly bool getDelegateForFunctionPointerGenericExists; private readonly bool generateSupportedOSPlatformAttributes; @@ -320,6 +321,7 @@ public Generator(string metadataLibraryPath, Docs? docs, GeneratorOptions option this.parseOptions = parseOptions; this.volatileCode = new(this.committedCode); + 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.getDelegateForFunctionPointerGenericExists = this.compilation?.GetTypeByMetadataName(typeof(Marshal).FullName)?.GetMembers(nameof(Marshal.GetDelegateForFunctionPointer)).Any(m => m is IMethodSymbol { IsGenericMethod: true }) is true; this.generateDefaultDllImportSearchPathsAttribute = this.compilation?.GetTypeByMetadataName(typeof(DefaultDllImportSearchPathsAttribute).FullName) is object; @@ -3694,15 +3696,18 @@ private IEnumerable CreateAdditionalTypeDefPWSTRMembers .AddArgumentListArguments(Argument(thisValue))))) .WithSemicolonToken(SemicolonWithLineFeed); - // internal Span AsSpan() => this.Value is null ? default : new Span(this.Value, this.Length); - TypeSyntax spanChar = MakeSpanOfT(PredefinedType(Token(SyntaxKind.CharKeyword))); - ExpressionSyntax thisLength = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, ThisExpression(), IdentifierName("Length")); - ExpressionSyntax spanCreation = ObjectCreationExpression(spanChar).AddArgumentListArguments(Argument(thisValue), Argument(thisLength)); - ExpressionSyntax conditional = ConditionalExpression(thisValueIsNull, DefaultExpression(spanChar), spanCreation); - yield return MethodDeclaration(spanChar, Identifier("AsSpan")) - .AddModifiers(TokenWithSpace(this.Visibility)) - .WithExpressionBody(ArrowExpressionClause(conditional)) - .WithSemicolonToken(SemicolonWithLineFeed); + if (this.canUseSpan) + { + // internal Span AsSpan() => this.Value is null ? default : new Span(this.Value, this.Length); + TypeSyntax spanChar = MakeSpanOfT(PredefinedType(Token(SyntaxKind.CharKeyword))); + ExpressionSyntax thisLength = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, ThisExpression(), IdentifierName("Length")); + ExpressionSyntax spanCreation = ObjectCreationExpression(spanChar).AddArgumentListArguments(Argument(thisValue), Argument(thisLength)); + ExpressionSyntax conditional = ConditionalExpression(thisValueIsNull, DefaultExpression(spanChar), spanCreation); + yield return MethodDeclaration(spanChar, Identifier("AsSpan")) + .AddModifiers(TokenWithSpace(this.Visibility)) + .WithExpressionBody(ArrowExpressionClause(conditional)) + .WithSemicolonToken(SemicolonWithLineFeed); + } #pragma warning restore SA1114 // Parameter list should follow declaration }