prevents declaring a constructor without importcpp fixes #22712 (#22715)

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
This commit is contained in:
Juan M Gómez
2023-09-18 07:26:21 +01:00
committed by GitHub
parent 5f9038a5d7
commit bd857151d8
2 changed files with 22 additions and 2 deletions

View File

@@ -1712,7 +1712,10 @@ proc addThis(c: PContext, n: PNode, t: PType, owner: TSymKind) =
n.add newSymNode(c.p.resultSym)
addParamOrResult(c, c.p.resultSym, owner)
#resolves nim's obj ctor inside cpp ctors see #22669
s.ast = c.semExpr(c, newTree(nkCall, t[0].sym.ast[0]))
var typAst = t[0].sym.ast[0]
if typAst.kind == nkPragmaExpr:
typAst = typAst[0]
s.ast = c.semExpr(c, newTree(nkCall, typAst))
proc addResult(c: PContext, n: PNode, t: PType, owner: TSymKind) =
template genResSym(s) =
@@ -2093,6 +2096,8 @@ proc semCppMember(c: PContext; s: PSym; n: PNode) =
typ = s.typ[0]
if typ == nil or typ.kind != tyObject:
localError(c.config, n.info, "constructor must return an object")
if sfImportc in typ.sym.flags:
localError(c.config, n.info, "constructor in an imported type needs importcpp pragma")
else:
typ = s.typ[1]
if typ.kind == tyPtr and not isCtor:
@@ -2369,7 +2374,7 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind,
# used for overload resolution (there is no instantiation of the symbol)
if s.kind notin {skMacro, skTemplate} and s.magic == mNone: paramsTypeCheck(c, s.typ)
var resultType: PType
if sfConstructor in s.flags:
if {sfConstructor, sfImportc} * s.flags == {sfConstructor}:
resultType = makePtrType(c, s.typ[0])
addThis(c, n, resultType, skProc)
else:

15
tests/cpp/t22712.nim Normal file
View File

@@ -0,0 +1,15 @@
discard """
targets: "cpp"
errormsg: "constructor in an imported type needs importcpp pragma"
line: 14
"""
{.emit: """/*TYPESECTION*/
struct CppStruct {
CppStruct();
};
""".}
type CppStruct {.importcpp.} = object
proc makeCppStruct(): CppStruct {.constructor.} =
discard