Skip to content

Commit 243f732

Browse files
committed
Fix nullable compiler warnings
1 parent f69cd48 commit 243f732

1 file changed

Lines changed: 23 additions & 15 deletions

File tree

engine/Sandbox.Generator/Units/CodeGen.cs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,12 @@ private static void HandleWrapSet( AttributeData attribute, Flags type, string c
270270

271271
if ( generatedFields.Add( setterFieldName ) )
272272
{
273-
master.AddToCurrentClass( $"[global::Sandbox.SkipHotload] private {staticModifier}global::System.Action<{propertyType}> {setterFieldName};\n", false );
273+
master.AddToCurrentClass( $"[global::Sandbox.SkipHotload] private {staticModifier}global::System.Action<{propertyType}> {setterFieldName} = null!;\n", false );
274274
}
275275

276276
if ( generatedFields.Add( getterFieldName ) )
277277
{
278-
master.AddToCurrentClass( $"[global::Sandbox.SkipHotload] private {staticModifier}global::System.Func<{propertyType}> {getterFieldName};\n", false );
278+
master.AddToCurrentClass( $"[global::Sandbox.SkipHotload] private {staticModifier}global::System.Func<{propertyType}> {getterFieldName} = null!;\n", false );
279279
}
280280

281281
// GET accessor
@@ -329,10 +329,11 @@ private static void HandleWrapSet( AttributeData attribute, Flags type, string c
329329
IdentifierName( setterFieldName ),
330330
setterLambda );
331331

332-
// Cached getter: __CachedGetter ??= () => PropertyName
332+
// Cached getter: __CachedGetter ??= () => PropertyName!
333333
// Calls the property by name, which goes through all wrapped getters
334334
// This avoids inlining wrapped getter code which would cause recursion
335-
var getterLambda = ParenthesizedLambdaExpression( IdentifierName( symbol.Name ) );
335+
var getterLambda = ParenthesizedLambdaExpression(
336+
PostfixUnaryExpression( SyntaxKind.SuppressNullableWarningExpression, IdentifierName( symbol.Name ) ) );
336337

337338
var cachedGetterExpr = AssignmentExpression(
338339
SyntaxKind.CoalesceAssignmentExpression,
@@ -344,13 +345,13 @@ private static void HandleWrapSet( AttributeData attribute, Flags type, string c
344345
AssignmentExpression(
345346
SyntaxKind.SimpleAssignmentExpression,
346347
IdentifierName( "Value" ),
347-
IdentifierName( "value" ) ),
348+
PostfixUnaryExpression( SyntaxKind.SuppressNullableWarningExpression, IdentifierName( "value" ) ) ), // Value = value!
348349

349350
AssignmentExpression(
350351
SyntaxKind.SimpleAssignmentExpression,
351352
IdentifierName( "Object" ),
352353
symbol.IsStatic
353-
? LiteralExpression( SyntaxKind.NullLiteralExpression )
354+
? PostfixUnaryExpression( SyntaxKind.SuppressNullableWarningExpression, LiteralExpression( SyntaxKind.NullLiteralExpression ) ) // null!
354355
: ThisExpression() ),
355356

356357
AssignmentExpression(
@@ -477,7 +478,7 @@ private static void HandleWrapGet( AttributeData attribute, Flags type, string c
477478

478479
if ( generatedFields.Add( getterFieldName ) )
479480
{
480-
master.AddToCurrentClass( $"[global::Sandbox.SkipHotload] private {staticModifier}global::System.Func<{propertyType}> {getterFieldName};\n", false );
481+
master.AddToCurrentClass( $"[global::Sandbox.SkipHotload] private {staticModifier}global::System.Func<{propertyType}> {getterFieldName} = null!;\n", false );
481482
}
482483

483484
// SET accessor
@@ -492,7 +493,11 @@ private static void HandleWrapGet( AttributeData attribute, Flags type, string c
492493

493494
// Get the current getter body - this allows get wrappers to chain
494495
var directGetterBody = GetDirectGetterBody( existingGetter );
495-
var getterLambda = ParenthesizedLambdaExpression( directGetterBody );
496+
// Wrap expressions in null suppression to avoid CS8603 for nullable properties: () => field!
497+
var getterLambdaBody = directGetterBody is ExpressionSyntax expr
498+
? PostfixUnaryExpression( SyntaxKind.SuppressNullableWarningExpression, expr )
499+
: directGetterBody;
500+
var getterLambda = ParenthesizedLambdaExpression( getterLambdaBody );
496501

497502
// Cached getter: __CachedGetter ??= () => <current getter body>
498503
var cachedGetterExpr = AssignmentExpression(
@@ -514,13 +519,13 @@ private static void HandleWrapGet( AttributeData attribute, Flags type, string c
514519
AssignmentExpression(
515520
SyntaxKind.SimpleAssignmentExpression,
516521
IdentifierName( "Value" ),
517-
defaultValueExpression ),
522+
PostfixUnaryExpression( SyntaxKind.SuppressNullableWarningExpression, defaultValueExpression ) ), // Value = expr!
518523

519524
AssignmentExpression(
520525
SyntaxKind.SimpleAssignmentExpression,
521526
IdentifierName( "Object" ),
522527
symbol.IsStatic
523-
? LiteralExpression( SyntaxKind.NullLiteralExpression )
528+
? PostfixUnaryExpression( SyntaxKind.SuppressNullableWarningExpression, LiteralExpression( SyntaxKind.NullLiteralExpression ) ) // null!
524529
: ThisExpression() ),
525530

526531
AssignmentExpression(
@@ -565,11 +570,14 @@ private static void HandleWrapGet( AttributeData attribute, Flags type, string c
565570

566571
var returnTypeSyntax = ParseTypeName( propertyType );
567572

573+
// return ((PropertyType)callback(...))!;
568574
statements.Add(
569575
ReturnStatement(
570-
CastExpression(
571-
returnTypeSyntax,
572-
invocation ) ) );
576+
PostfixUnaryExpression(
577+
SyntaxKind.SuppressNullableWarningExpression,
578+
CastExpression(
579+
returnTypeSyntax,
580+
invocation ) ) ) );
573581

574582
var get = AccessorDeclaration( SyntaxKind.GetAccessorDeclaration )
575583
.WithBody( Block( statements ) )
@@ -633,7 +641,7 @@ private static ExpressionSyntax BuildWrappedMethodExpression( IMethodSymbol symb
633641
}
634642
else
635643
{
636-
genericArgsExpression = LiteralExpression( SyntaxKind.NullLiteralExpression );
644+
genericArgsExpression = PostfixUnaryExpression( SyntaxKind.SuppressNullableWarningExpression, LiteralExpression( SyntaxKind.NullLiteralExpression ) ); // null!
637645
}
638646

639647
var assignments = new List<ExpressionSyntax>
@@ -647,7 +655,7 @@ private static ExpressionSyntax BuildWrappedMethodExpression( IMethodSymbol symb
647655
SyntaxKind.SimpleAssignmentExpression,
648656
IdentifierName( "Object" ),
649657
symbol.IsStatic
650-
? LiteralExpression( SyntaxKind.NullLiteralExpression )
658+
? PostfixUnaryExpression( SyntaxKind.SuppressNullableWarningExpression, LiteralExpression( SyntaxKind.NullLiteralExpression ) ) // null!
651659
: ThisExpression() ),
652660

653661
AssignmentExpression(

0 commit comments

Comments
 (0)