Skip to content
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

Track nullable state in try statements #31082

Merged
merged 5 commits into from
Nov 13, 2018

Conversation

gafter
Copy link
Member

@gafter gafter commented Nov 9, 2018

Fixes #30561

@gafter gafter added this to the 16.0.P2 milestone Nov 9, 2018
@gafter gafter self-assigned this Nov 9, 2018
@gafter gafter requested review from cston and a team November 9, 2018 22:58
@333fred
Copy link
Member

333fred commented Nov 12, 2018

@gafter looks like there are some merge conflicts to resolve. #Resolved

@333fred
Copy link
Member

333fred commented Nov 12, 2018

Done review pass (commit 1) #Resolved

@gafter
Copy link
Member Author

gafter commented Nov 12, 2018

@cston @333fred All comments have been addressed. Do you have any further comments? #Resolved

Copy link
Member

@333fred 333fred left a comment

Choose a reason for hiding this comment

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

LGTM (commit 4)

}
catch (System.Exception)
{
_ = s.Length; // warning 1
Copy link
Member

@jcouv jcouv Nov 13, 2018

Choose a reason for hiding this comment

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

_ = s.Length; // warning 1 [](start = 12, length = 26)

test idea: if we did s = string.Empty; here, I assume that warning 2 disappears. #Closed

Copy link
Member Author

Choose a reason for hiding this comment

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

No. It is still possible for "something" to be thrown that isn't caught, even though catching it is not expressible. The language cannot express catching "everything".


In reply to: 232855231 [](ancestors = 232855231)

Copy link
Member

Choose a reason for hiding this comment

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

Even catch { } will not catch everything?


In reply to: 232871216 [](ancestors = 232871216,232855231)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, catch {} will catch "everything" thrown. However, it would be incorrect to treat that as a special case that suppresses the warning in the finally, as it is still possible for s to be null in the finally (because an exception could occur on entry to the catch clause).


In reply to: 233190864 [](ancestors = 233190864,232871216,232855231)

@jcouv jcouv self-assigned this Nov 13, 2018
MayThrow();
s = string.Empty;
}
catch (System.Exception)
Copy link
Member

@jcouv jcouv Nov 13, 2018

Choose a reason for hiding this comment

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

catch (System.Exception) [](start = 8, length = 24)

test ideas:
Consider testing with catch that catches some other exception type? I think this will not affect the analysis, but when I saw the tests, I assumed that System.Exception was handled specially (as a catch all).

Consider some tests with multiple catch blocks.

Also, maybe some nested try/catch scenarios? #Resolved

Copy link
Member Author

Choose a reason for hiding this comment

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

System.Exception is not handled specially.

Adding a test for multiple catch blocks.

Not adding nested scenarios because it is all compositional. There is no special handling of nesting for try blocks. Trying to test nesting of constructs would cause an explosion of the tests.


In reply to: 232864656 [](ancestors = 232864656)

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, I will add some nested scenarios. There is nesting-specific state handling code.


In reply to: 233151144 [](ancestors = 233151144,232864656)

Optional<TLocalState> oldTryState = _tryState;
_tryState = AllBitsSet();
VisitTryBlock(tryBlock, node, ref tryState);
var tts = _tryState.Value;
Copy link
Member

@jcouv jcouv Nov 13, 2018

Choose a reason for hiding this comment

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

tts [](start = 20, length = 3)

nit: even though this is moving existing code, consider giving tts and ots proper names. #Resolved

Copy link
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 4)

Do we need to update the speclet for nullable to reflect this change, or is this already implied by the base spec?

@gafter gafter requested review from cston and agocke November 13, 2018 17:30
}

[Fact, WorkItem(30561, "https://github.com/dotnet/roslyn/issues/30561")]
public void SetNullableStateInNestedTry_01()
Copy link
Member Author

@gafter gafter Nov 13, 2018

Choose a reason for hiding this comment

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

SetNullableStateInNestedTry_01 [](start = 20, length = 30)

@jcouv: this tests nested try statements as suggested. #Resolved

Copy link
Member

Choose a reason for hiding this comment

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

Got it. Thanks!


In reply to: 233200599 [](ancestors = 233200599)

@gafter
Copy link
Member Author

gafter commented Nov 13, 2018

Do we need to update the speclet for nullable to reflect this change, or is this already implied by the base spec?

The precise flow-analysis rules for nullable analysis are not specified, so there isn't anything to update.

@gafter
Copy link
Member Author

gafter commented Nov 13, 2018

@jaredpar For approval, please

@jaredpar
Copy link
Member

approved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants