fmt: preserve local fn parameter, when a module const with the same name is declared

pull/11323/head
Delyan Angelov 2021-08-27 09:22:53 +03:00
parent bdf11d969a
commit 16455a7660
2 changed files with 30 additions and 6 deletions

View File

@ -46,6 +46,7 @@ pub mut:
inside_lambda bool inside_lambda bool
inside_const bool inside_const bool
is_mbranch_expr bool // match a { x...y { } } is_mbranch_expr bool // match a { x...y { } }
fn_scope &ast.Scope = voidptr(0)
} }
pub fn fmt(file ast.File, table &ast.Table, pref &pref.Preferences, is_debug bool) string { pub fn fmt(file ast.File, table &ast.Table, pref &pref.Preferences, is_debug bool) string {
@ -879,6 +880,11 @@ pub fn (mut f Fmt) anon_fn(node ast.AnonFn) {
} }
fn (mut f Fmt) fn_body(node ast.FnDecl) { fn (mut f Fmt) fn_body(node ast.FnDecl) {
prev_fn_scope := f.fn_scope
f.fn_scope = node.scope
defer {
f.fn_scope = prev_fn_scope
}
if node.language == .v { if node.language == .v {
if !node.no_body { if !node.no_body {
f.write(' {') f.write(' {')
@ -1710,12 +1716,18 @@ pub fn (mut f Fmt) ident(node ast.Ident) {
} else if node.kind == .blank_ident { } else if node.kind == .blank_ident {
f.write('_') f.write('_')
} else { } else {
// Force usage of full path to const in the same module: mut is_local := false
// `println(minute)` => `println(time.minute)` if !isnil(f.fn_scope) {
// This makes it clear that a module const is being used if _ := f.fn_scope.find_var(node.name) {
// (since V's consts are no longer ALL_CAP). is_local = true
// ^^^ except for `main`, where consts are allowed to not have a `main.` prefix. }
if !node.name.contains('.') && !f.inside_const { }
if !is_local && !node.name.contains('.') && !f.inside_const {
// Force usage of full path to const in the same module:
// `println(minute)` => `println(time.minute)`
// This makes it clear that a module const is being used
// (since V's consts are no longer ALL_CAP).
// ^^^ except for `main`, where consts are allowed to not have a `main.` prefix.
mod := f.cur_mod mod := f.cur_mod
full_name := mod + '.' + node.name full_name := mod + '.' + node.name
if obj := f.file.global_scope.find(full_name) { if obj := f.file.global_scope.find(full_name) {

View File

@ -0,0 +1,12 @@
module xyz
const params = []string{}
pub fn abc(params []string) {
if params.len > 5 {
println('more')
}
for x in params {
println(x)
}
}