From 9c5aeb62b263c9320701c39ebb55504c630eaf08 Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+tobealive@users.noreply.github.com> Date: Sat, 17 Jun 2023 15:39:10 +0200 Subject: [PATCH] checker: disallow import names and import aliases, shadowing the module name (#18431) --- vlib/crypto/hmac/hmac_test.v | 21 +++++++++---------- vlib/v/checker/checker.v | 14 +++++++++++++ .../tests/import_mod_as_duplicate_err.out | 12 +++++++++++ .../tests/import_mod_as_duplicate_err.vv | 7 +++++++ .../tests/import_mod_duplicate_as_sub_err.out | 12 +++++++++++ .../tests/import_mod_duplicate_as_sub_err.vv | 8 +++++++ .../tests/import_mod_duplicate_err.out | 12 +++++++++++ .../checker/tests/import_mod_duplicate_err.vv | 7 +++++++ .../tests/import_mod_sub_as_duplicate_err.out | 12 +++++++++++ .../tests/import_mod_sub_as_duplicate_err.vv | 5 +++++ .../tests/import_mod_sub_duplicate_as_mod.out | 5 +++++ .../tests/import_mod_sub_duplicate_as_mod.vv | 7 +++++++ .../tests/import_mod_sub_duplicate_err.out | 12 +++++++++++ .../tests/import_mod_sub_duplicate_err.vv | 5 +++++ 14 files changed, 128 insertions(+), 11 deletions(-) create mode 100644 vlib/v/checker/tests/import_mod_as_duplicate_err.out create mode 100644 vlib/v/checker/tests/import_mod_as_duplicate_err.vv create mode 100644 vlib/v/checker/tests/import_mod_duplicate_as_sub_err.out create mode 100644 vlib/v/checker/tests/import_mod_duplicate_as_sub_err.vv create mode 100644 vlib/v/checker/tests/import_mod_duplicate_err.out create mode 100644 vlib/v/checker/tests/import_mod_duplicate_err.vv create mode 100644 vlib/v/checker/tests/import_mod_sub_as_duplicate_err.out create mode 100644 vlib/v/checker/tests/import_mod_sub_as_duplicate_err.vv create mode 100644 vlib/v/checker/tests/import_mod_sub_duplicate_as_mod.out create mode 100644 vlib/v/checker/tests/import_mod_sub_duplicate_as_mod.vv create mode 100644 vlib/v/checker/tests/import_mod_sub_duplicate_err.out create mode 100644 vlib/v/checker/tests/import_mod_sub_duplicate_err.vv diff --git a/vlib/crypto/hmac/hmac_test.v b/vlib/crypto/hmac/hmac_test.v index 010757faa5..2d3d978981 100644 --- a/vlib/crypto/hmac/hmac_test.v +++ b/vlib/crypto/hmac/hmac_test.v @@ -1,7 +1,6 @@ // tests are taken from https://tools.ietf.org/html/rfc2202 module hmac -import crypto.hmac import crypto.md5 import crypto.sha1 import crypto.sha256 @@ -67,7 +66,7 @@ fn test_hmac_md5() { ] mut result := '' for i, key in hmac.keys { - result = hmac.new(key, hmac.data[i], md5.sum, md5.block_size).hex() + result = new(key, hmac.data[i], md5.sum, md5.block_size).hex() assert result == md5_expected_results[i] } } @@ -84,7 +83,7 @@ fn test_hmac_sha1() { ] mut result := '' for i, key in hmac.keys { - result = hmac.new(key, hmac.data[i], sha1.sum, sha1.block_size).hex() + result = new(key, hmac.data[i], sha1.sum, sha1.block_size).hex() assert result == sha1_expected_results[i] } } @@ -101,7 +100,7 @@ fn test_hmac_sha224() { ] mut result := '' for i, key in hmac.keys { - result = hmac.new(key, hmac.data[i], sha256.sum224, sha256.block_size).hex() + result = new(key, hmac.data[i], sha256.sum224, sha256.block_size).hex() assert result == sha224_expected_results[i] } } @@ -118,7 +117,7 @@ fn test_hmac_sha256() { ] mut result := '' for i, key in hmac.keys { - result = hmac.new(key, hmac.data[i], sha256.sum, sha256.block_size).hex() + result = new(key, hmac.data[i], sha256.sum, sha256.block_size).hex() assert result == sha256_expected_results[i] } } @@ -135,7 +134,7 @@ fn test_hmac_sha384() { ] mut result := '' for i, key in hmac.keys { - result = hmac.new(key, hmac.data[i], sha512.sum384, sha512.block_size).hex() + result = new(key, hmac.data[i], sha512.sum384, sha512.block_size).hex() assert result == sha384_expected_results[i] } } @@ -152,7 +151,7 @@ fn test_hmac_sha512() { ] mut result := '' for i, key in hmac.keys { - result = hmac.new(key, hmac.data[i], sha512.sum512, sha512.block_size).hex() + result = new(key, hmac.data[i], sha512.sum512, sha512.block_size).hex() assert result == sha512_expected_results[i] } } @@ -162,10 +161,10 @@ fn test_hmac_equal() { mac1_2 := '7641c48a3b4aa8f887c07b3e83f96affb89c978fed8c96fcbbf4ad596eebfe496f9f16da6cd080ba393c6f365ad72b50d15c71bfb1d6b81f66a911786c6ce932'.bytes() mac2_1 := '164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737'.bytes() mac2_2 := '164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737'.bytes() - assert hmac.equal(mac1_1, mac1_2) - assert hmac.equal(mac2_1, mac2_2) - assert !hmac.equal(mac1_1, mac2_1) - assert !hmac.equal(mac1_1, mac2_2) + assert equal(mac1_1, mac1_2) + assert equal(mac2_1, mac2_2) + assert !equal(mac1_1, mac2_1) + assert !equal(mac1_1, mac2_2) } // not yet supported by crypto module diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 1f382d01e2..c2f7cccc86 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -169,6 +169,20 @@ pub fn (mut c Checker) check(ast_file_ &ast.File) { c.reset_checker_state_at_start_of_new_file() c.change_current_file(ast_file) for i, ast_import in ast_file.imports { + // Imports with the same path and name (self-imports and module name conflicts with builtin module imports) + if c.mod == ast_import.mod { + c.error('cannot import `${ast_import.mod}` into a module with the same name', + ast_import.mod_pos) + } + // Duplicates of regular imports with the default alias (modname) and `as` imports with a custom alias + if c.mod == ast_import.alias { + if c.mod == ast_import.mod.all_after_last('.') { + c.error('cannot import `${ast_import.mod}` into a module with the same name', + ast_import.mod_pos) + } + c.error('cannot import `${ast_import.mod}` as `${ast_import.alias}` into a module with the same name', + ast_import.alias_pos) + } for sym in ast_import.syms { full_name := ast_import.mod + '.' + sym.name if full_name in c.const_names { diff --git a/vlib/v/checker/tests/import_mod_as_duplicate_err.out b/vlib/v/checker/tests/import_mod_as_duplicate_err.out new file mode 100644 index 0000000000..57e543a7b6 --- /dev/null +++ b/vlib/v/checker/tests/import_mod_as_duplicate_err.out @@ -0,0 +1,12 @@ +vlib/v/checker/tests/import_mod_as_duplicate_err.vv:3:16: error: cannot import `json` as `json2` into a module with the same name + 1 | module json2 + 2 | + 3 | import json as json2 + | ~~~~~ + 4 | + 5 | const used = true +vlib/v/checker/tests/import_mod_as_duplicate_err.vv:1:1: error: project must include a `main` module or be a shared library (compile with `v -shared`) + 1 | module json2 + | ^ + 2 | + 3 | import json as json2 diff --git a/vlib/v/checker/tests/import_mod_as_duplicate_err.vv b/vlib/v/checker/tests/import_mod_as_duplicate_err.vv new file mode 100644 index 0000000000..4bf7ab642c --- /dev/null +++ b/vlib/v/checker/tests/import_mod_as_duplicate_err.vv @@ -0,0 +1,7 @@ +module json2 + +import json as json2 + +const used = true + +println(json2.used) diff --git a/vlib/v/checker/tests/import_mod_duplicate_as_sub_err.out b/vlib/v/checker/tests/import_mod_duplicate_as_sub_err.out new file mode 100644 index 0000000000..1e404ae0c3 --- /dev/null +++ b/vlib/v/checker/tests/import_mod_duplicate_as_sub_err.out @@ -0,0 +1,12 @@ +vlib/v/checker/tests/import_mod_duplicate_as_sub_err.vv:3:8: error: cannot import `json` into a module with the same name + 1 | module json + 2 | + 3 | import json as vjson + | ~~~~ + 4 | + 5 | const used = true +vlib/v/checker/tests/import_mod_duplicate_as_sub_err.vv:1:1: error: project must include a `main` module or be a shared library (compile with `v -shared`) + 1 | module json + | ^ + 2 | + 3 | import json as vjson diff --git a/vlib/v/checker/tests/import_mod_duplicate_as_sub_err.vv b/vlib/v/checker/tests/import_mod_duplicate_as_sub_err.vv new file mode 100644 index 0000000000..de36380812 --- /dev/null +++ b/vlib/v/checker/tests/import_mod_duplicate_as_sub_err.vv @@ -0,0 +1,8 @@ +module json + +import json as vjson + +const used = true + +println(json.used) +println(vjson.used) diff --git a/vlib/v/checker/tests/import_mod_duplicate_err.out b/vlib/v/checker/tests/import_mod_duplicate_err.out new file mode 100644 index 0000000000..212aed5530 --- /dev/null +++ b/vlib/v/checker/tests/import_mod_duplicate_err.out @@ -0,0 +1,12 @@ +vlib/v/checker/tests/import_mod_duplicate_err.vv:3:8: error: cannot import `json` into a module with the same name + 1 | module json + 2 | + 3 | import json + | ~~~~ + 4 | + 5 | const used = true +vlib/v/checker/tests/import_mod_duplicate_err.vv:1:1: error: project must include a `main` module or be a shared library (compile with `v -shared`) + 1 | module json + | ^ + 2 | + 3 | import json diff --git a/vlib/v/checker/tests/import_mod_duplicate_err.vv b/vlib/v/checker/tests/import_mod_duplicate_err.vv new file mode 100644 index 0000000000..81770577a3 --- /dev/null +++ b/vlib/v/checker/tests/import_mod_duplicate_err.vv @@ -0,0 +1,7 @@ +module json + +import json + +const used = true + +println(json.used) diff --git a/vlib/v/checker/tests/import_mod_sub_as_duplicate_err.out b/vlib/v/checker/tests/import_mod_sub_as_duplicate_err.out new file mode 100644 index 0000000000..89bef753a4 --- /dev/null +++ b/vlib/v/checker/tests/import_mod_sub_as_duplicate_err.out @@ -0,0 +1,12 @@ +vlib/v/checker/tests/import_mod_sub_as_duplicate_err.vv:3:19: error: cannot import `x.json2` as `json` into a module with the same name + 1 | module json + 2 | + 3 | import x.json2 as json + | ~~~~ + 4 | + 5 | println(json.Any(json.null)) +vlib/v/checker/tests/import_mod_sub_as_duplicate_err.vv:1:1: error: project must include a `main` module or be a shared library (compile with `v -shared`) + 1 | module json + | ^ + 2 | + 3 | import x.json2 as json diff --git a/vlib/v/checker/tests/import_mod_sub_as_duplicate_err.vv b/vlib/v/checker/tests/import_mod_sub_as_duplicate_err.vv new file mode 100644 index 0000000000..b2266cc566 --- /dev/null +++ b/vlib/v/checker/tests/import_mod_sub_as_duplicate_err.vv @@ -0,0 +1,5 @@ +module json + +import x.json2 as json + +println(json.Any(json.null)) diff --git a/vlib/v/checker/tests/import_mod_sub_duplicate_as_mod.out b/vlib/v/checker/tests/import_mod_sub_duplicate_as_mod.out new file mode 100644 index 0000000000..eb4990bc3f --- /dev/null +++ b/vlib/v/checker/tests/import_mod_sub_duplicate_as_mod.out @@ -0,0 +1,5 @@ +vlib/v/checker/tests/import_mod_sub_duplicate_as_mod.vv:1:1: error: project must include a `main` module or be a shared library (compile with `v -shared`) + 1 | module json2 + | ^ + 2 | + 3 | import x.json2 as json diff --git a/vlib/v/checker/tests/import_mod_sub_duplicate_as_mod.vv b/vlib/v/checker/tests/import_mod_sub_duplicate_as_mod.vv new file mode 100644 index 0000000000..0e46c450c2 --- /dev/null +++ b/vlib/v/checker/tests/import_mod_sub_duplicate_as_mod.vv @@ -0,0 +1,7 @@ +module json2 + +import x.json2 as json + +any := json.Any(json.null) + +println(any) diff --git a/vlib/v/checker/tests/import_mod_sub_duplicate_err.out b/vlib/v/checker/tests/import_mod_sub_duplicate_err.out new file mode 100644 index 0000000000..735cd71abc --- /dev/null +++ b/vlib/v/checker/tests/import_mod_sub_duplicate_err.out @@ -0,0 +1,12 @@ +vlib/v/checker/tests/import_mod_sub_duplicate_err.vv:3:8: error: cannot import `x.json2` into a module with the same name + 1 | module json2 + 2 | + 3 | import x.json2 + | ~~~~~~~ + 4 | + 5 | println(json2.Any(json2.null)) +vlib/v/checker/tests/import_mod_sub_duplicate_err.vv:1:1: error: project must include a `main` module or be a shared library (compile with `v -shared`) + 1 | module json2 + | ^ + 2 | + 3 | import x.json2 diff --git a/vlib/v/checker/tests/import_mod_sub_duplicate_err.vv b/vlib/v/checker/tests/import_mod_sub_duplicate_err.vv new file mode 100644 index 0000000000..38e350c3db --- /dev/null +++ b/vlib/v/checker/tests/import_mod_sub_duplicate_err.vv @@ -0,0 +1,5 @@ +module json2 + +import x.json2 + +println(json2.Any(json2.null))