Make pattern matching in macros a compiler error. close #173

This commit is contained in:
2023-02-25 16:22:05 -07:00
parent 610aea7861
commit e5d22ae6ad
4 changed files with 56 additions and 0 deletions

View File

@@ -257,6 +257,19 @@ class Helpers {
var varsInScope:Array<Var> = [];
function makeSwitchPattern(patternExp:ReaderExp):Array<Expr> {
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):
patternExp.checkNumArgs(2, 2, "(when <guard> <pattern>)");
if (guard != null)

View File

@@ -371,6 +371,10 @@ class SpecialForms {
false;
}
if (k.hscript && isTupleCase) {
throw KissError.fromExp(wholeExp, "tuple-matching is not supported in a macro");
}
var b = wholeExp.expBuilder();
var defaultExpr = switch (cases[-1].def) {
case CallExp({pos: _, def: Symbol("otherwise")}, otherwiseExps):

View File

@@ -435,6 +435,10 @@ class BasicTestCase extends Test {
_testLambdaTypeAnnotations();
}
function testCaseMacroError() {
_testCaseMacroError();
}
var aNullToPrint = null;
}

View File

@@ -742,6 +742,41 @@ From:[(assert false (+ \"false \" \"should \" \"have \" \"been \" \"true\"))]" m
(Assert.isTrue (lambdaTest 5 6))
(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 []
(Assert.equals 5 staticProp)
(Assert.equals 9 (set staticProp 9))