Skip to content

Commit 75f9880

Browse files
authored
fix(cpp1): do not pass UDT literal as NTTP (#489)
1 parent 0e061bd commit 75f9880

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
@@ -3154,16 +3154,20 @@ class cppfront
31543154
&& n.expr->get_postfix_expression_node()
31553155
&& n.expr->get_postfix_expression_node()->expr
31563156
);
3157-
if (auto t = n.expr->get_postfix_expression_node()->expr->get_token();
3158-
t
3159-
&& is_literal(t->type())
3160-
&& t->type() != lexeme::StringLiteral
3161-
&& t->type() != lexeme::FloatLiteral
3162-
&& std::ssize(n.ops) > 0
3163-
&& *n.ops[0].op == "as"
3164-
)
31653157
{
3166-
as_on_literal = true;
3158+
auto& p = n.expr->get_postfix_expression_node()->expr;
3159+
if (auto t = p->get_token();
3160+
t
3161+
&& is_literal(t->type())
3162+
&& t->type() != lexeme::StringLiteral
3163+
&& t->type() != lexeme::FloatLiteral
3164+
&& !std::get<primary_expression_node::literal>(p->expr)->user_defined_suffix
3165+
&& std::ssize(n.ops) > 0
3166+
&& *n.ops[0].op == "as"
3167+
)
3168+
{
3169+
as_on_literal = true;
3170+
}
31673171
}
31683172

31693173
for (

0 commit comments

Comments
 (0)