edubart / nelua-lang

Minimal, efficient, statically-typed and meta-programmable systems programming language heavily inspired by Lua, which compiles to C and native code.
https://nelua.io
MIT License
1.99k stars 64 forks source link

Function variables can collide with C identifiers #243

Closed jrfondren closed 9 months ago

jrfondren commented 9 months ago

Code example

require 'string'
require 'C.time'
local function needed() -- at the toplevel, a unitname prefix is added
  local time = C.time(nilptr)
  local tm: C.tm
  local buf: [100]byte
  C.localtime_r(&time, &tm)
  C.strftime(&buf, #buf, '%F', &tm)
  print(cstring(string.copy({&buf, #buf})))
end
needed()

This fails to build with:

/home/jfondren/.cache/nelua/tm.c: In function ‘tm_needed’:
/home/jfondren/.cache/nelua/tm.c:1634:17: error: called object ‘time’ is not a function or function pointer
 1634 |   time_t time = time((nlctime_t_ptr)NULL);
      |                 ^~~~
/home/jfondren/.cache/nelua/tm.c:1634:10: note: declared here
 1634 |   time_t time = time((nlctime_t_ptr)NULL);
      |          ^~~~
/home/jfondren/.cache/nelua/tm.c:1635:15: error: expected ‘,’ or ‘;’ before ‘{’ token
 1635 |   tm tm = (tm){0};
      |               ^
error: C compilation for '/home/jfondren/.cache/nelua/tm' failed

Expected behavior

For the code generator to fudge names as needed, especially for local variables, or to produce an error about conflicts (which might be less confusing in the presence of cemit)

Workaround

Change variable names to avoid conflicts when they show up. Simple cases like this are at least pretty clear.

Environment

x86_64 linux Nelua 0.2.0-dev Build number: 1609 Git date: 2023-11-15 18:57:47 -0300 Git hash: 19917edbf61fac431256f14682c331d8af1d9f34 Semantic version: 0.2.0-dev.1609+19917edb Copyright (C) 2019-2023 Eduardo Bart (https://nelua.io/)

edubart commented 9 months ago

Fixed, thanks for reporting.