Skip to content

Commit 198581a

Browse files
committed
fix(cpp1): do not pass UDT literal as NTTP
1 parent 52a2798 commit 198581a

6 files changed

+49
-9
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <chrono>
2+
using namespace std::chrono_literals;
3+
main: () = {
4+
[[assert: 10 as i32 == 10]]
5+
[[assert: 10LL as i32 == 10]]
6+
[[assert: 10s as std::chrono::seconds == 10s]]
7+
}

regression-tests/test-results/gcc-13/mixed-bugfix-for-literal-as-nttp.cpp.execution

Whitespace-only changes.

regression-tests/test-results/gcc-13/mixed-bugfix-for-literal-as-nttp.cpp.output

Whitespace-only changes.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
3+
//=== Cpp2 type declarations ====================================================
4+
5+
6+
#include "cpp2util.h"
7+
8+
9+
10+
//=== Cpp2 type definitions and function declarations ===========================
11+
12+
#include <chrono>
13+
using namespace std::chrono_literals;
14+
#line 3 "mixed-bugfix-for-literal-as-nttp.cpp2"
15+
auto main() -> int;
16+
17+
18+
//=== Cpp2 function definitions =================================================
19+
20+
21+
#line 3 "mixed-bugfix-for-literal-as-nttp.cpp2"
22+
auto main() -> int{
23+
cpp2::Default.expects(cpp2::as_<cpp2::i32, 10>()==10, "");
24+
cpp2::Default.expects(cpp2::as_<cpp2::i32, 10LL>()==10, "");
25+
cpp2::Default.expects(cpp2::as_<std::chrono::seconds>(10s)==10s, "");
26+
}
27+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mixed-bugfix-for-literal-as-nttp.cpp2... ok (mixed Cpp1/Cpp2, Cpp2 code passes safety checks)
2+

source/cppfront.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3152,16 +3152,20 @@ class cppfront
31523152
&& n.expr->get_postfix_expression_node()
31533153
&& n.expr->get_postfix_expression_node()->expr
31543154
);
3155-
if (auto t = n.expr->get_postfix_expression_node()->expr->get_token();
3156-
t
3157-
&& is_literal(t->type())
3158-
&& t->type() != lexeme::StringLiteral
3159-
&& t->type() != lexeme::FloatLiteral
3160-
&& std::ssize(n.ops) > 0
3161-
&& *n.ops[0].op == "as"
3162-
)
31633155
{
3164-
as_on_literal = true;
3156+
auto& p = n.expr->get_postfix_expression_node()->expr;
3157+
if (auto t = p->get_token();
3158+
t
3159+
&& is_literal(t->type())
3160+
&& t->type() != lexeme::StringLiteral
3161+
&& t->type() != lexeme::FloatLiteral
3162+
&& !std::get<primary_expression_node::literal>(p->expr)->user_defined_suffix
3163+
&& std::ssize(n.ops) > 0
3164+
&& *n.ops[0].op == "as"
3165+
)
3166+
{
3167+
as_on_literal = true;
3168+
}
31653169
}
31663170

31673171
for (

0 commit comments

Comments
 (0)