Skip to content

Commit 53504bb

Browse files
committed
Add test for generating code in enclosing namespace
1 parent 6d2cf46 commit 53504bb

14 files changed

+526
-509
lines changed

regression-tests/pure2-function-body-reflection.cpp2

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

2-
xyzzy: namespace = {
2+
ns: namespace = {
33

4-
// This function will be visible as a namespace member while reflecting on xyzzy::test
4+
// This function will be visible as a namespace member while reflecting on ns::test
55
sample_function_before_type: () = { }
66

77
test: @sample_traverser type =
@@ -75,9 +75,11 @@ test: @sample_traverser type =
7575
}
7676
}
7777

78-
// This function will not be visible as a namespace member while reflecting on xyzzy::test
78+
// This function will not be visible as a namespace member while reflecting on ns::test
7979
sample_function_after_type: () = { }
8080

8181
}
8282

83-
main: () = { }
83+
main: () = {
84+
std::cout << "calling generated function ns::add_1... ns::add_1(42) returned (ns::add_1(42))$\n";
85+
}

regression-tests/pure2-initialization-safety-with-else-if.cpp2

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ main: (args) = {
2525
}
2626

2727
ad_test: type = {
28-
intermediate_default_no_init: (x: double, y: double) -> (r: double) = {
29-
t: double;
30-
t = x + y;
28+
intermediate_default_no_init: (x: double, y: double) -> (r: double) = {
29+
t: double;
30+
t = x + y;
3131

32-
r = t; // OK, after t but it's a return value
33-
}
32+
r = t; // OK, after t but it's a return value
33+
}
3434
}
3535

