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

Provide better error message for 32bit vs 64bit missmatch. #22352

Closed
treeform opened this issue Jul 31, 2023 · 2 comments
Closed

Provide better error message for 32bit vs 64bit missmatch. #22352

treeform opened this issue Jul 31, 2023 · 2 comments

Comments

@treeform
Copy link
Contributor

Summary

When using 64bit Nim with 32bit C nothing but confusion happens.

Description

For some reason or another using a 64bit Nim with 32bit GCC happens way too often. The error message it produces is very confusing. Nim should be able to error or generate the C code that errors (through #error directive) and explains what happens. Some thing like:

"Nim's 64bit mode used with C's 32bit mode. Please install the right compiler or pass the right flags."

It seems like everyone has run into this and always has been confused by it:

Alternatives

  • The C code can have an #error directive instead of the assert.
  • The Nim compiler can warn about this even before generating the C code.
  • Nim can generate 64/32 bit independed code instead.

Examples

nim c -r foo64.nim 

Output:

...
...
...
Nim's 64bit mode used with C's 32bit mode. Please install the right compiler or pass the right flags.

Backwards Compatibility

No response

Links

No response

@demotomohiro
Copy link
Contributor

#19549 added error message to pointer size check assert and it was merged to Nim 2.0.

You can see how that error message looks like on console with following code:

{.emit: """
#define NIM_INTBITS 123
NIM_STATIC_ASSERT(sizeof(NI) == sizeof(void*) && NIM_INTBITS == sizeof(NI)*8, "Pointer size mismatch between Nim and C/C++ backend. You probably need to setup the backend compiler for target CPU.");
"""}

It seems like everyone has run into this and always has been confused by it:

These threads were created before Nim 2.0 and that NIM_STATIC_ASSERT didn't have error message in Nim 1.6.14 and older.

  • The C code can have an #error directive instead of the assert.

#error directive is a part of C preprocessor and it is used with #if branch.
But it cannot evaluate sizeof operator.
So following code result in compile error.

#define NIM_INTBITS 64
#if sizeof(NI) == sizeof(void*) && NIM_INTBITS == sizeof(NI)*8
#else
#error "Some error message"
#endif

@treeform
Copy link
Contributor Author

treeform commented Sep 8, 2023

Thank you for adding this. Hopefully it will help some people!

@treeform treeform closed this as completed Sep 8, 2023
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

No branches or pull requests

2 participants