Bug 11042 – Inconsistent "static condition" behaviors

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-09-15T04:27:00Z
Last change time
2014-09-03T01:31:49Z
Keywords
accepts-invalid, pull, rejects-valid
Assigned to
nobody
Creator
k.hara.pg

Comments

Comment #0 by k.hara.pg — 2013-09-15T04:27:03Z
Inside static if condition, the logical operators || and && will be evaluated lazily. static if (true || error) { pragma(msg, "OK"); } else { pragma(msg, "NG"); } // --> prints "OK" This is useful feature, but it doesn't work on static assert condition. static assert(true || error); // --> Error: undefined identifier error This is inconsistent. I think static assert should also evaluate the condition lazily. ---- There's one associated bug. static if (is(typeof(true || error))) { pragma(msg, "OK"); } else { pragma(msg, "NG"); } // --> prints "OK" The expression "true || error" should not have valid type, but inside static if condition, the lazy evaluation is incorrectly invoked. It's definitely a bug. ---- Template constraint is the one another place for "static condition". Currently it behaves like "static if" condition. Its behavior should also be fixed.
Comment #1 by k.hara.pg — 2013-09-15T04:39:27Z
Comment #2 by andrej.mitrovich — 2013-09-15T04:45:33Z
I would find it even more useful if we extended this to templates, for example here's the workaround code I have to use in one of my libraries: alias storages = ParameterStorageClassTuple; // from Phobos /** Check whether $(D T) is a handler function which can be called with the $(D Types). */ template isEventHandler(T, Types...) if (isSomeFunction!T) { alias stores = storages!T; // eager evaluation workaround static if (!stores.length) enum bool isEventHandler = false; else enum bool isEventHandler = is(typeof(T.init(Types.init))) && stores[0] == stcType.scope_ && is(ReturnType!T == void); } If I'm not using this "static if", the "stores[0]" code will issue a compiler error (out of bounds) if there are no parameters. But, "is(typeof(T.init(Types.init)))" would already fail before it, however it's not lazy so "stores[0]" is evaluated eagerly and the compiler emits an error. Can we do anything about this? Perhaps enabling lazy evaluation for manifest constants in an eponymous template would work.. I'm not sure.
Comment #3 by andrej.mitrovich — 2013-09-15T04:46:13Z
Oh wait I just realized this is exactly what http://d.puremagic.com/issues/show_bug.cgi?id=9073 was for, so it got rejected? Shame..
Comment #4 by github-bugzilla — 2014-09-03T01:31:48Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/296859699b29b1ef562155ba7b4f7558b4b3b616 fix Issue 11042 - Inconsistent "static condition" behaviors Divide `SCOPEcondition` and `SCOPEconstraint` properly. https://github.com/D-Programming-Language/dmd/commit/a24ed801e918e0f4926ce4a879625e50c65c8659 Merge pull request #2558 from 9rnsr/fix11042 Issue 11042 - Inconsistent "static condition" behaviors