3636
ok: () = {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
calling generated function ns::add_1... ns::add_1(42) returned 43
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
calling generated function ns::add_1... ns::add_1(42) returned 43
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
calling generated function ns::add_1... ns::add_1(42) returned 43
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
calling generated function ns::add_1... ns::add_1(42) returned 43

regression-tests/test-results/pure2-function-body-reflection.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#line 1 "pure2-function-body-reflection.cpp2"
1010

1111
#line 2 "pure2-function-body-reflection.cpp2"
12-
namespace xyzzy {
12+
namespace ns {
1313

1414
#line 7 "pure2-function-body-reflection.cpp2"
1515
class test;
@@ -23,11 +23,14 @@ class test;
2323
#line 1 "pure2-function-body-reflection.cpp2"
2424

2525
#line 2 "pure2-function-body-reflection.cpp2"
26-
namespace xyzzy {
26+
namespace ns {
2727

28-
// This function will be visible as a namespace member while reflecting on xyzzy::test
28+
// This function will be visible as a namespace member while reflecting on ns::test
2929
auto sample_function_before_type() -> void;
30+
[[nodiscard]] auto add_1(auto const& x) -> decltype(auto);
3031

32+
33+
#line 7 "pure2-function-body-reflection.cpp2"
3134
class test
3235
{
3336
public: [[nodiscard]] static auto one_liner(cpp2::impl::in<double> a, cpp2::impl::in<double> b, cpp2::impl::in<double> c) -> decltype(auto);
@@ -65,7 +68,7 @@ using branches_ret = double;
6568
#line 76 "pure2-function-body-reflection.cpp2"
6669
};
6770

68-
// This function will not be visible as a namespace member while reflecting on xyzzy::test
71+
// This function will not be visible as a namespace member while reflecting on ns::test
6972
auto sample_function_after_type() -> void;
7073

7174
}
@@ -77,11 +80,13 @@ auto main() -> int;
7780
#line 1 "pure2-function-body-reflection.cpp2"
7881

7982
#line 2 "pure2-function-body-reflection.cpp2"
80-
namespace xyzzy {
83+
namespace ns {
8184

8285
#line 5 "pure2-function-body-reflection.cpp2"
8386
auto sample_function_before_type() -> void{}
8487

88+
[[nodiscard]] auto add_1(auto const& x) -> decltype(auto) { return x + 1; }
89+
8590
#line 9 "pure2-function-body-reflection.cpp2"
8691
[[nodiscard]] auto test::one_liner(cpp2::impl::in<double> a, cpp2::impl::in<double> b, cpp2::impl::in<double> c) -> decltype(auto) { return (a + c) * b; }
8792

@@ -170,5 +175,7 @@ auto sample_function_after_type() -> void{}
170175
}
171176

172177
#line 83 "pure2-function-body-reflection.cpp2"
173-
auto main() -> int{}
178+
auto main() -> int{
179+
std::cout << "calling generated function ns::add_1... ns::add_1(42) returned " + cpp2::to_string(ns::add_1(42)) + "\n";
180+
}
174181

regression-tests/test-results/pure2-function-body-reflection.cpp2.output

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pure2-function-body-reflection.cpp2...Type: test
2-
is a member of namespace xyzzy which contains the following names:
2+
is a member of namespace ns which contains the following names:
33
sample_function_before_type
44
Declaration: one_liner
55
Function: one_liner

regression-tests/test-results/pure2-initialization-safety-with-else-if.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#line 27 "pure2-initialization-safety-with-else-if.cpp2"
1212
class ad_test;
13-
13+
1414

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

@@ -22,10 +22,10 @@ class ad_test {
2222
using intermediate_default_no_init_ret = double;
2323

2424
#line 28 "pure2-initialization-safety-with-else-if.cpp2"
25-
public: [[nodiscard]] static auto intermediate_default_no_init(cpp2::impl::in<double> x, cpp2::impl::in<double> y) -> intermediate_default_no_init_ret;
26-
public: ad_test() = default;
27-
public: ad_test(ad_test const&) = delete; /* No 'that' constructor, suppress copy */
28-
public: auto operator=(ad_test const&) -> void = delete;
25+
public: [[nodiscard]] static auto intermediate_default_no_init(cpp2::impl::in<double> x, cpp2::impl::in<double> y) -> intermediate_default_no_init_ret;
26+
public: ad_test() = default;
27+
public: ad_test(ad_test const&) = delete; /* No 'that' constructor, suppress copy */
28+
public: auto operator=(ad_test const&) -> void = delete;
2929

3030

3131
#line 34 "pure2-initialization-safety-with-else-if.cpp2"
@@ -65,14 +65,14 @@ auto main(int const argc_, char** argv_) -> int{
6565
}
6666

6767
#line 28 "pure2-initialization-safety-with-else-if.cpp2"
68-
[[nodiscard]] auto ad_test::intermediate_default_no_init(cpp2::impl::in<double> x, cpp2::impl::in<double> y) -> intermediate_default_no_init_ret{
69-
cpp2::impl::deferred_init<double> r;
68+
[[nodiscard]] auto ad_test::intermediate_default_no_init(cpp2::impl::in<double> x, cpp2::impl::in<double> y) -> intermediate_default_no_init_ret{
69+
cpp2::impl::deferred_init<double> r;
7070
#line 29 "pure2-initialization-safety-with-else-if.cpp2"
71-
cpp2::impl::deferred_init<double> t;
72-
t.construct(x + y);
71+
cpp2::impl::deferred_init<double> t;
72+
t.construct(x + y);
7373

74-
r.construct(cpp2::move(t.value()));// OK, after t but it's a return value
75-
return std::move(r.value()); }
74+
r.construct(cpp2::move(t.value()));// OK, after t but it's a return value
75+
return std::move(r.value()); }
7676

7777
#line 36 "pure2-initialization-safety-with-else-if.cpp2"
7878
auto ok() -> void{

regression-tests/test-results/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

2-
cppfront compiler v0.8.2 Build A812:1040
2+
cppfront compiler v0.8.2 Build A815:1039
33
SPDX-License-Identifier Apache-2.0 WITH LLVM-exception
44
Copyright (c) 2022-2025 Herb Sutter

0 commit comments

Comments
 (0)