-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Let null
contribute its nullability to method type re-inference
#43571
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4045,6 +4045,147 @@ void M<T>(T t) | |
); | ||
} | ||
|
||
[Fact, WorkItem(43536, "https://github.com/dotnet/roslyn/issues/43536")] | ||
public void MethodTypeInference_WithDefaultAndNull() | ||
{ | ||
var source = @" | ||
class C | ||
{ | ||
void M(string s) | ||
{ | ||
Infer(s, default).ToString(); // 1 | ||
Infer(s, null).ToString(); // 2 | ||
} | ||
|
||
T Infer<T>(T t1, T t2) => throw null!; | ||
}"; | ||
|
||
var comp = CreateNullableCompilation(source); | ||
comp.VerifyDiagnostics( | ||
// (6,9): warning CS8602: Dereference of a possibly null reference. | ||
// Infer(s, default).ToString(); // 1 | ||
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "Infer(s, default)").WithLocation(6, 9), | ||
// (7,9): warning CS8602: Dereference of a possibly null reference. | ||
// Infer(s, null).ToString(); // 2 | ||
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "Infer(s, null)").WithLocation(7, 9) | ||
); | ||
} | ||
|
||
[Fact, WorkItem(43536, "https://github.com/dotnet/roslyn/issues/43536")] | ||
public void MethodTypeInference_WithNull_TwoConversions() | ||
{ | ||
var source = @" | ||
class C | ||
{ | ||
void M(string s) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
M2(null); // 1 | ||
} | ||
|
||
void M2(Optional<object> o) => throw null!; | ||
} | ||
|
||
public readonly struct Optional<T> | ||
{ | ||
public Optional(T value) => throw null!; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Not used. |
||
public static implicit operator Optional<T>(T value) => throw null!; | ||
} | ||
"; | ||
var comp = CreateNullableCompilation(source); | ||
comp.VerifyDiagnostics( | ||
// (6,12): warning CS8625: Cannot convert null literal to non-nullable reference type. | ||
// M2(null); // 1 | ||
Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(6, 12) | ||
); | ||
} | ||
|
||
[Fact, WorkItem(43536, "https://github.com/dotnet/roslyn/issues/43536")] | ||
public void MethodTypeInference_WithLambdaReturningDefaultAndNull() | ||
{ | ||
var source = @" | ||
class C | ||
{ | ||
void M(string s1, string? s2) | ||
{ | ||
Infer(s1, () => default); // 1 | ||
Infer(s1, () => null); // 2 | ||
|
||
Infer(s2, () => default); | ||
Infer(s2, () => null); | ||
} | ||
|
||
T Infer<T>(T t1, System.Func<T> t2) => throw null!; | ||
}"; | ||
|
||
var comp = CreateCompilationWithIndexAndRange(source, options: WithNonNullTypesTrue()); | ||
comp.VerifyDiagnostics( | ||
// (6,25): warning CS8603: Possible null reference return. | ||
// Infer(s1, () => default); // 1 | ||
Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default").WithLocation(6, 25), | ||
// (7,25): warning CS8603: Possible null reference return. | ||
// Infer(s1, () => null); // 2 | ||
Diagnostic(ErrorCode.WRN_NullReferenceReturn, "null").WithLocation(7, 25) | ||
); | ||
} | ||
|
||
[Fact, WorkItem(43536, "https://github.com/dotnet/roslyn/issues/43536")] | ||
public void MethodTypeInference_WithNullAndNestedNullability() | ||
{ | ||
var source = @" | ||
class C<U> | ||
{ | ||
void M(C<string> c1, C<string?> c2) | ||
{ | ||
Infer(c1, default).ToString(); | ||
Infer(c1, null).ToString(); | ||
|
||
Infer(c2, default).ToString(); // 1 | ||
Infer(c2, null).ToString(); // 2 | ||
} | ||
|
||
T Infer<T>(C<T>? t1, C<T>? t2) => throw null!; | ||
}"; | ||
|
||
var comp = CreateCompilationWithIndexAndRange(source, options: WithNonNullTypesTrue()); | ||
comp.VerifyDiagnostics( | ||
// (9,9): warning CS8602: Dereference of a possibly null reference. | ||
// Infer(c2, default).ToString(); // 1 | ||
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "Infer(c2, default)").WithLocation(9, 9), | ||
// (10,9): warning CS8602: Dereference of a possibly null reference. | ||
// Infer(c2, null).ToString(); // 2 | ||
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "Infer(c2, null)").WithLocation(10, 9) | ||
); | ||
} | ||
|
||
[Fact, WorkItem(43536, "https://github.com/dotnet/roslyn/issues/43536")] | ||
public void MethodTypeInference_WithNullAndNestedNullability2() | ||
{ | ||
var source = @" | ||
class C<U> | ||
{ | ||
void M(string c1, string? c2) | ||
{ | ||
Infer(c1, default).ToString(); | ||
Infer(c1, null).ToString(); | ||
|
||
Infer(c2, default).ToString(); // 1 | ||
Infer(c2, null).ToString(); // 2 | ||
} | ||
|
||
T Infer<T>(T t1, C<T>? t2) => throw null!; | ||
}"; | ||
|
||
var comp = CreateCompilationWithIndexAndRange(source, options: WithNonNullTypesTrue()); | ||
comp.VerifyDiagnostics( | ||
// (9,9): warning CS8602: Dereference of a possibly null reference. | ||
// Infer(c2, default).ToString(); // 1 | ||
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "Infer(c2, default)").WithLocation(9, 9), | ||
// (10,9): warning CS8602: Dereference of a possibly null reference. | ||
// Infer(c2, null).ToString(); // 2 | ||
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "Infer(c2, null)").WithLocation(10, 9) | ||
); | ||
} | ||
|
||
[Fact] | ||
public void SpeakableInference_MethodTypeInference_WithTuple() | ||
{ | ||
|
@@ -29801,8 +29942,8 @@ public class C | |
{ | ||
public void Main() | ||
{ | ||
Copy(null, out string s); // warn | ||
s/*T:string!*/.ToString(); // ok | ||
Copy(null, out string s); // 1 | ||
s/*T:string?*/.ToString(); // 2 | ||
} | ||
public static void Copy<T>(T key, out T value) => throw null!; | ||
} | ||
|
@@ -29811,9 +29952,12 @@ public void Main() | |
VerifyOutVar(c, "string!"); | ||
c.VerifyTypes(); | ||
c.VerifyDiagnostics( | ||
// (6,14): warning CS8625: Cannot convert null literal to non-nullable reference type. | ||
// Copy(null, out string s); // warn | ||
Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(6, 14) | ||
// (6,24): warning CS8600: Converting null literal or possible null value to non-nullable type. | ||
// Copy(null, out string s); // 1 | ||
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "string s").WithLocation(6, 24), | ||
// (7,9): warning CS8602: Dereference of a possibly null reference. | ||
// s/*T:string?*/.ToString(); // 2 | ||
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s").WithLocation(7, 9) | ||
); | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It feels like the current behavior of
null
is expected: we pass an untypedBoundLiteral
toMethodTypeInferrer
. But the behavior ofdefault
is unexpected: we pass a typedBoundDefaultExpression
toMethodTypeInferrer
where the type was only determined after runningMethodTypeInferrer
in initial binding.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.
Just to clarify, you're saying we should keep the current behavior for
null
(Infer("", null)
would inferstring!
and warn, rather thanstring?
)?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.
Not necessarily. I'm suggesting the arguments passed to
MethodTypeInferrer
inNullableWalker
should match the typed/untyped-ness of the arguments passed toMethodTypeInferrer
in initial binding. Sonull
anddefault
would both be passed as untyped arguments. CouldNullableWalker
fix up the nullability of the inferred type arguments afterMethodTypeInferrer
returns? Such an approach would also need to handle lambda return types so it might get complicated.In reply to: 416076359 [](ancestors = 416076359)