Divisions by zero in dead branches cause compiler errors

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Divisions by zero in dead branches cause compiler errors

Petr Skočík
I've just noticed `_Static_assert( !0 || 1/0, "");` fails to compile on
tinycc.

I had an overflow-checking macro expand to code like this and got a
compiler error from it on tinycc.

I've since changed the macro to have a redundant ternary in the divisor
(which works around the error and it should affect optimizing
compilers), but just in case somebody wants to fix this...

It seems to me like simply commenting out the

                if (const_wanted)
                    tcc_error("division by zero in constant");

at tccgen.c:2090 could do the trick.

Then we get a less specific "constant expression expected" instead of
"division by zero in constant" for  "enum { a = 1/0 };", but perhaps
fancy diagnostics are best left to big compilers.

Regards,
Petr Skocik

_______________________________________________
Tinycc-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel
Reply | Threaded
Open this post in threaded view
|

Re: Divisions by zero in dead branches cause compiler errors

Michael Matz-4
Hello,

On Wed, 15 Jan 2020, Petr Skočík wrote:

> I've just noticed `_Static_assert( !0 || 1/0, "");` fails to compile on
> tinycc.

Thanks for the report, that shows that unevaluated sub-expressions of
const expressions were mishandled.  We also diagnosed (incorrectly)

   static int i = 2 || 1 / 0;

(which even is literally written down in the standard).

> It seems to me like simply commenting out the
>
>                if (const_wanted)
>                    tcc_error("division by zero in constant");
>
> at tccgen.c:2090 could do the trick.
>
> Then we get a less specific "constant expression expected" instead of
> "division by zero in constant" for  "enum { a = 1/0 };", but perhaps
> fancy diagnostics are best left to big compilers.
Yeah, I've chosen a more complete solution, so that the latter is still
diagnosed, but the former isn't.


Ciao,
Michael.
_______________________________________________
Tinycc-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel