Skip to content

Commit

Permalink
refs nim-lang#18278: do not gag fatal msgs
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Jun 17, 2021
1 parent 5a3456d commit f86e0c0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
13 changes: 8 additions & 5 deletions compiler/lineinfos.nim
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ proc createDocLink*(urlSuffix: string): string =

type
TMsgKind* = enum
errUnknown, errInternal, errIllFormedAstX, errCannotOpenFile,
# fatal errors
errUnknown, errFatal, errInternal,
# non-fatal errors
errIllFormedAstX, errCannotOpenFile,
errXExpected,
errGridTableNotImplemented,
errMarkdownIllformedTable,
Expand All @@ -38,7 +41,7 @@ type
errProveInit, # deadcode
errGenerated,
errUser,

# warnings
warnCannotOpenFile = "CannotOpenFile", warnOctalEscape = "OctalEscape",
warnXIsNeverRead = "XIsNeverRead", warnXmightNotBeenInit = "XmightNotBeenInit",
warnDeprecated = "Deprecated", warnConfigDeprecated = "ConfigDeprecated",
Expand All @@ -64,7 +67,7 @@ type
warnCannotOpen = "CannotOpen",
warnFileChanged = "FileChanged",
warnUser = "User",

# hints
hintSuccess = "Success", hintSuccessX = "SuccessX",
hintCC = "CC",
hintLineTooLong = "LineTooLong", hintXDeclaredButNotUsed = "XDeclaredButNotUsed",
Expand All @@ -83,6 +86,7 @@ type
const
MsgKindToStr*: array[TMsgKind, string] = [
errUnknown: "unknown error",
errFatal: "fatal error: $1",
errInternal: "internal error: $1",
errIllFormedAstX: "illformed AST: $1",
errCannotOpenFile: "cannot open '$1'",
Expand Down Expand Up @@ -180,8 +184,7 @@ const
]

const
fatalMin* = errUnknown
fatalMax* = errInternal
fatalMsgs* = {errUnknown..errInternal}
errMin* = errUnknown
errMax* = errUser
warnMin* = warnCannotOpenFile
Expand Down
3 changes: 1 addition & 2 deletions compiler/modules.nim
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ proc compileSystemModule*(graph: ModuleGraph) =

proc wantMainModule*(conf: ConfigRef) =
if conf.projectFull.isEmpty:
fatal(conf, newLineInfo(conf, AbsoluteFile(commandLineDesc), 1, 1), errGenerated,
"command expects a filename")
fatal(conf, gCmdLineInfo, "command expects a filename")
conf.projectMainIdx = fileInfoIdx(conf, addFileExt(conf.projectFull, NimExt))

proc compileProject*(graph: ModuleGraph; projectFileIdx = InvalidFileIdx) =
Expand Down
15 changes: 11 additions & 4 deletions compiler/msgs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ To create a stacktrace, rerun compilation with './koch temp $1 <file>', see $2 f
quit 1

proc handleError(conf: ConfigRef; msg: TMsgKind, eh: TErrorHandling, s: string) =
if msg >= fatalMin and msg <= fatalMax:
if msg in fatalMsgs:
if conf.cmd == cmdIdeTools: log(s)
quit(conf, msg)
if msg >= errMin and msg <= errMax or
Expand Down Expand Up @@ -498,6 +498,12 @@ proc liMessage*(conf: ConfigRef; info: TLineInfo, msg: TMsgKind, arg: string,
color: ForegroundColor
ignoreMsg = false
sev: Severity
let errorOutputsOld = conf.m.errorOutputs
if msg in fatalMsgs:
# don't gag, refs bug #7080, bug #18278; this can happen with `{.fatal.}`
# or inside a `tryConstExpr`.
conf.m.errorOutputs = {eStdOut, eStdErr}

let kind = if msg in warnMin..hintMax and msg != hintUserRaw: $msg else: "" # xxx not sure why hintUserRaw is special
case msg
of errMin..errMax:
Expand Down Expand Up @@ -553,6 +559,9 @@ proc liMessage*(conf: ConfigRef; info: TLineInfo, msg: TMsgKind, arg: string,
KindFormat % $hintMsgOrigin,
resetStyle, conf.unitSep)
handleError(conf, msg, eh, s)
if msg in fatalMsgs:
# most likely would have died here but just in case, we restore state
conf.m.errorOutputs = errorOutputsOld

template rawMessage*(conf: ConfigRef; msg: TMsgKind, args: openArray[string]) =
let arg = msgKindToString(msg) % args
Expand All @@ -561,9 +570,7 @@ template rawMessage*(conf: ConfigRef; msg: TMsgKind, args: openArray[string]) =
template rawMessage*(conf: ConfigRef; msg: TMsgKind, arg: string) =
liMessage(conf, unknownLineInfo, msg, arg, eh = doAbort, instLoc())

template fatal*(conf: ConfigRef; info: TLineInfo, msg: TMsgKind, arg = "") =
# this fixes bug #7080 so that it is at least obvious 'fatal' was executed.
conf.m.errorOutputs = {eStdOut, eStdErr}
template fatal*(conf: ConfigRef; info: TLineInfo, arg = "", msg = errFatal) =
liMessage(conf, info, msg, arg, doAbort, instLoc())

template globalAssert*(conf: ConfigRef; cond: untyped, info: TLineInfo = unknownLineInfo, arg = "") =
Expand Down
2 changes: 1 addition & 1 deletion compiler/pragmas.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ proc singlePragma(c: PContext, sym: PSym, n: PNode, i: var int,
let s = expectStrLit(c, it)
recordPragma(c, it, "error", s)
localError(c.config, it.info, errUser, s)
of wFatal: fatal(c.config, it.info, errUser, expectStrLit(c, it))
of wFatal: fatal(c.config, it.info, expectStrLit(c, it))
of wDefine: processDefine(c, it)
of wUndef: processUndef(c, it)
of wCompile: processCompile(c, it)
Expand Down

0 comments on commit f86e0c0

Please sign in to comment.