Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5d0c30d

Browse files
committed
C#: Fix nullable reference type handling in type mention extraction
1 parent dad5166 commit 5d0c30d

14 files changed

Lines changed: 77 additions & 97 deletions

File tree

csharp/extractor/Semmle.Extraction.CSharp/Entities/TypeMention.cs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,36 @@ private TypeMention(Context cx, TypeSyntax syntax, IEntity parent, Type type, Mi
2323
this.loc = loc;
2424
}
2525

26-
private static TypeSyntax GetElementType(TypeSyntax type)
26+
private TypeSyntax GetArrayElementType(TypeSyntax type)
2727
{
2828
switch (type)
2929
{
3030
case ArrayTypeSyntax ats:
31-
return GetElementType(ats.ElementType);
31+
return GetArrayElementType(ats.ElementType);
32+
case NullableTypeSyntax nts:
33+
// int[]? -> int[] -> int
34+
// int? -> int?
35+
return cx.GetTypeInfo(nts.ElementType).Type.IsReferenceType
36+
? GetArrayElementType(nts.ElementType)
37+
: nts;
3238
case PointerTypeSyntax pts:
33-
return GetElementType(pts.ElementType);
39+
return GetArrayElementType(pts.ElementType);
3440
default:
3541
return type;
3642
}
3743
}
3844

39-
private static Type GetElementType(Type type)
45+
private static Type GetArrayElementType(Type type)
4046
{
4147
switch (type)
4248
{
4349
case ArrayType at:
44-
return GetElementType(at.ElementType.Type);
50+
return GetArrayElementType(at.ElementType.Type);
4551
case NamedType nt when nt.symbol.IsBoundSpan() ||
4652
nt.symbol.IsBoundReadOnlySpan():
4753
return nt.TypeArguments.Single();
4854
case PointerType pt:
49-
return GetElementType(pt.PointedAtType);
55+
return GetArrayElementType(pt.PointedAtType);
5056
default:
5157
return type;
5258
}
@@ -59,18 +65,25 @@ protected override void Populate(TextWriter trapFile)
5965
case SyntaxKind.ArrayType:
6066
case SyntaxKind.PointerType:
6167
Emit(trapFile, loc ?? syntax.GetLocation(), parent, type);
62-
Create(cx, GetElementType(syntax), this, GetElementType(type));
68+
Create(cx, GetArrayElementType(syntax), this, GetArrayElementType(type));
6369
return;
6470
case SyntaxKind.NullableType:
6571
var nts = (NullableTypeSyntax)syntax;
6672
if (type is NamedType nt)
6773
{
68-
Emit(trapFile, loc ?? syntax.GetLocation(), parent, type);
69-
Create(cx, nts.ElementType, this, nt.symbol.IsReferenceType ? nt : nt.TypeArguments[0]);
74+
if (!nt.symbol.IsReferenceType)
75+
{
76+
Emit(trapFile, loc ?? syntax.GetLocation(), parent, type);
77+
Create(cx, nts.ElementType, this, nt.TypeArguments[0]);
78+
}
79+
else
80+
{
81+
Create(cx, nts.ElementType, parent, type);
82+
}
7083
}
7184
else if (type is ArrayType array)
7285
{
73-
Create(cx, nts.ElementType, parent, array);
86+
Create(cx, nts.ElementType, parent, type);
7487
}
7588
return;
7689
case SyntaxKind.TupleType:

csharp/ql/test/library-tests/csharp8/PrintAst.expected

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,10 @@ NullableRefTypes.cs:
139139
# 6| [Class] MyClass
140140
# 9| 5: [Field] A
141141
# 9| -1: [TypeMention] MyClass
142-
# 9| 1: [TypeMention] MyClass
143142
# 10| 6: [Field] B
144143
# 10| -1: [TypeMention] MyClass
145144
# 13| 7: [IndexerProperty] C
146145
# 13| -1: [TypeMention] MyClass
147-
# 13| 1: [TypeMention] MyClass
148146
# 13| 3: [Getter] get_C
149147
# 13| 4: [NullLiteral] null
150148
# 14| 8: [IndexerProperty] D
@@ -153,7 +151,6 @@ NullableRefTypes.cs:
153151
# 14| 4: [ThisAccess] this access
154152
# 17| 9: [Indexer] Item
155153
# 17| -1: [TypeMention] MyClass
156-
# 17| 1: [TypeMention] MyClass
157154
#-----| 1: (Parameters)
158155
# 17| 0: [Parameter] i
159156
# 17| -1: [TypeMention] int
@@ -175,52 +172,42 @@ NullableRefTypes.cs:
175172
#-----| 1: (Parameters)
176173
# 19| 0: [Parameter] i
177174
# 19| -1: [TypeMention] MyClass
178-
# 19| 1: [TypeMention] MyClass
179175
# 19| 3: [Getter] get_Item
180176
#-----| 2: (Parameters)
181177
# 19| 0: [Parameter] i
182178
# 19| 4: [ThisAccess] this access
183179
# 22| 12: [Field] G1
184180
# 22| -1: [TypeMention] MyClass[]
185181
# 22| 1: [TypeMention] MyClass
186-
# 22| 1: [TypeMention] MyClass
187182
# 23| 13: [Field] G2
188183
# 23| -1: [TypeMention] MyClass[]
189184
# 23| 1: [TypeMention] MyClass
190-
# 23| 1: [TypeMention] MyClass
191185
# 24| 14: [Field] G3
192186
# 24| -1: [TypeMention] MyClass[]
193187
# 24| 1: [TypeMention] MyClass
194-
# 24| 1: [TypeMention] MyClass
195188
# 25| 15: [Field] H
196189
# 25| -1: [TypeMention] MyClass[][]
197190
# 25| 1: [TypeMention] MyClass
198-
# 25| 1: [TypeMention] MyClass
199191
# 26| 16: [Method] ArrayFn1
200192
# 26| -1: [TypeMention] MyClass[]
201193
# 26| 1: [TypeMention] MyClass
202194
#-----| 2: (Parameters)
203195
# 26| 0: [Parameter] x
204196
# 26| -1: [TypeMention] MyClass[][]
205197
# 26| 1: [TypeMention] MyClass
206-
# 26| 1: [TypeMention] MyClass
207-
# 26| 1: [TypeMention] MyClass
208198
# 26| 4: [ThrowExpr] throw ...
209199
# 26| 0: [NullLiteral] null
210200
# 27| 17: [Method] ArrayFn2
211201
# 27| -1: [TypeMention] MyClass[]
212202
# 27| 1: [TypeMention] MyClass
213-
# 27| 1: [TypeMention] MyClass
214203
#-----| 2: (Parameters)
215204
# 27| 0: [Parameter] x
216205
# 27| -1: [TypeMention] MyClass[][]
217206
# 27| 1: [TypeMention] MyClass
218-
# 27| 1: [TypeMention] MyClass
219207
# 27| 4: [ThrowExpr] throw ...
220208
# 27| 0: [NullLiteral] null
221209
# 30| 18: [Method] M
222210
# 30| -1: [TypeMention] MyClass
223-
# 30| 1: [TypeMention] MyClass
224211
# 30| 4: [NullLiteral] null
225212
# 31| 19: [Method] N
226213
# 31| -1: [TypeMention] MyClass
@@ -232,7 +219,6 @@ NullableRefTypes.cs:
232219
# 32| -1: [TypeMention] MyClass
233220
# 32| 1: [Parameter] b
234221
# 32| -1: [TypeMention] MyClass
235-
# 32| 1: [TypeMention] MyClass
236222
# 32| 4: [BlockStmt] {...}
237223
# 35| 21: [Method] Locals
238224
# 35| -1: [TypeMention] Void
@@ -246,7 +232,6 @@ NullableRefTypes.cs:
246232
# 38| 1: [LocalVariableDeclStmt] ... ...;
247233
# 38| 0: [LocalVariableDeclAndInitExpr] MyClass b = ...
248234
# 38| -1: [TypeMention] MyClass
249-
# 38| 1: [TypeMention] MyClass
250235
# 38| 0: [LocalVariableAccess] access to local variable b
251236
# 38| 1: [NullLiteral] null
252237
# 39| 2: [LocalVariableDeclStmt] ... ...;
@@ -268,7 +253,6 @@ NullableRefTypes.cs:
268253
# 47| -1: [TypeMention] MyClass
269254
# 48| 24: [Event] P
270255
# 48| -1: [TypeMention] Del
271-
# 48| 1: [TypeMention] Del
272256
# 48| 3: [AddEventAccessor] add_P
273257
#-----| 2: (Parameters)
274258
# 48| 0: [Parameter] value
@@ -296,7 +280,6 @@ NullableRefTypes.cs:
296280
# 65| 31: [Field] items2
297281
# 65| -1: [TypeMention] Generic<MyClass, MyClass, IDisposable, MyClass>
298282
# 65| 1: [TypeMention] MyClass
299-
# 65| 1: [TypeMention] MyClass
300283
# 65| 2: [TypeMention] MyClass
301284
# 65| 3: [TypeMention] IDisposable
302285
# 65| 4: [TypeMention] MyClass
@@ -314,7 +297,6 @@ NullableRefTypes.cs:
314297
# 73| 0: [LocalVariableDeclStmt] ... ...;
315298
# 73| 0: [LocalVariableDeclAndInitExpr] MyClass x = ...
316299
# 73| -1: [TypeMention] MyClass
317-
# 73| 1: [TypeMention] MyClass
318300
# 73| 0: [LocalVariableAccess] access to local variable x
319301
# 73| 1: [NullLiteral] null
320302
# 74| 1: [ExprStmt] ...;
@@ -334,7 +316,6 @@ NullableRefTypes.cs:
334316
# 84| 0: [LocalVariableDeclStmt] ... ...;
335317
# 84| 0: [LocalVariableDeclAndInitExpr] String x = ...
336318
# 84| -1: [TypeMention] string
337-
# 84| 1: [TypeMention] string
338319
# 84| 0: [LocalVariableAccess] access to local variable x
339320
# 84| 1: [StringLiteral] "source"
340321
# 85| 1: [LocalVariableDeclStmt] ... ...;
@@ -363,7 +344,6 @@ NullableRefTypes.cs:
363344
# 93| 0: [LocalVariableDeclStmt] ... ...;
364345
# 93| 0: [LocalVariableDeclAndInitExpr] String x = ...
365346
# 93| -1: [TypeMention] string
366-
# 93| 1: [TypeMention] string
367347
# 93| 0: [LocalVariableAccess] access to local variable x
368348
# 93| 1: [StringLiteral] "source"
369349
# 94| 1: [LocalVariableDeclStmt] ... ...;
@@ -396,15 +376,13 @@ NullableRefTypes.cs:
396376
#-----| 2: (Parameters)
397377
# 104| 0: [Parameter] r
398378
# 104| -1: [TypeMention] MyClass
399-
# 104| 1: [TypeMention] MyClass
400379
# 104| 4: [RefExpr] ref ...
401380
# 104| 0: [ParameterAccess] access to parameter r
402381
# 105| 7: [Method] ReturnsRef3
403382
# 105| -1: [TypeMention] MyClass
404383
#-----| 2: (Parameters)
405384
# 105| 0: [Parameter] r
406385
# 105| -1: [TypeMention] MyClass
407-
# 105| 1: [TypeMention] MyClass
408386
# 105| 4: [RefExpr] ref ...
409387
# 105| 0: [ParameterAccess] access to parameter r
410388
# 106| 8: [Method] ReturnsRef4
@@ -426,7 +404,6 @@ NullableRefTypes.cs:
426404
#-----| 2: (Parameters)
427405
# 108| 0: [Parameter] r
428406
# 108| -1: [TypeMention] MyClass
429-
# 108| 1: [TypeMention] MyClass
430407
# 108| 4: [RefExpr] ref ...
431408
# 108| 0: [ParameterAccess] access to parameter r
432409
# 110| 11: [Method] Parameters1
@@ -436,12 +413,10 @@ NullableRefTypes.cs:
436413
# 110| -1: [TypeMention] MyClass
437414
# 110| 1: [Parameter] p2
438415
# 110| -1: [TypeMention] MyClass
439-
# 110| 1: [TypeMention] MyClass
440416
# 110| 4: [ThrowExpr] throw ...
441417
# 110| 0: [NullLiteral] null
442418
# 112| 12: [Field] Property
443419
# 112| -1: [TypeMention] MyClass
444-
# 112| 1: [TypeMention] MyClass
445420
# 113| 13: [IndexerProperty] RefProperty
446421
# 113| -1: [TypeMention] MyClass
447422
# 113| 3: [Getter] get_RefProperty
@@ -465,23 +440,18 @@ NullableRefTypes.cs:
465440
# 121| 1: [TypeMention] MyStruct
466441
# 123| 9: [Field] e
467442
# 123| -1: [TypeMention] MyClass
468-
# 123| 1: [TypeMention] MyClass
469443
# 124| 10: [Field] f
470444
# 124| -1: [TypeMention] MyClass[]
471445
# 124| 1: [TypeMention] MyClass
472-
# 124| 1: [TypeMention] MyClass
473446
# 125| 11: [Field] g
474447
# 125| -1: [TypeMention] MyClass[]
475448
# 125| 1: [TypeMention] MyClass
476449
# 126| 12: [Field] h
477450
# 126| -1: [TypeMention] MyClass[]
478451
# 126| 1: [TypeMention] MyClass
479-
# 126| 1: [TypeMention] MyClass
480452
# 128| 13: [Field] i
481453
# 128| -1: [TypeMention] MyClass[,][][,,]
482454
# 128| 1: [TypeMention] MyClass
483-
# 128| 1: [TypeMention] MyClass
484-
# 128| 1: [TypeMention] MyClass
485455
# 129| 14: [Field] j
486456
# 129| -1: [TypeMention] MyClass[,,][,][]
487457
# 129| 1: [TypeMention] MyClass
@@ -491,9 +461,6 @@ NullableRefTypes.cs:
491461
# 131| 16: [Field] l
492462
# 131| -1: [TypeMention] MyClass[,,][,,,][][,]
493463
# 131| 1: [TypeMention] MyClass
494-
# 131| 1: [TypeMention] MyClass
495-
# 131| 1: [TypeMention] MyClass
496-
# 131| 1: [TypeMention] MyClass
497464
# 136| [Class] ToStringWithTypes2
498465
# 138| 5: [Field] a
499466
# 138| -1: [TypeMention] MyStruct?
@@ -511,23 +478,18 @@ NullableRefTypes.cs:
511478
# 141| 1: [TypeMention] MyStruct
512479
# 143| 9: [Field] e
513480
# 143| -1: [TypeMention] MyClass
514-
# 143| 1: [TypeMention] MyClass
515481
# 144| 10: [Field] f
516482
# 144| -1: [TypeMention] MyClass[]
517483
# 144| 1: [TypeMention] MyClass
518-
# 144| 1: [TypeMention] MyClass
519484
# 145| 11: [Field] g
520485
# 145| -1: [TypeMention] MyClass[]
521486
# 145| 1: [TypeMention] MyClass
522487
# 146| 12: [Field] h
523488
# 146| -1: [TypeMention] MyClass[]
524489
# 146| 1: [TypeMention] MyClass
525-
# 146| 1: [TypeMention] MyClass
526490
# 148| 13: [Field] i
527491
# 148| -1: [TypeMention] MyClass[,][][,,]
528492
# 148| 1: [TypeMention] MyClass
529-
# 148| 1: [TypeMention] MyClass
530-
# 148| 1: [TypeMention] MyClass
531493
# 149| 14: [Field] j
532494
# 149| -1: [TypeMention] MyClass[,,][,][]
533495
# 149| 1: [TypeMention] MyClass
@@ -537,9 +499,6 @@ NullableRefTypes.cs:
537499
# 151| 16: [Field] l
538500
# 151| -1: [TypeMention] MyClass[,,][,,,][][,]
539501
# 151| 1: [TypeMention] MyClass
540-
# 151| 1: [TypeMention] MyClass
541-
# 151| 1: [TypeMention] MyClass
542-
# 151| 1: [TypeMention] MyClass
543502
# 154| [Class] DisabledNullability
544503
# 156| 5: [Field] f1
545504
# 156| -1: [TypeMention] MyClass
@@ -565,13 +524,11 @@ NullableRefTypes.cs:
565524
# 171| [Class] TestNullableFlowStates
566525
# 173| 5: [Method] MaybeNull
567526
# 173| -1: [TypeMention] string
568-
# 173| 1: [TypeMention] string
569527
# 175| 6: [Method] Check
570528
# 175| -1: [TypeMention] Void
571529
#-----| 2: (Parameters)
572530
# 175| 0: [Parameter] isNull
573531
# 175| -1: [TypeMention] string
574-
# 175| 1: [TypeMention] string
575532
# 177| 7: [Method] Count
576533
# 177| -1: [TypeMention] int
577534
# 179| 8: [Method] LoopUnrolling
@@ -580,7 +537,6 @@ NullableRefTypes.cs:
580537
# 181| 0: [LocalVariableDeclStmt] ... ...;
581538
# 181| 0: [LocalVariableDeclAndInitExpr] String x = ...
582539
# 181| -1: [TypeMention] string
583-
# 181| 1: [TypeMention] string
584540
# 181| 0: [LocalVariableAccess] access to local variable x
585541
# 181| 1: [MethodCall] call to method MaybeNull
586542
# 183| 1: [ExprStmt] ...;
@@ -610,7 +566,6 @@ NullableRefTypes.cs:
610566
# 195| 0: [LocalVariableDeclStmt] ... ...;
611567
# 195| 0: [LocalVariableDeclAndInitExpr] String y = ...
612568
# 195| -1: [TypeMention] string
613-
# 195| 1: [TypeMention] string
614569
# 195| 0: [LocalVariableAccess] access to local variable y
615570
# 195| 1: [MethodCall] call to method MaybeNull
616571
# 197| 1: [TryStmt] try {...} ...
@@ -631,7 +586,6 @@ NullableRefTypes.cs:
631586
#-----| 2: (Parameters)
632587
# 209| 0: [Parameter] o
633588
# 209| -1: [TypeMention] object
634-
# 209| 1: [TypeMention] object
635589
# 210| 4: [BlockStmt] {...}
636590
# 211| 0: [LocalVariableDeclStmt] ... ...;
637591
# 211| 0: [LocalVariableDeclAndInitExpr] Type t = ...
@@ -647,20 +601,17 @@ NullableRefTypes.cs:
647601
#-----| 2: (Parameters)
648602
# 215| 0: [Parameter] list
649603
# 215| -1: [TypeMention] List<String>
650-
# 215| 1: [TypeMention] List<String>
651-
# 215| 1: [TypeMention] string
604+
# 215| 1: [TypeMention] string
652605
# 216| 4: [BlockStmt] {...}
653606
# 217| 0: [LocalVariableDeclStmt] ... ...;
654607
# 217| 0: [LocalVariableDeclAndInitExpr] String a = ...
655608
# 217| -1: [TypeMention] string
656-
# 217| 1: [TypeMention] string
657609
# 217| 0: [LocalVariableAccess] access to local variable a
658610
# 217| 1: [FieldAccess] access to field Field
659611
# 217| -1: [MethodCall] call to method GetSelf
660612
# 218| 1: [LocalVariableDeclStmt] ... ...;
661613
# 218| 0: [LocalVariableDeclAndInitExpr] String b = ...
662614
# 218| -1: [TypeMention] string
663-
# 218| 1: [TypeMention] string
664615
# 218| 0: [LocalVariableAccess] access to local variable b
665616
# 218| 1: [IndexerCall] access to indexer
666617
# 218| -1: [ParameterAccess] access to parameter list
@@ -680,7 +631,6 @@ NullableRefTypes.cs:
680631
# 220| -1: [MethodCall] call to method GetSelf
681632
# 223| 12: [Method] GetSelf
682633
# 223| -1: [TypeMention] TestNullableFlowStates
683-
# 223| 1: [TypeMention] TestNullableFlowStates
684634
# 225| 13: [Field] Field
685635
# 225| -1: [TypeMention] string
686636
StaticLocalFunctions.cs:

0 commit comments

Comments
 (0)