Skip to content

Commit

Permalink
Allow yield return in lock (#43988)
Browse files Browse the repository at this point in the history
* Allow `yield return` in `lock`

* Remove lock statement restriction from iterator yield
  • Loading branch information
jcouv authored Dec 17, 2024
1 parent 469b13a commit 6cc8019
Showing 1 changed file with 0 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ f1_keywords:
- "CS4013"
- "CS8154"
- "CS8176"
- "CS9237"
- "CS9238"
- "CS9239"
helpviewer_keywords:
Expand All @@ -28,7 +27,6 @@ helpviewer_keywords:
- "CS4013"
- "CS8154"
- "CS8176"
- "CS9237"
- "CS9238"
- "CS9239"
ms.date: 07/02/2024
Expand All @@ -51,7 +49,6 @@ That's by design. The text closely matches the text of the compiler error / warn
- [**CS4013**](#ref-safety-in-iterator-methods): *Instance of type cannot be used inside a nested function, query expression, iterator block or async method*
- [**CS8154**](#structure-of-an-iterator-method): *The body cannot be an iterator block because it returns by reference*
- [**CS8176**](#ref-safety-in-iterator-methods): *Iterators cannot have by-reference locals*
- [**CS9237**](#restrictions-on-iterator-methods): *'yield return' should not be used in the body of a lock statement*
- [**CS9238**](#restrictions-on-iterator-methods): *Cannot use 'yield return' in an 'unsafe' block*
- [**CS9239**](#restrictions-on-iterator-methods): *The `&` operator cannot be used on parameters or local variables in iterator methods.*

Expand Down Expand Up @@ -82,15 +79,13 @@ The body of an iterator method must conform to restrictions on the `yield return
- **CS1626**: *Cannot yield a value in the body of a try block with a catch clause*
- **CS1631**: *Cannot yield a value in the body of a catch clause*
- **CS1629**: *Unsafe code may not appear in iterators*
- **CS9237**: *''yield return' should not be used in the body of a lock statement*
- **CS9238**: *Cannot use 'yield return' in an 'unsafe' block*
- **CS9239**: *The `&` operator cannot be used on parameters or local variables in iterator methods.*

These errors indicate that your code violates safety rules because an iterator returns an element and resumes to generate the next element:

- You can't `yield return` from a `catch` or `finally` clause.
- You can't `yield return` from a `try` block with a catch clause.
- You can't `yield return` from inside a `lock` statement block. Doing so can cause deadlocks.
- You can't `yield return` from an `unsafe` block. The context for an iterator creates a nested `safe` block within the enclosing `unsafe` block.
- You can't use the `&` operator to take the address of a variable in an iterator method.

Expand Down

0 comments on commit 6cc8019

Please sign in to comment.