Skip to content

[BUG] Cannot use as with std::optional in inspect expression #1061

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

Closed
bluetarpmedia opened this issue May 9, 2024 · 2 comments
Closed

[BUG] Cannot use as with std::optional in inspect expression #1061

bluetarpmedia opened this issue May 9, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@bluetarpmedia
Copy link
Contributor

Describe the bug
Using as on a std::optional in an inspect expression causes a C++ compiler error.

To Reproduce
Run cppfront on this code:

main: () -> int = {
    opt: std::optional<int> = 10;

    std::cout << "Value: " << inspect opt -> int {
        is int = opt as int;
        is _ = 0;
    } << "\n";

    return 0;
}

and then compile with a C++ compiler. Clang reports:

main.cpp2:5:185: error: no viable conversion from returned value of type 'const nonesuch_' to function return type 'int'
    5 |         if (cpp2::impl::is<int>(_expr)) { if constexpr( requires{cpp2::impl::as<int>(opt);} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF((cpp2::impl::as<int>(opt))),int> ) return cpp2::impl::as<int>(opt); else return int{}; else return int{}; }
      |                                                                                                                                                                                         ^~~~~~~~~~~~~~~~~~~~~~~~

Repro on Godbolt

@hsutter
Copy link
Owner

hsutter commented May 15, 2024

Thanks! This should now be fixed with the above commit, please check.

@bluetarpmedia
Copy link
Contributor Author

All good now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants