Skip to content

Commit 3faeae3

Browse files
committed
Apply 113 and add test case, closes #113
1 parent 5355204 commit 3faeae3

File tree

5 files changed

+64
-8
lines changed

5 files changed

+64
-8
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
f: () -> (ri : int = 0) = {
3+
pred := :(e:_) -> bool = { return e == 1; };
4+
ri = 42;
5+
return;
6+
}
7+
8+
g: () -> (ri : int) = {
9+
ri = 0;
10+
pred := :(e:_) -> bool = { return e == 1; };
11+
ri = 42;
12+
return;
13+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// ----- Cpp2 support -----
2+
#define CPP2_USE_MODULES Yes
3+
#include "cpp2util.h"
4+
5+
6+
#line 2 "pure2-look-up-parameter-across-unnamed-function.cpp2"
7+
struct f__ret {
8+
int ri;
9+
};
10+
#line 3 "pure2-look-up-parameter-across-unnamed-function.cpp2"
11+
[[nodiscard]] auto f() -> f__ret;
12+
#line 8 "pure2-look-up-parameter-across-unnamed-function.cpp2"
13+
struct g__ret {
14+
int ri;
15+
};
16+
#line 9 "pure2-look-up-parameter-across-unnamed-function.cpp2"
17+
[[nodiscard]] auto g() -> g__ret;
18+
19+
//=== Cpp2 definitions ==========================================================
20+
21+
#line 1 "pure2-look-up-parameter-across-unnamed-function.cpp2"
22+
23+
[[nodiscard]] auto f() -> f__ret{
24+
int ri = 0;
25+
#line 3 "pure2-look-up-parameter-across-unnamed-function.cpp2"
26+
auto pred { [](auto const& e) -> bool{return e == 1; } };
27+
ri = 42;
28+
return { std::move(ri) };
29+
}
30+
31+
[[nodiscard]] auto g() -> g__ret{
32+
cpp2::deferred_init<int> ri;
33+
#line 9 "pure2-look-up-parameter-across-unnamed-function.cpp2"
34+
ri.construct(0);
35+
auto pred { [](auto const& e) -> bool{return e == 1; } };
36+
ri.value() = 42;
37+
return { std::move(ri.value()) };
38+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pure2-look-up-parameter-across-unnamed-function.cpp2... ok (all Cpp2, passes safety checks)
2+

source/cppfront.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,15 +1086,12 @@ class cppfront
10861086
}
10871087

10881088
in_definite_init = is_definite_initialization(n.identifier);
1089-
if (in_synthesized_multi_return) {
1090-
printer.print_cpp2(".value()", n.position());
1091-
}
1092-
else if (!in_definite_init && !in_parameter_list) {
1089+
if (!in_definite_init && !in_parameter_list) {
10931090
if (auto decl = sema.get_local_declaration_of(*n.identifier);
10941091
is_local_name &&
10951092
decl &&
10961093
// note pointer equality: if we're not in the actual declaration of n.identifier
1097-
decl->identifier != n.identifier &&
1094+
(in_synthesized_multi_return || decl->identifier != n.identifier) &&
10981095
// and this variable was uninitialized
10991096
!decl->initializer &&
11001097
// and it's either a non-parameter or an out parameter
@@ -1104,6 +1101,9 @@ class cppfront
11041101
printer.print_cpp2(".value()", n.position());
11051102
}
11061103
}
1104+
else if (in_synthesized_multi_return) {
1105+
printer.print_cpp2(".value()", n.position());
1106+
}
11071107

11081108
if (add_std_move || add_std_forward) {
11091109
printer.print_cpp2(")", n.position());

source/sema.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ class sema
226226
{
227227
}
228228

229-
// Get the declaration of t within the same function
229+
// Get the declaration of t within the same named function
230230
//
231231
auto get_local_declaration_of(token const& t) -> declaration_sym const*
232232
{
@@ -252,9 +252,12 @@ class sema
252252
{
253253
auto const& decl = std::get<symbol::active::declaration>(ri->sym);
254254

255-
// Don't look beyond the current function
255+
// Don't look beyond the start of the current named (has identifier) function
256+
// (an unnamed function is ok to look beyond)
256257
assert(decl.declaration);
257-
if (decl.declaration->type.index() == declaration_node::function) {
258+
if (decl.declaration->type.index() == declaration_node::function &&
259+
decl.declaration->identifier)
260+
{
258261
return nullptr;
259262
}
260263

0 commit comments

Comments
 (0)