Skip to content

wasm2c: fix not all control paths return a value warning #24206

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

marler8997
Copy link
Contributor

@marler8997 marler8997 commented Jun 17, 2025

I got the following warning when compiling zig-wasm2c:

C:\git\zig\stage1\wasm.h(55) : warning C4715: 'WasmMut_toC': not all control paths return a value

This is because the C compiler doesn't understand that the panic function from panic.h is noreturn. I addressed this copying the body of the panic function into the call-site directly which is just a printf and abort. Now the compiler knows the codepath afterwards is unreachable because abort has the proper function decorators to mark it noreturn.

@alexrp
Copy link
Member

alexrp commented Jun 19, 2025

I think adding a return NULL to WasmMut_toC, as in WasmValType_toC, is better than introducing compiler/target checks here.

@marler8997
Copy link
Contributor Author

Well, adding "noreturn" to the panic declaration not only fixes this warning, but, also improves all the other places panic is called by letting the compiler know it doesn't return, which gives the compiler/optimize more accurate information. That seems like a win/win to me? I'm not seeing a downside?

@alexrp
Copy link
Member

alexrp commented Jun 19, 2025

We have a policy of keeping the stage1/ C code as compiler/target-agnostic as possible. The only exception to this rule is zig.h which comes from lib/.

@marler8997 marler8997 force-pushed the wasm2cPanicNoreturn branch from b915073 to 9e5bf1b Compare June 19, 2025 04:24
@marler8997
Copy link
Contributor Author

I see. I think I can work with that. I just inlined the body of the custom panic function with the call to fprintf/abort. No more warning since the abort function has whatever noreturn decorator the toolchain requires.

@marler8997 marler8997 changed the title wasm2c warnings: add a noreturn attribute to panic wasm2c: fix not all control paths return a value warning Jun 19, 2025
I got the following warning when compiling zig-wasm2c:

C:\git\zig\stage1\wasm.h(55) : warning C4715: 'WasmMut_toC': not all control paths return a value

This is because the C compiler doesn't understand that the panic
function from `panic.h` is noreturn.  I addressed this making panic
a macro so the caller sees that abort is called and is able to see
that it's a "noreturn codepath".
@marler8997 marler8997 force-pushed the wasm2cPanicNoreturn branch from 9e5bf1b to 52d0bed Compare June 19, 2025 04:35
@marler8997
Copy link
Contributor Author

Ooo nevermind, I think I came up with something better. I changed panic to a macro so the call to abort is inlined at EVERY call site :)

@alexrp
Copy link
Member

alexrp commented Jun 19, 2025

But you're still relying on the compiler being able to figure out that abort doesn't return. A dead return is nice because it's fully portable.

@marler8997
Copy link
Contributor Author

We're not relying on the compiler to know that abort is noreturn for the program to behave correctly. It's an extra piece of information that the compiler/optimizer can use to eliminate dead branches (and erroneous warnings).

@jacobly0
Copy link
Member

jacobly0 commented Jun 19, 2025

An optimizer that understands noreturn already understands that a function that calls noreturn functions on every path is also noreturn. The attribute exists so that the optimizer knows whether external functions that it can't analyze are noreturn. Also this program compiles and runs in 0m0.725s, it does not need optimizations at this time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants