compiler: show name of instantiating context in error traces (#6763) (#9207)

This commit is contained in:
xzfc
2018-10-11 14:34:56 +07:00
committed by Andreas Rumpf
parent da4215af6a
commit 8fc7cecfa2
9 changed files with 31 additions and 19 deletions

View File

@@ -1784,3 +1784,6 @@ template typeCompleted*(s: PSym) =
incl s.flags, sfNoForward
template getBody*(s: PSym): PNode = s.ast[bodyPos]
template detailedInfo*(sym: PSym): string =
sym.name.s

View File

@@ -248,7 +248,7 @@ type
## some close token.
errorOutputs*: TErrorOutputs
msgContext*: seq[TLineInfo]
msgContext*: seq[tuple[info: TLineInfo, detail: string]]
lastError*: TLineInfo
filenameToIndexTbl*: Table[string, FileIndex]
fileInfos*: seq[TFileInfo]

View File

@@ -139,8 +139,8 @@ const
proc getInfoContextLen*(conf: ConfigRef): int = return conf.m.msgContext.len
proc setInfoContextLen*(conf: ConfigRef; L: int) = setLen(conf.m.msgContext, L)
proc pushInfoContext*(conf: ConfigRef; info: TLineInfo) =
conf.m.msgContext.add(info)
proc pushInfoContext*(conf: ConfigRef; info: TLineInfo; detail: string = "") =
conf.m.msgContext.add((info, detail))
proc popInfoContext*(conf: ConfigRef) =
setLen(conf.m.msgContext, len(conf.m.msgContext) - 1)
@@ -149,7 +149,7 @@ proc getInfoContext*(conf: ConfigRef; index: int): TLineInfo =
let L = conf.m.msgContext.len
let i = if index < 0: L + index else: index
if i >=% L: result = unknownLineInfo()
else: result = conf.m.msgContext[i]
else: result = conf.m.msgContext[i].info
template toFilename*(conf: ConfigRef; fileIdx: FileIndex): string =
if fileIdx.int32 < 0 or conf == nil:
@@ -340,20 +340,26 @@ proc exactEquals*(a, b: TLineInfo): bool =
proc writeContext(conf: ConfigRef; lastinfo: TLineInfo) =
const instantiationFrom = "template/generic instantiation from here"
const instantiationOfFrom = "template/generic instantiation of `$1` from here"
var info = lastinfo
for i in 0 ..< len(conf.m.msgContext):
if conf.m.msgContext[i] != lastinfo and conf.m.msgContext[i] != info:
let context = conf.m.msgContext[i]
if context.info != lastinfo and context.info != info:
if conf.structuredErrorHook != nil:
conf.structuredErrorHook(conf, conf.m.msgContext[i], instantiationFrom,
Severity.Error)
conf.structuredErrorHook(conf, context.info, instantiationFrom,
Severity.Error)
else:
let message = if context.detail == "":
instantiationFrom
else:
instantiationOfFrom.format(context.detail)
styledMsgWriteln(styleBright,
PosFormat % [toMsgFilename(conf, conf.m.msgContext[i]),
coordToStr(conf.m.msgContext[i].line.int),
coordToStr(conf.m.msgContext[i].col+1)],
PosFormat % [toMsgFilename(conf, context.info),
coordToStr(context.info.line.int),
coordToStr(context.info.col+1)],
resetStyle,
instantiationFrom)
info = conf.m.msgContext[i]
message)
info = context.info
proc ignoreMsgBecauseOfIdeTools(conf: ConfigRef; msg: TMsgKind): bool =
msg >= errGenerated and conf.cmd == cmdIdeTools and optIdeDebug notin conf.globalOptions

View File

@@ -444,7 +444,7 @@ const
proc semMacroExpr(c: PContext, n, nOrig: PNode, sym: PSym,
flags: TExprFlags = {}): PNode =
pushInfoContext(c.config, nOrig.info)
pushInfoContext(c.config, nOrig.info, sym.detailedInfo)
markUsed(c.config, n.info, sym, c.graph.usageSym)
styleCheckUse(n.info, sym)

View File

@@ -26,7 +26,7 @@ proc semTemplateExpr(c: PContext, n: PNode, s: PSym,
flags: TExprFlags = {}): PNode =
markUsed(c.config, n.info, s, c.graph.usageSym)
styleCheckUse(n.info, s)
pushInfoContext(c.config, n.info)
pushInfoContext(c.config, n.info, s.detailedInfo)
result = evalTemplate(n, s, getCurrOwner(c), c.config, efFromHlo in flags)
if efNoSemCheck notin flags: result = semAfterMacroCall(c, n, result, s, flags)
popInfoContext(c.config)

View File

@@ -348,7 +348,7 @@ proc generateInstance(c: PContext, fn: PSym, pt: TIdTable,
let gp = n.sons[genericParamsPos]
internalAssert c.config, gp.kind != nkEmpty
n.sons[namePos] = newSymNode(result)
pushInfoContext(c.config, info)
pushInfoContext(c.config, info, fn.detailedInfo)
var entry = TInstantiation.new
entry.sym = result
# we need to compare both the generic types and the concrete types:

View File

@@ -34,9 +34,9 @@ proc add(x: var string; y: char)
required type: var string
but expression 'k' is of type: Alias
t3330.nim(48, 8) template/generic instantiation from here
t3330.nim(48, 8) template/generic instantiation of `add` from here
t3330.nim(55, 6) Foo: 'bar.value' cannot be assigned to
t3330.nim(48, 8) template/generic instantiation from here
t3330.nim(48, 8) template/generic instantiation of `add` from here
t3330.nim(56, 6) Foo: 'bar.x' cannot be assigned to
expression: test(bar)'''

View File

@@ -1,6 +1,6 @@
discard """
nimout: '''tmsginfo.nim(21, 1) Warning: foo1 [User]
tmsginfo.nim(22, 13) template/generic instantiation from here
tmsginfo.nim(22, 13) template/generic instantiation of `foo2` from here
tmsginfo.nim(15, 10) Warning: foo2 [User]
tmsginfo.nim(23, 1) Hint: foo3 [User]
tmsginfo.nim(19, 7) Hint: foo4 [User]

View File

@@ -53,7 +53,10 @@ let
pegLineError =
peg"{[^(]*} '(' {\d+} ', ' {\d+} ') ' ('Error') ':' \s* {.*}"
pegLineTemplate =
peg"{[^(]*} '(' {\d+} ', ' {\d+} ') ' 'template/generic instantiation from here'.*"
peg"""
{[^(]*} '(' {\d+} ', ' {\d+} ') '
'template/generic instantiation' ( ' of `' [^`]+ '`' )? ' from here' .*
"""
pegOtherError = peg"'Error:' \s* {.*}"
pegSuccess = peg"'Hint: operation successful'.*"
pegOfInterest = pegLineError / pegOtherError