Skip to content

Commit 843ef69

Browse files
committed
Fix a bug when default value as nested
1 parent 22ddba5 commit 843ef69

2 files changed

Lines changed: 16 additions & 11 deletions

File tree

src/MemoryPack.Generator/MemoryPackGenerator.Emitter.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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 containgTypeFullName = containingType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
1435+
var typeName = typeSymbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat);
1436+
return $"{containgTypeFullName}.{typeName}";
1437+
}
1438+
return typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
14341439
}
14351440
}

src/MemoryPack.Generator/Properties/launchSettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"profiles": {
33
"MemoryPack.Generator": {
44
"commandName": "DebugRoslynComponent",
5-
"targetProject": "..\\..\\sandbox\\SandboxConsoleApp\\SandboxConsoleApp.csproj"
5+
"targetProject": "../../sandbox/SandboxConsoleApp/SandboxConsoleApp.csproj"
66
}
77
}
8-
}
8+
}

0 commit comments

Comments
 (0)