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

cgenerator.lua assertion error when an auto parameter takes (notbool and bool or bool) #234

Closed jrfondren closed 10 months ago

jrfondren commented 10 months ago

Bug description

I noticed that niltype is falsy, which I didn't expect (although it having 'nil' in the name is a hint), and found this while demonstrating falsy values.

Code example

local function f(b: auto) end
f(''      and true or false) -- error
f(0       and true or false) -- error
f(false   and true or false) -- ok
f(true    and false or true) -- ok
f(nil     and false or true) -- error
f(niltype and false or true) -- error
f(niltype and 'true' or 'false') -- ok

Environment

x86_64 linux Nelua 0.2.0-dev Build number: 1588 Git date: 2023-09-16 16:20:44 -0300 Git hash: 596fcca5c77932da8a07c249de59a9dff3099495 Semantic version: 0.2.0-dev.1588+596fcca5 Copyright (C) 2019-2022 Eduardo Bart (https://nelua.io/)

edubart commented 10 months ago

Thanks for reporting this, this was a regression I made while doing a refactor a while ago. Fixed in https://github.com/edubart/nelua-lang/commit/20d784a00e389e3d26f75f1142214048b42efe32

Be careful when using and + or as a ternary operator, because it's not really a ternary operator.