Skip to content
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: Attribute noreturn functions #764

Closed
riverar opened this issue Dec 3, 2021 · 8 comments · Fixed by #788
Closed

Proposal: Attribute noreturn functions #764

riverar opened this issue Dec 3, 2021 · 8 comments · Fixed by #788
Assignees
Labels
enhancement New feature or request usability Touch-up to improve the user experience for a language projection

Comments

@riverar
Copy link
Collaborator

riverar commented Dec 3, 2021

I propose that win32metadata emit an attribute (e.g. Windows.Win32.Interop.NoReturnAttribute) attached to functions and methods that do not return to the caller (e.g. FatalExit). This will allow languages, such as Rust (never, !), to generate smarter bindings, enabling better optimization of noreturn code paths, detection of dead code paths (that is, code that is mistakenly placed after the call of a noreturn function), and other benefits.

Some noreturn APIs are already annotated in the source material.

Continuing with the FatalExit example:

WINBASEAPI __analysis_noreturn VOID WINAPI FatalExit(
    _In_ int ExitCode
);

Some other forms found in the source material to be aware of:

_Analysis_noreturn_
DECLSPEC_NORETURN
__declspec(noreturn)

A theoretical example of how this could look in metadata:

[NoReturn]
[DllImport("KERNEL32", ExactSpelling = true)]
[SupportedOSPlatform("windows5.1.2600")]
public static extern void FatalExit([In] int ExitCode);
@AArnott
Copy link
Member

AArnott commented Dec 3, 2021

Bonus points if the metadata can reuse the DoesNotReturnAttribute.

@riverar
Copy link
Collaborator Author

riverar commented Dec 4, 2021

@AArnott Good catch. I was looking for an existing attribute and missed this one. I would definitely prefer this over a new one.

@mikebattista mikebattista added enhancement New feature or request usability Touch-up to improve the user experience for a language projection labels Dec 6, 2021
@mikebattista
Copy link
Contributor

Assigning to @sotteson1 to see about scraping this from the headers and emitting as DoesNotReturnAttribute.

@sotteson1
Copy link
Contributor

I'm wondering why a function that returns void needs an attribute to indicate it doesn't return anything. Isn't that what returning void means?

@kennykerr
Copy link
Contributor

It's not that it doesn't return anything - it's that it doesn't return at all. Functions like abort, std::terminate, etc. will all result in some kind of process termination and compilers appreciate knowing this. Here's the C++ version of this:

https://en.cppreference.com/w/cpp/language/attributes/noreturn

@sotteson1
Copy link
Contributor

sotteson1 commented Dec 6, 2021

Oh, got it.

@mikebattista
Copy link
Contributor

https://task.ms/37680894

@kennykerr
Copy link
Contributor

RaiseException is still lacking the DoesNotReturn attribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request usability Touch-up to improve the user experience for a language projection
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants