From b2e79dbebd5a46c12a4b0b5b224547c002925681 Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Sun, 23 Aug 2020 05:42:25 +0530 Subject: [PATCH] parser: add an error for `import mod.sub as mod` and `import mod as mod` (#6194) --- examples/net_raw_http.v | 2 +- vlib/math/stats/stats_test.v | 2 +- vlib/v/checker/tests/import_mod_as_mod_err.out | 5 +++++ vlib/v/checker/tests/import_mod_as_mod_err.vv | 5 +++++ vlib/v/checker/tests/import_mod_sub_as_sub_err.out | 5 +++++ vlib/v/checker/tests/import_mod_sub_as_sub_err.vv | 5 +++++ vlib/v/parser/parser.v | 3 +++ vlib/x/net/udp_test.v | 2 +- 8 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 vlib/v/checker/tests/import_mod_as_mod_err.out create mode 100644 vlib/v/checker/tests/import_mod_as_mod_err.vv create mode 100644 vlib/v/checker/tests/import_mod_sub_as_sub_err.out create mode 100644 vlib/v/checker/tests/import_mod_sub_as_sub_err.vv diff --git a/examples/net_raw_http.v b/examples/net_raw_http.v index e3b4f20a2d..c03ad818bd 100644 --- a/examples/net_raw_http.v +++ b/examples/net_raw_http.v @@ -1,5 +1,5 @@ // Simple raw HTTP head request -import x.net as net +import x.net import time // Make a new connection diff --git a/vlib/math/stats/stats_test.v b/vlib/math/stats/stats_test.v index 45ffc04b52..7b4bbbe75e 100644 --- a/vlib/math/stats/stats_test.v +++ b/vlib/math/stats/stats_test.v @@ -1,4 +1,4 @@ -import math.stats as stats +import math.stats import math fn test_freq() { diff --git a/vlib/v/checker/tests/import_mod_as_mod_err.out b/vlib/v/checker/tests/import_mod_as_mod_err.out new file mode 100644 index 0000000000..1f0bb93312 --- /dev/null +++ b/vlib/v/checker/tests/import_mod_as_mod_err.out @@ -0,0 +1,5 @@ +vlib/v/checker/tests/import_mod_as_mod_err.v:1:16: error: import alias `math as math` is redundant + 1 | import math as math + | ~~~~ + 2 | + 3 | fn main() { diff --git a/vlib/v/checker/tests/import_mod_as_mod_err.vv b/vlib/v/checker/tests/import_mod_as_mod_err.vv new file mode 100644 index 0000000000..2a125349ad --- /dev/null +++ b/vlib/v/checker/tests/import_mod_as_mod_err.vv @@ -0,0 +1,5 @@ +import math as math + +fn main() { + println(math.e) +} diff --git a/vlib/v/checker/tests/import_mod_sub_as_sub_err.out b/vlib/v/checker/tests/import_mod_sub_as_sub_err.out new file mode 100644 index 0000000000..6c9a63c246 --- /dev/null +++ b/vlib/v/checker/tests/import_mod_sub_as_sub_err.out @@ -0,0 +1,5 @@ +vlib/v/checker/tests/import_mod_sub_as_sub_err.v:1:25: error: import alias `encoding.utf8 as utf8` is redundant + 1 | import encoding.utf8 as utf8 + | ~~~~ + 2 | + 3 | fn main() { diff --git a/vlib/v/checker/tests/import_mod_sub_as_sub_err.vv b/vlib/v/checker/tests/import_mod_sub_as_sub_err.vv new file mode 100644 index 0000000000..81846bfd70 --- /dev/null +++ b/vlib/v/checker/tests/import_mod_sub_as_sub_err.vv @@ -0,0 +1,5 @@ +import encoding.utf8 as utf8 + +fn main() { + println(utf8.validate_str('añçá')) +} diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 518bc1352f..99008a78f5 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1495,6 +1495,9 @@ fn (mut p Parser) import_stmt() ast.Import { if p.tok.kind == .key_as { p.next() mod_alias = p.check_name() + if mod_alias == mod_name.split('.').last() { + p.error_with_pos('import alias `$mod_name as $mod_alias` is redundant', p.prev_tok.position()) + } } node := ast.Import{ pos: pos diff --git a/vlib/x/net/udp_test.v b/vlib/x/net/udp_test.v index d47e692065..ef86475709 100644 --- a/vlib/x/net/udp_test.v +++ b/vlib/x/net/udp_test.v @@ -1,4 +1,4 @@ -import x.net as net +import x.net import time fn echo_server(_c net.UdpConn) {