checker: prohibit selective const imports (#10574)

pull/10581/head
shadowninja55 2021-06-26 11:09:52 -04:00 committed by GitHub
parent 02f4f635cf
commit eb5afb7403
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 5 deletions

View File

@ -117,6 +117,13 @@ pub fn new_checker(table &ast.Table, pref &pref.Preferences) &Checker {
pub fn (mut c Checker) check(ast_file &ast.File) {
c.change_current_file(ast_file)
for i, ast_import in ast_file.imports {
for sym in ast_import.syms {
full_name := ast_import.mod + '.' + sym.name
if full_name in c.const_names {
c.error('cannot selectively import constant `$sym.name` from `$ast_import.mod`, import `$ast_import.mod` and use `$full_name` instead',
sym.pos)
}
}
for j in 0 .. i {
if ast_import.mod == ast_file.imports[j].mod {
c.error('`$ast_import.mod` was already imported on line ${

View File

@ -0,0 +1,3 @@
vlib/v/checker/tests/selective_const_import.vv:1:15: error: cannot selectively import constant `second` from `time`, import `time` and use `time.second` instead
1 | import time { second }
| ~~~~~~

View File

@ -0,0 +1 @@
import time { second }

View File

@ -1,6 +1,6 @@
module main
import geometry { Line, Point, PointCond, Shape, module_name, point_str }
import geometry { Line, Point, PointCond, Shape, point_str }
fn point_is(p Point, cond PointCond) bool {
return cond(p)
@ -39,10 +39,6 @@ fn test_imported_symbols_functions() {
assert point_str(p0) == '20 40'
}
fn test_imported_symbols_constants() {
assert module_name == 'geometry'
}
fn vertex_count(s Shape) int {
return match s {
.circle { 0 }