This commit is contained in:
Timothee Cour 2019-08-15 06:55:48 -07:00 • committed by Andreas Rumpf
commit 511298242d
5 changed files with 18 additions and 4 deletions

View file

@ -825,7 +825,8 @@ proc genFieldCheck(p: BProc, e: PNode, obj: Rope, field: PSym) =
v.r.add(".") v.r.add(".")
v.r.add(disc.sym.loc.r) v.r.add(disc.sym.loc.r)
genInExprAux(p, it, u, v, test) genInExprAux(p, it, u, v, test)
let strLit = genStringLiteral(p.module, newStrNode(nkStrLit, field.name.s)) let msg = genFieldError(field, disc.sym)
let strLit = genStringLiteral(p.module, newStrNode(nkStrLit, msg))
if op.magic == mNot: if op.magic == mNot:
linefmt(p, cpsStmts, linefmt(p, cpsStmts,
"if ($1) #raiseFieldError($2);$n", "if ($1) #raiseFieldError($2);$n",

View file

@ -1157,9 +1157,10 @@ proc genCheckedFieldOp(p: PProc, n: PNode, addrTyp: PType, r: var TCompRes) =
useMagic(p, "raiseFieldError") useMagic(p, "raiseFieldError")
useMagic(p, "makeNimstrLit") useMagic(p, "makeNimstrLit")
let msg = genFieldError(field, disc)
lineF(p, "if ($1[$2.$3]$4undefined) { raiseFieldError(makeNimstrLit($5)); }$n", lineF(p, "if ($1[$2.$3]$4undefined) { raiseFieldError(makeNimstrLit($5)); }$n",
setx.res, tmp, disc.loc.r, if negCheck: ~"!==" else: ~"===", setx.res, tmp, disc.loc.r, if negCheck: ~"!==" else: ~"===",
makeJSString(field.name.s)) makeJSString(msg))
if addrTyp != nil and mapType(p, addrTyp) == etyBaseIndex: if addrTyp != nil and mapType(p, addrTyp) == etyBaseIndex:
r.typ = etyBaseIndex r.typ = etyBaseIndex

View file

@ -1575,3 +1575,15 @@ proc getTokSym*(r: TSrcGen): PSym =
result = r.tokens[r.idx-1].sym result = r.tokens[r.idx-1].sym
else: else:
result = nil result = nil
proc quoteExpr*(a: string): string {.inline.} =
## can be used for quoting expressions in error msgs.
"'" & a & "'"
proc genFieldError*(field: PSym, disc: PSym): string =
## this needs to be in a module accessible by jsgen, ccgexprs, and vm to
## provide this error msg FieldError; msgs would be better but it does not
## import ast
result = field.name.s.quoteExpr & " is not accessible using discriminant " &
disc.name.s.quoteExpr & " of type " &
disc.owner.name.s.quoteExpr

View file

@ -26,7 +26,7 @@ proc raiseIndexError() {.compilerproc, noinline.} =
sysFatal(IndexError, "index out of bounds") sysFatal(IndexError, "index out of bounds")
proc raiseFieldError(f: string) {.compilerproc, noinline.} = proc raiseFieldError(f: string) {.compilerproc, noinline.} =
sysFatal(FieldError, f, " is not accessible") sysFatal(FieldError, f)
proc chckIndx(i, a, b: int): int = proc chckIndx(i, a, b: int): int =
if i >= a and i <= b: if i >= a and i <= b:

View file

@ -163,7 +163,7 @@ proc raiseIndexError(i, a, b: int) {.compilerproc, noreturn.} =
raise newException(IndexError, formatErrorIndexBound(int(i), int(a), int(b))) raise newException(IndexError, formatErrorIndexBound(int(i), int(a), int(b)))
proc raiseFieldError(f: string) {.compilerproc, noreturn.} = proc raiseFieldError(f: string) {.compilerproc, noreturn.} =
raise newException(FieldError, f & " is not accessible") raise newException(FieldError, f)
proc setConstr() {.varargs, asmNoStackFrame, compilerproc.} = proc setConstr() {.varargs, asmNoStackFrame, compilerproc.} =
asm """ asm """