Improve some compiler errors

This commit is contained in:
2023-03-03 11:24:00 -07:00
parent 269ddb6fde
commit ef51ac0f80
2 changed files with 18 additions and 8 deletions

View File

@@ -61,7 +61,7 @@ class Helpers {
}; };
} }
public static function parseComplexType(path:String, ?from:ReaderExp):ComplexType { public static function parseComplexType(path:String, ?from:ReaderExp, mustResolve=false):ComplexType {
// Trick Haxe into parsing it for us: // Trick Haxe into parsing it for us:
var typeCheckStr = 'var thing:$path;'; var typeCheckStr = 'var thing:$path;';
var errorMessage = 'Haxe could not parse a complex type from `$path` in `${typeCheckStr}`'; var errorMessage = 'Haxe could not parse a complex type from `$path` in `${typeCheckStr}`';
@@ -75,7 +75,7 @@ class Helpers {
} }
try { try {
var typeCheckExpr = Context.parse(typeCheckStr, Context.currentPos()); var typeCheckExpr = Context.parse(typeCheckStr, Context.currentPos());
return switch (typeCheckExpr.expr) { var t = switch (typeCheckExpr.expr) {
case EVars([{ case EVars([{
type: complexType type: complexType
}]): }]):
@@ -84,6 +84,16 @@ class Helpers {
throwError(); throwError();
return null; return null;
}; };
if (mustResolve) {
try {
var pos = if (from != null) from.macroPos() else Context.currentPos();
Context.resolveType(t, pos);
} catch (e:Dynamic) {
errorMessage = 'Type not found: $path';
throwError();
}
}
return t;
} catch (err) { } catch (err) {
throwError(); throwError();
return null; return null;

View File

@@ -429,17 +429,17 @@ class SpecialForms {
if (args.length == 3) { if (args.length == 3) {
pkg = switch (args.shift().def) { pkg = switch (args.shift().def) {
case Symbol(pkg): pkg; case Symbol(pkg): pkg;
default: throw KissError.fromExp(args[0], '$whichArg argument to (the... ) should be a valid haxe package'); default: throw KissError.fromExp(wholeExp, '$whichArg argument to (the... ) should be a valid haxe package');
}; };
whichArg = "second"; whichArg = "second";
} }
var type = switch (args[0].def) { var type = switch (args[0].def) {
case Symbol(type): type; case Symbol(type): type;
default: throw KissError.fromExp(args[0], '$whichArg argument to (the... ) should be a valid type'); default: throw KissError.fromExp(wholeExp, '$whichArg argument to (the... ) should be a valid type');
}; };
if (pkg.length > 0) if (pkg.length > 0)
type = pkg + "." + type; type = pkg + "." + type;
ECheckType(k.convert(args[1]), Helpers.parseComplexType(type, args[0])).withMacroPosOf(wholeExp); ECheckType(k.convert(args[1]), Helpers.parseComplexType(type, wholeExp, true)).withMacroPosOf(wholeExp);
}; };
k.doc("try", 1, null, "(try <thing> <catches...>)"); k.doc("try", 1, null, "(try <thing> <catches...>)");
@@ -458,7 +458,7 @@ class SpecialForms {
def: Symbol(name) | TypedExp(_, {pos: _, def: Symbol(name)}) def: Symbol(name) | TypedExp(_, {pos: _, def: Symbol(name)})
} }
]): name; ]): name;
default: throw KissError.fromExp(catchArgs[0], 'first argument to (catch... ) should be a one-element argument list'); default: throw KissError.fromExp(catchKissExp, 'first argument to (catch... ) should be a one-element argument list');
}, },
type: switch (catchArgs[0].def) { type: switch (catchArgs[0].def) {
case ListExp([{pos: _, def: TypedExp(type, _)}]): case ListExp([{pos: _, def: TypedExp(type, _)}]):
@@ -522,9 +522,9 @@ class SpecialForms {
if (args.length > 1) { if (args.length > 1) {
switch (args[1].def) { switch (args[1].def) {
case Symbol(typePath): case Symbol(typePath):
t = Helpers.parseComplexType(typePath, args[1]); t = Helpers.parseComplexType(typePath, wholeExp, true);
default: default:
throw KissError.fromExp(args[1], 'second argument to cast should be a type path symbol'); throw KissError.fromExp(wholeExp, 'second argument to cast should be a type path symbol');
} }
} }
ECast(e, t).withMacroPosOf(wholeExp); ECast(e, t).withMacroPosOf(wholeExp);