Skip to content

Commit 8b8ae95

Browse files
authored
Merge pull request #262 from Cysharp/hadashiA/fix-default-value3
Fix default value detection
2 parents ce4c03a + 9509ba1 commit 8b8ae95

2 files changed

Lines changed: 30 additions & 13 deletions

File tree

src/MemoryPack.Generator/MemoryPackGenerator.Emitter.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,12 +1376,12 @@ string EmitConstantValue(object? constantValue)
13761376
string x => $"\"{x}\"",
13771377
char x => $"'{x}'",
13781378
float x => $"{x}f",
1379-
decimal x => $"{x}D",
1379+
decimal x => $"{x}M",
13801380
bool x => x ? "true" : "false",
13811381
_ => constantValue.ToString()
13821382
};
13831383
}
1384-
return "null";
1384+
return "default!";
13851385
}
13861386

13871387
string EmitExpression(ExpressionSyntax expression)
@@ -1392,13 +1392,13 @@ string EmitExpression(ExpressionSyntax expression)
13921392
{
13931393
var memberAccess = (MemberAccessExpressionSyntax)expression;
13941394
var memberSymbol = semanticModel.GetSymbolInfo(memberAccess.Name).Symbol;
1395-
if (memberSymbol is INamedTypeSymbol namedTypeSymbol && namedTypeSymbol.TypeKind == TypeKind.Enum)
1395+
if (memberSymbol is INamedTypeSymbol { TypeKind: TypeKind.Enum } namedTypeSymbol)
13961396
{
1397-
return $"{GetTypeFullName(namedTypeSymbol, semanticModel)}.{memberAccess.Name}";
1397+
return $"{GetTypeFullName(namedTypeSymbol)}.{memberAccess.Name}";
13981398
}
1399-
if (memberSymbol is IFieldSymbol fieldSymbol && fieldSymbol.ContainingType.TypeKind == TypeKind.Enum)
1399+
if (memberSymbol is IFieldSymbol { Type.TypeKind: TypeKind.Enum } fieldSymbol)
14001400
{
1401-
return $"{GetTypeFullName(fieldSymbol.ContainingType, semanticModel)}.{fieldSymbol.Name}";
1401+
return $"{GetTypeFullName(fieldSymbol.Type)}.{fieldSymbol.Name}";
14021402
}
14031403
break;
14041404
}
@@ -1412,7 +1412,7 @@ string EmitExpression(ExpressionSyntax expression)
14121412
var arguments = string.Join(", ",
14131413
objectCreation.ArgumentList?.Arguments.Select(arg =>
14141414
EmitExpression(arg.Expression)) ?? Enumerable.Empty<string>());
1415-
return $"new {x.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}({arguments})";
1415+
return $"new {GetTypeFullName(x)}({arguments})";
14161416
}
14171417
break;
14181418
}
@@ -1426,10 +1426,15 @@ string EmitExpression(ExpressionSyntax expression)
14261426
return expression.ToString();
14271427
}
14281428

1429-
string GetTypeFullName(ITypeSymbol typeSymbol, SemanticModel semanticModel)
1429+
static string GetTypeFullName(ITypeSymbol typeSymbol)
14301430
{
1431-
var containingType = typeSymbol.ContainingType;
1432-
var containingTypeFullName = containingType == null ? "" : GetTypeFullName(containingType, semanticModel) + ".";
1433-
return containingTypeFullName + typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
1431+
if (typeSymbol.ContainingType is { } containingType)
1432+
{
1433+
// nested type
1434+
var containingTypeFullName = containingType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
1435+
var typeName = typeSymbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat);
1436+
return $"{containingTypeFullName}.{typeName}";
1437+
}
1438+
return typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
14341439
}
14351440
}

tests/MemoryPack.Tests/Models/DefaultValues.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23

34
namespace MemoryPack.Tests.Models;
@@ -26,14 +27,21 @@ partial class FieldDefaultValue
2627
[MemoryPackable]
2728
partial class PropertyDefaultValue
2829
{
30+
internal enum NestedEnum
31+
{
32+
A, B
33+
}
34+
2935
public int X { get; set; }
3036
public int Y { get; set; } = 12345;
3137
public float Z { get; set; } = 678.9f;
3238
public string S { get; set; } = "aaaaaaaaa";
3339
public bool B { get; set; } = true;
3440
public List<string> Alpha { get; set; } = new List<string>(new HashSet<string>());
35-
public TestEnum TestEnum { get; set; } = TestEnum.A;
41+
public TestEnum E { get; set; } = TestEnum.A;
42+
public NestedEnum E2 { get; set; } = NestedEnum.A;
3643
public (TestEnum, List<string>) Tuple { get; set; } = (TestEnum.A, new List<string>(new HashSet<string>()));
44+
public DateTime Struct { get; set; } = default!;
3745
}
3846

3947
[MemoryPackable]
@@ -44,14 +52,18 @@ partial class CtorParamDefaultValue
4452
public float Z;
4553
public string S;
4654
public bool B;
55+
public decimal D;
56+
public DateTime StructValue;
4757

4858
[MemoryPackConstructor]
49-
public CtorParamDefaultValue(int x, int y = 12345, float z = 678.9f, string s = "aaaaaa", bool b = true)
59+
public CtorParamDefaultValue(int x, int y = 12345, float z = 678.9f, string s = "aaaaaa", bool b = true, decimal d = 99M, DateTime structValue = default)
5060
{
5161
X = x;
5262
Y = y;
5363
Z = z;
5464
S = s;
5565
B = b;
66+
D = d;
67+
StructValue = structValue;
5668
}
5769
}

0 commit comments

Comments
 (0)