diff options
author | Yuriy Glukhov <yglukhov@users.noreply.github.com> | 2017-11-19 03:32:39 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2017-11-19 02:32:39 +0100 |
commit | e1ed34627f0f99e580e3e20d5782f582a08c887e (patch) | |
tree | a219f9742755ef447ddc7e7c4519953de3e3867d | |
parent | 963184fea6858b270e1d6a69707c94b2511674dc (diff) | |
download | Nim-e1ed34627f0f99e580e3e20d5782f582a08c887e.tar.gz |
Fixes #5979 (#5980)
-rw-r--r-- | compiler/ccgexprs.nim | 10 | ||||
-rw-r--r-- | tests/cpp/tcasts.nim | 10 |
2 files changed, 18 insertions, 2 deletions
diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index 45b2be558..9db136ed1 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -1645,8 +1645,14 @@ proc genSomeCast(p: BProc, e: PNode, d: var TLoc) = putIntoDest(p, d, e, "(($1) ($2))" % [getClosureType(p.module, etyp, clHalfWithEnv), rdCharLoc(a)], a.storage) else: - putIntoDest(p, d, e, "(($1) ($2))" % - [getTypeDesc(p.module, e.typ), rdCharLoc(a)], a.storage) + let srcTyp = skipTypes(e.sons[1].typ, abstractRange) + # C++ does not like direct casts from pointer to shorter integral types + if srcTyp.kind in {tyPtr, tyPointer} and etyp.kind in IntegralTypes: + putIntoDest(p, d, e, "(($1) (ptrdiff_t) ($2))" % + [getTypeDesc(p.module, e.typ), rdCharLoc(a)], a.storage) + else: + putIntoDest(p, d, e, "(($1) ($2))" % + [getTypeDesc(p.module, e.typ), rdCharLoc(a)], a.storage) proc genCast(p: BProc, e: PNode, d: var TLoc) = const ValueTypes = {tyFloat..tyFloat128, tyTuple, tyObject, tyArray} diff --git a/tests/cpp/tcasts.nim b/tests/cpp/tcasts.nim new file mode 100644 index 000000000..35f06234d --- /dev/null +++ b/tests/cpp/tcasts.nim @@ -0,0 +1,10 @@ +discard """ + cmd: "nim cpp $file" + output: "" +""" + +block: #5979 + var a = 'a' + var p: pointer = cast[pointer](a) + var c = cast[char](p) + doAssert(c == 'a') |