Skip to content

Conversation

@timotheecour
Copy link
Member

@timotheecour timotheecour commented Mar 22, 2021

refs #17292

let f = () => (discard)

now renders as:

let f = () => (discard)

instead of this:

let f = () => discard

which is illegal (it was making runnableExamples invalid prior to #17282, but the underlying bug remained and affected things like parseExpr, parseStmt, or operations based on renderModule)

@Araq
Copy link
Member

Araq commented Mar 23, 2021

Hardly acceptable. Special case, what about an empty return? Why is a statement list with a discard special for the renderer? Makes no sense to me.

@timotheecour
Copy link
Member Author

(return) is not legal but (discard) is legal and useful; for eg, it's the simplest way to write a sugar lambda returning void: let f = (x: int) => (discard)

these work as you'd expect after this PR:

macro deb(a) =
  echo a.repr

deb:
  block:
    discard
  discard

  block:
    when true: discard

  # let a = b => discard # illegal
  let a = b => (discard) # legal but, before this PR, repr rendered it incorrectly

  block:
    return

unless you can find an example that gets worse after this PR, I think this is a useful bugfix.

@Araq
Copy link
Member

Araq commented Mar 23, 2021

Why does

  block:
    discard

not produce the () after this patch? Subtle logic here...

@timotheecour timotheecour force-pushed the pr_refs_17292_discard_stmtexpr branch from 5c12f68 to 959572f Compare April 2, 2021 22:23
@timotheecour
Copy link
Member Author

timotheecour commented Apr 2, 2021

PTAL

Why does ...
not produce the () after this patch? Subtle logic here...

because:

  • block: discard is handled by gstmts directly
  • a=>(discard) is handled by gsub

the PR does the correct thing in both cases. I've added more tests.

@timotheecour timotheecour force-pushed the pr_refs_17292_discard_stmtexpr branch 2 times, most recently from eb0f803 to 0a27db1 Compare April 3, 2021 07:52
@timotheecour timotheecour force-pushed the pr_refs_17292_discard_stmtexpr branch from 0a27db1 to c88d69a Compare April 3, 2021 07:54
@timotheecour
Copy link
Member Author

@Araq ping on this

@Araq
Copy link
Member

Araq commented Apr 9, 2021

Still reviewing it -- it's adhoc and as I said, who knows if return doesn't need the same. And then what about break and raise?

@timotheecour
Copy link
Member Author

Still reviewing it -- it's adhoc and as I said, who knows if return doesn't need the same. And then what about break and raise?

I can't find any case involving return, break, raise where repr gives incorrect results, and I can't find a case that would break as a result of this PR. This PR fixes a known bug, if we later find another bug, it can always be fixed then.

@Araq Araq merged commit 1b65b9c into nim-lang:devel Apr 16, 2021
@timotheecour timotheecour deleted the pr_refs_17292_discard_stmtexpr branch April 16, 2021 17:01
timotheecour added a commit to timotheecour/Nim that referenced this pull request Apr 16, 2021
PMunch pushed a commit to PMunch/Nim that referenced this pull request Mar 28, 2022
…card` which gave illegal code (nim-lang#17455)

* refs nim-lang#17292 fix `repr` with (discard)
* add tests
* add more tests
PMunch pushed a commit to PMunch/Nim that referenced this pull request Mar 28, 2022
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.

2 participants