Skip to content

[5.0] [ClangImporter] NS_ERROR_ENUMs can be redefined in separate modules #20572

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

Merged
merged 1 commit into from
Nov 14, 2018
Merged
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
32 changes: 21 additions & 11 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2025,7 +2025,7 @@ getClangTopLevelOwningModule(ClangNode Node,
}

static bool isVisibleFromModule(const ClangModuleUnit *ModuleFilter,
const ValueDecl *VD) {
ValueDecl *VD) {
assert(ModuleFilter);

auto ContainingUnit = VD->getDeclContext()->getModuleScopeContext();
Expand All @@ -2040,24 +2040,34 @@ static bool isVisibleFromModule(const ClangModuleUnit *ModuleFilter,

auto ClangNode = VD->getClangNode();
if (!ClangNode) {
// If we synthesized a ValueDecl, it won't have a Clang node. But so far
// all the situations where we synthesize top-level declarations are
// situations where we don't have to worry about C redeclarations.
// We should only consider the declaration visible from its owning module.
// If we synthesized a ValueDecl, it won't have a Clang node. Find the
// associated declaration that /does/ have a Clang node, and use that.
auto *SynthesizedTypeAttr =
VD->getAttrs().getAttribute<ClangImporterSynthesizedTypeAttr>();
assert(SynthesizedTypeAttr);

// When adding new ClangImporterSynthesizedTypeAttr::Kinds, make sure that
// the above statement still holds: "we don't want to allow these
// declarations to be treated as present in multiple modules".
switch (SynthesizedTypeAttr->getKind()) {
case ClangImporterSynthesizedTypeAttr::Kind::NSErrorWrapper:
case ClangImporterSynthesizedTypeAttr::Kind::NSErrorWrapperAnon:
case ClangImporterSynthesizedTypeAttr::Kind::NSErrorWrapperAnon: {
ASTContext &Ctx = ContainingUnit->getASTContext();
auto LookupFlags =
NominalTypeDecl::LookupDirectFlags::IgnoreNewExtensions;
auto WrapperStruct = cast<StructDecl>(VD);
TinyPtrVector<ValueDecl *> LookupResults =
WrapperStruct->lookupDirect(Ctx.Id_Code, LookupFlags);
assert(!LookupResults.empty() && "imported error enum without Code");

auto CodeEnumIter = llvm::find_if(LookupResults,
[&](ValueDecl *Member) -> bool {
return Member->getDeclContext() == WrapperStruct;
});
assert(CodeEnumIter != LookupResults.end() &&
"could not find Code enum in wrapper struct");
assert((*CodeEnumIter)->hasClangNode());
ClangNode = (*CodeEnumIter)->getClangNode();
break;
}

return false;
}
}

// Macros can be "redeclared" by putting an equivalent definition in two
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
@import Foundation;
#ifndef NO_IMPORT_BASE_FROM_REDECLARED
@import Base;
#endif

extern NSString * const SomeErrorDomain;
// typedef NS_ERROR_ENUM(SomeErrorDomain, SomeErrorCode);
typedef enum SomeErrorCode : long SomeErrorCode;
enum __attribute__((ns_error_domain(SomeErrorDomain))) SomeErrorCode : long;
enum __attribute__((ns_error_domain(SomeErrorDomain))) SomeErrorCode : long
#ifdef NO_IMPORT_BASE_FROM_REDECLARED
{
SomeErrorX,
SomeErrorY
}
#endif
;
6 changes: 6 additions & 0 deletions test/ClangImporter/enum-error-redeclared.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck %s -verify -enable-objc-interop -I %S/Inputs/custom-modules/RedeclaredErrorEnum
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck %s -verify -enable-objc-interop -I %S/Inputs/custom-modules/RedeclaredErrorEnum -DIMPORT_BASE
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck %s -verify -enable-objc-interop -I %S/Inputs/custom-modules/RedeclaredErrorEnum -DIMPORT_BASE -Xcc -DNO_IMPORT_BASE_FROM_REDECLARED

#if IMPORT_BASE
import Base
#endif

import Redeclared

Expand Down