From 22ddba508c4c2328ad1c89fc7d3e67b0d0bd25cf Mon Sep 17 00:00:00 2001 From: hadashiA Date: Thu, 21 Mar 2024 10:17:30 +0900 Subject: [PATCH 1/4] Fix decimal literal --- src/MemoryPack.Generator/MemoryPackGenerator.Emitter.cs | 2 +- tests/MemoryPack.Tests/Models/DefaultValues.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MemoryPack.Generator/MemoryPackGenerator.Emitter.cs b/src/MemoryPack.Generator/MemoryPackGenerator.Emitter.cs index 4246386..cada6f6 100644 --- a/src/MemoryPack.Generator/MemoryPackGenerator.Emitter.cs +++ b/src/MemoryPack.Generator/MemoryPackGenerator.Emitter.cs @@ -1376,7 +1376,7 @@ string EmitConstantValue(object? constantValue) string x => $"\"{x}\"", char x => $"'{x}'", float x => $"{x}f", - decimal x => $"{x}D", + decimal x => $"{x}M", bool x => x ? "true" : "false", _ => constantValue.ToString() }; diff --git a/tests/MemoryPack.Tests/Models/DefaultValues.cs b/tests/MemoryPack.Tests/Models/DefaultValues.cs index ab878bc..84156fa 100644 --- a/tests/MemoryPack.Tests/Models/DefaultValues.cs +++ b/tests/MemoryPack.Tests/Models/DefaultValues.cs @@ -46,7 +46,7 @@ partial class CtorParamDefaultValue public bool B; [MemoryPackConstructor] - public CtorParamDefaultValue(int x, int y = 12345, float z = 678.9f, string s = "aaaaaa", bool b = true) + public CtorParamDefaultValue(int x, int y = 12345, float z = 678.9f, string s = "aaaaaa", bool b = true, decimal d = 99M) { X = x; Y = y; From 843ef693e5196a1d59adfd63ebddcc1d75838139 Mon Sep 17 00:00:00 2001 From: hadashiA Date: Thu, 21 Mar 2024 10:37:11 +0900 Subject: [PATCH 2/4] Fix a bug when default value as nested --- .../MemoryPackGenerator.Emitter.cs | 23 +++++++++++-------- .../Properties/launchSettings.json | 4 ++-- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/MemoryPack.Generator/MemoryPackGenerator.Emitter.cs b/src/MemoryPack.Generator/MemoryPackGenerator.Emitter.cs index cada6f6..1e1755a 100644 --- a/src/MemoryPack.Generator/MemoryPackGenerator.Emitter.cs +++ b/src/MemoryPack.Generator/MemoryPackGenerator.Emitter.cs @@ -1392,13 +1392,13 @@ string EmitExpression(ExpressionSyntax expression) { var memberAccess = (MemberAccessExpressionSyntax)expression; var memberSymbol = semanticModel.GetSymbolInfo(memberAccess.Name).Symbol; - if (memberSymbol is INamedTypeSymbol namedTypeSymbol && namedTypeSymbol.TypeKind == TypeKind.Enum) + if (memberSymbol is INamedTypeSymbol { TypeKind: TypeKind.Enum } namedTypeSymbol) { - return $"{GetTypeFullName(namedTypeSymbol, semanticModel)}.{memberAccess.Name}"; + return $"{GetTypeFullName(namedTypeSymbol)}.{memberAccess.Name}"; } - if (memberSymbol is IFieldSymbol fieldSymbol && fieldSymbol.ContainingType.TypeKind == TypeKind.Enum) + if (memberSymbol is IFieldSymbol { Type.TypeKind: TypeKind.Enum } fieldSymbol) { - return $"{GetTypeFullName(fieldSymbol.ContainingType, semanticModel)}.{fieldSymbol.Name}"; + return $"{GetTypeFullName(fieldSymbol.Type)}.{fieldSymbol.Name}"; } break; } @@ -1412,7 +1412,7 @@ string EmitExpression(ExpressionSyntax expression) var arguments = string.Join(", ", objectCreation.ArgumentList?.Arguments.Select(arg => EmitExpression(arg.Expression)) ?? Enumerable.Empty()); - return $"new {x.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}({arguments})"; + return $"new {GetTypeFullName(x)}({arguments})"; } break; } @@ -1426,10 +1426,15 @@ string EmitExpression(ExpressionSyntax expression) return expression.ToString(); } - string GetTypeFullName(ITypeSymbol typeSymbol, SemanticModel semanticModel) + static string GetTypeFullName(ITypeSymbol typeSymbol) { - var containingType = typeSymbol.ContainingType; - var containingTypeFullName = containingType == null ? "" : GetTypeFullName(containingType, semanticModel) + "."; - return containingTypeFullName + typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); + if (typeSymbol.ContainingType is { } containingType) + { + // nested type + var containgTypeFullName = containingType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); + var typeName = typeSymbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat); + return $"{containgTypeFullName}.{typeName}"; + } + return typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); } } diff --git a/src/MemoryPack.Generator/Properties/launchSettings.json b/src/MemoryPack.Generator/Properties/launchSettings.json index 5fa6874..c84c957 100644 --- a/src/MemoryPack.Generator/Properties/launchSettings.json +++ b/src/MemoryPack.Generator/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "MemoryPack.Generator": { "commandName": "DebugRoslynComponent", - "targetProject": "..\\..\\sandbox\\SandboxConsoleApp\\SandboxConsoleApp.csproj" + "targetProject": "../../sandbox/SandboxConsoleApp/SandboxConsoleApp.csproj" } } -} \ No newline at end of file +} From 393778099ce1e8c0af66d1a9be0a88c4a35c8128 Mon Sep 17 00:00:00 2001 From: hadashiA Date: Thu, 21 Mar 2024 10:46:08 +0900 Subject: [PATCH 3/4] Fix a bug when default value is struct type --- .../MemoryPackGenerator.Emitter.cs | 6 +++--- tests/MemoryPack.Tests/Models/DefaultValues.cs | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/MemoryPack.Generator/MemoryPackGenerator.Emitter.cs b/src/MemoryPack.Generator/MemoryPackGenerator.Emitter.cs index 1e1755a..c264cf1 100644 --- a/src/MemoryPack.Generator/MemoryPackGenerator.Emitter.cs +++ b/src/MemoryPack.Generator/MemoryPackGenerator.Emitter.cs @@ -1381,7 +1381,7 @@ string EmitConstantValue(object? constantValue) _ => constantValue.ToString() }; } - return "null"; + return "default!"; } string EmitExpression(ExpressionSyntax expression) @@ -1431,9 +1431,9 @@ static string GetTypeFullName(ITypeSymbol typeSymbol) if (typeSymbol.ContainingType is { } containingType) { // nested type - var containgTypeFullName = containingType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); + var containingTypeFullName = containingType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); var typeName = typeSymbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat); - return $"{containgTypeFullName}.{typeName}"; + return $"{containingTypeFullName}.{typeName}"; } return typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); } diff --git a/tests/MemoryPack.Tests/Models/DefaultValues.cs b/tests/MemoryPack.Tests/Models/DefaultValues.cs index 84156fa..c13aa0a 100644 --- a/tests/MemoryPack.Tests/Models/DefaultValues.cs +++ b/tests/MemoryPack.Tests/Models/DefaultValues.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; namespace MemoryPack.Tests.Models; @@ -26,14 +27,21 @@ partial class FieldDefaultValue [MemoryPackable] partial class PropertyDefaultValue { + internal enum NestedEnum + { + A, B + } + public int X { get; set; } public int Y { get; set; } = 12345; public float Z { get; set; } = 678.9f; public string S { get; set; } = "aaaaaaaaa"; public bool B { get; set; } = true; public List Alpha { get; set; } = new List(new HashSet()); - public TestEnum TestEnum { get; set; } = TestEnum.A; + public TestEnum E { get; set; } = TestEnum.A; + public NestedEnum E2 { get; set; } = NestedEnum.A; public (TestEnum, List) Tuple { get; set; } = (TestEnum.A, new List(new HashSet())); + public DateTime Struct { get; set; } = default!; } [MemoryPackable] @@ -44,14 +52,18 @@ partial class CtorParamDefaultValue public float Z; public string S; public bool B; + public decimal D; + public DateTime StructValue; [MemoryPackConstructor] - public CtorParamDefaultValue(int x, int y = 12345, float z = 678.9f, string s = "aaaaaa", bool b = true, decimal d = 99M) + public CtorParamDefaultValue(int x, int y = 12345, float z = 678.9f, string s = "aaaaaa", bool b = true, decimal d = 99M, DateTime structValue = default) { X = x; Y = y; Z = z; S = s; B = b; + D = d; + StructValue = structValue; } } From 9509ba121b4709c141c92c76621da9b0ed2a12d9 Mon Sep 17 00:00:00 2001 From: hadashiA Date: Thu, 21 Mar 2024 10:51:04 +0900 Subject: [PATCH 4/4] Remove debug --- src/MemoryPack.Generator/Properties/launchSettings.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MemoryPack.Generator/Properties/launchSettings.json b/src/MemoryPack.Generator/Properties/launchSettings.json index c84c957..5fa6874 100644 --- a/src/MemoryPack.Generator/Properties/launchSettings.json +++ b/src/MemoryPack.Generator/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "MemoryPack.Generator": { "commandName": "DebugRoslynComponent", - "targetProject": "../../sandbox/SandboxConsoleApp/SandboxConsoleApp.csproj" + "targetProject": "..\\..\\sandbox\\SandboxConsoleApp\\SandboxConsoleApp.csproj" } } -} +} \ No newline at end of file