v icon indicating copy to clipboard operation
v copied to clipboard

"Builder Error" when erroneously defining field `gg gg.Context = unsafe {nil}` not as a pointer in a structure

Open mikelgarai opened this issue 3 years ago • 1 comments

V doctor:

OS: linux, "Arch Linux"
Processor: 8 cpus, 64bit, little endian, Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
CC version: cc (GCC) 12.1.0

getwd: /home/mikel/dev/v/breakout
vmodules: /home/mikel/.vmodules
vroot: /home/mikel/dev/v/v
vexe: /home/mikel/dev/v/v/v
vexe mtime: 2022-07-24 12:41:55
is vroot writable: true
is vmodules writable: true
V full version: V 0.3.0 2d7406a

Git version: git version 2.36.1
Git vroot status: weekly.2022.29-56-g2d7406a8
.git/config present: true
thirdparty/tcc status: thirdparty-linux-amd64 827f7452

What did you do? v -g -o vdbg cmd/v && vdbg breakout.v

module main

import os
import time
import gx
import gg
// import sokol.sapp

const (
	win_width       = 800
	win_height      = 600
	text_size       = 24
	ball_speed      = 400
	pad_speed       = 500
	pad_size        = V2{100, 20}
	ball_d          = 20
)

struct V2 {
	x f32
	y f32
}

fn (a V2) str() string {
	return '{$a.x, $a.y}'
}

fn (a V2) + (b V2) V2 {
	return V2{a.x + b.x, a.y + b.y}
}

fn (a V2) - (b V2) V2 {
	return V2{a.x - b.x, a.y - b.y}
}

struct Brick {
	pos V2
	dead bool
}

struct Game {
mut:
	gg gg.Context = unsafe {nil}

	something int
	ball_pos V2
	pad_pos V2
	bricks []Brick
	frame_time  time.StopWatch = time.new_stopwatch()
}

fn frame(mut game &Game) {
	game.gg.begin()
	game.draw_scene()
	game.gg.end()
}

fn on_event(e &gg.Event, mut game &Game) {
}

fn main() {
	mut game := &Game{}
	mut fpath := os.resource_abs_path(os.join_path('..', 'assets', 'fonts', 'VeraMono.ttf'))

	game.gg = gg.new_context(
		bg_color: gx.black
		width: win_width
		height: win_height
		create_window: true
		window_title: 'V Breakout'
		user_data: game
		frame_fn: frame
		event_fn: on_event
		font_path: fpath // wait_events: true
	)
	game.init_game()
	game.gg.run()
}

fn (mut g Game) init_game() {
	//g.pad_pos = [win_width/2 - pad_size.x/2, win_height - 100 - pad_size.y/2]
	//g.ball_pos = [g.pad_pos.x + pad_size.x/2 - ball_d / 2, g.pad_pos.y - ball_d]
}

fn (mut g Game) draw_scene() {
	g.gg.draw_rect_filled(100, 100, 50, 50, gx.rgb(255, 0, 0))
}


What did you expect to see?

I was trying to do a game to play around with the language and this builder error happened.

So, I was copying a bit of tetris.v to do a simple breakout to test the language, and I copied the line gg gg.Context = unsafe {nil} with a mistake, I basically forgot to declare it as pointer (missing &).

But instead of this call failing with type mismatch:

	game.gg = gg.new_context( ...)

I got the builder error bellow. This only happened when adding any additional field to the "Game" struct, with only the gg context field:

struct Game {
mut:
       gg gg.Context = unsafe {nil}
}

it was working fine, it was even drawing a rectangle in a window.

Fixing the compilation mistake so it has the & on Game.gg declaration makes it work again.

offtopic: why force struct names to be uppercase? I prefer when my programming style can be defined by me, even if sometimes goes against the standard library. I personally always use _t for types and only lowercase...

What did you see instead?

==================
/usr/lib64/crt1.o: error: Invalid relocation entry [15] '.rela.debug_info' @ 000004f5
tcc: error: file 'crt1.o' not found
/tmp/v_1000/breakout.9500493379387292503.tmp.c:24976: warning: assignment discards qualifiers from pointer target type
/tmp/v_1000/breakout.9500493379387292503.tmp.c:27100: error: field expected
...
==================
(Use `v -cg` to print the entire error message)

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

mikelgarai avatar Jul 24 '22 14:07 mikelgarai

The problem is reproducible with tcc (the default), but is not with -cc clang or -cc gcc. I suspect that V generates C code that tcc is not yet smart enough to parse in that case. Good find.

spytheman avatar Jul 25 '22 08:07 spytheman