Skip to content

Commit

Permalink
fix(sem): fix crash routine pragma redefinition (#1445)
Browse files Browse the repository at this point in the history
## Summary

The compiler no longer crashes when encountering a pragma redefinition
error on a routine. This was due to unnecessary asserts in error message
code

## Details
* dropped the asserts that added no value
* dropped the redundancy in the error message itself, no need to repeat
the symbol for the definition, as they will be the same
* error message generation now properly uses string format ( `%` )
instead of concatenation

Fixes #1444

---------

Co-authored-by: zerbina <[email protected]>
  • Loading branch information
saem and zerbina authored Sep 3, 2024
1 parent fd9dbe5 commit c587666
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
4 changes: 2 additions & 2 deletions compiler/ast/report_enums.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1057,11 +1057,11 @@ const
rsemRedefinitionOf,
rsemInvalidMethodDeclarationOrder, # [s, witness]
rsemIllegalCallconvCapture, # [symbol, owner]
rsemDeprecated # [symbol, use-instead]
rsemDeprecated, # [symbol, use-instead]
rsemUnexpectedPragmaInDefinitionOf,
}

rsemReportOneSym* = {
rsemUnexpectedPragmaInDefinitionOf,
rsemDoubleCompletionOf,

rsemOverrideSafetyMismatch,
Expand Down
4 changes: 1 addition & 3 deletions compiler/ast/reports_sem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,7 @@ proc reportSymbols*(
ast: PNode = nil
): SemReport =
case kind
of rsemReportTwoSym: assert symbols.len == 2
of rsemReportOneSym: assert symbols.len == 1
of rsemReportListSym: discard
of rsemReportTwoSym, rsemReportOneSym, rsemReportListSym: discard
else: assert false, $kind

result = SemReport(kind: kind, ast: ast)
Expand Down
11 changes: 6 additions & 5 deletions compiler/front/cli_reporter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1578,11 +1578,12 @@ proc reportBody*(conf: ConfigRef, r: SemReport): string =
result = "'method' needs a parameter that has an object type"

of rsemUnexpectedPragmaInDefinitionOf:
let proto = r.symbols[0]
let s = r.symbols[1]
result = "pragmas are only allowed in the header of a proc; redefinition of $1" %
("'" & proto.name.s & "' from " & conf $ proto.info &
" '" & s.name.s & "' from " & conf $ s.info)
let
proto = r.symbols[0]
s = r.symbols[1]
result = ("pragmas are only allowed in the header of a routine; '$1' " &
"header at $2 redefined at $3") %
[proto.name.s, conf $ proto.info, conf $ s.info]

of rsemDisjointFields:
result = ("The fields '$1' and '$2' cannot be initialized together, " &
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
discard """
description: "Error when routine def has pragma not in prototype"
errormsg: "pragmas are only allowed in the header of a routine; 'a'"
line: 8
"""

proc a()
proc a() {.gcsafe.} = discard

0 comments on commit c587666

Please sign in to comment.