-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Clarify extern caller unsafe methods #9883
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
base: main
Are you sure you want to change the base?
Conversation
proposals/unsafe-evolution.md
Outdated
| Only `extern` methods which are recognizable from metadata are handled, i.e., methods with the `pinvokeimpl` flag in IL. | ||
| Therefore, `extern` methods without `DllImport` attribute are not recognized as implicitly `unsafe` when used from a different assembly. | ||
| Such `extern` methods exposed as API should not be a common scenario anyway and there is already a warning for `extern` methods without any attribute | ||
| (albeit absence of that warning doesn't guarantee the method will be recognized as `unsafe` in metadata). | ||
| One can mark such methods as `unsafe` explicitly if needed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would this be a caveat? The goal is that any extern method is unsafe, period.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extern without DllImport are not useful anyway I think and we would need to synthesize the RequiresUnsafe attribute for them. Which also means it would be different if such API was compiled by old or new compiler (whereas as currently proposed, even extern apis compiled by old compilers would be recognized in the same way).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would we need to synthesize anything for them? They're extern, they're just always unsafe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would we need to synthesize anything for them? They're extern, they're just always unsafe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we can't tell whether they are extern from metadata, that's what I'm trying to say by this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extern methods without any attribute look just like plain ordinary empty methods in IL.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should be able to tell by checking whether the method has IL implementation (method is implemented by IL).
If the method has no implementation in IL and it is not abstract (there may be a few more cases like that), it is extern -> unsafe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extern without DllImport are not useful anyway
extern without DllImport are used by https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.unsafeaccessorattribute
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, it seems we can recognize extern methods by them having RVA 0, thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what to do with this: object.GetType (and probably other methods) on .net framework is marked as extern, so it would be an error to use it outside unsafe under the new rules.
No description provided.