Make pattern matching in macros a compiler error. close #173
This commit is contained in:
@@ -257,6 +257,19 @@ class Helpers {
|
|||||||
var varsInScope:Array<Var> = [];
|
var varsInScope:Array<Var> = [];
|
||||||
function makeSwitchPattern(patternExp:ReaderExp):Array<Expr> {
|
function makeSwitchPattern(patternExp:ReaderExp):Array<Expr> {
|
||||||
return switch (patternExp.def) {
|
return switch (patternExp.def) {
|
||||||
|
case _ if (k.hscript):
|
||||||
|
trace(patternExp);
|
||||||
|
var patternExpr = k.forCaseParsing().convert(patternExp);
|
||||||
|
[switch (patternExpr.expr) {
|
||||||
|
case EConst(CString(_, _)):
|
||||||
|
patternExpr;
|
||||||
|
case EConst(CInt(_) | CFloat(_)):
|
||||||
|
patternExpr;
|
||||||
|
case EConst(CIdent("null")):
|
||||||
|
patternExpr;
|
||||||
|
default:
|
||||||
|
throw KissError.fromExp(caseExp, "case expressions in macros can only match literal values");
|
||||||
|
}];
|
||||||
case CallExp({pos: _, def: Symbol("when")}, whenExps):
|
case CallExp({pos: _, def: Symbol("when")}, whenExps):
|
||||||
patternExp.checkNumArgs(2, 2, "(when <guard> <pattern>)");
|
patternExp.checkNumArgs(2, 2, "(when <guard> <pattern>)");
|
||||||
if (guard != null)
|
if (guard != null)
|
||||||
|
|||||||
@@ -371,6 +371,10 @@ class SpecialForms {
|
|||||||
false;
|
false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (k.hscript && isTupleCase) {
|
||||||
|
throw KissError.fromExp(wholeExp, "tuple-matching is not supported in a macro");
|
||||||
|
}
|
||||||
|
|
||||||
var b = wholeExp.expBuilder();
|
var b = wholeExp.expBuilder();
|
||||||
var defaultExpr = switch (cases[-1].def) {
|
var defaultExpr = switch (cases[-1].def) {
|
||||||
case CallExp({pos: _, def: Symbol("otherwise")}, otherwiseExps):
|
case CallExp({pos: _, def: Symbol("otherwise")}, otherwiseExps):
|
||||||
|
|||||||
@@ -435,6 +435,10 @@ class BasicTestCase extends Test {
|
|||||||
_testLambdaTypeAnnotations();
|
_testLambdaTypeAnnotations();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testCaseMacroError() {
|
||||||
|
_testCaseMacroError();
|
||||||
|
}
|
||||||
|
|
||||||
var aNullToPrint = null;
|
var aNullToPrint = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -742,6 +742,41 @@ From:[(assert false (+ \"false \" \"should \" \"have \" \"been \" \"true\"))]" m
|
|||||||
(Assert.isTrue (lambdaTest 5 6))
|
(Assert.isTrue (lambdaTest 5 6))
|
||||||
(lambdaTest2))
|
(lambdaTest2))
|
||||||
|
|
||||||
|
(defMacro __testCaseMacroError1 []
|
||||||
|
(case ["thing" 2] `5))
|
||||||
|
|
||||||
|
(defMacro __testCaseMacroError2 []
|
||||||
|
(case 5
|
||||||
|
((Some v) `false)
|
||||||
|
(otherwise `false)))
|
||||||
|
|
||||||
|
(defMacro __testCaseMacroError3 []
|
||||||
|
(case 5
|
||||||
|
([not this] `false)
|
||||||
|
(otherwise `false)))
|
||||||
|
|
||||||
|
(defMacro __testCaseMacroError4 [s]
|
||||||
|
(case (symbolNameValue s)
|
||||||
|
("rightSymbol" `true)
|
||||||
|
(otherwise `false)))
|
||||||
|
|
||||||
|
(defMacro __testCaseMacroError5 [n]
|
||||||
|
(case (eval n)
|
||||||
|
(5 `true)
|
||||||
|
(otherwise `false)))
|
||||||
|
|
||||||
|
(function _testCaseMacroError []
|
||||||
|
(assertThrowsAtCompileTime
|
||||||
|
(__testCaseMacroError1))
|
||||||
|
(assertThrowsAtCompileTime
|
||||||
|
(__testCaseMacroError2))
|
||||||
|
(assertThrowsAtCompileTime
|
||||||
|
(__testCaseMacroError3))
|
||||||
|
(Assert.isTrue (__testCaseMacroError4 rightSymbol))
|
||||||
|
(Assert.isFalse (__testCaseMacroError4 wrongSymbol))
|
||||||
|
(Assert.isTrue (__testCaseMacroError5 5))
|
||||||
|
(Assert.isFalse (__testCaseMacroError5 6)))
|
||||||
|
|
||||||
(function _testHaxeProperties []
|
(function _testHaxeProperties []
|
||||||
(Assert.equals 5 staticProp)
|
(Assert.equals 5 staticProp)
|
||||||
(Assert.equals 9 (set staticProp 9))
|
(Assert.equals 9 (set staticProp 9))
|
||||||
|
|||||||
Reference in New Issue
Block a user