Skip to content

Commit

Permalink
ast,fmt: further improve type detection of user defined typenames, ex…
Browse files Browse the repository at this point in the history
…tend tests (#21004)
  • Loading branch information
ttytm committed Mar 13, 2024
1 parent 094b032 commit 1e1846f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 21 deletions.
37 changes: 21 additions & 16 deletions vlib/v/ast/types.v
Expand Up @@ -1471,28 +1471,33 @@ fn (t Table) shorten_user_defined_typenames(original_name string, import_aliases
if alias := import_aliases[original_name] {
return alias
}
mut parts := original_name.split('.')
if parts.len > 1 {
// mod.submod.submod2.Type => submod2.Type
if !parts[..parts.len - 1].any(it.contains('[')) {
mod_idx := parts.len - 2
if t.is_fmt {
parts[mod_idx] = original_name.all_before_last('.')
}
if alias := import_aliases[parts[mod_idx]] {
parts[mod_idx] = alias
} else if t.cmod_prefix != '' && original_name.starts_with(t.cmod_prefix) {
mut mod, mut typ := original_name.rsplit_once('.') or { return original_name }
if !mod.contains('[') {
if !t.is_fmt {
mod = mod.all_after_last('.')
}
if alias := import_aliases[mod] {
mod = alias
} else if t.cmod_prefix != '' {
if alias := import_aliases[t.cmod_prefix + mod] {
mod = alias
} else {
// cur_mod.Type => Type
return original_name.all_after(t.cmod_prefix)
}
return parts[mod_idx..].join('.')
}
if original_name.contains('[]') {
// []mod.name
return original_name.all_after('.')
}
// E.g.: []mod.Type => []Type; []mod.submod.Type => []submod.Type;
// imported_mod.Type[mod.Result[[]mod.Token]] => imported_mod.Type[Result[[]Token]]
if original_name.contains('[]') {
if lhs, typ_after_lsbr := original_name.split_once('[') {
if mod_, typ_before_lsbr := lhs.rsplit_once('.') {
mod = mod_
typ = '${typ_before_lsbr}[${typ_after_lsbr}'
}
}
}
return original_name
return '${mod}.${typ}'
}

@[minify]
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/checker/tests/check_err_msg_with_generics.out
@@ -1,6 +1,6 @@
vlib/v/checker/tests/check_err_msg_with_generics.vv:15:10: error: cannot cast struct `BSTree[Result[[]Token, Err[string]]]` to `int`
vlib/v/checker/tests/check_err_msg_with_generics.vv:15:10: error: cannot cast struct `datatypes.BSTree[Result[[]Token, Err[string]]]` to `int`
13 | fn test_err_msg() {
14 | typ := datatypes.BSTree[Result[[]Token, Err[string]]]{}
15 | println(int(typ))
| ~~~~~~~~
16 | }
16 | }
5 changes: 2 additions & 3 deletions vlib/v/fmt/fmt_test.v
Expand Up @@ -73,10 +73,9 @@ fn test_fmt() {
run_fmt(mut input_files)
}

// NOTE: enable with upcommming `__input.vv` / `__expected.vv` files for vmodules
/* fn test_fmt_vmodules() {
fn test_fmt_vmodules() {
vmodules_tdir := os.join_path(vroot, 'vlib', 'v', 'fmt', 'testdata', 'vmodules')
os.setenv('VMODULES', vmodules_tdir, true)
mut input_files := os.walk_ext(vmodules_tdir, '_input.vv')
run_fmt(mut input_files)
} */
}
@@ -0,0 +1,7 @@
// The module name has to match the modules dir name.
module submod_type_alias

// The submod dir structure that is imported has to be existent.
import bar.baz

type MyAlias = bar.baz.Baz

0 comments on commit 1e1846f

Please sign in to comment.