summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
authorflywind <xzsflywind@gmail.com>2022-02-26 03:33:28 +0800
committerGitHub <noreply@github.com>2022-02-25 20:33:28 +0100
commit9c17a32e0ec45de83c21d84f98be7f351ccd1172 (patch)
tree2822034e739d0e678d5f3369f8e0f23d052b2007 /compiler
parentef3f343ec2979d22a5d4e51f4328e4a4c21d68dc (diff)
downloadNim-9c17a32e0ec45de83c21d84f98be7f351ccd1172.tar.gz
fix #19266; allow reassign discriminant field (#19567)
* add inUncheckedAssignSection

* add one more test
Diffstat (limited to 'compiler')
-rw-r--r--compiler/ccgexprs.nim16
-rw-r--r--compiler/ccgstmts.nim2
-rw-r--r--compiler/cgendata.nim1
3 files changed, 17 insertions, 2 deletions
diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim
index 1f1035115..9848366a1 100644
--- a/compiler/ccgexprs.nim
+++ b/compiler/ccgexprs.nim
@@ -2932,7 +2932,21 @@ proc expr(p: BProc, n: PNode, d: var TLoc) =
      nkFromStmt, nkTemplateDef, nkMacroDef, nkStaticStmt:
     discard
   of nkPragma: genPragma(p, n)
-  of nkPragmaBlock: expr(p, n.lastSon, d)
+  of nkPragmaBlock:
+    var inUncheckedAssignSection = 0
+    let pragmaList = n[0]
+    for pi in pragmaList:
+      if whichPragma(pi) == wCast:
+        case whichPragma(pi[1])
+        of wUncheckedAssign:
+          inUncheckedAssignSection = 1
+        else:
+          discard
+
+    inc p.inUncheckedAssignSection, inUncheckedAssignSection
+    expr(p, n.lastSon, d)
+    dec p.inUncheckedAssignSection, inUncheckedAssignSection
+
   of nkProcDef, nkFuncDef, nkMethodDef, nkConverterDef:
     if n[genericParamsPos].kind == nkEmpty:
       var prc = n[namePos].sym
diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim
index 58a6dd015..240fa55c8 100644
--- a/compiler/ccgstmts.nim
+++ b/compiler/ccgstmts.nim
@@ -1566,7 +1566,7 @@ proc asgnFieldDiscriminant(p: BProc, e: PNode) =
   initLocExpr(p, e[0], a)
   getTemp(p, a.t, tmp)
   expr(p, e[1], tmp)
-  if optTinyRtti notin p.config.globalOptions:
+  if optTinyRtti notin p.config.globalOptions and p.inUncheckedAssignSection == 0:
     let field = dotExpr[1].sym
     genDiscriminantCheck(p, a, tmp, dotExpr[0].typ, field)
     message(p.config, e.info, warnCaseTransition)
diff --git a/compiler/cgendata.nim b/compiler/cgendata.nim
index 88b8d4090..449076725 100644
--- a/compiler/cgendata.nim
+++ b/compiler/cgendata.nim
@@ -99,6 +99,7 @@ type
     withinTryWithExcept*: int # required for goto based exception handling
     withinBlockLeaveActions*: int # complex to explain
     sigConflicts*: CountTable[string]
+    inUncheckedAssignSection*: int
 
   TTypeSeq* = seq[PType]
   TypeCache* = Table[SigHash, Rope]