Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 7 additions & 8 deletions clang/lib/Sema/CheckExprLifetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,13 @@ checkExprLifetimeImpl(Sema &SemaRef, const InitializedEntity *InitEntity,
if (LK == LK_FullExpression)
return;

if (LK == LK_Extended && SemaRef.getLangOpts().CPlusPlus23) {
if (const auto *VD = dyn_cast_if_present<VarDecl>(InitEntity->getDecl())) {
if (VD->isCXXForRangeImplicitVar())
return;
}
}

// FIXME: consider moving the TemporaryVisitor and visitLocalsRetained*
// functions to a dedicated class.
auto TemporaryVisitor = [&](const IndirectLocalPath &Path, Local L,
Expand Down Expand Up @@ -1341,14 +1348,6 @@ checkExprLifetimeImpl(Sema &SemaRef, const InitializedEntity *InitEntity,
}

if (IsGslPtrValueFromGslTempOwner && DiagLoc.isValid()) {

if (SemaRef.getLangOpts().CPlusPlus23) {
if (const VarDecl *VD =
dyn_cast_if_present<VarDecl>(InitEntity->getDecl());
VD && VD->isCXXForRangeImplicitVar())
return false;
}

SemaRef.Diag(DiagLoc, diag::warn_dangling_lifetime_pointer)
<< DiagRange;
return false;
Expand Down
5 changes: 3 additions & 2 deletions clang/test/SemaCXX/attr-lifetimebound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,9 @@ namespace p0936r0_examples {

std::vector make_vector();
void use_reversed_range() {
// FIXME: Don't expose the name of the internal range variable.
for (auto x : reversed(make_vector())) {} // expected-warning {{temporary implicitly bound to local reference will be destroyed at the end of the full-expression}}
// No warning here because C++23 extends the lifetime of the temporary
// in a range-based for loop.
for (auto x : reversed(make_vector())) {}
}

template <typename K, typename V>
Expand Down
Loading