-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
"Requires unsafe
context" error in an unsafe
iterator
#76514
Comments
According to the design note, [/* unsafe context */ A]
unsafe IEnumerable<int> M3(
/* unsafe context */ int*[] x)
{ // safe context
yield return 1;
} |
Well that's sad. I thought my example should be equivalent to |
Thanks @huoyaoyuan for digging up the reference. This was indeed by design: "If an iterator declaration is marked with the unsafe modifier, the signature is in an unsafe scope but the iterator block used to implement that iterator still defines a safe scope." |
The speclet has the motivation in https://github.com/dotnet/csharplang/blob/main/proposals/csharp-13.0/ref-unsafe-in-iterators-async.md#alternatives:
That indeed wasn't considered (fwiw it looks like an overly complicated language rule to me), but feel free to open a discussion for that in https://github.com/dotnet/csharplang. |
Version Used:
Compiler version: '4.12.0-3.24572.7 (dfa7fc6)'. Language version: 13.0.
Steps to Reproduce:
unsafe IEnumerable<int> M() { int* i = null; yield return 1; }
(sharplab)
Diagnostic Id:
Expected Behavior:
Compiles, since the entire method is unsafe so pointers should be allowed without an explicit
unsafe
block.Actual Behavior:
Doesn't compile, you have to use
unsafe { int* i = null; }
.The text was updated successfully, but these errors were encountered: