vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.58k stars 2.15k forks source link

Compiler error on sumtype #12759

Open eimfach opened 2 years ago

eimfach commented 2 years ago

V doctor:

OS: macos, macOS, 11.6, 20G165
Processor: 4 cpus, 64bit, little endian, Intel(R) Core(TM) i5-6267U CPU @ 2.90GHz
CC version: Apple clang version 13.0.0 (clang-1300.0.29.3)

getwd: /Users/robingruenke/Documents/workspaces/v/examples/gg
vmodules: /Users/robingruenke/.vmodules
vroot: /Users/robingruenke/Documents/workspaces/v
vexe: /Users/robingruenke/Documents/workspaces/v/v
vexe mtime: 2021-12-08 05:57:44
is vroot writable: true
is vmodules writable: true
V full version: V 0.2.4 7a0b63e.525791f

Git version: git version 2.30.1 (Apple Git-130)
Git vroot status: weekly.2021.48-47-g525791fa-dirty
.git/config present: true
thirdparty/tcc status: thirdparty-macos-amd64 689c8a02

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

module main

import gg
import gx
import rand
import rand.util as rutil

const (
    win_width  = 800
    win_height = 800
)

struct App {
mut:
    gg          &gg.Context
    grid          [][]Position
}

struct Bacteria {
mut:
    genome gx.Color
}

type Inhabitant = Bacteria

struct Occupied {
mut:
    inhabitant Inhabitant
}

struct Empty {}

type Occupation = Empty | Occupied

struct Position {
mut:
    x   f32
    y f32
    occupation Occupation
}

fn main() {
    mut app := &App{
        gg: 0
    }
    app.gg = gg.new_context(
        bg_color: gx.white
        width: win_width
        height: win_height
        create_window: true
        window_title: 'Rectangles'
        frame_fn: frame
        user_data: app
        init_fn: init
    )
    app.gg.run()
}

[live]
fn init(mut app App) {

    mut bacteria := []Bacteria{len: 800, cap: 800, init: Bacteria{genome: gx.red}}

    for i := 0; i < bacteria.len; i++ {     
        bacteria[i].genome = rand_color()
    }

    app.grid = [][]Position{
        len: 80, 
        cap: 80, 
        init: []Position{len: 80, cap: 80, init: Position{x: 0, y: 0, occupation: Empty{}}}
    }

    for i := 0; i < app.grid.len; i++ {
        for j := 0; j < app.grid[i].len; j++ {
            app.grid[i][j].x = j * 10
            app.grid[i][j].y = i * 10

            if bacteria.len > 0 && rand.int_in_range(0, 1) == 1{
                b := bacteria.pop()
                app.grid[i][j].occupation = Occupied{inhabitant: b}
            }
        }
    }
}

fn frame(app &App) {
    app.gg.begin()
    app.draw()
    app.gg.end()
}

[live]
fn (app &App) draw() {
    // app.gg.draw_text_def(300,300, 'привет')

    // for i := 0; i < app.bacteria.len; i++ {
    //  b := &app.bacteria[i]
    //  app.gg.draw_circle_with_segments(b.pos.x, b.pos.y, 5, 30, b.genome)
    // }

    for i := 0; i < app.grid.len; i++ {
        for j := 0; j < app.grid[i].len; j++ {
            pos := &app.grid[i][j]
            color := match pos.occupation {
                Empty {gx.black}
                Occupied {gx.red}
            }

            if color != gx.black {
                app.gg.draw_circle_with_segments(pos.x, pos.y, 10, 30, color)
            }

        }
    }

}

fn rand_color() gx.Color {
    c1 := rand.int_in_range(0,63)
    c2 := rand.int_in_range(64, 127)
    c3 := rand.int_in_range(127, 191)

    mut colors := [c1, c2, c3]
    rutil.shuffle(mut colors, 0)

    return gx.rgb(byte(colors[0]),
     byte(colors[1]), 
     byte(colors[2])
     )
}

What did you expect to see?

Either a message on what I did wrong or a running application

What did you see instead?

INFO: compile with `v -live rectangles.v `, if you want to use the [live] function main.init .
INFO: compile with `v -live rectangles.v `, if you want to use the [live] function draw .
==================
                                                                                                         ^~~~~~~~~~~~~~~~~~~~~~~
/tmp/v_501/rectangles.10328074168220918149.tmp.c:2462:19: error: field has incomplete type 'main__Inhabitant' (aka 'struct main__Bacteria')
        main__Inhabitant inhabitant;
                         ^
/tmp/v_501/rectangles.10328074168220918149.tmp.c:111:16: note: forward declaration of 'struct main__Bacteria'
typedef struct main__Bacteria main__Bacteria;
               ^
/tmp/v_501/rectangles.10328074168220918149.tmp.c:5371:4: warning: expression result unused [-Wunused-value]
  (*(int*)_t1.data);
   ^~~~~~~~~~~~~~~
/tmp/v_501/rectangles.10328074168220918149.tmp.c:15830:4: warning: expression result unused [-Wunused-value]
  (*(bool*)_t7.data);
...
==================
(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
felipensp commented 4 months ago

Can you try again with a newer version?