Skip to content

Update implicity declared type expression nullability:#36078

Merged
chsienki merged 4 commits intodotnet:masterfrom
chsienki:var_decl_type_fix
Jun 5, 2019
Merged

Update implicity declared type expression nullability:#36078
chsienki merged 4 commits intodotnet:masterfrom
chsienki:var_decl_type_fix

Conversation

@chsienki
Copy link
Copy Markdown
Member

Small fix for semantic model bug I found working on other correctness bits

  • When updating the implicit type in a declaration also update the bound type declaration nullability
  • Add tests

- When updating the implicit type in a declaration also update the bound type declaration nullability
- Add tests
@chsienki chsienki marked this pull request as ready for review May 31, 2019 02:30
@chsienki chsienki requested a review from a team as a code owner May 31, 2019 02:30
@chsienki chsienki requested a review from 333fred May 31, 2019 02:30
@chsienki
Copy link
Copy Markdown
Member Author

@dotnet/roslyn-compiler for review please

}

[Fact]
public void InferredDeclarationType()
Copy link
Copy Markdown
Member

@jcouv jcouv May 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

@chsienki chsienki May 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@jcouv
Copy link
Copy Markdown
Member

jcouv commented May 31, 2019

        if (local.IsRef)

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)

Copy link
Copy Markdown
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done with review pass (iteration 1)

@jcouv jcouv self-assigned this May 31, 2019
return;
var/*T:object!*/ y3 = x;

x = null;
Copy link
Copy Markdown
Member

@333fred 333fred May 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider also doing this with x2. #Resolved

@333fred
Copy link
Copy Markdown
Member

333fred commented May 31, 2019

Done review pass (commit 1)

- Always record type for inferred declarations
- Add nullable tests
- Update nullable public api tests
@chsienki
Copy link
Copy Markdown
Member Author

chsienki commented Jun 3, 2019

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()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Copy Markdown
Contributor

@cston cston Jun 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SetAnalyzedNullability(node.DeclaredTypeOpt, new VisitResult(valueType, type), true) [](start = 20, length = 84)

Is this necessary? (What is the observable effect?) #Closed

Copy link
Copy Markdown
Member Author

@chsienki chsienki Jun 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@jcouv jcouv added this to the 16.2.P3 milestone Jun 3, 2019
using var /*T:C!*/ y4 = x2;

using (var /*T:C?*/ y5 = x) { }
using (var /*T:C?*/ y6 = x) { }
Copy link
Copy Markdown
Member

@jcouv jcouv Jun 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x [](start = 33, length = 1)

Should be x2? #Closed


x = null;
var /*T:C?*/ y12 = x;
using var /*T:C?*/ y13 = x;
Copy link
Copy Markdown
Member

@jcouv jcouv Jun 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using var /T:C?/ y13 = x; [](start = 8, length = 27)

Duplicate with y3? #Closed

Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(29, 14)
);
comp.VerifyTypes();
}
Copy link
Copy Markdown
Member

@jcouv jcouv Jun 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} [](start = 8, length = 1)

Please also test foreach (var ...) (no deconstruction) #Closed

Copy link
Copy Markdown
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM Thanks (iteration 3) modulo an extra test (foreach)

@333fred
Copy link
Copy Markdown
Member

333fred commented Jun 3, 2019

LGTM (commit 3), also modulo additional suggestions from Julien.

- Record nullability for inferred foreach variable
- Add foreach test
@chsienki chsienki merged commit efcbc23 into dotnet:master Jun 5, 2019
@chsienki chsienki deleted the var_decl_type_fix branch June 5, 2019 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants