From 56bf8bc9af83cae81674b2212fa5deacfa012518 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Fri, 24 Nov 2023 14:10:38 -0500 Subject: [PATCH 1/2] Add example for unreachable code Fixes #937 I admit this rule confused me, and I asked to add the example. After reading the updated, I'm ambivalent about adding the example. I don't want to move it into the bullet list, as that breaks the flow even more. Let's discuss if it's needed at the meeting. --- standard/variables.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/standard/variables.md b/standard/variables.md index 744b61945..6b157c33f 100644 --- a/standard/variables.md +++ b/standard/variables.md @@ -343,6 +343,19 @@ The definite-assignment state of *v* on the control flow transfer to a reachable - If *v* is a pattern variable declared in the *switch_label*: “definitely assigned”. - Otherwise, the state of *v* is the same as the stat of *v* after *expr*. +> *Example*: The third rule eliminates the need for the compiler to warn if an unassigned variable is accessed in unreachable code. The state of *b* is "definitely assigned" in the unreachable switch label `case 2 when b`. +> +> ```csharp +> bool b; +> switch (1) +> { +> case 2 when b: // b is definitely assigned here. +> break; +> } +> ``` +> +> *end example* + A consequence of these rules is that a pattern variable declared in a *switch_label* will be “not definitely assigned” in the statements of its switch section if it is not the only reachable switch label in its section. > *Example*: From dd8aa08986762faba9a31b474a1d92b6a55e7cdc Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 27 Nov 2023 10:49:03 -0500 Subject: [PATCH 2/2] respond to feedback --- standard/variables.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/standard/variables.md b/standard/variables.md index 6b157c33f..768a8e286 100644 --- a/standard/variables.md +++ b/standard/variables.md @@ -343,7 +343,7 @@ The definite-assignment state of *v* on the control flow transfer to a reachable - If *v* is a pattern variable declared in the *switch_label*: “definitely assigned”. - Otherwise, the state of *v* is the same as the stat of *v* after *expr*. -> *Example*: The third rule eliminates the need for the compiler to warn if an unassigned variable is accessed in unreachable code. The state of *b* is "definitely assigned" in the unreachable switch label `case 2 when b`. +> *Example*: The third rule eliminates the need for the compiler to issue an error if an unassigned variable is accessed in unreachable code. The state of *b* is "definitely assigned" in the unreachable switch label `case 2 when b`. > > ```csharp > bool b;