summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorxzfc <xzfc@users.noreply.github.com>2018-10-11 14:34:56 +0700
committerAndreas Rumpf <rumpf_a@web.de>2018-10-11 09:34:56 +0200
commit8fc7cecfa21d666f6ae0f778570f14c92a8d223a (patch)
treec39643771b4fb3fef7433ae68377024a3c35f5e6
parentda4215af6a5046662db736ce2d5500ce0f38fe1b (diff)
downloadNim-8fc7cecfa21d666f6ae0f778570f14c92a8d223a.tar.gz
compiler: show name of instantiating context in error traces (#6763) (#9207)
-rw-r--r--compiler/ast.nim3
-rw-r--r--compiler/lineinfos.nim2
-rw-r--r--compiler/msgs.nim28
-rw-r--r--compiler/sem.nim2
-rw-r--r--compiler/semexprs.nim2
-rw-r--r--compiler/seminst.nim2
-rw-r--r--tests/concepts/t3330.nim4
-rw-r--r--tests/macros/tmsginfo.nim2
-rw-r--r--tests/testament/tester.nim5
9 files changed, 31 insertions, 19 deletions
diff --git a/compiler/ast.nim b/compiler/ast.nim
index 36c017358..ebb5937e0 100644
--- a/compiler/ast.nim
+++ b/compiler/ast.nim
@@ -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
diff --git a/compiler/lineinfos.nim b/compiler/lineinfos.nim
index 8749e764d..cd160313c 100644
--- a/compiler/lineinfos.nim
+++ b/compiler/lineinfos.nim
@@ -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]
diff --git a/compiler/msgs.nim b/compiler/msgs.nim
index d817b2956..dee1081f9 100644
--- a/compiler/msgs.nim
+++ b/compiler/msgs.nim
@@ -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
diff --git a/compiler/sem.nim b/compiler/sem.nim
index b05b33e12..0d336724c 100644
--- a/compiler/sem.nim
+++ b/compiler/sem.nim
@@ -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)
diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim
index 0516fb718..f4b603589 100644
--- a/compiler/semexprs.nim
+++ b/compiler/semexprs.nim
@@ -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)
diff --git a/compiler/seminst.nim b/compiler/seminst.nim
index 4bf1e6ef2..833edacac 100644
--- a/compiler/seminst.nim
+++ b/compiler/seminst.nim
@@ -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:
diff --git a/tests/concepts/t3330.nim b/tests/concepts/t3330.nim
index 8021db827..3d0dde5d1 100644
--- a/tests/concepts/t3330.nim
+++ b/tests/concepts/t3330.nim
@@ -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)'''
diff --git a/tests/macros/tmsginfo.nim b/tests/macros/tmsginfo.nim
index ebdce0155..f08a231fb 100644
--- a/tests/macros/tmsginfo.nim
+++ b/tests/macros/tmsginfo.nim
@@ -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]
diff --git a/tests/testament/tester.nim b/tests/testament/tester.nim
index f1f07b3c0..15f44f871 100644
--- a/tests/testament/tester.nim
+++ b/tests/testament/tester.nim
@@ -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