Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cgen error with mutexes #18088

Closed
i582 opened this issue Apr 30, 2023 · 4 comments · Fixed by #21949
Closed

Cgen error with mutexes #18088

i582 opened this issue Apr 30, 2023 · 4 comments · Fixed by #21949
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.

Comments

@i582
Copy link
Contributor

i582 commented Apr 30, 2023

Describe the bug

Code: https://vosca.dev/p/c8b88b8807

import sync
import time

struct SafeCounter {
mut:
    mt sync.Mutex = sync.new_mutex()
pub mut:
    value map[string]int
}

fn (mut c SafeCounter) inc(key string) {
    c.mt.@lock()
    defer {
        c.mt.unlock()
    }
    c.value[key]++
}

fn (mut c SafeCounter) value(key string) int {
    c.mt.@lock()
    defer {
        c.mt.unlock()
    }
    return c.value[key]
}

fn main() {
    mut counter := &SafeCounter{}

    for i := 0; i < 5; i++ {
        spawn fn [mut counter] () {
            for j := 0; j < 100; j++ {
                counter.inc('key')
            }
        }()
    }

    time.sleep(1 * time.second)
    println(counter.value('key'))
}

Expected Behavior

No errors as on my macOS machine

Current Behavior

Output:

/tmp/v_60000/../../../../../../box/code.v:28: warning: assignment makes integer from pointer without a cast
/tmp/v_60000/../../../../../../box/code.v:28: warning: cast between pointer and integer of different size
/tmp/v_60000/../../../../../../box/code.v:28: error: field expected
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

Exited with error status 1

Reproduction Steps

Run code above in playground (Ubuntu 22.04)

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.3.3 fc4c431.e738d67

Environment details (OS name and version, etc.)

V full version: V 0.3.3 fc4c431.e738d67
OS: linux, Ubuntu 22.04.2 LTS
Processor: 2 cpus, 64bit, little endian, Intel(R) Xeon(R) CPU E5-2670 v2 @ 2.50GHz

getwd: /home/pmakhnev/playground
vexe: /home/pmakhnev/v/v
vexe mtime: 2023-04-29 18:00:07

vroot: OK, value: /home/pmakhnev/v
VMODULES: OK, value: /root/.vmodules
VTMP: OK, value: /tmp/v_0

Git version: git version 2.34.1
Git vroot status: weekly.2023.17-12-ge738d671 (1 commit(s) behind V master)
.git/config present: true

CC version: cc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
thirdparty/tcc status: thirdparty-linux-amd64 12f392c3
@i582 i582 added the Bug This tag is applied to issues which reports bugs. label Apr 30, 2023
@felipensp felipensp added the Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. label May 1, 2023
@felipensp felipensp added the Unit: cgen Bugs/feature requests, that are related to the default C generating backend. label May 31, 2023
@i582 i582 closed this as completed Nov 25, 2023
@medvednikov medvednikov reopened this Nov 25, 2023
@felipensp
Copy link
Member

In this case the code must be mt &sync.Mutex = sync.new_mutex(), since new_mutex() returns &sync.Mutex.

@medvednikov we should warn on the checker right?

@medvednikov
Copy link
Member

Yeah, it should be an error.

I'm surprised it isn't already. Autoderef perhaps?

@felipensp
Copy link
Member

Already fixed.

@felipensp felipensp reopened this Jun 15, 2024
@felipensp
Copy link
Member

Building with -no-retry-compilation -cc clang-17

==================
   ^~~~~~~~~~~~~~~~~~
/tmp/v_1000/bug.01J0E583B93W38QS5ADRDV7BZE.tmp.c:19170:86: error: incompatible pointer to integer conversion initializing 'int' with an expression of type 'sync__Mutex *' (aka 'struct sync__Mutex *') [-Wint-conversion]
        main__SafeCounter* counter = ((main__SafeCounter*)memdup(&(main__SafeCounter){.mt = sync__new_mutex(),.value = new_map_noscan_value(sizeof(string), sizeof(int), &map_hash_string, &map_eq_string, &map_clone_string, &map_free_string),}, sizeof(main__SafeCounter)));
                                                                                            ^~~~~~~~~~~~~~~~~
1 warning and 1 error generated.
...
==================

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants