cppfront
cppfront copied to clipboard
[BUG] Ambiguous `as` cast with `-add-source-info`
trafficstars
Title: Ambiguous as cast with -add-source-info.
Minimal reproducer (https://cpp2.godbolt.org/z/9hqWeos6T):
main: () = {
i := 0;
_ = i as std::size_t;
}
Commands:
cppfront main.cpp2
clang++18 -std=c++23 -stdlib=libc++ -lc++abi -pedantic-errors -Wall -Wextra -Wconversion -Werror=unused-result -I . main.cpp
Expected result: Same as without -add-source-info.
Actual result and error:
Cpp2 lowered to Cpp1:
#define CPP2_USE_SOURCE_LOCATION Yes
//=== Cpp2 type declarations ====================================================
#include "cpp2util.h"
//=== Cpp2 type definitions and function declarations ===========================
auto main() -> int;
//=== Cpp2 function definitions =================================================
auto main() -> int{
auto i {0};
static_cast<void>(cpp2::as_<std::size_t>(std::move(i)));
}
Output:
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:470:44: warning: unused parameter 'where' [-Wunused-parameter]
470 | auto assert_in_bounds(auto&& x, auto&& arg CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT) -> decltype(auto)
| ^
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:369:76: note: expanded from macro 'CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT'
369 | #define CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT , std::source_location where = std::source_location::current()
| ^
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1894:52: error: call to 'as' is ambiguous
1894 | else if constexpr( std::is_same_v< CPP2_TYPEOF(as<C>(CPP2_FORWARD(x))), nonesuch_ > ) {
| ^~~~~
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:270:66: note: expanded from macro 'CPP2_TYPEOF'
270 | #define CPP2_TYPEOF(x) std::remove_cvref_t<decltype(x)>
| ^
build/main.cpp:20:27: note: in instantiation of function template specialization 'cpp2::as_<unsigned long, int>' requested here
20 | static_cast<void>(cpp2::as_<std::size_t>(std::move(i)));
| ^
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1144:6: note: candidate function [with C = unsigned long, auto:2 = int]
1144 | auto as(auto const&) -> auto {
| ^
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1174:23: note: candidate function [with C = unsigned long, x:auto = int]
1174 | inline constexpr auto as(auto const& x CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT) -> auto
| ^
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1896:13: error: static assertion failed due to requirement 'program_violates_type_safety_guarantee<unsigned long, int>': No safe 'as' cast available - please check your cast
1896 | program_violates_type_safety_guarantee<C, CPP2_TYPEOF(x)>,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1901:12: error: call to 'as' is ambiguous
1901 | return as<C>(CPP2_FORWARD(x));
| ^~~~~
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1144:6: note: candidate function [with C = unsigned long, auto:2 = int]
1144 | auto as(auto const&) -> auto {
| ^
raw.githubusercontent.com/hsutter/cppfront/main/include/cpp2util.h:1174:23: note: candidate function [with C = unsigned long, x:auto = int]
1174 | inline constexpr auto as(auto const& x CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT) -> auto
| ^
1 warning and 3 errors generated.