Skip to content

Commit 1e1846f

Browse files
authored
ast,fmt: further improve type detection of user defined typenames, extend tests (#21004)
1 parent 094b032 commit 1e1846f

File tree

5 files changed

+32
-21
lines changed

5 files changed

+32
-21
lines changed

vlib/v/ast/types.v

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,28 +1471,33 @@ fn (t Table) shorten_user_defined_typenames(original_name string, import_aliases
14711471
if alias := import_aliases[original_name] {
14721472
return alias
14731473
}
1474-
mut parts := original_name.split('.')
1475-
if parts.len > 1 {
1476-
// mod.submod.submod2.Type => submod2.Type
1477-
if !parts[..parts.len - 1].any(it.contains('[')) {
1478-
mod_idx := parts.len - 2
1479-
if t.is_fmt {
1480-
parts[mod_idx] = original_name.all_before_last('.')
1481-
}
1482-
if alias := import_aliases[parts[mod_idx]] {
1483-
parts[mod_idx] = alias
1484-
} else if t.cmod_prefix != '' && original_name.starts_with(t.cmod_prefix) {
1474+
mut mod, mut typ := original_name.rsplit_once('.') or { return original_name }
1475+
if !mod.contains('[') {
1476+
if !t.is_fmt {
1477+
mod = mod.all_after_last('.')
1478+
}
1479+
if alias := import_aliases[mod] {
1480+
mod = alias
1481+
} else if t.cmod_prefix != '' {
1482+
if alias := import_aliases[t.cmod_prefix + mod] {
1483+
mod = alias
1484+
} else {
14851485
// cur_mod.Type => Type
14861486
return original_name.all_after(t.cmod_prefix)
14871487
}
1488-
return parts[mod_idx..].join('.')
14891488
}
1490-
if original_name.contains('[]') {
1491-
// []mod.name
1492-
return original_name.all_after('.')
1489+
}
1490+
// E.g.: []mod.Type => []Type; []mod.submod.Type => []submod.Type;
1491+
// imported_mod.Type[mod.Result[[]mod.Token]] => imported_mod.Type[Result[[]Token]]
1492+
if original_name.contains('[]') {
1493+
if lhs, typ_after_lsbr := original_name.split_once('[') {
1494+
if mod_, typ_before_lsbr := lhs.rsplit_once('.') {
1495+
mod = mod_
1496+
typ = '${typ_before_lsbr}[${typ_after_lsbr}'
1497+
}
14931498
}
14941499
}
1495-
return original_name
1500+
return '${mod}.${typ}'
14961501
}
14971502

14981503
@[minify]
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
vlib/v/checker/tests/check_err_msg_with_generics.vv:15:10: error: cannot cast struct `BSTree[Result[[]Token, Err[string]]]` to `int`
1+
vlib/v/checker/tests/check_err_msg_with_generics.vv:15:10: error: cannot cast struct `datatypes.BSTree[Result[[]Token, Err[string]]]` to `int`
22
13 | fn test_err_msg() {
33
14 | typ := datatypes.BSTree[Result[[]Token, Err[string]]]{}
44
15 | println(int(typ))
55
| ~~~~~~~~
6-
16 | }
6+
16 | }

vlib/v/fmt/fmt_test.v

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,9 @@ fn test_fmt() {
7373
run_fmt(mut input_files)
7474
}
7575

76-
// NOTE: enable with upcommming `__input.vv` / `__expected.vv` files for vmodules
77-
/* fn test_fmt_vmodules() {
76+
fn test_fmt_vmodules() {
7877
vmodules_tdir := os.join_path(vroot, 'vlib', 'v', 'fmt', 'testdata', 'vmodules')
7978
os.setenv('VMODULES', vmodules_tdir, true)
8079
mut input_files := os.walk_ext(vmodules_tdir, '_input.vv')
8180
run_fmt(mut input_files)
82-
} */
81+
}
File renamed without changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// The module name has to match the modules dir name.
2+
module submod_type_alias
3+
4+
// The submod dir structure that is imported has to be existent.
5+
import bar.baz
6+
7+
type MyAlias = bar.baz.Baz

0 commit comments

Comments
 (0)