v icon indicating copy to clipboard operation
v copied to clipboard

Error: field does not exist with embedded structure and sumtype variants

Open Tanguy-SALMON opened this issue 4 years ago • 1 comments

V doctor:

OS: linux, "Manjaro Linux"
Processor: 12 cpus, 64bit, little endian, Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
CC version: cc (GCC) 11.1.0

getwd: /home/tanguy
vmodules: /home/tanguy/.vmodules
vroot: /var/www/vproject/vorigin
vexe: /var/www/vproject/vorigin/v
vexe mtime: 2021-08-23 07:20:48
is vroot writable: true
is vmodules writable: true
V full version: V 0.2.2 51d7aed

Git version: git version 2.32.0
Git vroot status: weekly.2021.33.2-58-g51d7aede (4 commit(s) behind V master)
.git/config present: true
thirdparty/tcc status: thirdparty-linux-amd64 333c14de

What did you do? v -g -o vdbg cmd/v && vdbg /var/www/vproject/scarp/scrap_embleded_stuct_enum.v

module main

struct Node {
mut:
    tag string
    content []Tree
}
struct Empty {
	text string
}

type Tree = Empty | Node

fn main() {
  
    mut body := Node{tag: "body"}

	body.content << Node{tag: "p"}
	body.content[0].content << Node{tag: "i"}
	body.content[0].content[0].content << Node{tag: "j"}
	body.content[0].content[0].content[0].content << Node{tag: "k"}
	body.content[0].content[0].content[0].content[0].content << Empty{text: "hello world"}

    println(body)
}

What did you expect to see?

No error. I expect to get the same result doing with the same logic without enum type, like :

module main

struct Node {
mut:
    tag string
    content []Node
}
struct Empty {
	text string
}

fn main() {
  
    mut body := Node{tag: "body"}

	body.content << Node{tag: "p"}
	body.content[0].content << Node{tag: "i"}
	body.content[0].content[0].content << Node{tag: "j"}
	body.content[0].content[0].content[0].content << Node{tag: "k"}
	body.content[0].content[0].content[0].content[0].content << Node{tag: "l"}

    println(body)
}

What did you see instead?

/var/www/vproject/scarp/scrap_embleded_stuct_enum.v:19:18: error: field `content` does not exist or have the same type in all sumtype variants
   17 | 
   18 |     body.content << Node{tag: "p"}
   19 |     body.content[0].content << Node{tag: "i"}
      |                     ~~~~~~~
   20 |     body.content[0].content[0].content << Node{tag: "j"}
   21 |     body.content[0].content[0].content[0].content << Node{tag: "k"}
/var/www/vproject/scarp/scrap_embleded_stuct_enum.v:20:18: error: field `content` does not exist or have the same type in all sumtype variants
   18 |     body.content << Node{tag: "p"}
   19 |     body.content[0].content << Node{tag: "i"}
   20 |     body.content[0].content[0].content << Node{tag: "j"}
      |                     ~~~~~~~
   21 |     body.content[0].content[0].content[0].content << Node{tag: "k"}
   22 |     body.content[0].content[0].content[0].content[0].content << Empty{text: "lssdsd"}
/var/www/vproject/scarp/scrap_embleded_stuct_enum.v:21:18: error: field `content` does not exist or have the same type in all sumtype variants
   19 |     body.content[0].content << Node{tag: "i"}
   20 |     body.content[0].content[0].content << Node{tag: "j"}
   21 |     body.content[0].content[0].content[0].content << Node{tag: "k"}
      |                     ~~~~~~~
   22 |     body.content[0].content[0].content[0].content[0].content << Empty{text: "lssdsd"}
   23 |
/var/www/vproject/scarp/scrap_embleded_stuct_enum.v:22:18: error: field `content` does not exist or have the same type in all sumtype variants
   20 |     body.content[0].content[0].content << Node{tag: "j"}
   21 |     body.content[0].content[0].content[0].content << Node{tag: "k"}
   22 |     body.content[0].content[0].content[0].content[0].content << Empty{text: "hello world"}
      |                     ~~~~~~~
   23 | 
   24 |     println(body)

Tanguy-SALMON avatar Aug 24 '21 02:08 Tanguy-SALMON

I don't think this is a bug, is it @medvednikov?

Surely body.content[0] is a Tree, and so:

error: field `content` does not exist or have the same type in all sumtype variants
   19 |     body.content[0].content << Node{tag: "i"}
      |                     ~~~~~~~

is correct?

edam avatar Oct 10 '24 17:10 edam