Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix tests and address feedback
  • Loading branch information
layomia committed Nov 15, 2021
commit 5987dc996cb6fee910befe7e546487665f091a46
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,13 @@ private sealed class Parser

private readonly HashSet<TypeGenerationSpec> _implicitlyRegisteredTypes = new();

private readonly HashSet<ValueTuple<Type, DiagnosticDescriptor, string[]>> _typeLevelDiagnostics = new();
/// <summary>
/// A list of diagnostics emitted at the type level. This is cached and emitted between the processing of context types
/// in order to properly retrieve [JsonSerializable] attribute applications for top-level types (since a type might occur
/// both at top-level and in nested object graphs, with no guarantee of the order that we will see the type).
/// The element tuple types specifies the serializable type, the kind of diagnostic to emit, and the diagnostic message.
/// </summary>
private readonly List<(Type, DiagnosticDescriptor, string[])> _typeLevelDiagnostics = new();

private JsonKnownNamingPolicy _currentContextNamingPolicy;

Expand Down Expand Up @@ -246,6 +252,11 @@ public Parser(Compilation compilation, in JsonSourceGenerationContext sourceGene

foreach (ClassDeclarationSyntax classDeclarationSyntax in classDeclarationSyntaxList)
{
// Ensure context-scoped metadata caches are empty.
Debug.Assert(_typeGenerationSpecCache.Count == 0);
Debug.Assert(_implicitlyRegisteredTypes.Count == 0);
Debug.Assert(_typeLevelDiagnostics.Count == 0);

CompilationUnitSyntax compilationUnitSyntax = classDeclarationSyntax.FirstAncestorOrSelf<CompilationUnitSyntax>();
SemanticModel compilationSemanticModel = compilation.GetSemanticModel(compilationUnitSyntax.SyntaxTree);

Expand Down Expand Up @@ -342,7 +353,7 @@ public Parser(Compilation compilation, in JsonSourceGenerationContext sourceGene
contextGenSpecList ??= new List<ContextGenerationSpec>();
contextGenSpecList.Add(contextGenSpec);

// Clear the cache of generated metadata between the processing of context classes.
// Clear the caches of generated metadata between the processing of context classes.
_typeGenerationSpecCache.Clear();
_implicitlyRegisteredTypes.Clear();
_typeLevelDiagnostics.Clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,20 @@ private static bool HasJsonConstructorAttribute(ConstructorInfo constructorInfo)

public static Location? GetDiagnosticLocation(this Type type)
{
TypeWrapper? typeWrapper = type as TypeWrapper;
Debug.Assert(typeWrapper != null);
return typeWrapper.Location;
Debug.Assert(type is TypeWrapper);
return ((TypeWrapper)type).Location;
}

public static Location? GetDiagnosticLocation(this PropertyInfo propertyInfo)
{
PropertyInfoWrapper? propertyInfoWrapper = propertyInfo as PropertyInfoWrapper;
Debug.Assert(propertyInfoWrapper != null);
return propertyInfoWrapper.Location;
Debug.Assert(propertyInfo is PropertyInfoWrapper);
return ((PropertyInfoWrapper)propertyInfo).Location;
}

public static Location? GetDiagnosticLocation(this FieldInfo fieldInfo)
{
FieldInfoWrapper? fieldInfoWrapper = fieldInfo as FieldInfoWrapper;
Debug.Assert(fieldInfoWrapper != null);
return fieldInfoWrapper.Location;
Debug.Assert(fieldInfo is FieldInfoWrapper);
return ((FieldInfoWrapper)fieldInfo).Location;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using System.Text.Encodings.Web;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
using Xunit;

namespace System.Text.Json.SourceGeneration.UnitTests
Expand Down Expand Up @@ -340,16 +339,20 @@ public partial class MyJsonContext : JsonSerializerContext
internal static void CheckDiagnosticMessages(
DiagnosticSeverity level,
ImmutableArray<Diagnostic> diagnostics,
(TextSpan Location, string Message)[] expectedDiags)
(Location Location, string Message)[] expectedDiags,
bool sort = true)
{
(TextSpan Location, string Message)[] actualDiags = diagnostics
(Location Location, string Message)[] actualDiags = diagnostics
.Where(diagnostic => diagnostic.Severity == level)
.Select(diagnostic => (diagnostic.Location.SourceSpan, diagnostic.GetMessage()))
.Select(diagnostic => (diagnostic.Location, diagnostic.GetMessage()))
.ToArray();

// Can't depend on reflection order when generating type metadata.
Array.Sort(actualDiags);
Array.Sort(expectedDiags);
if (sort)
{
// Can't depend on reflection order when generating type metadata.
Array.Sort(actualDiags);
Array.Sort(expectedDiags);
}

if (CultureInfo.CurrentUICulture.Name.StartsWith("en", StringComparison.OrdinalIgnoreCase))
{
Expand Down
Loading