Skip to content

Commit e1d0c05

Browse files
committed
Fixed bug in inspect expression (test convertible, not same)
And fixed a bug with correctly handling escaped `\"` in expanding string literals
1 parent 5ff759c commit e1d0c05

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

include/cpp2util.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,13 @@ auto as( X const& x ) -> auto&& {
524524
return x;
525525
}
526526

527+
template< typename C, typename X >
528+
auto as( X const& x ) -> auto
529+
requires (!std::is_same_v<C, X> && requires { C{x}; })
530+
{
531+
return C{x};
532+
}
533+
527534
template< typename C, typename X >
528535
requires std::is_base_of_v<C, X>
529536
auto as( X&& x ) -> C&& {

regression-tests/test-results/pure2-inspect-expression-with-as-in-generic-function.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ auto print_an_int(auto const& x) -> void{
2222
<< std::setw(30) << typeid(x).name()
2323
<< " value is "
2424
<< [&] () -> std::string { auto&& __expr = x;
25-
if (cpp2::is<int>(__expr)) { if constexpr( requires{std::to_string(cpp2::as<int>(x));} ) if constexpr( std::is_same_v<std::string, CPP2_TYPEOF(std::to_string(cpp2::as<int>(x)))> ) return std::to_string(cpp2::as<int>(x)); else return std::string{}; else return std::string{}; }
25+
if (cpp2::is<int>(__expr)) { if constexpr( requires{std::to_string(cpp2::as<int>(x));} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(std::to_string(cpp2::as<int>(x))),std::string> ) return std::to_string(cpp2::as<int>(x)); else return std::string{}; else return std::string{}; }
2626
else return "not an int"; }
2727
()
2828
<< "\n";

regression-tests/test-results/pure2-type-safety-2-with-inspect-expression.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ auto test_generic(auto const& x) -> void{
3535
<< std::setw(30) << typeid(x).name()
3636
<< " value is "
3737
<< [&] () -> std::string { auto&& __expr = x;
38-
if (cpp2::is<int>(__expr)) { if constexpr( requires{std::to_string(cpp2::as<int>(x));} ) if constexpr( std::is_same_v<std::string, CPP2_TYPEOF(std::to_string(cpp2::as<int>(x)))> ) return std::to_string(cpp2::as<int>(x)); else return std::string{}; else return std::string{}; }
38+
if (cpp2::is<int>(__expr)) { if constexpr( requires{std::to_string(cpp2::as<int>(x));} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(std::to_string(cpp2::as<int>(x))),std::string> ) return std::to_string(cpp2::as<int>(x)); else return std::string{}; else return std::string{}; }
3939
else return "not an int"; }
4040
()
4141
<< "\n";

source/cppfront.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ class cppfront
895895
++pos;
896896

897897
// Now we're on the first character of the string itself
898-
for ( ; pos < length && text[pos] != '"'; ++pos)
898+
for ( ; pos < length && (text[pos] != '"' || text[pos-1] == '\\'); ++pos)
899899
{
900900
// Find the next )$
901901
if (text[pos] == '$' && text[pos-1] == ')')
@@ -907,7 +907,7 @@ class cppfront
907907
// "next" in the string is the "last" one encountered in the backwards scan
908908
auto last_nonwhitespace = '\0';
909909

910-
for( ; text[open] != '"'; --open)
910+
for( ; text[open] != '"' || text[open-1] != '\\'; --open)
911911
{
912912
if (text[open] == ')') {
913913
++paren_depth;
@@ -1222,7 +1222,7 @@ class cppfront
12221222
auto return_prefix = std::string{};
12231223
auto return_suffix = std::string{";"}; // use this to tack the ; back on in the alternative body
12241224
if (is_expression) {
1225-
return_prefix = "{ if constexpr( requires{" + statement + ";} ) if constexpr( std::is_same_v<" + result_type + ", CPP2_TYPEOF(" + statement + ")> ) return ";
1225+
return_prefix = "{ if constexpr( requires{" + statement + ";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(" + statement + ")," + result_type + "> ) return ";
12261226
return_suffix += " }";
12271227
}
12281228

0 commit comments

Comments
 (0)