v icon indicating copy to clipboard operation
v copied to clipboard

bug: mutable reference passing

Open kendfss opened this issue 2 years ago • 0 comments

Describe the bug

Passing mutable references into non-mutating functions without dereferencing (forgive any spelling errors) does not yield a normal error and is also not handled well by the compiler.

Expected Behavior

print hello, world.. That said, if I dereference App a in the call to Button.draw, the expected behaviour is realized.

Current Behavior

builder error:

C error. This should never happen.

This is a compiler bug, please report it using v bug file.v.

https://github.com/vlang/v/issues/new/choose

You can also use #help on Discord: https://discord.gg/vlang

/tmp/v_1000/bug.1233867846453961251.tmp.c:23635: warning: assignment discards qualifiers from pointer target type /tmp/v_1000/bug.1233867846453961251.tmp.c:25767: error: cannot convert 'struct main__App *' to 'struct main__App' /tmp/v_1000/bug.1233867846453961251.tmp.c:23635: warning: assignment discards qualifiers from pointer target type /tmp/v_1000/bug.1233867846453961251.tmp.c:25767: error: cannot convert 'struct main__App *' to 'struct main__App'

Reproduction Steps

see App.draw method.

import gg

struct App{
  buttons []Button
  theme Theme
mut:
  ctx gg.Context
}

struct Button{
  draw fn(App)
}

struct Theme{}

fn (mut a App) draw() {
  for button in a.buttons {
    button.draw(a)
  }
}

fn main() {
  println('hello, world.')
}

Possible Solution

I think a "type" or "pointer" error would be reasonable (and probably best for my use case), though annoying and compatibility violating, here. But if it is expected that the compiler handles these silently then the final C file should not wind up trying to convert the struct main__App * to a struct main__App (and I don't know enough C to be sure I'm not about to say something stupid) unless there's a way to dereference it

Additional Information/Context

No response

V version

V 0.3.3 b7b6c23

Environment details (OS name and version, etc.)

OS: linux, Linux Mint 20.3 Processor: 4 cpus, 64bit, little endian, Intel(R) Core(TM) i5-5350U CPU @ 1.80GHz CC version: cc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0

getwd: /home/kendfss/.vmodules/henrixounez/vpng vmodules: /home/kendfss/.vmodules vroot: /home/kendfss/gitclone/clones/vlang/v vexe: /home/kendfss/gitclone/clones/vlang/v/v vexe mtime: 2023-02-28 03:15:54 is vroot writable: true is vmodules writable: true V full version: V 0.3.3 38b02c9.b7b6c23

Git version: git version 2.25.1 Git vroot status: weekly.2023.09-4-gb7b6c236 .git/config present: true thirdparty/tcc status: thirdparty-linux-amd64 12f392c3

kendfss avatar Feb 28 '23 03:02 kendfss