Skip to content

Commit 70ce8c6

Browse files
authored
Merge pull request #77932 from swiftlang/egorzhdan/spurious-lifetime-diags
[cxx-interop] Do not emit spurious lifetime diagnostics for C++ types
2 parents ff9bb49 + 8202dfd commit 70ce8c6

File tree

4 files changed

+41
-15
lines changed

4 files changed

+41
-15
lines changed

include/swift/ClangImporter/ClangImporterRequests.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,15 @@ struct CxxRecordSemanticsDescriptor final {
327327
const clang::RecordDecl *decl;
328328
ASTContext &ctx;
329329

330-
CxxRecordSemanticsDescriptor(const clang::RecordDecl *decl,
331-
ASTContext &ctx)
332-
: decl(decl), ctx(ctx) {}
330+
/// Whether to emit warnings for missing destructor or copy constructor
331+
/// whenever the classification of the type assumes that they exist (e.g. for
332+
/// a value type).
333+
bool shouldDiagnoseLifetimeOperations;
334+
335+
CxxRecordSemanticsDescriptor(const clang::RecordDecl *decl, ASTContext &ctx,
336+
bool shouldDiagnoseLifetimeOperations = true)
337+
: decl(decl), ctx(ctx),
338+
shouldDiagnoseLifetimeOperations(shouldDiagnoseLifetimeOperations) {}
333339

334340
friend llvm::hash_code hash_value(const CxxRecordSemanticsDescriptor &desc) {
335341
return llvm::hash_combine(desc.decl);

lib/ClangImporter/ClangImporter.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7827,15 +7827,17 @@ CxxRecordSemantics::evaluate(Evaluator &evaluator,
78277827

78287828
if (!hasDestroyTypeOperations(cxxDecl) ||
78297829
(!hasCopyTypeOperations(cxxDecl) && !hasMoveTypeOperations(cxxDecl))) {
7830-
if (hasUnsafeAPIAttr(cxxDecl))
7831-
desc.ctx.Diags.diagnose({}, diag::api_pattern_attr_ignored,
7832-
"import_unsafe", decl->getNameAsString());
7833-
if (hasOwnedValueAttr(cxxDecl))
7834-
desc.ctx.Diags.diagnose({}, diag::api_pattern_attr_ignored,
7835-
"import_owned", decl->getNameAsString());
7836-
if (hasIteratorAPIAttr(cxxDecl))
7837-
desc.ctx.Diags.diagnose({}, diag::api_pattern_attr_ignored,
7838-
"import_iterator", decl->getNameAsString());
7830+
if (desc.shouldDiagnoseLifetimeOperations) {
7831+
if (hasUnsafeAPIAttr(cxxDecl))
7832+
desc.ctx.Diags.diagnose({}, diag::api_pattern_attr_ignored,
7833+
"import_unsafe", decl->getNameAsString());
7834+
if (hasOwnedValueAttr(cxxDecl))
7835+
desc.ctx.Diags.diagnose({}, diag::api_pattern_attr_ignored,
7836+
"import_owned", decl->getNameAsString());
7837+
if (hasIteratorAPIAttr(cxxDecl))
7838+
desc.ctx.Diags.diagnose({}, diag::api_pattern_attr_ignored,
7839+
"import_iterator", decl->getNameAsString());
7840+
}
78397841

78407842
return CxxRecordSemanticsKind::MissingLifetimeOperation;
78417843
}

lib/ClangImporter/ImportDecl.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,21 @@ bool ClangImporter::Implementation::recordHasReferenceSemantics(
156156
if (!isa<clang::CXXRecordDecl>(decl) && !ctx.LangOpts.CForeignReferenceTypes)
157157
return false;
158158

159-
auto semanticsKind =
160-
evaluateOrDefault(ctx.evaluator,
161-
CxxRecordSemantics({decl, ctx}), {});
159+
// At this point decl might not be fully imported into Swift yet, which
160+
// means we might not have asked Clang to generate its implicit members, such
161+
// as copy or move constructors. This would cause CxxRecordSemanticsRequest to
162+
// return MissingLifetimeOperation if the type is not a foreign reference
163+
// type. Note that this doesn't affect the correctness of this function, since
164+
// those implicit members aren't required for foreign reference types.
165+
166+
// To avoid emitting spurious diagnostics, let's disable them here. Types with
167+
// missing lifetime operations would get diagnosed later, once their members
168+
// are fully instantiated.
169+
auto semanticsKind = evaluateOrDefault(
170+
ctx.evaluator,
171+
CxxRecordSemantics(
172+
{decl, ctx, /* shouldDiagnoseLifetimeOperations */ false}),
173+
{});
162174
return semanticsKind == CxxRecordSemanticsKind::Reference;
163175
}
164176

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %target-typecheck-verify-swift -I %S/Inputs -cxx-interoperability-mode=upcoming-swift
2+
3+
import CxxStdlib
4+
import StdString
5+
6+
let _ = HasMethodThatReturnsString().getString()

0 commit comments

Comments
 (0)