Skip to content

fix(cpp1): do not pass UDT literal as NTTP #489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions regression-tests/mixed-bugfix-for-literal-as-nttp.cpp2
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <chrono>
using namespace std::chrono_literals;
main: () = {
[[assert: 10 as i32 == 10]]
[[assert: 10LL as i32 == 10]]
[[assert: 10s as std::chrono::seconds == 10s]]
}
27 changes: 27 additions & 0 deletions regression-tests/test-results/mixed-bugfix-for-literal-as-nttp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@


//=== Cpp2 type declarations ====================================================


#include "cpp2util.h"



//=== Cpp2 type definitions and function declarations ===========================

#include <chrono>
using namespace std::chrono_literals;
#line 3 "mixed-bugfix-for-literal-as-nttp.cpp2"
auto main() -> int;


//=== Cpp2 function definitions =================================================


#line 3 "mixed-bugfix-for-literal-as-nttp.cpp2"
auto main() -> int{
cpp2::Default.expects(cpp2::as_<cpp2::i32, 10>()==10, "");
cpp2::Default.expects(cpp2::as_<cpp2::i32, 10LL>()==10, "");
cpp2::Default.expects(cpp2::as_<std::chrono::seconds>(10s)==10s, "");
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mixed-bugfix-for-literal-as-nttp.cpp2... ok (mixed Cpp1/Cpp2, Cpp2 code passes safety checks)

22 changes: 13 additions & 9 deletions source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3152,16 +3152,20 @@ class cppfront
&& n.expr->get_postfix_expression_node()
&& n.expr->get_postfix_expression_node()->expr
);
if (auto t = n.expr->get_postfix_expression_node()->expr->get_token();
t
&& is_literal(t->type())
&& t->type() != lexeme::StringLiteral
&& t->type() != lexeme::FloatLiteral
&& std::ssize(n.ops) > 0
&& *n.ops[0].op == "as"
)
{
as_on_literal = true;
auto& p = n.expr->get_postfix_expression_node()->expr;
if (auto t = p->get_token();
t
&& is_literal(t->type())
&& t->type() != lexeme::StringLiteral
&& t->type() != lexeme::FloatLiteral
&& !std::get<primary_expression_node::literal>(p->expr)->user_defined_suffix
&& std::ssize(n.ops) > 0
&& *n.ops[0].op == "as"
)
{
as_on_literal = true;
}
}

for (
Expand Down