checker: prohibit selective const imports (#10574)
parent
02f4f635cf
commit
eb5afb7403
|
@ -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 ${
|
||||
|
|
|
@ -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 }
|
||||
| ~~~~~~
|
|
@ -0,0 +1 @@
|
|||
import time { second }
|
|
@ -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 }
|
||||
|
|
Loading…
Reference in New Issue