You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Today we disallow any usage of pointers in iterators, due to the issues involved with those pointers and the associated state moving to the heap. But we allow pointers in async methods (which have similar constraints) with the restriction that awaits can't be used in an unsafe context, e.g. this is allowed:
unsafe(...){
...// no awaits}await ...;unsafe(...){
...// no awaits}
but this is not allowed:
unsafe(...){await ...;}
We could allow unsafe code in iterators, but with a similar constraint: yield is not allowed in unsafe contexts, e.g. this would be allowed:
unsafe(...){
...// no yields}yieldreturn ...;unsafe(...){
...// no yields}
More broadly, do you know of any CLR restrictions about spilling unmanaged pointers across the state machine? AFAIK, that seems the be the main restriction around async and unsafe, so theoretically the compiler could support spilling unmanaged pointers, as long as there are no issues with doing this in the CLR.
More broadly, do you know of any CLR restrictions about spilling unmanaged pointers across the state machine?
Are you asking about enabling not just:
unsafe
{
...// no yields}yieldreturn ...;
unsafe
{
...// no yields}
but also:
unsafe
{
...yield return ...;
...}
?
If so, even if it could be made to work (e.g. storing the pointers into the state machine), I think it would be dangerous, as it'd be incredibly easy to write bad code, e.g.
This discussion was converted from issue #510 on September 08, 2020 20:16.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
@stephentoub commented on Tue Apr 21 2015
Today we disallow any usage of pointers in iterators, due to the issues involved with those pointers and the associated state moving to the heap. But we allow pointers in async methods (which have similar constraints) with the restriction that awaits can't be used in an unsafe context, e.g. this is allowed:
but this is not allowed:
We could allow unsafe code in iterators, but with a similar constraint: yield is not allowed in unsafe contexts, e.g. this would be allowed:
@paulomorgado commented on Tue Apr 28 2015
That makes sense!
@whoisj commented on Mon May 11 2015
how do I thumbs up this?
er... +1!
@agocke commented on Fri May 13 2016
@stephentoub Seems fine to me.
More broadly, do you know of any CLR restrictions about spilling unmanaged pointers across the state machine? AFAIK, that seems the be the main restriction around
async
andunsafe
, so theoretically the compiler could support spilling unmanaged pointers, as long as there are no issues with doing this in the CLR.@stephentoub commented on Fri May 13 2016
Are you asking about enabling not just:
but also:
?
If so, even if it could be made to work (e.g. storing the pointers into the state machine), I think it would be dangerous, as it'd be incredibly easy to write bad code, e.g.
@agocke commented on Fri May 13 2016
@stephentoub Yup, very dangerous, but it is
unsafe
, to be fair.@agocke commented on Fri May 13 2016
Certain things, like
fixed
, would still have to be prohibited outright, obviously.Beta Was this translation helpful? Give feedback.
All reactions