Skip to content

Commit

Permalink
checker: fix checker generic alias type (fix #23474) (#23475)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Jan 16, 2025
1 parent e5153e7 commit c680984
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
7 changes: 2 additions & 5 deletions vlib/v/checker/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,8 @@ fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
continue
}
field_is_generic := field.typ.has_flag(.generic)
if !c.ensure_generic_type_specify_type_names(field.typ, field.type_pos, c.table.final_sym(field.typ).kind in [
.array,
.array_fixed,
.map,
], field_is_generic) {
if c.table.type_kind(field.typ) != .alias
&& !c.ensure_generic_type_specify_type_names(field.typ, field.type_pos, c.table.final_sym(field.typ).kind in [.array, .array_fixed, .map], field_is_generic) {
continue
}
if field_is_generic {
Expand Down
69 changes: 69 additions & 0 deletions vlib/v/tests/generics/generic_alias_map_type_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
module main

import arrays

pub struct Tree {
pub:
// Type of structural node, `value` should be empty
type string
// Content of data node, `type` should be empty
value string
pub mut:
// Child nodes
kids []&Tree
}

@[params]
struct TreeCloneParams {
kids []&Tree
}

// Makes new derived node with different kids id defined.
pub fn (tree Tree) clone(p TreeCloneParams) &Tree {
return &Tree{
type: tree.type
value: tree.value
kids: p.kids
}
}

// Collection of hask tools for processing tree.
pub type TreeBelt = map[string]fn (input &Tree, belt TreeBelt) []&Tree

// Hask tool for processing node.
pub type TreeHack = fn (input &Tree, belt TreeBelt) []&Tree

pub type TreeContext = map[string]string

@[params]
struct TreeHackParams {
belt TreeBelt
context ?TreeContext
}

// Transform tree through context with transformers
pub fn (tree Tree) hack(p TreeHackParams) []&Tree {
return arrays.concat(arrays.flatten(tree.kids.map(it.hack_self(p))))
}

pub fn (tree Tree) hack_self(p TreeHackParams) []&Tree {
mut handle := fn [tree] (input &Tree, belt TreeBelt) []&Tree {
return [
input.clone(kids: input.hack(belt: belt)),
]
}

if action := p.belt[tree.type] {
handle = action
}

return handle(tree, p.belt)
}

fn test_main() {
t := Tree{}
r := t.hack_self(belt: TreeBelt(map[string]fn (&Tree, TreeBelt) []&Tree{}))
assert r[0].type == ''
assert r[0].value == ''
assert r[0].kids.len == 0
}

0 comments on commit c680984

Please sign in to comment.