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

untyped param is typed when there's an overload with a typed param in same position #17164

Open
timotheecour opened this issue Feb 24, 2021 · 1 comment

Comments

@timotheecour
Copy link
Member

timotheecour commented Feb 24, 2021

I strongly suspect this explains a few of other issues related to untped ...
EDIT: indeed, it's root cause for #14827 plus other issues listed there.

  • an untyped param is incorrectly reported as typed (nnkSym) when the caller is overloaded
  • callsite on the other hand correctly reports the AST as untyped

this is the root cause for the CI failure in #17156 when (undone in b81a987) when let checked = conditions was attempted in place of let checked = callsite()[1] (ie, callsite was required to make it work for the reduced example refs #17156 (comment)).

Example

when defined case9:
  import macros

  macro check2(body: untyped): untyped =
    echo "---"
    echo callsite().repr
    let a1 = body
    let a2 = callsite()[1]
    echo a1.treeRepr
    echo a2.treeRepr

  block:
    let check2 = 123
    var b1 = 1
    check2(b1)

  block:
    var b2 = 1
    check2(b2)

  block:
    proc check2(a: float, b: string) = discard
    var b3 = 1
    check2(b3)

  block:
    macro check2(a: typed, b: string) = discard
    var b4 = 1
    check2(b4)

  block:
    macro check2(a: untyped, b: string) = discard
    var b5 = 1
    check2(b5)

Current Output

---
check2(b1)
Sym "b1"
Ident "b1"
---
check2(b2)
Ident "b2"
Ident "b2"
---
check2(b3)
Sym "b3"
Ident "b3"
---
check2(b4)
Sym "b4"
Ident "b4"
---
check2(b5)
Ident "b5"
Ident "b5"

Expected Output

all Sym should be Ident

Additional Information

1.5.1 c274e67

this is a bit tricky. What's probably happening is that when compiler semchecks check2(b3), since check2 is overloaded, it runs sigmatch to pick the correct overload, which (currently) involves sem-checking the params (b3); by that point b3 is a typed AST.

For some reason callsite is correct, so the compiler must be caching the original AST somewhere.

proposed solution

make sigmatch smarter so that it avoids semchecking a param that's untyped if it can determine it's ok to do so;
TODO: elaborate on details...

@timotheecour timotheecour changed the title untyped param is typed when caller is overloaded untyped param is typed when there's an overload with a typed param in same position Feb 24, 2021
@Araq
Copy link
Member

Araq commented Feb 24, 2021

Well known sigmatch bug, hard to fix, unfortunately. sigmatch could really see some refactorings...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants