Skip to content

Commit

Permalink
fixes #10948 (#10949)
Browse files Browse the repository at this point in the history
  • Loading branch information
cooldome authored and Araq committed Apr 3, 2019
1 parent eaca62f commit 734da95
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/cgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ proc initLocExprSingleUse(p: BProc, e: PNode, result: var TLoc) =
discard "bug #8202; enforce evaluation order for nested calls for C++ too"
# We may need to consider that 'f(g())' cannot be rewritten to 'tmp = g(); f(tmp)'
# if 'tmp' lacks a move/assignment operator.
if e[0].kind == nkSym and sfConstructor in e[0].sym.flags:
if e[0].kind == nkSym and sfCompileToCpp in e[0].sym.flags:
result.flags.incl lfSingleUse
else:
result.flags.incl lfSingleUse
Expand Down
39 changes: 39 additions & 0 deletions tests/cpp/tretvar.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
discard """
targets: "cpp"
output: '''test1
xest1
'''
"""
{.passC: "-std=c++14".}

{.experimental: "dotOperators".}

import macros

type
stdString {.importcpp: "std::string", header: "<string>".} = object
stdUniquePtr[T] {.importcpp: "std::unique_ptr", header: "<memory>".} = object

proc c_str(a: stdString): cstring {.importcpp: "(char *)(#.c_str())", header: "<string>".}

proc len(a: stdString): csize {.importcpp: "(#.length())", header: "<string>".}

proc setChar(a: var stdString, i: csize, c: char) {.importcpp: "(#[#] = #)", header: "<string>".}

proc `*`*[T](this: stdUniquePtr[T]): var T {.noSideEffect, importcpp: "(* #)", header: "<memory>".}

proc make_unique_str(a: cstring): stdUniquePtr[stdString] {.importcpp: "std::make_unique<std::string>(#)", header: "<string>".}

macro `.()`*[T](this: stdUniquePtr[T], name: untyped, args: varargs[untyped]): untyped =
result = nnkCall.newTree(
nnkDotExpr.newTree(
newNimNode(nnkPar).add(prefix(this, "*")),
name
)
)
copyChildrenTo(args, result)

var val = make_unique_str("test1")
echo val.c_str()
val.setChar(0, 'x')
echo val.c_str()

0 comments on commit 734da95

Please sign in to comment.