diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index 2240545062..6e203dad5c 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -825,7 +825,8 @@ proc genFieldCheck(p: BProc, e: PNode, obj: Rope, field: PSym) = v.r.add(".") v.r.add(disc.sym.loc.r) 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: linefmt(p, cpsStmts, "if ($1) #raiseFieldError($2);$n", diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index 82f445352f..f858bd32c1 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -1157,9 +1157,10 @@ proc genCheckedFieldOp(p: PProc, n: PNode, addrTyp: PType, r: var TCompRes) = useMagic(p, "raiseFieldError") useMagic(p, "makeNimstrLit") + let msg = genFieldError(field, disc) lineF(p, "if ($1[$2.$3]$4undefined) { raiseFieldError(makeNimstrLit($5)); }$n", setx.res, tmp, disc.loc.r, if negCheck: ~"!==" else: ~"===", - makeJSString(field.name.s)) + makeJSString(msg)) if addrTyp != nil and mapType(p, addrTyp) == etyBaseIndex: r.typ = etyBaseIndex diff --git a/compiler/renderer.nim b/compiler/renderer.nim index c018a2cad3..2eb9ab4062 100644 --- a/compiler/renderer.nim +++ b/compiler/renderer.nim @@ -1575,3 +1575,15 @@ proc getTokSym*(r: TSrcGen): PSym = result = r.tokens[r.idx-1].sym else: 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 diff --git a/lib/system/chcks.nim b/lib/system/chcks.nim index 0d5d16ab4c..52642bbf91 100644 --- a/lib/system/chcks.nim +++ b/lib/system/chcks.nim @@ -26,7 +26,7 @@ proc raiseIndexError() {.compilerproc, noinline.} = sysFatal(IndexError, "index out of bounds") proc raiseFieldError(f: string) {.compilerproc, noinline.} = - sysFatal(FieldError, f, " is not accessible") + sysFatal(FieldError, f) proc chckIndx(i, a, b: int): int = if i >= a and i <= b: diff --git a/lib/system/jssys.nim b/lib/system/jssys.nim index 077c81d594..67fc9b3fb8 100644 --- a/lib/system/jssys.nim +++ b/lib/system/jssys.nim @@ -163,7 +163,7 @@ proc raiseIndexError(i, a, b: int) {.compilerproc, noreturn.} = raise newException(IndexError, formatErrorIndexBound(int(i), int(a), int(b))) proc raiseFieldError(f: string) {.compilerproc, noreturn.} = - raise newException(FieldError, f & " is not accessible") + raise newException(FieldError, f) proc setConstr() {.varargs, asmNoStackFrame, compilerproc.} = asm """