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

runnableExamples can now be used for var|let|const|type|fwd routines (all declarations now work); fix #18305 RFCs/309 #18604

Closed

Conversation

timotheecour
Copy link
Member

@timotheecour timotheecour commented Jul 28, 2021

before PR

impossible to attach a runnableExamples to a var|let|const|type|fwd routine

after PR

we can attach a runnableExamples to a var|let|const|type|fwd routine, so that any declaration can have an attached runnableExamples that will appear next to it in docs

examples

  • see extensive tests added to this PR
    here's a simple one:
## in top level
runnableExamples: discard # ditto
## ditto
runnableExamples: discard # ditto

type Foo* = object
runnableExamples: discard # shows below docs for Foo
runnableExamples: discard # ditto
## ditto

const Digits* = {'0'..'9'}
runnableExamples: assert Digits.len == 10 # shows below docs for Digits

it also works for fwd declarations, even if the implementation is in another include file:

# main.nim:
proc fn*()
runnableExamples: discard # will be below docs for `fn`
include impl

# impl.nim:
proc fn() = discard

compatibility note

a key aspect of this PR is that it won't break any compiler code or user macros, since the AST is not modified (instead, all that changes is how docgen interprets how runnableExamples are attached).

# in foo.nim
## in top level
runnableExamples: discard # in top level
type Foo* = object
runnableExamples: discard # previously in top level; now shows in docs below `Foo`, as would be the case for what we're used to in routines

new warning: warnInvalidDocSection

it currently triggers for https://nim-lang.github.io/Nim/switch_memory.html#memalign,csize,csize which was indeed incorrect (the comments are lumped on top but should be attached to APIs instead)

alternative considered and rejected

TLDR: it would break user defined macros.

attaching an extra PNode to declarations (eg const) as follows:

const Digits* = {'0'..'9'}
  runnableExamples: assert Digits.len == 10

This would not work well, because it would break both compiler code (fixable, but tricky) and user defined macros (more problematic since we have no control);

indeed the following:

const foo* = 1
  runnableExamples: discard

would become:

ConstSection
  ConstDef
    Postfix
      Ident "*"
      Ident "foo"
    Empty
    IntLit 1
    Call
      Ident "runnableExamples"
      StmtList
        DiscardStmt
          Empty

and this would break user defined macros for example code using n[^2] to access the type (in const foo: int = 1) would now point to the wrong node; note that n[^2] is indeed so far the correct thing to use instead of n[1] so that it works with const foo1, foo2: int = 1 etc; ditto with n[^1] to access the RHS.

links

@timotheecour timotheecour changed the title runnableExamples can now be used for var|let|const|type|fwd routines runnableExamples can now be used for var|let|const|type|fwd routines (all declarations) Jul 28, 2021
@timotheecour timotheecour changed the title runnableExamples can now be used for var|let|const|type|fwd routines (all declarations) runnableExamples can now be used for var|let|const|type|fwd routines (all declarations now work) Jul 28, 2021
else: discard
elif isRunnableExamplesRoot(n):
if state in {rsStart, rsComment, rsRunnable}:
let (rdoccmd, code) = prepareExample(d, n, topLevel)
Copy link
Member Author

Choose a reason for hiding this comment

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

this block is just re-indented

@@ -614,6 614,25 @@ type RunnableState = enum
rsRunnable
rsDone

when false:
proc toStr(a: ItemFragment): string =
Copy link
Member Author

Choose a reason for hiding this comment

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

useful for debugging; I've used this in a few PR's; i can remove or move to another PR if needed

@timotheecour timotheecour marked this pull request as draft July 28, 2021 08:48
@timotheecour timotheecour force-pushed the pr_runnableExamples_everywhere branch from d116d55 to 747ef22 Compare July 28, 2021 21:11
@timotheecour timotheecour marked this pull request as ready for review July 28, 2021 22:29
@timotheecour timotheecour changed the title runnableExamples can now be used for var|let|const|type|fwd routines (all declarations now work) runnableExamples can now be used for var|let|const|type|fwd routines (all declarations now work); fix #18305 RFCs/309 Jul 28, 2021
@timotheecour timotheecour requested a review from Araq July 28, 2021 23:10
@timotheecour timotheecour added the Ready For Review (please take another look): ready for next review round label Jul 28, 2021
@timotheecour timotheecour force-pushed the pr_runnableExamples_everywhere branch from 67f2d77 to 0f0b389 Compare July 29, 2021 21:46
@timotheecour
Copy link
Member Author

ping @Araq before this bitrots

compiler/ast.nim Outdated
@@ -863,6 863,7 @@ type
bitsize*: int
alignment*: int # for alignment
else: nil
examplesAttached*: seq[PNode]
Copy link
Member

Choose a reason for hiding this comment

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

Store it in a side-channel instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@timotheecour
Copy link
Member Author

PTAL

@timotheecour
Copy link
Member Author

ping @Araq

@timotheecour
Copy link
Member Author

ping @Araq, refs nim-lang/website#301 (comment); your only comment from #18604 (comment) was addressed a while ago

Comment on lines 1173 to 1174
if not d.examplesAttached.hasKey(d.lastSym.id): d.examplesAttached[d.lastSym.id] = @[]
d.examplesAttached[d.lastSym.id].add n
Copy link
Member

Choose a reason for hiding this comment

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

Use mputOrGet?

@stale
Copy link

stale bot commented Sep 30, 2022

This pull request has been automatically marked as stale because it has not had recent activity. If you think it is still a valid PR, please rebase it on the latest devel; otherwise it will be closed. Thank you for your contributions.

@stale stale bot added the stale Staled PR/issues; remove the label after fixing them label Sep 30, 2022
@stale stale bot closed this Nov 1, 2022
@Araq Araq reopened this Nov 2, 2022
@stale stale bot removed the stale Staled PR/issues; remove the label after fixing them label Nov 2, 2022
@Araq
Copy link
Member

Araq commented Nov 2, 2022

Ping @a-mr please take a look.

@a-mr
Copy link
Contributor

a-mr commented Nov 4, 2022

ok @Araq , but i'm looking into Nim external "smart links" in my free time right now, i hope this PR is not too urgent, i'll try to check it a few days later.

Offhand this needs at least 1) rebasing 2) test examples needs to be checked if they are not conflated too much.

@Araq
Copy link
Member

Araq commented Nov 5, 2022

Take all the time you need. Thank you!

@a-mr
Copy link
Contributor

a-mr commented Jul 18, 2023

I rebased the branch locally, but I have doubts about the proposed syntax itself...

IMHO it would be more uniformly if it looked like:

const Digits* =
  ## Docs
  runnableExamples:
    assert '0' in Digits
  {'0'..'9'}
#instead of the proposed:
# const Digits* = {'0'..'9'}  ## Docs
# runnableExamples: assert Digits.len == 10 # shows below docs for Digits

type Foo* =
  runnableExamples:
    discard
  object
    ...
#instead of:
# type Foo* = object
# runnableExamples: discard

It would be more logical if it was called addRunnableExamples instead, but I don't like such an imperative style anyway. E.g. it's ugly and fragile that at the top level runnableExamples means one thing — appending to the module description, and after first symbols it means another thing — appending to that symbol.

Copy link
Contributor

This pull request is stale because it has been open for 1 year with no activity. Contribute more commits on the pull request and rebase it on the latest devel, or it will be closed in 30 days. Thank you for your contributions.

@github-actions github-actions bot added the stale Staled PR/issues; remove the label after fixing them label Jul 25, 2024
Copy link
Contributor

This pull request has been marked as stale and closed due to inactivity after 395 days.

@github-actions github-actions bot closed this Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ready For Review (please take another look): ready for next review round stale Staled PR/issues; remove the label after fixing them
Projects
None yet
Development

Successfully merging this pull request may close these issues.

runnableExamples doesn't work for type|const|let|var allow runnableExamples in forward procs
3 participants