@@ -25,20 +25,23 @@ internal static class MarshalInfo
2525 List < string ? > marshalAsParamsStrings = new ( ) ;
2626 if ( collection is IArrayCreationOperation arrayCreation )
2727 {
28- var arrayLength = arrayCreation . DimensionSizes [ 0 ] . ConstantValue ;
29- if ( ! arrayLength . HasValue || ( ( int ) arrayLength . Value ! ) != argumentCount )
30- {
31- diagnostics . Add ( Diagnostic . Create ( Constants . InvalidMarshalParamsAsArrayLengthDescriptor , location ) ) ;
32- }
33- else if ( arrayCreation . Initializer is not null )
28+ if ( arrayCreation . Initializer is not null && ( argumentCount > 0 ) )
3429 {
3530 foreach ( var elementValue in arrayCreation . Initializer . ElementValues )
3631 {
3732 cancellationToken . ThrowIfCancellationRequested ( ) ;
3833 GetMarshalAsFromOperation ( elementValue , cancellationToken , argumentCount , diagnostics , location , marshalAsParamsStrings ) ;
34+ if ( marshalAsParamsStrings . Count == argumentCount )
35+ {
36+ break ;
37+ }
38+ }
39+ for ( int count = marshalAsParamsStrings . Count ; count < argumentCount ; ++ count )
40+ {
41+ marshalAsParamsStrings . Add ( null ) ;
3942 }
4043 }
41- // else (no initializer), default to no marshaling
44+ // else (no initializer or no arguments ), default to no marshaling
4245 }
4346 else if ( ! collection . ConstantValue . HasValue ) // argument is not null
4447 {
@@ -54,7 +57,7 @@ internal static class MarshalInfo
5457 return marshalAsParamsStrings . Count > 0 ? marshalAsParamsStrings . ToImmutableArray ( ) : null ;
5558 }
5659
57- private static void GetMarshalAsFromField ( IFieldReferenceOperation fieldReference , CancellationToken cancellationToken , int argumentCount , List < Diagnostic > diagnostics , Location location , List < string ? > marshalAsStrings )
60+ private static void GetMarshalAsFromField ( IFieldReferenceOperation fieldReference , CancellationToken cancellationToken , int argumentCount , List < string ? > marshalAsStrings )
5861 {
5962 // `GetOperation` is only returning `null` for the relevant `SyntaxNode`s here, so we have to manually parse the field initializer
6063 // see <https://stackoverflow.com/q/75916082/1136311>
@@ -64,22 +67,20 @@ private static void GetMarshalAsFromField(IFieldReferenceOperation fieldReferenc
6467 bool isInsideArrayInitializer = false ;
6568 bool isInsideNewExpression = false ;
6669 bool isInsideObjectInitializer = false ;
67- bool addedArrayLengthDiagnostic = false ;
6870 var addMarshalAsString = ( ) =>
6971 {
7072 if ( sb . Length != 0 )
7173 {
7274 marshalAsStrings . Add ( sb . ToString ( ) ) ;
73- if ( isArray && ! addedArrayLengthDiagnostic && marshalAsStrings . Count > argumentCount )
74- {
75- addedArrayLengthDiagnostic = true ;
76- diagnostics . Add ( Diagnostic . Create ( Constants . InvalidMarshalParamsAsArrayLengthDescriptor , location ) ) ;
77- }
7875 _ = sb . Clear ( ) ;
7976 }
8077 } ;
8178 foreach ( var syntaxToken in fieldDeclaration . DescendantTokens ( ) )
8279 {
80+ if ( marshalAsStrings . Count == argumentCount )
81+ {
82+ return ;
83+ }
8384 var token = syntaxToken . ToString ( ) ;
8485 switch ( token )
8586 {
@@ -112,7 +113,15 @@ private static void GetMarshalAsFromField(IFieldReferenceOperation fieldReferenc
112113 addMarshalAsString ( ) ;
113114 continue ;
114115 case "null" :
116+ if ( isArray && ! isInsideArrayInitializer )
117+ {
118+ return ;
119+ }
115120 marshalAsStrings . Add ( null ) ;
121+ if ( ! isArray && ! isInsideObjectInitializer )
122+ {
123+ return ;
124+ }
116125 continue ;
117126 case "," :
118127 if ( isInsideObjectInitializer )
@@ -158,7 +167,14 @@ private static void GetMarshalAsFromOperation(IOperation value, CancellationToke
158167 }
159168 if ( value is IFieldReferenceOperation fieldReference && fieldReference . Field . IsReadOnly )
160169 {
161- GetMarshalAsFromField ( fieldReference , cancellationToken , argumentCount , diagnostics , location , marshalAsStrings ) ;
170+ GetMarshalAsFromField ( fieldReference , cancellationToken , argumentCount , marshalAsStrings ) ;
171+ if ( fieldReference . Field . Type is IArrayTypeSymbol && ( marshalAsStrings . Count > 0 ) )
172+ {
173+ for ( int count = marshalAsStrings . Count ; count < argumentCount ; ++ count )
174+ {
175+ marshalAsStrings . Add ( null ) ;
176+ }
177+ }
162178 return ;
163179 }
164180 IObjectCreationOperation ? objectCreation = value as IObjectCreationOperation ;
@@ -183,7 +199,7 @@ private static void GetMarshalAsFromOperation(IOperation value, CancellationToke
183199 public static void GetMarshalAsFromOperation ( IOperation value , CancellationToken cancellationToken , List < Diagnostic > diagnostics , Location location , out string ? marshalAsString )
184200 {
185201 List < string ? > marshalAsStrings = new ( 1 ) ;
186- GetMarshalAsFromOperation ( value , cancellationToken , 0 , diagnostics , location , marshalAsStrings ) ;
202+ GetMarshalAsFromOperation ( value , cancellationToken , 1 , diagnostics , location , marshalAsStrings ) ;
187203 marshalAsString = marshalAsStrings . FirstOrDefault ( ) ;
188204 }
189205 }
0 commit comments