checker: check for receiver name clashing with global var (fix #22698) (#22708)

This commit is contained in:
Felipe Pena 2024-10-31 01:46:08 -03:00 committed by GitHub
parent 63a192ccb3
commit 730b3f5e0f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 32 additions and 0 deletions

View file

@ -192,6 +192,10 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
}
}
if node.is_method {
if node.receiver.name in c.global_names {
c.error('cannot use global variable name `${node.receiver.name}` as receiver',
node.receiver_pos)
}
if node.receiver.typ.has_flag(.option) {
c.error('option types cannot have methods', node.receiver_pos)
}

View file

@ -0,0 +1,7 @@
vlib/v/checker/tests/globals/global_receiver_var_name_err.vv:17:9: error: cannot use global variable name `game` as receiver
15 | }
16 |
17 | fn (mut game Game) init() {
| ~~~~~~~~~
18 | game.world = GameObject{}
19 | }

View file

@ -0,0 +1,21 @@
module main
struct Game {
mut:
world GameObject
object_id u32
}
__global (
game Game
)
struct GameObject {
id int = game.object_id++
}
fn (mut game Game) init() {
game.world = GameObject{}
}
fn main() {}