From 273d5ddf1710b0426daa8d063ee1d9469b6803a7 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Mon, 3 Apr 2023 14:38:20 +0800 Subject: [PATCH] fixes #20846; warn on overloaded `=copy` with refc --- compiler/condsyms.nim | 1 + compiler/lineinfos.nim | 2 ++ compiler/nim.cfg | 4 ++++ compiler/semstmts.nim | 2 ++ 4 files changed, 9 insertions(+) diff --git a/compiler/condsyms.nim b/compiler/condsyms.nim index a499b71429..d3273d3a9a 100644 --- a/compiler/condsyms.nim +++ b/compiler/condsyms.nim @@ -154,3 +154,4 @@ proc initDefines*(symbols: StringTableRef) = defineSymbol("nimHasGenericDefine") defineSymbol("nimHasDefineAliases") defineSymbol("nimHasWarnBareExcept") + defineSymbol("nimHasWarnCopyHookForRefc") diff --git a/compiler/lineinfos.nim b/compiler/lineinfos.nim index 89aff57df8..b72051008c 100644 --- a/compiler/lineinfos.nim +++ b/compiler/lineinfos.nim @@ -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` diff --git a/compiler/nim.cfg b/compiler/nim.cfg index b9c8fdc084..cdd5e52092 100644 --- a/compiler/nim.cfg +++ b/compiler/nim.cfg @@ -46,3 +46,7 @@ define:useStdoutAsStdmsg @if nimHasWarnBareExcept: warningAserror[BareExcept]:on @end + +@if nimHasWarnCopyHookForRefc: + warningAserror[CopyHookForRefc]:on +@end diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 6d1ce388dd..ac0247630f 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -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]