From 272489ec3823776ac4a2a323d71c1b4d4cd9ceda Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Sun, 6 Aug 2023 18:09:46 +0100 Subject: [PATCH 1/3] Fix sizeof/new hint with parentheses Fixes #566. --- regression-tests/pure2-sizeof-new-error.cpp2 | 6 ++++++ .../test-results/pure2-sizeof-new-error.cpp2.output | 5 +++++ source/parse.h | 10 ++++------ 3 files changed, 15 insertions(+), 6 deletions(-) create mode 100644 regression-tests/pure2-sizeof-new-error.cpp2 create mode 100644 regression-tests/test-results/pure2-sizeof-new-error.cpp2.output diff --git a/regression-tests/pure2-sizeof-new-error.cpp2 b/regression-tests/pure2-sizeof-new-error.cpp2 new file mode 100644 index 0000000000..6eda268d1e --- /dev/null +++ b/regression-tests/pure2-sizeof-new-error.cpp2 @@ -0,0 +1,6 @@ +main: () = { + p := new i32; + p = new(i32); + i := sizeof(i32); + i = sizeof i32; +} diff --git a/regression-tests/test-results/pure2-sizeof-new-error.cpp2.output b/regression-tests/test-results/pure2-sizeof-new-error.cpp2.output new file mode 100644 index 0000000000..86e43cd092 --- /dev/null +++ b/regression-tests/test-results/pure2-sizeof-new-error.cpp2.output @@ -0,0 +1,5 @@ +pure2-sizeof-new-error.cpp2... +pure2-sizeof-new-error.cpp2(2,14): error: use 'new', not 'new i32' +pure2-sizeof-new-error.cpp2(3,12): error: use 'new', not 'new i32' +pure2-sizeof-new-error.cpp2(4,16): error: use 'sizeof', not 'sizeof i32' +pure2-sizeof-new-error.cpp2(5,16): error: use 'sizeof', not 'sizeof i32' diff --git a/source/parse.h b/source/parse.h index fb29ea859f..1ab0f7e860 100644 --- a/source/parse.h +++ b/source/parse.h @@ -4880,12 +4880,10 @@ class parser } else { - if (*n->identifier == "new") { - error( "use 'new<" + curr().to_string(true) + ">', not 'new " + curr().to_string(true) + "'", false); - return {}; - } - if (*n->identifier == "sizeof") { - error( "use 'sizeof(" + curr().to_string(true) + ")', not 'sizeof " + curr().to_string(true) + "'", false); + if (*n->identifier == "new" || *n->identifier == "sizeof") { + auto id = n->identifier->to_string(true); + auto& arg = (curr().type() == lexeme::LeftParen) ? *peek(1) : curr(); + error( "use '" + id + "<" + arg.to_string(true) + ">', not '" + id + " " + arg.to_string(true) + "'", false); return {}; } if (*n->identifier == "co_await" || *n->identifier == "co_yield") { From 813e03491d3da091c8aa44f599e27cc4d110cd4e Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Sun, 6 Aug 2023 19:59:51 +0100 Subject: [PATCH 2/3] Fix generating `sizeof(T)` --- regression-tests/mixed-fixed-type-aliases.cpp2 | 3 +++ source/cppfront.cpp | 9 ++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/regression-tests/mixed-fixed-type-aliases.cpp2 b/regression-tests/mixed-fixed-type-aliases.cpp2 index 7fd741264d..8f59d8b321 100644 --- a/regression-tests/mixed-fixed-type-aliases.cpp2 +++ b/regression-tests/mixed-fixed-type-aliases.cpp2 @@ -1,5 +1,6 @@ #include #include +#include namespace my { using u16 = float; @@ -13,9 +14,11 @@ test: (x:_) = { } main: (args) -> int = { + assert(sizeof == 4); y: my::u16 = 42; test(y); + assert(sizeof == 2); z: u16 = 42; test(z); diff --git a/source/cppfront.cpp b/source/cppfront.cpp index fbd7d49e57..6b5816a6f8 100644 --- a/source/cppfront.cpp +++ b/source/cppfront.cpp @@ -1666,7 +1666,14 @@ class cppfront assert(n.identifier); emit(*n.identifier, is_qualified); // inform the identifier if we know this is qualified - if (n.open_angle != source_position{}) { + if (*n.identifier == "sizeof") { + printer.print_cpp2("(", n.open_angle); + auto& a = n.template_args[0]; + try_emit(a.arg); + try_emit(a.arg); + printer.print_cpp2(")", n.close_angle); + } + else if (n.open_angle != source_position{}) { printer.print_cpp2("<", n.open_angle); auto first = true; for (auto& a : n.template_args) { From 5bec9fadfffa2dc22218e7e6ef4a041388ac4d45 Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Sun, 6 Aug 2023 20:11:50 +0100 Subject: [PATCH 3/3] Update generated .cpp --- .../test-results/mixed-fixed-type-aliases.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/regression-tests/test-results/mixed-fixed-type-aliases.cpp b/regression-tests/test-results/mixed-fixed-type-aliases.cpp index 25a2a41a0c..d575941ace 100644 --- a/regression-tests/test-results/mixed-fixed-type-aliases.cpp +++ b/regression-tests/test-results/mixed-fixed-type-aliases.cpp @@ -11,23 +11,24 @@ #include #include +#include namespace my { using u16 = float; } -#line 8 "mixed-fixed-type-aliases.cpp2" +#line 9 "mixed-fixed-type-aliases.cpp2" auto test(auto const& x) -> void; -#line 15 "mixed-fixed-type-aliases.cpp2" +#line 16 "mixed-fixed-type-aliases.cpp2" [[nodiscard]] auto main(int const argc_, char const* const* const argv_) -> int; //=== Cpp2 function definitions ================================================= -#line 8 "mixed-fixed-type-aliases.cpp2" +#line 9 "mixed-fixed-type-aliases.cpp2" auto test(auto const& x) -> void{ std::cout << std::boolalpha @@ -37,10 +38,12 @@ auto test(auto const& x) -> void{ [[nodiscard]] auto main(int const argc_, char const* const* const argv_) -> int{ auto args = cpp2::make_args(argc_, argv_); -#line 16 "mixed-fixed-type-aliases.cpp2" +#line 17 "mixed-fixed-type-aliases.cpp2" + assert(sizeof(my::u16)==4); my::u16 y {42}; test(std::move(y)); + assert(sizeof(cpp2::u16)==2); cpp2::u16 z {42}; test(std::move(z));