Skip to content

Commit 3b56e35

Browse files
authored
Merge pull request github#5277 from tamasvajk/feature/fix-name-resolution
Fix method name resolution issue with nullable suppression
2 parents 208a374 + b3d6d0c commit 3b56e35

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Name.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ public static Expression Create(ExpressionNodeInfo info)
1313

1414
var target = symbolInfo.Symbol;
1515

16+
if (target == null &&
17+
symbolInfo.CandidateReason == CandidateReason.OverloadResolutionFailure &&
18+
info.Node.Parent.IsKind(SyntaxKind.SuppressNullableWarningExpression))
19+
{
20+
target = symbolInfo.CandidateSymbols.FirstOrDefault();
21+
}
22+
1623
if (target == null && symbolInfo.CandidateReason == CandidateReason.OverloadResolutionFailure)
1724
{
1825
// The expression is probably a cast
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading;
4+
5+
class MyClass2
6+
{
7+
private static WaitCallback? s_signalMethod;
8+
private static WaitCallback SignalMethod => EnsureInitialized(ref s_signalMethod, () => new WaitCallback(M1!));
9+
10+
public static T EnsureInitialized<T>(ref T target, System.Func<T> valueFactory) where T : class { return target = valueFactory(); }
11+
12+
static void M1(object state)
13+
{
14+
}
15+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
suppressNullableWarnings
2+
| NameResolutionSuppressNullable.cs:8:110:8:112 | ...! | NameResolutionSuppressNullable.cs:8:110:8:111 | access to method M1 |
23
| NullableRefTypes.cs:85:20:85:21 | ...! | NullableRefTypes.cs:85:20:85:20 | access to local variable x |
34
| NullableRefTypes.cs:86:13:86:14 | ...! | NullableRefTypes.cs:86:13:86:13 | access to local variable x |
45
| NullableRefTypes.cs:88:13:88:14 | ...! | NullableRefTypes.cs:88:13:88:13 | access to local variable x |
@@ -206,6 +207,7 @@ returnTypes
206207
| NullableRefTypes.cs:215:10:215:20 | ElementTest | Void! |
207208
| NullableRefTypes.cs:223:48:223:54 | GetSelf | TestNullableFlowStates? |
208209
methodTypeArguments
210+
| NameResolutionSuppressNullable.cs:10:21:10:40 | EnsureInitialized | 0 | WaitCallback? |
209211
| NullableRefTypes.cs:51:12:51:15 | Q | 0 | MyClass! |
210212
| NullableRefTypes.cs:51:12:51:15 | Q | 0 | MyClass? |
211213
| NullableRefTypes.cs:67:10:67:21 | GenericFn | 0 | MyClass! |

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,42 @@ DefaultInterfaceMethods.cs:
121121
# 26| 7: [Method] Greet
122122
# 26| -1: [TypeMention] Void
123123
# 26| 4: [BlockStmt] {...}
124+
NameResolutionSuppressNullable.cs:
125+
# 5| [Class] MyClass2
126+
# 7| 5: [Field] s_signalMethod
127+
# 7| -1: [TypeMention] WaitCallback
128+
# 8| 6: [Property] SignalMethod
129+
# 8| -1: [TypeMention] WaitCallback
130+
# 8| 3: [Getter] get_SignalMethod
131+
# 8| 4: [MethodCall] call to method EnsureInitialized
132+
# 8| 0: [FieldAccess] access to field s_signalMethod
133+
# 8| 1: [LambdaExpr] (...) => ...
134+
# 8| 4: [ExplicitDelegateCreation] delegate creation of type WaitCallback
135+
# 8| -1: [TypeMention] WaitCallback
136+
# 8| 0: [SuppressNullableWarningExpr] ...!
137+
# 8| 0: [MethodAccess] access to method M1
138+
# 10| 7: [Method] EnsureInitialized
139+
# 10| -1: [TypeMention] T
140+
#-----| 1: (Type parameters)
141+
# 10| 0: [TypeParameter] T
142+
#-----| 2: (Parameters)
143+
# 10| 0: [Parameter] target
144+
# 10| -1: [TypeMention] T
145+
# 10| 1: [Parameter] valueFactory
146+
# 10| -1: [TypeMention] Func<T>
147+
# 10| 1: [TypeMention] T
148+
# 10| 4: [BlockStmt] {...}
149+
# 10| 0: [ReturnStmt] return ...;
150+
# 10| 0: [AssignExpr] ... = ...
151+
# 10| 0: [ParameterAccess] access to parameter target
152+
# 10| 1: [DelegateCall] delegate call
153+
# 10| -1: [ParameterAccess] access to parameter valueFactory
154+
# 12| 9: [Method] M1
155+
# 12| -1: [TypeMention] Void
156+
#-----| 2: (Parameters)
157+
# 12| 0: [Parameter] state
158+
# 12| -1: [TypeMention] object
159+
# 13| 4: [BlockStmt] {...}
124160
NullCoalescingAssignment.cs:
125161
# 3| [Class] NullCoalescingAssignment
126162
# 5| 5: [Method] NullCoalescing

0 commit comments

Comments
 (0)