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

Already on GitHub? Sign in to your account

Returning struct from nested function assertion #233

brechtvl opened this Issue Feb 12, 2013 · 5 comments


None yet
3 participants

brechtvl commented Feb 12, 2013

Compiling this

struct Test { float f; };

shader test()
    Test some_function()
        Test a;
        return a;

    Test b = some_function();

results in src/liboslcomp/typecheck.cpp:1241: Failed assertion '0': Don't know how to decode type code '49'

If some_function is not nested it still gives a compile error but no assertion is hit. I can try to fix this but I'm wondering if I should try to make it throw an error for this case or if returning structs should actually work?

Hi brechtvl,

I'm just diving into OSL and ran across this same issue. The problem doesn't seem to be limited by the scope (i.e., a nested function), but from my initial foray into the codebase it seems that returning a struct from a function is problematic on a few different levels.

The good news is that you can usually work around this by passing a struct in as an output parameter, which outflanks the issue, like so:

#include "stdosl.h"

struct MyColor { color c; };

void CalculateColor(output MyColor mc)
    mc.c = color(0.1, 0.2, 0.3);

shader test(output color outcolor = color(0.8))
    MyColor mc;
    outcolor = mc.c;

However, the spec does indicate that returning a struct from a function is valid (incidentally, the 'output' isn't strictly necessary in the current code incarnation, but it adheres better to the spec).

I don't know if you're building the libraries yourself, but since I was looking for a practical way to familiarize myself with the code I've been looking into fixing this. If there's any interest I can submit a patch once I find a way.


brechtvl commented Apr 3, 2013

The workaround is indeed simple, which is why I haven't spent much time trying to fix this, I had a quick look at the code but I couldn't see a simple solution.

Returning structs seems like something that should logically be supported, so I think a patch that fixes this would certainly be welcome.


lgritz commented Apr 3, 2013

It's intended to work, but obviously there's a bug. I just haven't had the time (or people telling me it's important) to dig into it yet.

Ya, and I wouldn't be surprised if your schedule has recently gotten more
hectic than it was before. I'd love to find a way to contribute, so if
you're interested in farming this out, let me know.
On Apr 3, 2013 9:17 AM, "Larry Gritz" notifications@github.com wrote:

It's intended to work, but obviously there's a bug. I just haven't had the
time (or people telling me it's important) to dig into it yet.

Reply to this email directly or view it on GitHubhttps://github.com/imageworks/OpenShadingLanguage/issues/233#issuecomment-15846628


lgritz commented Apr 27, 2016

This has been fixed.

@lgritz lgritz closed this Apr 27, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment