-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
fix #4165(static parameters + emit) #17613
Conversation
this should be legal IMO, static should be no different from const here. when true:
proc printStr(s: static float) =
const s2 = s
# {.emit: """printf("%g\n", `s`);""" .} # bug but should be fixed instead of disallowing
{.emit: """printf("%g\n", `s2`);""" .} # works
printStr(1.23) a fix similar to #17590 should be possible |
It seems a bit different. Variable |
the semantics should be:
Try this: if e.kind == skParam and e.typ != nil and e.typ.kind == tyStatic:
result.add e.typ.n
else:
result.add newSymNode(e)
incl(e.flags, sfUsed) This seems to work, in at least the cases where a const would work eg: when defined case5:
proc printStr(s: static float) =
const s2 = s
{.emit: """printf("%g\n", `s`);""" .} # works
{.emit: """printf("%g\n", `s2`);""" .} # works
printStr(1.23) as for the example in the bug report, it also wouldn't work with a const: when true:
proc printStr() =
const s = "foo"
# would not work, because a const gets inlined upon use
{.emit: "puts(`s`->data);" .}
printStr() so that's a separate issue, and would be possible via timotheecour#553: note that this works though, with my above patch: when true:
proc printStr(s: static string) =
{.emit: """printf("%s\n", "`s`");""" .}
printStr("abc") EDITon 2nd though, the way you did it is probably simpler/more general; the error message could include something like suggesting to define a
EDITbut maybe it's still useful (eg performance reasons or simplicity) to support this use case: when true:
proc printStr(s: static string) =
{.emit: """printf("%s\n", "`s`");""" .}
printStr("abc") which, ugly as it is, works in cases like this. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
in future work we should also disable accessing a const inside an emit, for the same reasons.
instead of this:
when true:
proc printStr(s: static string) =
# now illegal for static after this PR, but not yet for const or generic static param
{.emit: """printf("%s\n", "`s`");""" .}
printStr("abc")
proc printStr2[N: static string]() =
# ditto: this still "works" after this PR but in future work should give CT error
{.emit: """puts("`N`");""" .}
printStr2["Hello, Nim"]()
users should turn the const or static into a codegen'd value via for eg:
when true:
proc printStr(s: static string) =
let s2 = s # now s2 is codegen'd
{.emit: """printf("%s\n", `s2`->data);""" .}
# or this:
let s3 = s.cstring
{.emit: """printf("%s\n", `s3`);""" .}
printStr("abc")
fix #4165