hsutter / cppfront

A personal experimental C++ Syntax 2 -> Syntax 1 compiler
Other
5.24k stars 224 forks source link

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

Closed bluetarpmedia closed 1 month ago

bluetarpmedia commented 1 month ago

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 commented 1 month ago

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

bluetarpmedia commented 1 month ago

All good now!