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

Casting interface as itself generates invalid C code #17056

Closed
edam opened this issue Jan 20, 2023 · 0 comments · Fixed by #22335
Closed

Casting interface as itself generates invalid C code #17056

edam opened this issue Jan 20, 2023 · 0 comments · Fixed by #22335
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.

Comments

@edam
Copy link
Member

edam commented Jan 20, 2023

Describe the bug

Smartcasting an interface to itself creates invalid C-code and generates a casting function (in C) which unnecessarily duplicates the interface struct.

Expected Behavior

Smartcasting an interface to its self should have no effect!

Current Behavior

Causes C compiler error.

Reproduction Steps

The following code produces the problem:

interface Foo {}

fn (f &Foo) func() {}

struct Bar {}

fn main() {
    mut f := Foo(Bar{})
    if mut f is Foo {
        f.func()
    }
}

C compiler error:

error: cannot take the address of an rvalue of type 'main__Foo' (aka 'struct main__Foo')
                main__Foo_func(&(I_main__Foo_as_I_main__Foo(f)));
                               ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Possible Solution

No response

Additional Information/Context

In addition, the following unnecessary code is generated in C by V:

static inline bool I_main__Foo_is_I_main__Foo(main__Foo x) {
	return (x._typ == _main__Foo_main__Bar_index);
}
static inline main__Foo I_main__Foo_as_I_main__Foo(main__Foo x) {
	if (x._typ == _main__Foo_main__Bar_index) return I_main__Bar_to_Interface_main__Foo(x._main__Bar);
	_v_panic(string__plus(string__plus(tos3("`as_cast`: cannot convert "), tos3(v_typeof_interface_main__Foo(x._typ))), tos3(" to Foo")));
	return (main__Foo){0};
}

Issues with above generated code:

  • I_main__Foo_is_I_main__Foo(main__Foo x) is always True, isn't it? Or can x._typ be 0?
  • I_main__Foo_as_I_main__Foo(main__Foo x) is unnecessary, I think?
  • I_main__Foo_as_I_main__Foo(main__Foo x) calls I_main__Bar_to_Interface_main__Foo() which makes a NEW main.Foo and copies values in to it!

V version

V 0.4.0 207203f

Environment details (OS name and version, etc.)

macos

@edam edam added the Bug This tag is applied to issues which reports bugs. label Jan 20, 2023
@felipensp felipensp self-assigned this Aug 26, 2024
@felipensp felipensp added the Unit: cgen Bugs/feature requests, that are related to the default C generating backend. label Aug 26, 2024
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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants