Skip to content

Commit

Permalink
fix .line pragma ignoring column information (#860)
Browse files Browse the repository at this point in the history
## Summary

Use the column information provided by the tuple value, which fixes,
for example, stack traces produced by assertion failures in compile-time
contexts pointing to the wrong column.

In addition, the tuple passed to the `.line` pragma is now verified to
have the correct number of elements, which fixes the compiler crashing
when it's a tuple with less than the expected number of elements.

## Details

In `pragmaLine`:
- don't allow `nkPar` -- all literal tuple construction expressions have
  to use `nkTupleConstr` nodes
- require the provided tuple node to have exactly three elements
- use more descriptive names instead of `x` and `y`
- use the third tuple element's value (which is required to be an
  integer value) as the column information
- build the `TLineInfo` with `newLineInfo`, which also makes sure
  that out-of-range line and column numbers are properly handled (by
  clamping them)
- use the correct node when creating errors and use `wrapError` for
  proper propagation

In addition, the error message for when the provided tuple value doesn't
have the correct shape is slightly improved, now detailing how the
tuple is required to look like.

A test (`t9768.nim`) that relied on incorrect column being set is
adjusted.
  • Loading branch information
zerbina authored Aug 26, 2023
1 parent 323e1df commit 9efc16f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
3 changes: 2 additions & 1 deletion compiler/front/cli_reporter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,8 @@ proc reportBody*(conf: ConfigRef, r: SemReport): string =
result = "empty 'asm' statement"

of rsemLinePragmaExpectsTuple:
result = "tuple expected"
result = "a tuple value of the form '(string, int, int)' is expected," &
" but got: '" & r.ast.render & "'"

of rsemRaisesPragmaExpectsObject:
result = "invalid type for raises/tags list"
Expand Down
34 changes: 19 additions & 15 deletions compiler/sem/pragmas.nim
Original file line number Diff line number Diff line change
Expand Up @@ -826,23 +826,27 @@ proc pragmaLine(c: PContext, n: PNode): PNode =
if n.kind in nkPragmaCallKinds and n.len == 2:
n[1] = c.semConstExpr(c, n[1])
let a = n[1]
if a.kind in {nkPar, nkTupleConstr}:
if a.kind == nkTupleConstr and a.len == 3:
# unpack the tuple
var x = a[0]
var y = a[1]
if x.kind == nkExprColonExpr: x = x[1]
if y.kind == nkExprColonExpr: y = y[1]
if x.kind != nkStrLit:
result = c.config.newError(n,
PAstDiag(kind: adSemStringLiteralExpected))
elif y.kind != nkIntLit:
result = c.config.newError(n, PAstDiag(kind: adSemIntLiteralExpected))
let
path = a[0].skipColon
line = a[1].skipColon
col = a[2].skipColon

if path.kind == nkStrLit and line.kind == nkIntLit and
col.kind == nkIntLit:
n.info = newLineInfo(fileInfoIdx(c.config, AbsoluteFile(path.strVal)),
line.intVal.int,
col.intVal.int)
else:
n.info.fileIndex = fileInfoIdx(c.config, AbsoluteFile(x.strVal))
n.info.line = uint16(y.intVal)
else:
result = c.config.newError(
n, PAstDiag(kind: adSemLinePragmaExpectsTuple))
n[1] = c.config.newError(
a, PAstDiag(kind: adSemLinePragmaExpectsTuple))
elif a.kind != nkError:
n[1] = c.config.newError(
a, PAstDiag(kind: adSemLinePragmaExpectsTuple))

if n[1].kind == nkError:
result = c.config.wrapError(n)
else:
# sensible default:
n.info = getInfoContext(c.config, -1)
Expand Down
2 changes: 1 addition & 1 deletion tests/errmsgs/t9768.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ discard """
nimout: '''
stack trace: (most recent call last)
t9768.nim(28, 33) main
t9768.nim(23, 11) foo1
t9768.nim(23, 12) foo1
'''
"""

Expand Down
7 changes: 7 additions & 0 deletions tests/errmsgs/twrong_line_argument.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
discard """
errormsg: "a tuple value of the form '(string, int, int)' is expected, but got: '(\"\",)'"
line: 6
"""

{.line: ("",).}:
discard

2 comments on commit 9efc16f

@bung87
Copy link
Contributor

@bung87 bung87 commented on 9efc16f Aug 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how this takes effect? I thought without line pragma param it should go to n.info = getInfoContext(c.config, -1) while this patch only change if branch. I try to port this to my nim's PR still get t9768.nim(23, 3) foo1

@bung87
Copy link
Contributor

@bung87 bung87 commented on 9efc16f Aug 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

found nim error message is "t9768.nim(24, 3) a < 4", while here is "t9768.nim(23, 12)" there must has difference in other place

Please sign in to comment.