Explicitly mark struct as unmanaged #2036
-
This proposal is an extension of a small part of #688. ProposalA struct should have the ability to be marked as unmanaged, so that analysis can happen pre-compile time. Much like the generic constraint, the public unmanaged UnmanagedStruct
{
public int num;
} In the event of a managed type being used at any level of nesting in the type, the compiler will emit an error. public unmanaged AccidentallyManagedStruct
{
public int num;
public string whoops; // Not allowed!
}
MotivationIt would prevent a developer from editing an unmanaged type, adding a string (or other managed type), and then trawling through other seemingly unrelated code. DrawbacksDoesn't have many use cases, but would be especially useful for cases like the Unity Job System or low-level interop services. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Can you not do this already today with an analyzer? Just mark the structs however you want (an attribute would be fine), then just write an analyzer to check for these cases. |
Beta Was this translation helpful? Give feedback.
-
Yes, however I feel like this would make more sense to have wider language support rather than a Roslyn analyser as the keyword fits better with the generic constraint. The metadata that the attribute add isn't needed either as the type would just be a struct to the runtime. |
Beta Was this translation helpful? Give feedback.
-
I've started using analyzers for these sorts of restrictions. There was an initial time investment to learn how they work, but they're quite effective and allow me to define all sorts of constraints like this one without much trouble. That being said, I do see a benefit to this proposal: an |
Beta Was this translation helpful? Give feedback.
Can you not do this already today with an analyzer? Just mark the structs however you want (an attribute would be fine), then just write an analyzer to check for these cases.