summary refs log tree commit diff stats
path: root/compiler/semobjconstr.nim
diff options
context:
space:
mode:
authorringabout <43030857+ringabout@users.noreply.github.com>2023-07-25 18:08:32 +0800
committerGitHub <noreply@github.com>2023-07-25 12:08:32 +0200
commit1c2ccfad08191e936fadd52450b53dfea105a34d (patch)
treed848422b5cd391db4efb14bf2a5b7db2307266df /compiler/semobjconstr.nim
parentdce714b2598c41e36113a4339fb9fb14655bc090 (diff)
downloadNim-1c2ccfad08191e936fadd52450b53dfea105a34d.tar.gz
fixes #22301; fixes #22324; rejects branch initialization with a runtime discriminator with defaults (#22303)
* fixes #22301; rejects branch initialization with a runtime discriminator with defaults

* undefault nimPreviewRangeDefault

* fixes tests

* use oldCheckDefault
Diffstat (limited to 'compiler/semobjconstr.nim')
-rw-r--r--compiler/semobjconstr.nim13
1 files changed, 12 insertions, 1 deletions
diff --git a/compiler/semobjconstr.nim b/compiler/semobjconstr.nim
index d4eba2112..9b17676ee 100644
--- a/compiler/semobjconstr.nim
+++ b/compiler/semobjconstr.nim
@@ -21,6 +21,7 @@ type
                              # set this to true while visiting
                              # parent types.
     missingFields: seq[PSym] # Fields that the user failed to specify
+    checkDefault: bool       # Checking defaults
 
   InitStatus = enum # This indicates the result of object construction
     initUnknown
@@ -342,6 +343,16 @@ proc semConstructFields(c: PContext, n: PNode, constrCtx: var ObjConstrContext,
           # All bets are off. If any of the branches has a mandatory
           # fields we must produce an error:
           for i in 1..<n.len:
+            let branchNode = n[i]
+            if branchNode != nil:
+              let oldCheckDefault = constrCtx.checkDefault
+              constrCtx.checkDefault = true
+              let (_, defaults) = semConstructFields(c, branchNode[^1], constrCtx, flags)
+              constrCtx.checkDefault = oldCheckDefault
+              if len(defaults) > 0:
+                localError(c.config, discriminatorVal.info, "branch initialization " &
+                            "with a runtime discriminator is not supported " &
+                            "for a branch whose fields have default values.")
             discard collectMissingCaseFields(c, n[i], constrCtx, @[])
   of nkSym:
     let field = n.sym
@@ -353,7 +364,7 @@ proc semConstructFields(c: PContext, n: PNode, constrCtx: var ObjConstrContext,
       result.defaults.add newTree(nkExprColonExpr, n, field.ast)
     else:
       if efWantNoDefaults notin flags: # cannot compute defaults at the typeRightPass
-        let defaultExpr = defaultNodeField(c, n)
+        let defaultExpr = defaultNodeField(c, n, constrCtx.checkDefault)
         if defaultExpr != nil:
           result.status = initUnknown
           result.defaults.add newTree(nkExprColonExpr, n, defaultExpr)