Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rst: add missing line/column info for some warnings #18383

Merged
merged 29 commits into from
Jul 20, 2021

Conversation

a-mr
Copy link
Contributor

@a-mr a-mr commented Jun 28, 2021

Some rst node types are post-processed after parsing, that is in either resolveSubs or in rstgen.nim, while lines/columns are stored only in tokens during parsing stage. There was no way to print proper line/column for the corresponding warnings or errors. Now for all those 7 node kinds the line/column info is saved in a dedicated field PRstNode.loc.

Fixes bug 8 from #17340 .

@@ -77,6 +77,11 @@ type

PRstNode* = ref RstNode ## an RST node
RstNodeSeq* = seq[PRstNode]
PRstLocation* = ref object ## location inside a file
filename*: string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would try to reuse the TLineInfo mechanism.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see also NimLineInfo refs nim-lang/RFCs#387

lib/packages/docutils/rst.nim Outdated Show resolved Hide resolved
compiler/docgen.nim Outdated Show resolved Hide resolved
compiler/docgen.nim Outdated Show resolved Hide resolved
compiler/docgen.nim Outdated Show resolved Hide resolved
compiler/lineinfos.nim Outdated Show resolved Hide resolved
if fid.int < files.len:
result = files[fid.int]
else:
result = "input"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is input used for? is that same as compiler/msgs.nim:177:3: commandLineDesc* = "command line" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a fallback value, it should not happen. Alternatively a ValueError can be raised here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it shouldn't happen than maybe use doAssert? (ie indicates program logic bug, not user error)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, switched to doAssert.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... and doAssert caught a bug :-)

