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
[Proposal] Unsafe expression syntax #3677
Comments
I personally disagree with this. I would find that much less cluttered than the And that's something very important to me. I want unsafe code to stick out liek a sore thumb. It should intentionally be very visible because it needs extra special attention. |
Hey, thank you for chiming in!
I understand that point of view, but my counter point to that would be that I'm not sure that really matter so much at this point, where we can have technically unsafe code literally everywhere by using the various In that previous example, that whole snippet was technically unsafe. I'd argue that making just the part with the I guess my rationale here is: I think making this so explicit on purpose made perfect sense back when it was introduced, but I feel like now that we have all these other "safe-not-safe" APIs being available, it lost meaning in a way. I feel like giving users the ability to have the more compact syntax here would make life easier in these scenarios, with no real drawbacks in code safety 🤔 |
At the same point, you could just do: public unsafe ref T Value
{
get
{
#if SPAN_RUNTIME_SUPPORT
return ref MemoryMarshal.GetReference(this.span);
#else
ref T r0 = ref MemoryMarshal.GetReference(this.span);
return ref Unsafe.Add(ref r0, (IntPtr)(void*)(uint)this.index);
#endif
}
} This is already legal today and equally as "hidden". |
Yeah that works too, I just don't really like that as when you're on a target that takes the first branch the I mean, with the I'd personally use |
On second thought, to address what both @CyrusNajmabadi and @tannergooding said, I'd actually be happy if there was just an option to allow I mean, something like (100% made up name): <AllowUnrestrictedUnsafeBlocks>false</AllowUnrestrictedUnsafeBlocks> Adding this to the |
No, that's still a language change. As a general rule, we do not create dialects of C# controlled by a project file switch. If the normal language feature has to go from -100 points to 100 points to even be considered, a new dialect has to get to about 1,000,000 points. Nullable is the only such feature in recent memory, addressing a fundamental issue in C# that likely costs developers literally billions of dollars of time a year. I don't see explicitly allowing unsafe everywhere in the language as meeting that bar. |
I see, that makes perfect sense, thanks! I guess I was just thinking about how to me the original explicit toggle for I'll leave this issue as is then, |
Overview
This issue is about adding the expression syntax to
unsafe
contexts, in addition to the currently supported unsafe blocks.This would be analogous to eg.
checked
expressions, and it would bring the following benefits:unsafe
contexts in case where devs would be forced to move additional statements into the unsafe blocks, if they need to access the return value of the unsafe expression (as that would otherwise be out of scope).Example usage
Consider the following extension method (from here):
Currently I need to mark the entire method as
unsafe
, even though the only part requiring that context is just the(IntPtr)(void*)
cast in the expression on the second like. Yes I could just use an unsafe block for the second statement, but that'd add another indentation level and make the whole code much more cluttered and harder to follow.With support for unsafe expressions, the above could be rewritten as:
With the unsafe context being restricted to the single expression that actually needs it. This is even more useful when dealing with eg. multi-targeting, where only some code paths might require an unsafe expression, which would make the
unsafe
modifier applied to the entire method be redundant on those targets. A workaround here is to use an unsafe block, but as mentioned that both makes the code more verbose and with an extra indentation level. Consider the following (extracted from this type):Compare this with just being able to write that second part on the same indentation level, like so:
Syntax changes
From what I can see in the specs (here), this would require a new
unsafe_expression
type to be added to support the new expression syntax. This would then also need to be added to the list of supported expressions in theprimary_no_array_creation_expression
category (just likechecked_expression
).The text was updated successfully, but these errors were encountered: