mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: disallow import names and import aliases, shadowing the module name (#18431)
This commit is contained in:
parent
acfe785597
commit
9c5aeb62b2
@ -1,7 +1,6 @@
|
|||||||
// tests are taken from https://tools.ietf.org/html/rfc2202
|
// tests are taken from https://tools.ietf.org/html/rfc2202
|
||||||
module hmac
|
module hmac
|
||||||
|
|
||||||
import crypto.hmac
|
|
||||||
import crypto.md5
|
import crypto.md5
|
||||||
import crypto.sha1
|
import crypto.sha1
|
||||||
import crypto.sha256
|
import crypto.sha256
|
||||||
@ -67,7 +66,7 @@ fn test_hmac_md5() {
|
|||||||
]
|
]
|
||||||
mut result := ''
|
mut result := ''
|
||||||
for i, key in hmac.keys {
|
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]
|
assert result == md5_expected_results[i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -84,7 +83,7 @@ fn test_hmac_sha1() {
|
|||||||
]
|
]
|
||||||
mut result := ''
|
mut result := ''
|
||||||
for i, key in hmac.keys {
|
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]
|
assert result == sha1_expected_results[i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -101,7 +100,7 @@ fn test_hmac_sha224() {
|
|||||||
]
|
]
|
||||||
mut result := ''
|
mut result := ''
|
||||||
for i, key in hmac.keys {
|
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]
|
assert result == sha224_expected_results[i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -118,7 +117,7 @@ fn test_hmac_sha256() {
|
|||||||
]
|
]
|
||||||
mut result := ''
|
mut result := ''
|
||||||
for i, key in hmac.keys {
|
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]
|
assert result == sha256_expected_results[i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -135,7 +134,7 @@ fn test_hmac_sha384() {
|
|||||||
]
|
]
|
||||||
mut result := ''
|
mut result := ''
|
||||||
for i, key in hmac.keys {
|
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]
|
assert result == sha384_expected_results[i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -152,7 +151,7 @@ fn test_hmac_sha512() {
|
|||||||
]
|
]
|
||||||
mut result := ''
|
mut result := ''
|
||||||
for i, key in hmac.keys {
|
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]
|
assert result == sha512_expected_results[i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -162,10 +161,10 @@ fn test_hmac_equal() {
|
|||||||
mac1_2 := '7641c48a3b4aa8f887c07b3e83f96affb89c978fed8c96fcbbf4ad596eebfe496f9f16da6cd080ba393c6f365ad72b50d15c71bfb1d6b81f66a911786c6ce932'.bytes()
|
mac1_2 := '7641c48a3b4aa8f887c07b3e83f96affb89c978fed8c96fcbbf4ad596eebfe496f9f16da6cd080ba393c6f365ad72b50d15c71bfb1d6b81f66a911786c6ce932'.bytes()
|
||||||
mac2_1 := '164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737'.bytes()
|
mac2_1 := '164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737'.bytes()
|
||||||
mac2_2 := '164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737'.bytes()
|
mac2_2 := '164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737'.bytes()
|
||||||
assert hmac.equal(mac1_1, mac1_2)
|
assert equal(mac1_1, mac1_2)
|
||||||
assert hmac.equal(mac2_1, mac2_2)
|
assert equal(mac2_1, mac2_2)
|
||||||
assert !hmac.equal(mac1_1, mac2_1)
|
assert !equal(mac1_1, mac2_1)
|
||||||
assert !hmac.equal(mac1_1, mac2_2)
|
assert !equal(mac1_1, mac2_2)
|
||||||
}
|
}
|
||||||
|
|
||||||
// not yet supported by crypto module
|
// not yet supported by crypto module
|
||||||
|
@ -169,6 +169,20 @@ pub fn (mut c Checker) check(ast_file_ &ast.File) {
|
|||||||
c.reset_checker_state_at_start_of_new_file()
|
c.reset_checker_state_at_start_of_new_file()
|
||||||
c.change_current_file(ast_file)
|
c.change_current_file(ast_file)
|
||||||
for i, ast_import in ast_file.imports {
|
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 {
|
for sym in ast_import.syms {
|
||||||
full_name := ast_import.mod + '.' + sym.name
|
full_name := ast_import.mod + '.' + sym.name
|
||||||
if full_name in c.const_names {
|
if full_name in c.const_names {
|
||||||
|
12
vlib/v/checker/tests/import_mod_as_duplicate_err.out
Normal file
12
vlib/v/checker/tests/import_mod_as_duplicate_err.out
Normal file
@ -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
|
7
vlib/v/checker/tests/import_mod_as_duplicate_err.vv
Normal file
7
vlib/v/checker/tests/import_mod_as_duplicate_err.vv
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
module json2
|
||||||
|
|
||||||
|
import json as json2
|
||||||
|
|
||||||
|
const used = true
|
||||||
|
|
||||||
|
println(json2.used)
|
12
vlib/v/checker/tests/import_mod_duplicate_as_sub_err.out
Normal file
12
vlib/v/checker/tests/import_mod_duplicate_as_sub_err.out
Normal file
@ -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
|
8
vlib/v/checker/tests/import_mod_duplicate_as_sub_err.vv
Normal file
8
vlib/v/checker/tests/import_mod_duplicate_as_sub_err.vv
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
module json
|
||||||
|
|
||||||
|
import json as vjson
|
||||||
|
|
||||||
|
const used = true
|
||||||
|
|
||||||
|
println(json.used)
|
||||||
|
println(vjson.used)
|
12
vlib/v/checker/tests/import_mod_duplicate_err.out
Normal file
12
vlib/v/checker/tests/import_mod_duplicate_err.out
Normal file
@ -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
|
7
vlib/v/checker/tests/import_mod_duplicate_err.vv
Normal file
7
vlib/v/checker/tests/import_mod_duplicate_err.vv
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
module json
|
||||||
|
|
||||||
|
import json
|
||||||
|
|
||||||
|
const used = true
|
||||||
|
|
||||||
|
println(json.used)
|
12
vlib/v/checker/tests/import_mod_sub_as_duplicate_err.out
Normal file
12
vlib/v/checker/tests/import_mod_sub_as_duplicate_err.out
Normal file
@ -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
|
5
vlib/v/checker/tests/import_mod_sub_as_duplicate_err.vv
Normal file
5
vlib/v/checker/tests/import_mod_sub_as_duplicate_err.vv
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
module json
|
||||||
|
|
||||||
|
import x.json2 as json
|
||||||
|
|
||||||
|
println(json.Any(json.null))
|
5
vlib/v/checker/tests/import_mod_sub_duplicate_as_mod.out
Normal file
5
vlib/v/checker/tests/import_mod_sub_duplicate_as_mod.out
Normal file
@ -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
|
7
vlib/v/checker/tests/import_mod_sub_duplicate_as_mod.vv
Normal file
7
vlib/v/checker/tests/import_mod_sub_duplicate_as_mod.vv
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
module json2
|
||||||
|
|
||||||
|
import x.json2 as json
|
||||||
|
|
||||||
|
any := json.Any(json.null)
|
||||||
|
|
||||||
|
println(any)
|
12
vlib/v/checker/tests/import_mod_sub_duplicate_err.out
Normal file
12
vlib/v/checker/tests/import_mod_sub_duplicate_err.out
Normal file
@ -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
|
5
vlib/v/checker/tests/import_mod_sub_duplicate_err.vv
Normal file
5
vlib/v/checker/tests/import_mod_sub_duplicate_err.vv
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
module json2
|
||||||
|
|
||||||
|
import x.json2
|
||||||
|
|
||||||
|
println(json2.Any(json2.null))
|
Loading…
Reference in New Issue
Block a user