Skip to content

Commit

Permalink
fixes #10053 (#11951)
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour authored and Araq committed Aug 15, 2019
1 parent 296dfae commit 5112982
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
3 changes: 2 additions & 1 deletion compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion compiler/jsgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions compiler/renderer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/system/chcks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion lib/system/jssys.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand Down

0 comments on commit 5112982

Please sign in to comment.