Skip to content

Commit 64ab34b

Browse files
Merge pull request #618 from andreasfertig/fixIssue605
Fixed #605: Treat temporary as lambda initializer.
2 parents d74bfb4 + f92aee6 commit 64ab34b

3 files changed

Lines changed: 58 additions & 2 deletions

File tree

CodeGenerator.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3878,6 +3878,7 @@ void CodeGenerator::InsertArg(const CXXRecordDecl* stmt)
38783878
bool byConstRef{false};
38793879
auto fieldName{isThis ? kwInternalThis : name};
38803880
auto fieldDeclType{fd->getType()};
3881+
bool isMoved{};
38813882

38823883
std::string fname = StrCat("_"sv, name);
38833884

@@ -3923,13 +3924,23 @@ void CodeGenerator::InsertArg(const CXXRecordDecl* stmt)
39233924
) {
39243925
// this must go before adding the L or R-value reference, otherwise we get T& const instead of
39253926
// const T&
3926-
fieldDeclType.addConst();
3927+
3928+
if(exprWithoutImpCasts->isPRValue() and isa<CXXBindTemporaryExpr>(exprWithoutImpCasts) and
3929+
not exprWithoutImpCasts->getType().isConstQualified()) {
3930+
fieldDeclType = stmt->getASTContext().getRValueReferenceType(fieldDeclType);
3931+
EnableGlobalInsert(GlobalInserts::HeaderUtility);
3932+
fname = StrCat("std::move("sv, fname, ")"sv);
3933+
isMoved = true;
3934+
3935+
} else {
3936+
fieldDeclType.addConst();
3937+
}
39273938
}
39283939

39293940
if(exprWithoutImpCasts->isXValue()) {
39303941
fieldDeclType = stmt->getASTContext().getRValueReferenceType(fieldDeclType);
39313942

3932-
} else {
3943+
} else if(not isMoved) {
39333944
fieldDeclType = stmt->getASTContext().getLValueReferenceType(fieldDeclType);
39343945
}
39353946
}

tests/Issue605.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// cmdline:-std=c++23
2+
3+
#include <cstdio>
4+
#include <functional>
5+
#include <memory>
6+
7+
using namespace std;
8+
9+
int main()
10+
{
11+
[a=make_unique<int>(42)]() {};
12+
}

tests/Issue605.expect

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <utility> // std::move
2+
3+
#include <cstdio>
4+
#include <functional>
5+
#include <memory>
6+
7+
using namespace std;
8+
9+
int main()
10+
{
11+
12+
class __lambda_11_5
13+
{
14+
public:
15+
inline /*constexpr */ void operator()() const
16+
{
17+
}
18+
19+
private:
20+
std::unique_ptr<int, std::default_delete<int> > a;
21+
public:
22+
// inline /*constexpr */ __lambda_11_5(const __lambda_11_5 &) /* noexcept */ = delete;
23+
// inline /*constexpr */ __lambda_11_5 & operator=(const __lambda_11_5 &) /* noexcept */ = delete;
24+
// inline /*constexpr */ ~__lambda_11_5() noexcept = default;
25+
__lambda_11_5(std::unique_ptr<int, std::default_delete<int> > && _a)
26+
: a{std::move(_a)}
27+
{}
28+
29+
} __lambda_11_5{std::make_unique<int>(42)};
30+
31+
;
32+
return 0;
33+
}

0 commit comments

Comments
 (0)