Replies: 8 comments
-
You don't need any special text for local functions. All of its analyses will get the same result for reach ability. |
Beta Was this translation helpful? Give feedback.
-
@gafter I do not disagree outright. However, I do note the following issues:
I'm hesitant to remove the reference to local functions from the text prior to reviewing an updated form of the specification (1). However, (2) suggests that my wording may need to be revised somewhat. |
Beta Was this translation helpful? Give feedback.
-
@sharwell, that left my brain in a knot. Can you give examples of reachability vs. definite assignment? |
Beta Was this translation helpful? Give feedback.
-
@paulomorgado Don't brains always look like knots? Examples of reachability:
Examples of definite assignment:
As it turns out, the two concepts are described separately in the language specification, but reachability is implemented as a specific subset of the functionality of definite assignment. Specifically, paths in code which would result in unreachable code afterwards have the definite assignment behavior "all variables are definitely assigned after [whatever it is]". You can see this in cases like the following: public void Example()
{
int x;
//goto SkipTheError; // <-- uncomment this line and the compile error goes away
Console.WriteLine($"It would be an error to try and print {x}");
SkipTheError:
return;
} It happens to also be the case that definite assignment covers many special cases which reachability analysis does not. For example, reachability analysis doesn't cover many subexpressions. This can be seen in some bizarre cases like the following. Reachability analysis in the language says that both the true and false paths of the switch ((int)x)
{
case 1:
int y;
if ((bool?)null ?? throw new Exception())
{
Console.WriteLine(y);
}
else
{
Console.WriteLine(y);
}
default:
break;
} |
Beta Was this translation helpful? Give feedback.
-
I think I got it! So, to use definite assignment to determine reachability would be like having a phantom variable that is assigned before the statement and test for definite assignment. If it's not definitely assigned, than the statement is not reachable. Is that it? |
Beta Was this translation helpful? Give feedback.
-
@paulomorgado Close, except the phantom variable is declared but not assigned to, and you are testing to see if it is definitely assigned to determine that something is not reachable. |
Beta Was this translation helpful? Give feedback.
-
Do you know why only the first line here has an unreachable-code warning? void A() { throw new Exception(); return; }
void B() { return; throw new Exception(); } |
Beta Was this translation helpful? Give feedback.
-
The compiler has an exception (does not report unreachable code warnings) for the following three statement types:
|
Beta Was this translation helpful? Give feedback.
-
As pointed out by @gafter in #656 (comment), the language specification currently describes reachability separately from definite assignment. The separate definition of reachability ends up making the language specification more complex, while at the same time limiting the ability of conforming compilers to report warnings related to reachability. The underlying need for the limitation appears to be due to the following wording (emphasis mine):
I propose the following pair of changes to the C# language:
Relax the warning requirement related to reachability, as follows:
Eliminate the current description of reachability (which crosses into the descriptions of several statements), and replace it with the following (credit to @gafter for the original text, which I have modified to account for expressions and local functions):
While simultaneously simplifying the language and increasing the power of reachability analysis in C#, the above changes allow a conforming implementation to preserve compatibility for existing code bases. Should a complementing feature (e.g. warning waves or
/features:strict
) allow for breaking behavioral changes with respect to compiler warnings, these changes would enable an implementation to provide additional information regarding unreachable statements and thus reduce the likelihood of reachability-related programming errors to enter production.Beta Was this translation helpful? Give feedback.
All reactions