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

const folding happens too early in semantic phase so it's invisible to term rewriting macros and astToStr #9301

Open
timotheecour opened this issue Oct 11, 2018 · 3 comments
Labels
const `const x=expr` or `static: stmt`

Comments

@timotheecour
Copy link
Member

timotheecour commented Oct 11, 2018

import typetraits, strformat

block:
  template foo(cond: typed): string =
    astToStr(cond)

  block:
    const a = 1
    # a + 10 is const folded before astToStr sees it
    doAssert foo(a + 10) == "11"

  block:
    let a = 1
    doAssert foo(a + 2) == "a + 2"

block:
  template optMul{`*`(a, 2)}(a: int): int =
    1234

  # 3 * 2 is const folded before term rewriting sees it
  doAssert 3 * 2 == 6

  # term rewriting is activated
  let x = 3
  doAssert x * 2 == 1234

note:

for the TRM case, this is the root cause of what was observed by @kaushalmodi in https://scripter.co/notes/nim/#term-rewriting-macros "echo "Calculating 3 * 2:" # Why isn't term rewriting activated here?"

note:

in particular it affects doAssert: doAssert a == 2 will just show false failed as the expression obtained from astToStr instead of the more informative a == 2 failed

@Araq
Copy link
Member

Araq commented Oct 11, 2018

For the TR macros that's not a bug, TR macros are not about random rewritings, they are about optmizations and what has been reduced by the compiler doesn't require further optimization.

@timotheecour
Copy link
Member Author

after #9332 , doAssert a == 2 will now render as a == 2 instead of false

timotheecour added a commit to timotheecour/Nim that referenced this issue Oct 13, 2018
Araq pushed a commit that referenced this issue Oct 13, 2018
…9301 (#9332)

* fixes #8518; refs #9301; correctly render AST in doAssert condition
@timotheecour
Copy link
Member Author

timotheecour commented Oct 13, 2018

TR macros are not about random rewritings

well I was hoping it could be useful for "random rewritings" as you said (with caveat that any tool can be misused); a number of use cases come to mind, notably for debugging, eg warning when a certain pattern is found during compilation

krux02 pushed a commit to krux02/Nim that referenced this issue Oct 15, 2018
narimiran pushed a commit to narimiran/Nim that referenced this issue Oct 31, 2018
narimiran pushed a commit to narimiran/Nim that referenced this issue Nov 1, 2018
@timotheecour timotheecour added the const `const x=expr` or `static: stmt` label Oct 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
const `const x=expr` or `static: stmt`
Projects
None yet
Development

No branches or pull requests

2 participants