fixes #20846; warn on overloaded =copy with refc

This commit is contained in:
ringabout
2023-04-03 14:38:20 +08:00
parent cae539996a
commit 273d5ddf17
4 changed files with 9 additions and 0 deletions

View File

@@ -154,3 +154,4 @@ proc initDefines*(symbols: StringTableRef) =
defineSymbol("nimHasGenericDefine")
defineSymbol("nimHasDefineAliases")
defineSymbol("nimHasWarnBareExcept")
defineSymbol("nimHasWarnCopyHookForRefc")

View File

@@ -88,6 +88,7 @@ type
warnUnnamedBreak = "UnnamedBreak",
warnStmtListLambda = "StmtListLambda",
warnBareExcept = "BareExcept",
warnCopyHookForRefc = "CopyHookForRefc",
warnUser = "User",
# hints
hintSuccess = "Success", hintSuccessX = "SuccessX",
@@ -189,6 +190,7 @@ const
warnUnnamedBreak: "Using an unnamed break in a block is deprecated; Use a named block with a named break instead",
warnStmtListLambda: "statement list expression assumed to be anonymous proc; this is deprecated, use `do (): ...` or `proc () = ...` instead",
warnBareExcept: "$1",
warnCopyHookForRefc: "Overriding `=copy` hook is not reliable for refc",
warnUser: "$1",
hintSuccess: "operation successful: $#",
# keep in sync with `testament.isSuccess`

View File

@@ -46,3 +46,7 @@ define:useStdoutAsStdmsg
@if nimHasWarnBareExcept:
warningAserror[BareExcept]:on
@end
@if nimHasWarnCopyHookForRefc:
warningAserror[CopyHookForRefc]:on
@end

View File

@@ -1857,6 +1857,8 @@ proc semOverride(c: PContext, s: PSym, n: PNode) =
incl(s.flags, sfOverriden)
if name == "=":
message(c.config, n.info, warnDeprecated, "Overriding `=` hook is deprecated; Override `=copy` hook instead")
elif name == "=copy" and c.config.selectedGC == gcRefc:
message(c.config, n.info, warnCopyHookForRefc)
let t = s.typ
if t.len == 3 and t[0] == nil and t[1].kind == tyVar:
var obj = t[1][0]