parser: improve error message for `mod.unknownsubmod.Type` (#9976)

pull/10006/head
zakuro 2021-05-04 17:31:31 +09:00 committed by GitHub
parent 298fc533e4
commit 1e856c0f94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 49 additions and 8 deletions

View File

@ -330,17 +330,31 @@ pub fn (mut p Parser) parse_any_type(language ast.Language, is_ptr bool, check_d
} else if p.peek_tok.kind == .dot && check_dot {
// `module.Type`
// /if !(p.tok.lit in p.table.imports) {
if !p.known_import(name) {
p.error('unknown module `$p.tok.lit`')
return 0
}
if p.tok.lit in p.imports {
p.register_used_import(p.tok.lit)
}
mut mod := name
mut mod_pos := p.tok.position()
p.next()
p.check(.dot)
mut mod_last_part := mod
for p.peek_tok.kind == .dot {
mod_pos = mod_pos.extend(p.tok.position())
mod_last_part = p.tok.lit
mod += '.$mod_last_part'
p.next()
p.check(.dot)
}
if !p.known_import(mod) {
mut msg := 'unknown module `$mod`'
if mod.len > mod_last_part.len && p.known_import(mod_last_part) {
msg += '; did you mean `$mod_last_part`?'
}
p.error_with_pos(msg, mod_pos)
return 0
}
if mod in p.imports {
p.register_used_import(mod)
}
// prefix with full module
name = '${p.imports[name]}.$p.tok.lit'
name = '${p.imports[mod]}.$p.tok.lit'
if p.tok.lit.len > 0 && !p.tok.lit[0].is_capital() {
p.error('imported types must start with a capital letter')
return 0

View File

@ -0,0 +1,5 @@
vlib/v/parser/tests/struct_field_unknown_module_a.vv:2:7: error: unknown module `ui`
1 | struct Inter {
2 | code ui.KeyCode
| ~~
3 | }

View File

@ -0,0 +1,3 @@
struct Inter {
code ui.KeyCode
}

View File

@ -0,0 +1,5 @@
vlib/v/parser/tests/struct_field_unknown_module_b.vv:2:7: error: unknown module `term.unknownmod`
1 | struct Inter {
2 | code term.unknownmod.KeyCode
| ~~~~~~~~~~~~~~~
3 | }

View File

@ -0,0 +1,3 @@
struct Inter {
code term.unknownmod.KeyCode
}

View File

@ -0,0 +1,6 @@
vlib/v/parser/tests/struct_field_unknown_module_c.vv:4:7: error: unknown module `term.ui`; did you mean `ui`?
2 |
3 | struct Inter {
4 | code term.ui.KeyCode
| ~~~~~~~
5 | }

View File

@ -0,0 +1,5 @@
import term.ui
struct Inter {
code term.ui.KeyCode
}