Skip to content

Commit

Permalink
CodeGen: guard scoped keyword usage by language version (#8370)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsteinich authored Apr 11, 2023
1 parent 9fc7f32 commit e0bb170
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
7 changes: 7 additions & 0 deletions src/Orleans.CodeGenerator/LibraryTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Orleans.CodeGenerator.SyntaxGeneration;

namespace Orleans.CodeGenerator
Expand Down Expand Up @@ -178,6 +179,8 @@ public static LibraryTypes FromCompilation(Compilation compilation, CodeGenerato
Type("System.Collections.Immutable.ImmutableSortedSet`1"),
Type("System.Collections.Immutable.ImmutableStack`1"),
},

LanguageVersion = (compilation.SyntaxTrees.FirstOrDefault()?.Options as CSharpParseOptions)?.LanguageVersion
};

INamedTypeSymbol Type(string metadataName)
Expand Down Expand Up @@ -303,6 +306,8 @@ INamedTypeSymbol TypeOrDefault(string metadataName)
public INamedTypeSymbol FSharpSourceConstructFlagsOrDefault { get; private set; }
public INamedTypeSymbol RuntimeHelpers { get; private set; }

public LanguageVersion? LanguageVersion { get; private set; }

private readonly ConcurrentDictionary<ITypeSymbol, bool> _shallowCopyableTypes = new(SymbolEqualityComparer.Default);

public bool IsShallowCopyable(ITypeSymbol type)
Expand Down Expand Up @@ -445,5 +450,7 @@ public static WellKnownCopierDescription FindByUnderlyingType(this WellKnownCopi

return null;
}

public static bool HasScopedKeyword(this LibraryTypes libraryTypes) => libraryTypes.LanguageVersion is null or >= LanguageVersion.CSharp11;
}
}
20 changes: 10 additions & 10 deletions src/Orleans.CodeGenerator/SerializerGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ private static MemberDeclarationSyntax GenerateSerializeMethod(

if (type.IsValueType)
{
parameters[1] = parameters[1].WithModifiers(TokenList(Token(SyntaxKind.ScopedKeyword), Token(SyntaxKind.RefKeyword)));
parameters[1] = parameters[1].WithModifiers(libraryTypes.HasScopedKeyword() ? TokenList(Token(SyntaxKind.ScopedKeyword), Token(SyntaxKind.RefKeyword)) : TokenList(Token(SyntaxKind.RefKeyword)));
}

var res = MethodDeclaration(returnType, SerializeMethodName)
Expand Down Expand Up @@ -537,7 +537,7 @@ private static MemberDeclarationSyntax GenerateDeserializeMethod(

if (type.IsValueType)
{
parameters[1] = parameters[1].WithModifiers(TokenList(Token(SyntaxKind.ScopedKeyword), Token(SyntaxKind.RefKeyword)));
parameters[1] = parameters[1].WithModifiers(libraryTypes.HasScopedKeyword() ? TokenList(Token(SyntaxKind.ScopedKeyword), Token(SyntaxKind.RefKeyword)) : TokenList(Token(SyntaxKind.RefKeyword)));
}

var res = MethodDeclaration(returnType, DeserializeMethodName)
Expand Down Expand Up @@ -717,7 +717,7 @@ private static MemberDeclarationSyntax GenerateCompoundTypeWriteFieldMethod(
})))),
ReturnStatement()))
);

// C#: ReferenceCodec.MarkValueField(reader.Session);
innerBody.Add(ExpressionStatement(InvocationExpression(IdentifierName("ReferenceCodec").Member("MarkValueField"), ArgumentList(SingletonSeparatedList(Argument(writerParam.Member("Session")))))));
}
Expand Down Expand Up @@ -1023,7 +1023,7 @@ public BaseCodecFieldDescription(TypeSyntax fieldType, bool concreteType = false
public override bool IsInjected { get; }
}

internal sealed class ActivatorFieldDescription : GeneratedFieldDescription
internal sealed class ActivatorFieldDescription : GeneratedFieldDescription
{
public ActivatorFieldDescription(TypeSyntax fieldType, string fieldName) : base(fieldType, fieldName)
{
Expand All @@ -1048,7 +1048,7 @@ internal sealed class TypeFieldDescription : GeneratedFieldDescription
public TypeFieldDescription(TypeSyntax fieldType, string fieldName, TypeSyntax underlyingTypeSyntax, ITypeSymbol underlyingType) : base(fieldType, fieldName)
{
UnderlyingType = underlyingType;
UnderlyingTypeSyntax = underlyingTypeSyntax;
UnderlyingTypeSyntax = underlyingTypeSyntax;
}

public TypeSyntax UnderlyingTypeSyntax { get; }
Expand Down Expand Up @@ -1224,30 +1224,30 @@ public SerializableMember(LibraryTypes libraryTypes, ISerializableTypeDescriptio
private bool IsProperty => Member.Symbol is IPropertySymbol;

/// <summary>
/// Gets a value indicating whether or not this member represents an accessible field.
/// Gets a value indicating whether or not this member represents an accessible field.
/// </summary>
private bool IsGettableField => Field is { } field && _model.IsAccessible(0, field) && !IsObsolete;

/// <summary>
/// Gets a value indicating whether or not this member represents an accessible, mutable field.
/// Gets a value indicating whether or not this member represents an accessible, mutable field.
/// </summary>
private bool IsSettableField => Field is { } field && IsGettableField && !field.IsReadOnly;

/// <summary>
/// Gets a value indicating whether or not this member represents a property with an accessible, non-obsolete getter.
/// Gets a value indicating whether or not this member represents a property with an accessible, non-obsolete getter.
/// </summary>
private bool IsGettableProperty => Property?.GetMethod is { } getMethod && _model.IsAccessible(0, getMethod) && !IsObsolete;

/// <summary>
/// Gets a value indicating whether or not this member represents a property with an accessible, non-obsolete setter.
/// Gets a value indicating whether or not this member represents a property with an accessible, non-obsolete setter.
/// </summary>
private bool IsSettableProperty => Property?.SetMethod is { } setMethod && _model.IsAccessible(0, setMethod) && !setMethod.IsInitOnly && !IsObsolete;

/// <summary>
/// Gets syntax representing the type of this field.
/// </summary>
public TypeSyntax TypeSyntax => Member.Type.TypeKind == TypeKind.Dynamic
? PredefinedType(Token(SyntaxKind.ObjectKeyword))
? PredefinedType(Token(SyntaxKind.ObjectKeyword))
: _member.GetTypeSyntax(Member.Type);

/// <summary>
Expand Down

0 comments on commit e0bb170

Please sign in to comment.