diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 85840788d7d92b..fe6b93e585e8b6 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -883,27 +883,76 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast. } } } - if fn_name.ends_with('from_string') { - enum_name := fn_name.all_before('__static__') - full_enum_name := if !enum_name.contains('.') { - c.mod + '.' + enum_name - } else { - enum_name + } + // Enum.from_string, `mod.Enum.from_string('item')`, `Enum.from_string('item')` + if !found && fn_name.ends_with('__static__from_string') { + enum_name := fn_name.all_before('__static__') + mut full_enum_name := if !enum_name.contains('.') { + c.mod + '.' + enum_name + } else { + enum_name + } + mut idx := c.table.type_idxs[full_enum_name] + if idx > 0 { + // is from another mod. + if enum_name.contains('.') { + mut t_sym := c.table.sym_by_idx(idx) + if t_sym.kind == .alias { + parent_type := (t_sym.info as ast.Alias).parent_type + t_sym = c.table.sym(parent_type) + } + if t_sym.kind != .enum_ { + c.error('expected enum, but `${full_enum_name}` is ${t_sym.kind}', + node.pos) + return ast.void_type + } + if !t_sym.is_pub { + c.error('module `${t_sym.mod}` type `${t_sym.name}` is private', node.pos) + return ast.void_type + } } - idx := c.table.type_idxs[full_enum_name] - ret_typ := ast.Type(idx).set_flag(.option) - if node.args.len != 1 { - c.error('expected 1 argument, but got ${node.args.len}', node.pos) - } else { - node.args[0].typ = c.expr(mut node.args[0].expr) - if node.args[0].typ != ast.string_type { - styp := c.table.type_to_str(node.args[0].typ) - c.error('expected `string` argument, but got `${styp}`', node.pos) + } else if !enum_name.contains('.') { + // find from another mods. + for import_sym in c.file.imports { + full_enum_name = '${import_sym.mod}.${enum_name}' + idx = c.table.type_idxs[full_enum_name] + if idx < 1 { + continue + } + mut t_sym := c.table.sym_by_idx(idx) + if t_sym.kind == .alias { + parent_type := (t_sym.info as ast.Alias).parent_type + t_sym = c.table.sym(parent_type) } + if t_sym.kind != .enum_ { + c.error('expected enum, but `${full_enum_name}` is ${t_sym.kind}', + node.pos) + return ast.void_type + } + if !t_sym.is_pub { + c.error('module `${t_sym.mod}` type `${t_sym.name}` is private', node.pos) + return ast.void_type + } + break + } + } + if idx == 0 { + c.error('unknown enum `${enum_name}`', node.pos) + return ast.void_type + } + + ret_typ := ast.Type(idx).set_flag(.option) + if node.args.len != 1 { + c.error('expected 1 argument, but got ${node.args.len}', node.pos) + } else { + node.args[0].typ = c.expr(mut node.args[0].expr) + if node.args[0].typ != ast.string_type { + styp := c.table.type_to_str(node.args[0].typ) + c.error('expected `string` argument, but got `${styp}`', node.pos) } - node.return_type = ret_typ - return ret_typ } + node.return_type = ret_typ + return ret_typ } mut is_native_builtin := false if !found && c.pref.backend == .native { diff --git a/vlib/v/checker/tests/modules/enum_from_string_in_different_mods.out b/vlib/v/checker/tests/modules/enum_from_string_in_different_mods.out new file mode 100644 index 00000000000000..64ea6827d72846 --- /dev/null +++ b/vlib/v/checker/tests/modules/enum_from_string_in_different_mods.out @@ -0,0 +1,47 @@ +main.v:3:15: error: module `amod` type `MyEnum` is private + 1 | module main + 2 | + 3 | import amod { MyEnum } + | ~~~~~~ + 4 | + 5 | fn main() { +main.v:6:6: error: module `amod` type `amod.MyEnum` is private + 4 | + 5 | fn main() { + 6 | _ = MyEnum.from_string('item1') + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 7 | _ = amod.MyEnum.from_string('item1') + 8 | _ = amod.MyStruct.from_string('item1') +main.v:6:4: error: assignment mismatch: 1 variable(s) but `MyEnum.from_string()` returns 0 value(s) + 4 | + 5 | fn main() { + 6 | _ = MyEnum.from_string('item1') + | ^ + 7 | _ = amod.MyEnum.from_string('item1') + 8 | _ = amod.MyStruct.from_string('item1') +main.v:7:11: error: module `amod` type `amod.MyEnum` is private + 5 | fn main() { + 6 | _ = MyEnum.from_string('item1') + 7 | _ = amod.MyEnum.from_string('item1') + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 8 | _ = amod.MyStruct.from_string('item1') + 9 | } +main.v:7:4: error: assignment mismatch: 1 variable(s) but `amod.MyEnum.from_string()` returns 0 value(s) + 5 | fn main() { + 6 | _ = MyEnum.from_string('item1') + 7 | _ = amod.MyEnum.from_string('item1') + | ^ + 8 | _ = amod.MyStruct.from_string('item1') + 9 | } +main.v:8:11: error: expected enum, but `amod.MyStruct` is struct + 6 | _ = MyEnum.from_string('item1') + 7 | _ = amod.MyEnum.from_string('item1') + 8 | _ = amod.MyStruct.from_string('item1') + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 9 | } +main.v:8:4: error: assignment mismatch: 1 variable(s) but `amod.MyStruct.from_string()` returns 0 value(s) + 6 | _ = MyEnum.from_string('item1') + 7 | _ = amod.MyEnum.from_string('item1') + 8 | _ = amod.MyStruct.from_string('item1') + | ^ + 9 | } diff --git a/vlib/v/checker/tests/modules/enum_from_string_in_different_mods/amod/amod.v b/vlib/v/checker/tests/modules/enum_from_string_in_different_mods/amod/amod.v new file mode 100644 index 00000000000000..9cab87dbcffef3 --- /dev/null +++ b/vlib/v/checker/tests/modules/enum_from_string_in_different_mods/amod/amod.v @@ -0,0 +1,8 @@ +module amod + +enum MyEnum { + item1 + item2 +} + +pub struct MyStruct {} diff --git a/vlib/v/checker/tests/modules/enum_from_string_in_different_mods/main.v b/vlib/v/checker/tests/modules/enum_from_string_in_different_mods/main.v new file mode 100644 index 00000000000000..28c0e23f6b8188 --- /dev/null +++ b/vlib/v/checker/tests/modules/enum_from_string_in_different_mods/main.v @@ -0,0 +1,9 @@ +module main + +import amod { MyEnum } + +fn main() { + _ = MyEnum.from_string('item1') + _ = amod.MyEnum.from_string('item1') + _ = amod.MyStruct.from_string('item1') +} diff --git a/vlib/v/gen/c/auto_str_methods.v b/vlib/v/gen/c/auto_str_methods.v index de09f3c9c486d0..538894cfdc1661 100644 --- a/vlib/v/gen/c/auto_str_methods.v +++ b/vlib/v/gen/c/auto_str_methods.v @@ -1150,12 +1150,22 @@ fn should_use_indent_func(kind ast.Kind) bool { fn (mut g Gen) gen_enum_static_from_string(fn_name string) { enum_name := fn_name.all_before('__static__') - mod_enum_name := if !enum_name.contains('.') { + mut mod_enum_name := if !enum_name.contains('.') { g.cur_mod.name + '.' + enum_name } else { enum_name } - idx := g.table.type_idxs[mod_enum_name] + mut idx := g.table.type_idxs[mod_enum_name] + if idx == 0 && (enum_name.contains('.') || enum_name[0].is_capital()) { + // no cur mod, find from another mods. + for import_sym in g.file.imports { + mod_enum_name = '${import_sym.mod}.${enum_name}' + idx = g.table.type_idxs[mod_enum_name] + if idx > 0 { + break + } + } + } enum_typ := ast.Type(idx) enum_styp := g.typ(enum_typ) option_enum_typ := ast.Type(idx).set_flag(.option) @@ -1164,9 +1174,10 @@ fn (mut g Gen) gen_enum_static_from_string(fn_name string) { enum_field_vals := g.table.get_enum_field_vals(mod_enum_name) mut fn_builder := strings.new_builder(512) - g.definitions.writeln('static ${option_enum_styp} ${fn_name}(string name); // auto') + fn_name_no_dots := util.no_dots(fn_name) + g.definitions.writeln('static ${option_enum_styp} ${fn_name_no_dots}(string name); // auto') - fn_builder.writeln('static ${option_enum_styp} ${fn_name}(string name) {') + fn_builder.writeln('static ${option_enum_styp} ${fn_name_no_dots}(string name) {') fn_builder.writeln('\t${option_enum_styp} t1;') fn_builder.writeln('\tbool exists = false;') fn_builder.writeln('\tint inx = 0;') diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 2f396bf48ea49e..f38597fb1ca297 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -1803,7 +1803,7 @@ fn (mut g Gen) fn_call(node ast.CallExpr) { g.gen_enum_static_from_string(node.name) g.str_fn_names << node.name } - g.write('${node.name}(') + g.write('${util.no_dots(node.name)}(') g.call_args(node) g.write(')') } else { diff --git a/vlib/v/tests/modules/enum_from_string_in_different_mods/amod/amod.v b/vlib/v/tests/modules/enum_from_string_in_different_mods/amod/amod.v new file mode 100644 index 00000000000000..6231dc197caa5f --- /dev/null +++ b/vlib/v/tests/modules/enum_from_string_in_different_mods/amod/amod.v @@ -0,0 +1,6 @@ +module amod + +pub enum MyEnum { + item1 + item2 +} diff --git a/vlib/v/tests/modules/enum_from_string_in_different_mods/main_test.v b/vlib/v/tests/modules/enum_from_string_in_different_mods/main_test.v new file mode 100644 index 00000000000000..76a15e31276740 --- /dev/null +++ b/vlib/v/tests/modules/enum_from_string_in_different_mods/main_test.v @@ -0,0 +1,10 @@ +module main + +import amod { MyEnum } + +fn test_main() { + item1 := MyEnum.from_string('item1')? + assert item1 == MyEnum.item1 + item2 := amod.MyEnum.from_string('item2')? + assert item2 == MyEnum.item2 +}