@@ -480,7 +480,7 @@ bool Converter::NeedsMut(const clang::VarDecl *decl, clang::QualType type,
480480 auto *method_or_null =
481481 curr_function_ ? clang::dyn_cast<clang::CXXMethodDecl>(curr_function_)
482482 : nullptr ;
483- return ((hoisted_decls_.contains (decl) ||
483+ return ((hoisted_decls_.contains (decl) || NeedsScopedDestructor (decl) ||
484484 (!type.isConstQualified () && !type->isReferenceType ())) &&
485485 ((method_or_null == nullptr ) || !method_or_null->isVirtual ()) &&
486486 !IsGlobalVar (decl) && name != " _" );
@@ -627,8 +627,21 @@ bool Converter::VisitVarDecl(clang::VarDecl *decl) {
627627 return false ;
628628}
629629
630- void Converter::EmitScopedDestructor (const clang::VarDecl *decl) {
630+ bool Converter::NeedsScopedDestructor (const clang::VarDecl *decl) const {
631631 if (in_function_formals_ || !decl->isLocalVarDecl () || IsGlobalVar (decl)) {
632+ return false ;
633+ }
634+ auto type = decl->getType ();
635+ if (auto lambda = AsLambdaClass (type);
636+ lambda && LambdaNeedsDestruction (lambda)) {
637+ return true ;
638+ }
639+ return !type->isReferenceType () && !type->isArrayType () &&
640+ TypeNeedsDestruction (type);
641+ }
642+
643+ void Converter::EmitScopedDestructor (const clang::VarDecl *decl) {
644+ if (!NeedsScopedDestructor (decl)) {
632645 return ;
633646 }
634647 auto type = decl->getType ();
@@ -640,10 +653,6 @@ void Converter::EmitScopedDestructor(const clang::VarDecl *decl) {
640653 GetNamedDeclAsString (decl)));
641654 return ;
642655 }
643- if (type->isReferenceType () || type->isArrayType () ||
644- !TypeNeedsDestruction (type)) {
645- return ;
646- }
647656 auto name = GetNamedDeclAsString (decl);
648657 StrCat (token::kSemiColon ,
649658 std::format (" let _dtor_{0} = ScopedDestructorUnsafe::new(&raw mut "
0 commit comments