summary refs log tree commit diff stats
diff options
context:
space:
mode:
authoralaviss <alaviss@users.noreply.github.com>2019-01-23 22:35:34 +0700
committerAndreas Rumpf <rumpf_a@web.de>2019-01-23 16:35:34 +0100
commit35d96d8749809863bbbb6ead1e8cf887a95a94e5 (patch)
tree922c9ddb1310ecb4f67006200c353505e9b17012
parentbad5ad6dc764e2b1505d27a61b949079040a5f3c (diff)
downloadNim-35d96d8749809863bbbb6ead1e8cf887a95a94e5.tar.gz
compiler/sem*: better lineinfo for templates (#10428)
* compiler/sem*: better lineinfo for templates

Lineinfo for templates is inconsistant across the compiler, for example:

    doAssert true
    ^        ^

    a[int](10)
    ^^    ^

The `^` marks where the compiler thinks the template starts.

For qualified call, we got the same situation with `proc`s before #10427:

    system.once
          ^

Generics lineinfo within template declaration is also incorrect, for
example, this is where the compiler believes the `T` in `[T]` is:

    template a[T](b: T)
                  ^

This PR addresses all of these problems.

* nimsuggest: add tests for template highlighting
-rw-r--r--compiler/semexprs.nim14
-rw-r--r--compiler/semtypes.nim4
-rw-r--r--nimsuggest/tests/tgeneric_highlight.nim4
-rw-r--r--nimsuggest/tests/tqualified_highlight.nim5
-rw-r--r--nimsuggest/tests/ttemplate_highlight.nim9
5 files changed, 27 insertions, 9 deletions
diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim
index 96fefa4b8..37e2ca33f 100644
--- a/compiler/semexprs.nim
+++ b/compiler/semexprs.nim
@@ -24,15 +24,18 @@ const
 
 proc semTemplateExpr(c: PContext, n: PNode, s: PSym,
                      flags: TExprFlags = {}): PNode =
-  markUsed(c.config, n.info, s, c.graph.usageSym)
-  onUse(n.info, s)
+  let info = getCallLineInfo(n)
+  markUsed(c.config, info, s, c.graph.usageSym)
+  onUse(info, s)
+  # Note: This is n.info on purpose. It prevents template from creating an info
+  # context when called from an another template
   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)
 
   # XXX: A more elaborate line info rewrite might be needed
-  result.info = n.info
+  result.info = info
 
 proc semFieldAccess(c: PContext, n: PNode, flags: TExprFlags = {}): PNode
 
@@ -1084,8 +1087,9 @@ proc semSym(c: PContext, n: PNode, sym: PSym, flags: TExprFlags): PNode =
     if efNoEvaluateGeneric in flags and s.ast[genericParamsPos].len > 0 or
        (n.kind notin nkCallKinds and s.requiredParams > 0) or
        sfCustomPragma in sym.flags:
-      markUsed(c.config, n.info, s, c.graph.usageSym)
-      onUse(n.info, s)
+      let info = getCallLineInfo(n)
+      markUsed(c.config, info, s, c.graph.usageSym)
+      onUse(info, s)
       result = symChoice(c, n, s, scClosed)
     else:
       result = semTemplateExpr(c, n, s, flags)
diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim
index e4f7dc147..fbf363834 100644
--- a/compiler/semtypes.nim
+++ b/compiler/semtypes.nim
@@ -1015,8 +1015,8 @@ proc liftParamType(c: PContext, procKind: TSymKind, genericParams: PNode,
     result = addImplicitGeneric(copyType(paramType, getCurrOwner(c), false))
 
   of tyGenericParam:
-    markUsed(c.config, info, paramType.sym, c.graph.usageSym)
-    onUse(info, paramType.sym)
+    markUsed(c.config, paramType.sym.info, paramType.sym, c.graph.usageSym)
+    onUse(paramType.sym.info, paramType.sym)
     if tfWildcard in paramType.flags:
       paramType.flags.excl tfWildcard
       paramType.sym.kind = skType
diff --git a/nimsuggest/tests/tgeneric_highlight.nim b/nimsuggest/tests/tgeneric_highlight.nim
index 85bef7c43..334323613 100644
--- a/nimsuggest/tests/tgeneric_highlight.nim
+++ b/nimsuggest/tests/tgeneric_highlight.nim
@@ -1,8 +1,8 @@
 newSeq[int]()
 system.newSeq[int]()#[!]#
+offsetOf[int]()
 
 discard """
-disabled:true
 $nimsuggest --tester $file
 >highlight $1
 highlight;;skType;;1;;7;;3
@@ -15,4 +15,6 @@ highlight;;skProc;;2;;7;;6
 highlight;;skProc;;2;;7;;6
 highlight;;skType;;2;;14;;3
 highlight;;skProc;;2;;7;;6
+highlight;;skTemplate;;3;;0;;8
+highlight;;skType;;3;;9;;3
 """
diff --git a/nimsuggest/tests/tqualified_highlight.nim b/nimsuggest/tests/tqualified_highlight.nim
index fe7c889d6..1b34dbade 100644
--- a/nimsuggest/tests/tqualified_highlight.nim
+++ b/nimsuggest/tests/tqualified_highlight.nim
@@ -1,9 +1,12 @@
 system.echo#[!]#
+system.once
 
 discard """
-disabled:true
 $nimsuggest --tester $file
 >highlight $1
 highlight;;skProc;;1;;7;;4
 highlight;;skProc;;1;;7;;4
+highlight;;skTemplate;;2;;7;;4
+highlight;;skTemplate;;2;;7;;4
+highlight;;skTemplate;;2;;7;;4
 """
diff --git a/nimsuggest/tests/ttemplate_highlight.nim b/nimsuggest/tests/ttemplate_highlight.nim
new file mode 100644
index 000000000..2cbac3be5
--- /dev/null
+++ b/nimsuggest/tests/ttemplate_highlight.nim
@@ -0,0 +1,9 @@
+doAssert true#[!]#
+
+discard """
+$nimsuggest --tester $1
+>highlight $1
+highlight;;skTemplate;;1;;0;;8
+highlight;;skTemplate;;1;;0;;8
+highlight;;skEnumField;;1;;9;;4
+"""