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

addReadLine proc in syncio module causes incompatible pointer type error from GCC #588

Open
demotomohiro opened this issue Feb 20, 2025 · 3 comments

Comments

@demotomohiro
Copy link
Collaborator

In syncio module, fgets is declared as:

proc fgets(str: out array[bufsize, char]; n: int32; f: File): cstring {.
  importc: "fgets", header: "<stdio.h>".}

And addReadLine proc passes array[bufsize, char] type variable to fgets:

  var buf: array[bufsize, char]
  while fgets(buf, bufsize.int32, f) != nil:

This code generates following C code:

  AarrayAcS8ZS80_0_t buf_3;
  while ((!(((void*)fgets((&buf_3), ((NI32)bufsize_0_syn1lfpjv), f_22)) == NIM_NIL))){

This code result in compile error with GCC:

nifcache/syn1lfpjv.c:446:28: error: passing argument 1 of ‘fgets’ from incompatible pointer type [-Wincompatible-pointer-types]
  446 |   while ((!(((void*)fgets((&buf_3), ((NI32)bufsize_0_syn1lfpjv), f_22)) == NIM_NIL))){
      |                           ~^~~~~~~
      |                            |
      |                            AarrayAcS8ZS80_0_t *

fgets's first parameter type is char *__restrict __.

demotomohiro added a commit to demotomohiro/nif that referenced this issue Feb 20, 2025
@Araq
Copy link
Member

Araq commented Feb 20, 2025

NIFC should produce &buf_3.a for this.

@demotomohiro
Copy link
Collaborator Author

It seems NIFC should produce buf_3.a.
Following C code compiles without errors.

#include <stdio.h>

typedef struct {
  char a[80];
} Buf;
Buf buf;
int main() {
  fgets(buf.a, 80, stdin);
  return 0;
}

Buf when I change buf.a to &buf.a, I got incompatible pointer type error.

@Araq
Copy link
Member

Araq commented Feb 21, 2025

It should produce &buf.a[0], I think.

demotomohiro added a commit to demotomohiro/nif that referenced this issue Feb 21, 2025
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