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

Compile error in cube type impl if struct behind conditional #333

Open
torsteingrindvik opened this issue Dec 1, 2024 · 3 comments
Open

Comments

@torsteingrindvik
Copy link
Contributor

I minified an example to roughly the methods foo and bar in the code below.
I'd expect them both to work since they achieve similar things but only bar works.

#[derive(CubeType)]
struct Foo {
    a: f32,
}

#[cube]
impl Foo {
    // mismatched types
    // expected struct `ExpandElementTyped<_>`
    //    found struct `FooExpand`
    pub fn foo(&self, v: u32) -> Foo {
        if v > 0 {
            Foo { a: 1.0 }
        } else {
            Foo { a: 0.0 }
        }
    }

    // ok
    pub fn bar(&self, v: u32) -> Foo {
        let mut foo = Foo { a: 0.0 };

        if v > 0 {
            foo.a = 1.0
        }

        foo
    }
}

I'm guessing this is related to the conditional in the expansion, see image:

image

@nathanielsimard
Copy link
Member

Yeah it can't work since the condition isn't constant. I could work if v was tagged as comptime.

@torsteingrindvik
Copy link
Contributor Author

Yeah it can't work since the condition isn't constant. I could work if v was tagged as comptime.

Ok- it's not obvious to me why. So I'm thinking others users will inevitably end up with similar errors.

Is it feasible to emit a good span from the proc macro if this happens?
Along with a helpful error msg this corner case would be less confusing.

@nathanielsimard
Copy link
Member

That would be an improvement! Still unsure how to make good error messages when the error comes from the generated code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants