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.76k stars 2.16k forks source link

shared / lock: shared array inside shared struct produces 'C error' instead of compiletime error #15393

Closed WoodyAtHome closed 2 years ago

WoodyAtHome commented 2 years ago
woody@lappi:~/sources/v/bugs$ v doctor
OS: linux, Ubuntu 20.04.4 LTS
Processor: 4 cpus, 64bit, little endian, Intel(R) Core(TM) i5-3320M CPU @ 2.60GHz
CC version: cc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0

getwd: /home/woody/sources/v/bugs
vmodules: /home/woody/.vmodules
vroot: /home/woody/sources/v/v
vexe: /home/woody/sources/v/v/v
vexe mtime: 2022-08-10 09:11:06
is vroot writable: true
is vmodules writable: true
V full version: V 0.3.0 372857c.78d0255

Git version: git version 2.25.1
Git vroot status: weekly.2022.31-39-g78d0255e
.git/config present: true
thirdparty/tcc status: thirdparty-linux-amd64 827f7452

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

module main 

import time

struct Alarms {
    mut:
    times shared []time.Time
}

fn (shared alarms Alarms) add(alarm time.Time) {
    lock alarms.times {
        if alarms.times.any(it == alarm) {
            return
        }
        alarms.times << alarm
    }
}

fn main() {
    shared alarms := Alarms{
        times: []time.Time{cap: 10}
    }

    utc := time.utc()
    alarms.add(utc)
}

What did you expect to see? a compile error in line 11, because alarms is not locked

What did you see instead?

==================
woody@lappi:~/sources/v/bugs$ v run shared_inner_struct.v 
==================
/tmp/v_1000/shared_inner_struct.16976288648774250628.tmp.c:17041: error: struct or union 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

==================
(Use `v -cg` to print the entire error message)
spytheman commented 2 years ago

The problem is in the generated code for .any(). Changing the example to:

        for x in alarms.times {
            if x == alarm {
                return
            }
        }

instead of:

        if alarms.times.any(it == alarm) {
            return
        }

compiles fine.

spytheman commented 2 years ago
#1 12:29:43 ᛋ master /v/vnew❱colordiff /tmp/v_1000/ss.tmp.c /tmp/v_1000/ss.8248969718819491200.tmp.c
17009c17009
<               int _t1_len = _t1_orig.len;
---
>               int _t1_len = _t1_orig->val.len;
17011c17011
<                       time__Time it = ((time__Time*) _t1_orig.data)[_t2];
---
>                       time__Time it = ((time__Time*) _t1_orig->val.data)[_t2];

^ these changes in the generated code for any() make it compile

spytheman commented 2 years ago

Should be fixed in 1c6366e .