[Issue 13010] Use variable range propagation (VRP) for static assert

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Tue Jul 1 00:09:47 PDT 2014


https://issues.dlang.org/show_bug.cgi?id=13010

--- Comment #4 from Kenji Hara <k.hara.pg at gmail.com> ---
(In reply to Lionello Lunesu from comment #3)
> How do you feel about it when the code is changed to this:
> 
> void testvrp(ubyte l)
> {
>   immutable int i = l;
>   static assert(i >= 0);
> }

Looks not bad. In my thought, the value of i is not known at compile time, but
i >= 0 could be determined to true at compile time with VRP. And, it should be
handled in const-folding phase.

By advancing the thought, compiler will be able to generate "statement is not
reachable" warning in else branch of the code.

void test(ubyte n)
{
  immutable int i = n;
  if (i >= 0)   // const-folding with VRP will optimize the condition to true.
  { ... }
  else
  { ... }  // so the else branch could be determined to "not reachable"
}

--


More information about the Digitalmars-d-bugs mailing list