Skip to content

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

Closed
@bluetarpmedia

Description

@bluetarpmedia

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions