Skip to content

Suggestion: strict mode should detect yield-returning inside a lock #46004

@WizardBrony

Description

@WizardBrony

For example, the following code fails:

public class C
{
    private readonly object _lock = new object();

    public IEnumerable<int> Iterator()
    {
        lock (_lock)
            yield return 0;
    }
}

var c = new C();
foreach (var i in c.Iterator())
    await Task.Yield();

The compiler should generate an error (or at least a warning) informing the user that the currently-executing thread after yield-returning may not be the same thread that the lock was taken on. Considering that the compiler already prevents awaiting in the body of a lock statement (Error CS1996) for the same reason, I feel like this addition would follow suit.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions