-
-
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
[WIP] fix #13790 #13792
[WIP] fix #13790 #13792
Conversation
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.
Conversions are done with cstring(expr)
or cast[cstring](expr)
. Don't introduce new identifiers for conversion.
lib/system.nim
Outdated
template toCstring*(a: ptr char): cstring = cast[cstring](a) | ||
template toCstring*[N](a: ptr array[N, char]): cstring = cast[cstring](a) | ||
template toCstring*[N](a: array[N, char]): cstring = cast[cstring](a.addr) | ||
template toCstring*[N](a: ptr UncheckedArray[char]): cstring = cast[cstring](a) | ||
template toCstring*(a: UncheckedArray[char]): cstring = cast[cstring](a.addr) |
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.
Remove all of this. Do not add new stuff to system.nim unless it is required. And if it is required, justify why it needs to be in system.nim.
compiler/lexer.nim
Outdated
@@ -915,7 +915,7 @@ proc getSymbol(L: var TLexer, tok: var TToken) = | |||
proc endOperator(L: var TLexer, tok: var TToken, pos: int, | |||
hash: Hash) {.inline.} = | |||
var h = !$hash | |||
tok.ident = L.cache.getIdent(addr(L.buf[L.bufpos]), pos - L.bufpos, h) | |||
tok.ident = L.cache.getIdent(addr(L.buf[L.bufpos]).toCstring, pos - L.bufpos, h) |
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.
tok.ident = L.cache.getIdent(addr(L.buf[L.bufpos]).toCstring, pos - L.bufpos, h) | |
tok.ident = L.cache.getIdent(L.buf[L.bufpos].addr.toCstring, pos - L.bufpos, h) |
to be consistent with the line in 904. Also I agree with krux02 here, instead of toCstring you should use casts manually and remove toCstring.
proc setLen*(s: var string, newlen: Natural, isInit = true) {. | ||
magic: "SetLengthStr", noSideEffect.} | ||
## Sets the length of string `s` to `newlen`. | ||
## Sets the length of string `s` to `newlen`. If `isInit == true` and | ||
## If `newlen > s.len`, when `isInit == true`, new entries are '\0' including | ||
## the unreachable terminator n[s.len]. when `isInit == false`, only the | ||
## terminator is '\0' (for optimization). TODO: implement 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.
Please don't change setLen's semantics in this PR.
lib/system/dollars.nim
Outdated
@@ -1,3 +1,5 @@ | |||
const notJSnotNims = not defined(js) and not defined(nimscript) |
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.
Inline this const please
lib/system/dollars.nim
Outdated
proc strAppend*(result: var string, a: ptr char, n: int) {.inline.} = | ||
## appends `n` `char`'s from `a` to `result`, efficiently | ||
## note: should use a Slice[char] | ||
# D20200328T022947 | ||
let old = result.len | ||
result.setLen(old + n, isInit = false) # optimized here | ||
when notJSnotNims: | ||
copyMem(result[old].addr.toCstring, a, n) | ||
else: | ||
let a2 = cast[cstring](a) | ||
for i in 0..<n: result[old+i] = a2[i] | ||
|
||
proc strAppend*(result: var string, a: cstring) {.inline.} = | ||
# TODO: replace `CStrToStr` ? | ||
strAppend(result, cast[ptr char](a.unsafeAddr), a.len) |
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.
Doesn't belong in this PR either.
lib/system/repr.nim
Outdated
return $buf | ||
const N = (pointer.sizeof * 2) + 2 + 1 # 0x + hex addr + '\0' | ||
var buf {.noinit.}: array[N, char] | ||
result.strAppend buf[0].addr, c_sprintf(buf.toCstring, "%p", x) |
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.
result.strAppend buf[0].addr, c_sprintf(buf.toCstring, "%p", x) | |
discard c_sprintf(buf, "%p", x) | |
return $buf |
As strAppend really doesn't belong in this PR
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.
Agree with krux02 in that the toCstring templates should be inlined and removed from system.nim
Also please remove the unrelated strAppend and the setLen change from this PR.
Other than that nice :)
76619b8
to
6d331bf
Compare
## append `n` `char`'s from `a` to `result`, efficiently; | ||
## `\0` are insignificant. | ||
## note: should use a Slice[char] | ||
# D20200328T022947 |
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.
?
I do not understand why all those templates need to be public, |
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. |
488e35c
to
4f34842
Compare
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. |
WIP, stay tuned
ptr char
implicitly converts to cstring, resulting in undefined behavior #13790TODO
--legacy:implicitPtrCharToCstring
TODO after PR
nimNoArrayToCstringConversion
branches in code (but leave the symbol defined) which was introduced in 3308d26; it was introduced in 2017 so not needed for bootstrap anymore and it will simplify a lot of code; thatnimNoArrayToCstringConversion
was in same spirit as this PR and is related(EDIT: i did that in another PR in the meantime)