luaL_error() and related functions should be tagged as noreturn

59 views
Skip to first unread message

Maxime Henrion

unread,
Nov 29, 2025, 1:48:53 PM (4 days ago) Nov 29
to lua-l
Hi all,

I know next to nothing about LUA but thought it might be worth posting about this. I have been using some static analyzer tools for a codebase at work and noticed we got a number of false positives that are ultimately due to the compiler not realizing that the luaL_error() function does not return (as far as I can tell anyways).

This is of course very minor but it would be nice if this function (or maybe another function this one calls into) could be tagged with __attribute__((noreturn)) or whatever macro you guys use for this if there is any, if possible at all.

I hope you won't mind me not looking further into this or submitting a patch, I thought this was trivial enough that a simple e-mail would work.

Cheers,
Maxime

Sean Conner

unread,
Nov 29, 2025, 4:02:42 PM (3 days ago) Nov 29
to lu...@googlegroups.com
It was thus said that the Great Maxime Henrion once stated:
> This is of course very minor but it would be nice if this function (or
> maybe another function this one calls into) could be tagged with
> __attribute__((noreturn)) or whatever macro you guys use for this if there
> is any, if possible at all.
>
> I hope you won't mind me not looking further into this or submitting a
> patch, I thought this was trivial enough that a simple e-mail would work.

Marking a function as _Noreturn [1], but there are issues. luaL_error()
(and lua_error()) both don't return, but their signatures are such that one
can do:

return luaL_error(L,"there's an error");

to signal intent in a function that this is an exit path. Section 6.7.4.9
of the C11 standard states:

The implementation should produce a diagnostic message for a
function declared with a _Noreturn function specifier that appears
to be capable of returning to its caller.

This changes the signature of both lua_error() and luaL_error(), thus
potentially breaking a lot of code. If this were to be added, quite a bit
of code would have to be rewritten. I know I would have to rewrite quite a
bit of code if this was adapted, since I use the return luaL_error() idiom.

Also, Lua is primarily written to target C89, to be maximally portable.

-spc

[1] C11 finally standardizes this, with the new keyword _Noreturn and a
header file <stdnoreturn.h>.

Sainan

unread,
Nov 30, 2025, 3:27:20 AM (3 days ago) Nov 30
to lu...@googlegroups.com
The 'noreturn' attribute is in addition to and not instead of a return value.

It is standardised in C++ as [[noreturn]] and so could at the very least be used when Lua is compiled as C++ with macros, which is already something well-established in the project, e.g. such as how Lua makes use of C++ exceptions.

-- Sainan

Maxime Henrion

unread,
Nov 30, 2025, 1:02:12 PM (3 days ago) Nov 30
to lu...@googlegroups.com
Thank you for the reply, that makes sense!

--
You received this message because you are subscribed to a topic in the Google Groups "lua-l" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/lua-l/fpJyxsGt_po/unsubscribe.
To unsubscribe from this group and all its topics, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/20251129210235.GF9008%40brevard.conman.org.
Reply all
Reply to author
Forward
0 new messages