Skip to content

[cxx-interop] Support calling functions in inline namespaces #83752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions include/swift/ClangImporter/ClangModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ class ClangModuleUnit final : public LoadedFile {
}
};

bool isInlineNamespaceInside(const NominalType *outer,
const NominalType *inner);
}

#endif
6 changes: 6 additions & 0 deletions lib/AST/ASTVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@
#include "swift/AST/Stmt.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/AST/TypeRepr.h"
#include "swift/AST/Types.h"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/SourceManager.h"
#include "swift/ClangImporter/ClangModule.h"
#include "swift/Subsystems.h"
#include "llvm/ADT/SmallBitVector.h"
#include "llvm/ADT/SmallString.h"
Expand Down Expand Up @@ -3606,6 +3608,10 @@ class Verifier : public ASTWalker {
const char *what) {
if (srcTy->isEqual(destTy)) return;

if (isInlineNamespaceInside(srcTy->getAs<NominalType>(),
destTy->getAs<NominalType>()))
return;

if (auto srcMetatype = srcTy->getAs<AnyMetatypeType>()) {
if (auto destMetatype = destTy->getAs<AnyMetatypeType>()) {
return checkTrivialSubtype(E,
Expand Down
17 changes: 17 additions & 0 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8870,3 +8870,20 @@ swift::importer::getCxxRefConventionWithAttrs(const clang::Decl *decl) {

return std::nullopt;
}

bool swift::isInlineNamespaceInside(const NominalType *outer,
const NominalType *inner) {
if (!outer || !inner)
return false;
auto CDOuter = outer->getDecl()->getClangDecl();
if (!isa_and_nonnull<clang::NamespaceDecl>(CDOuter))
return false;

auto CDInner = inner->getDecl()->getClangDecl();
while (isa_and_nonnull<clang::NamespaceDecl>(CDInner) &&
cast<clang::NamespaceDecl>(CDInner)->isInline()) {
CDInner = cast<clang::Decl>(CDInner->getDeclContext());
}

return CDInner->getCanonicalDecl() == CDOuter->getCanonicalDecl();
}
13 changes: 13 additions & 0 deletions lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "swift/Basic/Defer.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Basic/type_traits.h"
#include "swift/ClangImporter/ClangModule.h"
#include "swift/SIL/Consumption.h"
#include "swift/SIL/DynamicCasts.h"
#include "swift/SIL/SILArgument.h"
Expand Down Expand Up @@ -1503,6 +1504,18 @@ RValue RValueEmitter::visitMetatypeConversionExpr(MetatypeConversionExpr *E,
SILValue metaBase =
SGF.emitRValueAsSingleValue(E->getSubExpr()).getUnmanagedValue();

if (isInlineNamespaceInside(E->getSubExpr()
->getType()
->getAs<AnyMetatypeType>()
->getInstanceType()
->getAs<NominalType>(),
E->getType()
->getAs<AnyMetatypeType>()
->getInstanceType()
->getAs<NominalType>()))
return RValue(SGF, E,
ManagedValue::forObjectRValueWithoutOwnership(metaBase));

// Metatype conversion casts in the AST might not be reflected as
// such in the SIL type system, for example, a cast from DynamicSelf.Type
// directly to its own Self.Type.
Expand Down
12 changes: 12 additions & 0 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@
#include "swift/Basic/StringExtras.h"
#include "swift/ClangImporter/ClangModule.h"
#include "swift/Sema/CSFix.h"
#include "swift/Sema/Constraint.h"
#include "swift/Sema/ConstraintSystem.h"
#include "swift/Sema/IDETypeChecking.h"
#include "swift/Sema/PreparedOverload.h"
#include "clang/AST/Decl.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Compiler.h"

using namespace swift;
Expand Down Expand Up @@ -7570,6 +7573,15 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
case TypeKind::Class: {
auto nominal1 = cast<NominalType>(desugar1);
auto nominal2 = cast<NominalType>(desugar2);

if (isa<EnumType>(nominal1) && kind == ConstraintKind::Bind) {
// Check for C++ inline namespaces
if (isInlineNamespaceInside(nominal1, nominal2)) {
nominal2 = nominal1;
type2 = type1;
}
}

if (nominal1->getDecl() == nominal2->getDecl())
conversionsOrFixes.push_back(ConversionRestrictionKind::DeepEquality);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: rm -rf %t
// RUN: split-file %s %t
// RUN: %target-swift-frontend -typecheck -verify -verify-ignore-unknown -I %t/Inputs %t/test.swift -enable-experimental-cxx-interop
// RUN: %target-swift-frontend -c -verify -verify-ignore-unknown -I %t/Inputs %t/test.swift -enable-experimental-cxx-interop

//--- Inputs/module.modulemap
module namespaces {
Expand All @@ -22,5 +22,5 @@ import namespaces;

// Swift's typechecker currently doesn't allow calling a function from inline namespace when it's referenced through the parent namespace.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: remove this comment.

func test() {
Parent.functionInInlineChild() // expected-error {{type of expression is ambiguous without a type annotation}}
Parent.functionInInlineChild()
}