-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Support null-conditional operator when the return value is a pointer type #80694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
9bbba4a
7384206
807300e
5de452e
b5ddcc3
f011de2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2892,5 +2892,167 @@ static void M(bool b, C? c1, C? c2) | |
""", | ||
graph, symbol); | ||
} | ||
|
||
[Fact] | ||
|
||
public void PointerReturnType_Simple() | ||
{ | ||
var source = """ | ||
public unsafe class A | ||
{ | ||
public byte* Ptr = null; | ||
} | ||
|
||
unsafe class Test | ||
{ | ||
static void M1(A a) | ||
{ | ||
byte* ptr = a?.Ptr; | ||
} | ||
|
||
static void M2(A a) | ||
{ | ||
var result = a?.Ptr; | ||
} | ||
} | ||
"""; | ||
var comp = CreateCompilation(source, options: TestOptions.UnsafeDebugDll); | ||
comp.VerifyEmitDiagnostics(); | ||
} | ||
|
||
[Fact] | ||
public void PointerReturnType_WithUsage() | ||
{ | ||
var source = """ | ||
using System; | ||
|
||
public unsafe class A | ||
{ | ||
public byte* Ptr = null; | ||
} | ||
|
||
unsafe class Test | ||
{ | ||
static void Main() | ||
{ | ||
var a = new A(); | ||
byte* ptr1 = a?.Ptr; | ||
Console.Write(ptr1 == null ? "null" : "not null"); | ||
|
||
a = null; | ||
byte* ptr2 = a?.Ptr; | ||
Console.Write(ptr2 == null ? " null" : " not null"); | ||
} | ||
} | ||
"""; | ||
var verifier = CompileAndVerify(source, options: TestOptions.UnsafeDebugExe, verify: Verification.Skipped, expectedOutput: "null null"); | ||
verifier.VerifyDiagnostics(); | ||
} | ||
|
||
[Fact] | ||
public void PointerReturnType_IntPointer() | ||
{ | ||
var source = """ | ||
unsafe class Test | ||
{ | ||
public int* Value = null; | ||
|
||
static void M(Test t) | ||
{ | ||
int* p = t?.Value; | ||
var v = t?.Value; | ||
} | ||
} | ||
"""; | ||
var comp = CreateCompilation(source, options: TestOptions.UnsafeDebugDll); | ||
comp.VerifyEmitDiagnostics(); | ||
} | ||
|
||
[Fact] | ||
public void PointerReturnType_VoidPointer() | ||
{ | ||
var source = """ | ||
unsafe class Test | ||
{ | ||
public void* Data = null; | ||
|
||
static void M(Test t) | ||
{ | ||
void* p = t?.Data; | ||
var d = t?.Data; | ||
} | ||
} | ||
"""; | ||
var comp = CreateCompilation(source, options: TestOptions.UnsafeDebugDll); | ||
comp.VerifyEmitDiagnostics(); | ||
} | ||
|
||
[Fact] | ||
public void PointerReturnType_Chained() | ||
{ | ||
var source = """ | ||
unsafe struct Node | ||
{ | ||
public Node* Next; | ||
public int Value; | ||
|
||
public Node(int v) { Next = null; Value = v; } | ||
} | ||
|
||
unsafe class Test | ||
{ | ||
public Node* Head = null; | ||
|
||
static void M(Test t) | ||
{ | ||
Node* n1 = t?.Head; | ||
} | ||
} | ||
"""; | ||
var comp = CreateCompilation(source, options: TestOptions.UnsafeDebugDll); | ||
comp.VerifyEmitDiagnostics(); | ||
} | ||
|
||
[Fact] | ||
public void PointerReturnType_StatementContext() | ||
{ | ||
var source = """ | ||
public unsafe class A | ||
{ | ||
public byte* Ptr = null; | ||
public void DoSomething() { } | ||
} | ||
|
||
unsafe class Test | ||
{ | ||
static void M(A a) | ||
{ | ||
a?.DoSomething(); // Statement context - method call works | ||
} | ||
} | ||
"""; | ||
var comp = CreateCompilation(source, options: TestOptions.UnsafeDebugDll); | ||
comp.VerifyEmitDiagnostics(); | ||
} | ||
|
||
[Fact] | ||
public void FunctionPointerReturnType_StillRejected() | ||
{ | ||
var source = """ | ||
unsafe class Test | ||
{ | ||
public delegate*<int, void> FPtr = null; | ||
|
||
static void M(Test t) | ||
{ | ||
var f = t?.FPtr; | ||
} | ||
} | ||
"""; | ||
var comp = CreateCompilation(source, options: TestOptions.UnsafeDebugDll); | ||
comp.VerifyEmitDiagnostics( | ||
// (7,19): error CS8978: 'delegate*<int, void>' cannot be made nullable. | ||
// var f = t?.FPtr; | ||
Diagnostic(ErrorCode.ERR_CannotBeMadeNullable, ".FPtr").WithArguments("delegate*<int, void>").WithLocation(7, 19)); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dotnet/roslyn-compiler i think this is correct. The spec says this: https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/expressions.md#1177-null-conditional-member-access
So my reading from teh spec is that this should be legal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/dotnet/csharpstandard/blob/draft-v8/standard/expressions.md#1288-null-conditional-member-access has revised the wording somewhat but I think Cyrus's interpretation still holds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The latest docs show this: