Browse Source

Throw an error if eelse isn't defined.

The Haxe compiler optimizes "`if(true) 1 else 0`" to 1, which can then be used as a value. However, "`if(true) 1`" results in `Void`, rather than 1, meaning it cannot be used as a value.

To match this behavior, `getValue()` should only return a value for an `EIf` expression if `eelse` is defined, regardless of what `econd` evaluates to.
player-03 10 years ago
parent
commit
ae0f8853e2
1 changed files with 6 additions and 2 deletions
  1. 6 2
      std/haxe/macro/ExprTools.hx

+ 6 - 2
std/haxe/macro/ExprTools.hx

@@ -244,8 +244,12 @@ class ExprTools {
 				obj;
 			case EArrayDecl(el): el.map(getValue);
 			case EIf(econd, eif, eelse) | ETernary(econd, eif, eelse):
-				var econd:Dynamic = getValue(econd);
-				econd ? getValue(eif) : getValue(eelse);
+				if (eelse == null) {
+					throw "If statements only have a value if the else clause is defined";
+				} else {
+					var econd:Dynamic = getValue(econd);
+					econd ? getValue(eif) : getValue(eelse);
+				}
 			case EUnop(op, false, e1):
 				var e1:Dynamic = getValue(e1);
 				switch (op) {