From 1bebc236bdd0bb4f3dfb9dadaeee91747f8c9741 Mon Sep 17 00:00:00 2001 From: metagn Date: Sat, 12 Oct 2024 22:20:21 +0300 Subject: [PATCH] fix type of reconstructed kind field node in field checking analysis [backport] (#24290) fixes #24021 The field checking for case object branches at some point generates a negated set `contains` check for the object discriminator. For enum types, this tries to generate a complement set and convert to a `contains` check in that instead. It obtains this type from the type of the element node in the `contains` check. `buildProperFieldCheck` creates the element node by changing a field access expression like `foo.z` into `foo.kind`. In order to do this, it copies the node `foo.z` and sets the field name in the node to the symbol `kind`. But when copying the node, the type of the original `foo.z` is retained. This means that the complement is performed on the type of the accessed field rather than the type of the discriminator, which causes problems when the accessed field is also an enum. To fix this, we properly set the type of the copied node to the type of the kind field. An alternative is just to make a new node instead. A lot of text for a single line change, I know, but this part of the codebase could use more explanation. --- compiler/guards.nim | 4 ++++ tests/objvariant/tcorrectcheckedfield.nim | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 tests/objvariant/tcorrectcheckedfield.nim diff --git a/compiler/guards.nim b/compiler/guards.nim index bbb2398674..553cc744df 100644 --- a/compiler/guards.nim +++ b/compiler/guards.nim @@ -1165,8 +1165,12 @@ proc buildProperFieldCheck(access, check: PNode; o: Operators): PNode = if check[1].kind == nkCurly: result = copyTree(check) if access.kind == nkDotExpr: + # change the access to the discriminator field access var a = copyTree(access) + # set field name to discriminator field name a[1] = check[2] + # set discriminator field type: important for `neg` + a.typ = check[2].typ result[2] = a # 'access.kind != nkDotExpr' can happen for object constructors # which we don't check yet diff --git a/tests/objvariant/tcorrectcheckedfield.nim b/tests/objvariant/tcorrectcheckedfield.nim new file mode 100644 index 0000000000..e5e67c727d --- /dev/null +++ b/tests/objvariant/tcorrectcheckedfield.nim @@ -0,0 +1,22 @@ +discard """ + matrix: "; --warning[ProveField]:on --warningAsError[ProveField]:on; --experimental:strictCaseObjects" +""" + +block: # issue #24021 + type + FooKind = enum + a + b + BiggerEnum = enum b1, b2, b3, b4, b5, b6, b7, b8, b9, b10 + Foo = object + case kind: FooKind + of a: discard + else: + z: BiggerEnum + + proc p(foo: Foo, val: int) = + case foo.kind + of a: + discard + else: + discard foo.z