Update implicity declared type expression nullability:#36078
Update implicity declared type expression nullability:#36078chsienki merged 4 commits intodotnet:masterfrom
Conversation
- When updating the implicit type in a declaration also update the bound type declaration nullability - Add tests
|
@dotnet/roslyn-compiler for review please |
| } | ||
|
|
||
| [Fact] | ||
| public void InferredDeclarationType() |
There was a problem hiding this comment.
InferredDeclarationType [](start = 20, length = 23)
Could you also test other kinds of implicit declarations?
M(out var x);
var (x, y) = p;
foreach (var (x, y) in pCollection) ...
using (var x = d) ...
using var x = d; (probably already covered by your change) #Resolved
There was a problem hiding this comment.
Deconstruction and foreach deconstruction won't work yet (see #35010 which I was working on when I discovered this :))
I'll try the others and see how they fair. #Resolved
Should we do something similar for ref locals? They apparently can have an inferred type as well: public class C {
public void M(string s) {
ref var s2 = ref s;
}
}
``` #Closed
---
Refers to: src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs:1374 in b450f7d. [](commit_id = b450f7da021d5504febbea29f74e0eb823a9212b, deletion_comment = False) |
jcouv
left a comment
There was a problem hiding this comment.
Done with review pass (iteration 1)
| return; | ||
| var/*T:object!*/ y3 = x; | ||
|
|
||
| x = null; |
There was a problem hiding this comment.
Consider also doing this with x2. #Resolved
|
Done review pass (commit 1) |
- Always record type for inferred declarations - Add nullable tests - Update nullable public api tests
|
Expanded tests and fixed ref local case for both API and nullable analysis. @dotnet/roslyn-compiler for second review please. |
|
|
||
| [Fact] | ||
| [WorkItem(35057, "https://github.com/dotnet/roslyn/issues/35057")] | ||
| public void RefLocal() |
There was a problem hiding this comment.
RefLocal [](start = 20, length = 8)
Is this test different from the preceding test?
|
|
||
| if (node.DeclaredTypeOpt != null) | ||
| { | ||
| SetAnalyzedNullability(node.DeclaredTypeOpt, new VisitResult(valueType, type), true); |
There was a problem hiding this comment.
SetAnalyzedNullability(node.DeclaredTypeOpt, new VisitResult(valueType, type), true) [](start = 20, length = 84)
Is this necessary? (What is the observable effect?) #Closed
There was a problem hiding this comment.
In the semantic model, you can query the var of the declaration and get the type. This ensures we correctly report the nullability in that case. #Resolved
| using var /*T:C!*/ y4 = x2; | ||
|
|
||
| using (var /*T:C?*/ y5 = x) { } | ||
| using (var /*T:C?*/ y6 = x) { } |
There was a problem hiding this comment.
x [](start = 33, length = 1)
Should be x2? #Closed
|
|
||
| x = null; | ||
| var /*T:C?*/ y12 = x; | ||
| using var /*T:C?*/ y13 = x; |
There was a problem hiding this comment.
using var /T:C?/ y13 = x; [](start = 8, length = 27)
Duplicate with y3? #Closed
| Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(29, 14) | ||
| ); | ||
| comp.VerifyTypes(); | ||
| } |
There was a problem hiding this comment.
} [](start = 8, length = 1)
Please also test foreach (var ...) (no deconstruction) #Closed
jcouv
left a comment
There was a problem hiding this comment.
LGTM Thanks (iteration 3) modulo an extra test (foreach)
|
LGTM (commit 3), also modulo additional suggestions from Julien. |
- Record nullability for inferred foreach variable - Add foreach test
Small fix for semantic model bug I found working on other correctness bits