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
2.06k stars 67 forks source link

comptime arguments have different behavior when it's called `context` #186

Closed Andre-LA closed 2 years ago

Andre-LA commented 2 years ago

Bug description

When using comptime parameters, when the parameter it's called "context" or any identifier that matches a field on _ENV, the behavior get's different and foo.value it's nil since the value it's from _ENV and not from the nelua parameter.

Code example

-- working function
local function foo(s: string <comptime>)
  ## print(s.value, require'inspect'(s, {depth=1}) )
  print('1 -->', s)
end

foo 'ABC'

-- wrong behavior function, although at runtime it works
local function bar(context: string <comptime>)
  ## print(context.value, require'inspect'(context, {depth=1}) )
  print('2 -->', context)
end

bar 'XYZ'

--[[ shell
ABC <1>{
  codename = "s",
  comptime = true,
  lvalue = true,
  name = "s",
  node = {...},
  scope = {...},
  type = {...},
  value = "ABC",
  <metatable> = {...}
}

nil <1>{
  afteranalyzes = {...},
  afterinfers = {...},
  ast = {...},
  context = <table 1>,
  generator = "c",
  nodestack = {...},
  ppcontext = {...},
  pragmas = {...},
  pragmastack = {...},
  reqscopes = {...},
  requires = {...},
  rootpragmas = {...},
  rootscope = {...},
  rootstate = {...},
  scope = {...},
  scopestack = {...},
  state = {...},
  statestack = {...},
  unresolvedcount = 1,
  usedbuiltins = {...},
  usedcodenames = {...},
  visitors = {...},
  <metatable> = {...}
}
1 -->   ABC
2 -->   XYZ
]]

Expected behavior

Both foo and bar functions working with the same behavior.

Workaround

Avoid using context or any other identifier from _ENV table.

Environment

edubart commented 2 years ago

Not really a bug, some variables names are present in _ENV and _G, in case there is a symbol name conflict the user must index symbols table instead, so use symbols.context.value instead of context.value in that case.

Name conflicts are expected, Lua has many names reserved already like table, string, type .. Any symbol using those names must be indexed via symbols table.

Andre-LA commented 2 years ago

That makes sense, I'll close the issue.