Skip to content

Commit 3937780

Browse files
committed
Fix a bug when default value is struct type
1 parent 843ef69 commit 3937780

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

src/MemoryPack.Generator/MemoryPackGenerator.Emitter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,7 @@ string EmitConstantValue(object? constantValue)
13811381
_ => constantValue.ToString()
13821382
};
13831383
}
1384-
return "null";
1384+
return "default!";
13851385
}
13861386

13871387
string EmitExpression(ExpressionSyntax expression)
@@ -1431,9 +1431,9 @@ static string GetTypeFullName(ITypeSymbol typeSymbol)
14311431
if (typeSymbol.ContainingType is { } containingType)
14321432
{
14331433
// nested type
1434-
var containgTypeFullName = containingType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
1434+
var containingTypeFullName = containingType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
14351435
var typeName = typeSymbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat);
1436-
return $"{containgTypeFullName}.{typeName}";
1436+
return $"{containingTypeFullName}.{typeName}";
14371437
}
14381438
return typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
14391439
}

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, decimal d = 99M)
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)