From 9cbfa882e680757ce44c85c6394178347103ced3 Mon Sep 17 00:00:00 2001
From: Ken <ken0x0a+github@gmail.com>
Date: Sat, 8 Jan 2022 23:35:10 +0900
Subject: [PATCH] ast: support attributes for `ast.SumType` (#13010)

---
 vlib/v/ast/ast.v                        |  1 +
 vlib/v/fmt/fmt.v                        |  1 +
 vlib/v/fmt/tests/sum_attributes_keep.vv | 14 ++++++++++++++
 vlib/v/parser/parser.v                  |  1 +
 4 files changed, 17 insertions(+)
 create mode 100644 vlib/v/fmt/tests/sum_attributes_keep.vv

diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v
index a02f1a00c7..114aa29619 100644
--- a/vlib/v/ast/ast.v
+++ b/vlib/v/ast/ast.v
@@ -1092,6 +1092,7 @@ pub:
 	comments      []Comment
 	typ           Type
 	generic_types []Type
+	attrs         []Attr // attributes of type declaration
 pub mut:
 	variants []TypeNode
 }
diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v
index 30d4fcfd0d..7568e8fcba 100644
--- a/vlib/v/fmt/fmt.v
+++ b/vlib/v/fmt/fmt.v
@@ -1293,6 +1293,7 @@ pub fn (mut f Fmt) fn_type_decl(node ast.FnTypeDecl) {
 }
 
 pub fn (mut f Fmt) sum_type_decl(node ast.SumTypeDecl) {
+	f.attrs(node.attrs)
 	start_pos := f.out.len
 	if node.is_pub {
 		f.write('pub ')
diff --git a/vlib/v/fmt/tests/sum_attributes_keep.vv b/vlib/v/fmt/tests/sum_attributes_keep.vv
new file mode 100644
index 0000000000..7d99a468de
--- /dev/null
+++ b/vlib/v/fmt/tests/sum_attributes_keep.vv
@@ -0,0 +1,14 @@
+[derive: 'DeserBincode,SerBincode']
+type OneOf = ItemA | ItemB
+
+[derive: 'DeserBincode,SerBincode']
+struct ItemA {
+	a string
+}
+
+[derive: 'DeserBincode,SerBincode']
+struct ItemB {
+	b   int
+	bb  u64
+	bbb string
+}
diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v
index 4f314fc93b..d8bc533684 100644
--- a/vlib/v/parser/parser.v
+++ b/vlib/v/parser/parser.v
@@ -3465,6 +3465,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
 			is_pub: is_pub
 			variants: sum_variants
 			generic_types: generic_types
+			attrs: p.attrs
 			pos: decl_pos
 			comments: comments
 		}