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 for program using $for variant in i.variants { in a .str() method of a sumtype #22993

Closed
spytheman opened this issue Nov 27, 2024 · 3 comments
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Comptime Features processed during compile time, like $if, $for, $env etc Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: markused Bugs/feature requests, that are related to the -skip-unused.

Comments

@spytheman
Copy link
Member

spytheman commented Nov 27, 2024

V doctor:

V full version: V 0.4.8 aeaf607.273adcc
OS: linux, Ubuntu 20.04.6 LTS
Processor: 4 cpus, 64bit, little endian, Intel(R) Core(TM) i3-3225 CPU @ 3.30GHz

getwd: /home/delian/code/v/qbe
vexe: /home/delian/code/v/v
vexe mtime: 2024-11-27 17:05:43

vroot: OK, value: /home/delian/code/v
VMODULES: OK, value: /home/delian/.vmodules
VTMP: OK, value: /tmp/v_1000

env VFLAGS: "-skip-unused"

Git version: git version 2.46.0
Git vroot status: weekly.2024.48-13-g273adcc3
.git/config present: true

CC version: cc (Ubuntu 10.5.0-1ubuntu1~20.04) 10.5.0
emcc version: N/A
thirdparty/tcc status: thirdparty-linux-amd64 0134e9b9

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

struct Add { x f32 y f32}
struct Sub { x f32 y f32}

fn (o Add) str() string { return 'Add ${o.x} ${o.y}' }
fn (o Sub) str() string { return 'Sub ${o.x} ${o.y}' }

pub type Instr = Add | Sub

pub fn (i Instr) str() string {
    $for variant in i.variants {
        if i is variant {
            return i.str()
        }
    }
    return ''
}							

fn main() {
	i1 := Instr(Add{1,2})
	println(i1)
	i2 := Instr(Sub{3,4})
	println(i2)
}

What did you expect to see?

a compiled program, with -skip-unused

What did you see instead?

================== C compilation error (from tcc): ==============
cc: /tmp/v_1000/a.01JDQC0JRVTZ1RR7M5J6AJ5V3W.tmp.c:5682: warning: implicit declaration of function 'main__Add_str'
cc: /tmp/v_1000/a.01JDQC0JRVTZ1RR7M5J6AJ5V3W.tmp.c:5682: error: cannot convert 'int' to 'struct string'
=================================================================
(You can pass `-cg`, or `-show-c-output` as well, to print all the C error messages).

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Huly®: V_0.6-21434

@spytheman spytheman added Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: markused Bugs/feature requests, that are related to the -skip-unused. Comptime Features processed during compile time, like $if, $for, $env etc labels Nov 27, 2024
@spytheman
Copy link
Member Author

Note, that using @[markused] for each .str() method of the variants makes it compile with -skip-unused . So does not using -skip-unused at all.

@spytheman
Copy link
Member Author

Another variation also errors at cgen (with autogenerated .str() methods for the variants):

struct Add { x f32 y f32}
struct Sub { x f32 y f32}

pub type Instr = Add | Sub

pub fn (i Instr) str() string {
    $for variant in i.variants {
        if i is variant {
            return i.str()
        }
    }
    return ''
}

fn main() {
        i1 := Instr(Add{1,2})
        println(i1)
        i2 := Instr(Sub{3,4})
        println(i2)
}

@felipensp felipensp self-assigned this Nov 27, 2024
@spytheman
Copy link
Member Author

Not sure why it did not close, but it does work after 2325b1b (#22994).

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. Comptime Features processed during compile time, like $if, $for, $env etc Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: markused Bugs/feature requests, that are related to the -skip-unused.
Projects
None yet
Development

No branches or pull requests

2 participants