tests/stdlib/trst.nim Outdated Show resolved Hide resolved
rnRef
rnLeaf 'brokenLink'
""")
removeFile("other.rst")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • not DRY
  • bad style to write in the source tree and with a generic name (eg what if you ^C testament and are left with un-clean git status, or worse, it overwrites another test file)
  • instead use either:
import stdtest/specialpaths
const tmpFile = buildDir / "D20210706T170748.rst"

or createTempFile from std/tempfiles

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copy pasted it from this very file. Let us postpone it for trst.nim refactoring PR.

compiler/lineinfos.nim Outdated Show resolved Hide resolved
@a-mr a-mr force-pushed the rst-warnings-missing-lineinfo branch from fe74530 to e8ede97 Compare July 11, 2021 12:47
also display errors/warnings properly when footnote references are from
different files
doAssert "<h2 id=\"another2\">Another2</h2>" in output10
doAssert "<h3 id=\"more3\"><center>More3</center></h3>" in output10

doAssert "<h1 id=\"level1\"><center>Level1</center></h1>" in output
Copy link
Member

@timotheecour timotheecour Jul 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about using triple quoted strings instead so that it's easier to copy paste without quoting, eg:

doAssert "<h1 id=\"level1\"><center>Level1</center></h1>" in output
=>
doAssert """<h1 id="level1"><center>Level1</center></h1>""" in output

can be done in separate PR though

it could even be written as:

for line in """
<h1 id="level1"><center>Level1</center></h1>
<h2 id="level2">Level2</h2>
...
""":
  doAssert line in output

if you want to maintain ordering there's also testutils.greedyOrderedSubsetLines

rstGenera.renderRstToOut(rst, output)
doAssert rstGenera.meta[metaTitle] == "Title0"
doAssert rstGenera.meta[metaSubTitle] == ""
doAssert output ==
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use multiline literal instead (can be done in separate PR)

@@ -628,7 +630,7 @@ let x = 1
let p2 = """<p>Par2 <tt class="docutils literal"><span class="pre">value2</span></tt>.</p>"""
let p3 = """<p>Par3 <tt class="docutils literal"><span class="pre">""" & id"value3" & "</span></tt>.</p>"
let p4 = """<p>Par4 <tt class="docutils literal"><span class="pre">value4</span></tt>.</p>"""
let expected = p1 & p2 & "\n" & p3 & "\n" & p4 & "\n"
let expected = p1 & p2 & "\n" & p3 & "\n" & p4
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto, multiline literal is much clearer than concatenating like this

"extra arguments were given to number-lines: ' let a = 1'")
check "" == output

test "code-block warning":
Copy link
Member

@timotheecour timotheecour Jul 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a blocker because the situation after this PR improves in all cases, but the line numbers and columns are off by ~1 for nested doc comments, eg:

before PR: all lines/cols are 1 (useless)
after PR: main3 is correct, main1 and main2 are off by ~1

proc main1*()=
  ##[
    .. code:: Nim
       :unsupportedField: anything
    .. code:: unsupportedLang
       anything
    ```anotherLang
    someCode
  ```
  ]##
  discard


proc main2*()=
  ##[
  .. code:: Nim
     :unsupportedField: anything
  .. code:: unsupportedLang
     anything
  ```anotherLang
  someCode
  ```
  ]##
  discard

proc main3*()=
  ## .. code:: Nim
  ##    :unsupportedField: anything
  ## .. code:: unsupportedLang
  ##    anything
  ## ```anotherLang
  ## someCode
  ## ```
  discard

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, line info is saved only for first # of ##, not for real comment inside.
We talked about this problem in timotheecour#658

footnotes: seq[FootnoteSubst] # correspondence b/w footnote label,
# number, order of occurrence
msgHandler: MsgHandler # How to handle errors.
findFile: FindFileHandler # How to find files.
filename: string
filenames*: seq[string] # map FileIndex -> file name (for storing
Copy link
Member

@timotheecour timotheecour Jul 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it's a map, shouldn't this be a Table? this would also simplify setCurrFilename , getFilename etc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with both ways, readability is the same.

  • seq[string] should be faster for typical cases when there is only 1 file
  • Table will be faster when there is >~ 10 files included

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think performance is any concern here (not a bottleneck), but it seemed to me Table would simplify code, but again, can be deferred to future work

Copy link
Member

@timotheecour timotheecour left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, remaining comments are not critical and can be addressed in a followup PR so that this PR doesn't bitrot

a-mr and others added 8 commits July 16, 2021 00:49
Co-authored-by: Timothee Cour <[email protected]>
Error: cannot prove that it's safe to initialize 'info' with the runtime value for the discriminator 'kind'
Copy link
Member

@timotheecour timotheecour left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still LGTM (remaing comments can be addressed in followup PR)

@narimiran
Copy link
Member

@a-mr I've pushed two commits, which remove imports of compiler/lineinfos (per @Araq's request). I wanted to let you know, so if you decide to push something more, to pull first.

@Araq Araq merged commit 8c7ee96 into nim-lang:devel Jul 20, 2021
PMunch pushed a commit to PMunch/Nim that referenced this pull request Mar 28, 2022
* rst: add missing line/column info for some warnings

* add workaround

* use TLineInfo/FileIndex for storing file names

* fix blank lines in include file (rm harmful strip)

* don't use ref TLineInfo

* return `hasToc` as output parameter for uniformity

* Update compiler/docgen.nim

Co-authored-by: Timothee Cour <[email protected]>

* Update compiler/docgen.nim

Co-authored-by: Timothee Cour <[email protected]>

* Update lib/packages/docutils/rst.nim

Co-authored-by: Timothee Cour <[email protected]>

* address review - stylistic things

* Update compiler/docgen.nim

Co-authored-by: Timothee Cour <[email protected]>

* unify RST warnings/errors names

* doAssert + minor name change

* fix a bug caught by doAssert

* apply strbasics.strip to final HTML/Latex

* rm redundant filename

* fix test after rebase

* delete `order` from rnFootnoteRef,

also display errors/warnings properly when footnote references are from
different files

* Update compiler/lineinfos.nim

Co-authored-by: Timothee Cour <[email protected]>

* Update lib/packages/docutils/rstast.nim

Co-authored-by: Timothee Cour <[email protected]>

* Update lib/packages/docutils/rstast.nim

Co-authored-by: Timothee Cour <[email protected]>

* Update lib/packages/docutils/rstast.nim

Co-authored-by: Timothee Cour <[email protected]>

* revert because of error:

Error: cannot prove that it's safe to initialize 'info' with the runtime value for the discriminator 'kind'

* Update lib/packages/docutils/rstgen.nim

Co-authored-by: Timothee Cour <[email protected]>

* apply suggestion

* Update lib/packages/docutils/rst.nim

Co-authored-by: Timothee Cour <[email protected]>

* add Table for string->file name mapping

* do not import compiler/lineinfos

* fix ambiguous calls

Co-authored-by: Timothee Cour <[email protected]>
Co-authored-by: narimiran <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants