Skip to content

Commit a2e03d5

Browse files
committed
Stage implementation of SE-0033 behind a flag, -enable-swift-newtype.
We're going to quarantine this feature behind a frontend flag for a bit.
1 parent 812557c commit a2e03d5

File tree

10 files changed

+32
-10
lines changed

10 files changed

+32
-10
lines changed

include/swift/ClangImporter/ClangImporterOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class ClangImporterOptions {
7070

7171
/// If true ignore the swift bridged attribute.
7272
bool DisableSwiftBridgeAttr = false;
73+
74+
/// Whether we should honor the swift_newtype attribute.
75+
bool HonorSwiftNewtypeAttr = false;
7376
};
7477

7578
} // end namespace swift

include/swift/Option/FrontendOptions.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ def enable_infer_import_as_member :
232232
Flag<["-"], "enable-infer-import-as-member">,
233233
HelpText<"Infer when a global could be imported as a member">;
234234

235+
def enable_swift_newtype :
236+
Flag<["-"], "enable-swift-newtype">,
237+
HelpText<"Enable the swift_newtype attribute">;
238+
235239
def enable_strip_ns_prefix :
236240
Flag<["-"], "enable-strip-ns-prefix">,
237241
HelpText<"Strip 'NS' prefix from Foundation entities">;

lib/ClangImporter/ClangImporter.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,7 @@ ClangImporter::Implementation::Implementation(ASTContext &ctx,
12091209
ImportForwardDeclarations(opts.ImportForwardDeclarations),
12101210
InferImportAsMember(opts.InferImportAsMember),
12111211
DisableSwiftBridgeAttr(opts.DisableSwiftBridgeAttr),
1212+
HonorSwiftNewtypeAttr(opts.HonorSwiftNewtypeAttr),
12121213
BridgingHeaderLookupTable(nullptr)
12131214
{
12141215
// Add filters to determine if a Clang availability attribute
@@ -2032,7 +2033,10 @@ static bool moduleIsInferImportAsMember(const clang::NamedDecl *decl,
20322033

20332034
// If this decl is associated with a swift_newtype typedef, return it, otherwise
20342035
// null
2035-
static clang::TypedefNameDecl *findSwiftNewtype(const clang::Decl *decl) {
2036+
static clang::TypedefNameDecl *findSwiftNewtype(const clang::Decl *decl,
2037+
bool honorSwiftNewtypeAttr) {
2038+
if (!honorSwiftNewtypeAttr) return nullptr;
2039+
20362040
if (auto varDecl = dyn_cast<clang::VarDecl>(decl))
20372041
if (auto typedefTy = varDecl->getType()->getAs<clang::TypedefType>())
20382042
if (typedefTy->getDecl()->hasAttr<clang::SwiftNewtypeAttr>())
@@ -2076,7 +2080,7 @@ auto ClangImporter::Implementation::importFullName(
20762080
break;
20772081
}
20782082
// Import onto a swift_newtype if present
2079-
} else if (auto newtypeDecl = findSwiftNewtype(D)) {
2083+
} else if (auto newtypeDecl = findSwiftNewtype(D, HonorSwiftNewtypeAttr)) {
20802084
result.EffectiveContext = newtypeDecl;
20812085
// Everything else goes into its redeclaration context.
20822086
} else {
@@ -2551,7 +2555,7 @@ auto ClangImporter::Implementation::importFullName(
25512555

25522556
// swift_newtype-ed declarations may have common words with the type name
25532557
// stripped.
2554-
if (auto newtypeDecl = findSwiftNewtype(D)) {
2558+
if (auto newtypeDecl = findSwiftNewtype(D, HonorSwiftNewtypeAttr)) {
25552559
// Skip a leading 'k' in a 'kConstant' pattern
25562560
if (baseName.size() >= 2 && baseName[0] == 'k' &&
25572561
clang::isUppercase(baseName[1]))

lib/ClangImporter/ImportDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ namespace {
12761276
return nullptr;
12771277

12781278
// Check for swift_newtype
1279-
if (!SwiftType) {
1279+
if (!SwiftType && Impl.HonorSwiftNewtypeAttr) {
12801280
if (auto newtypeAttr =
12811281
Decl->template getAttr<clang::SwiftNewtypeAttr>()) {
12821282
switch (newtypeAttr->getNewtypeKind()) {

lib/ClangImporter/ImportType.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,8 @@ namespace {
548548
Type mappedType = decl->getDeclaredType();
549549
ImportHint hint = ImportHint::None;
550550

551-
if (type->getDecl()->hasAttr<clang::SwiftNewtypeAttr>()) {
551+
if (Impl.HonorSwiftNewtypeAttr &&
552+
type->getDecl()->hasAttr<clang::SwiftNewtypeAttr>()) {
552553
hint = ImportHint::SwiftNewtype;
553554

554555
// For certain special typedefs, we don't want to use the imported type.

lib/ClangImporter/ImporterImpl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
263263
const bool ImportForwardDeclarations;
264264
const bool InferImportAsMember;
265265
const bool DisableSwiftBridgeAttr;
266+
const bool HonorSwiftNewtypeAttr;
266267

267268
constexpr static const char * const moduleImportBufferName =
268269
"<swift-imported-modules>";

lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,7 @@ static bool ParseClangImporterArgs(ClangImporterOptions &Opts,
894894
}
895895

896896
Opts.InferImportAsMember |= Args.hasArg(OPT_enable_infer_import_as_member);
897+
Opts.HonorSwiftNewtypeAttr |= Args.hasArg(OPT_enable_swift_newtype);
897898
Opts.DumpClangDiagnostics |= Args.hasArg(OPT_dump_clang_diagnostics);
898899

899900
if (Args.hasArg(OPT_embed_bitcode))

test/IDE/newtype.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-ide-test(mock-sdk: %clang-importer-sdk) -I %t -I %S/Inputs/custom-modules -print-module -source-filename %s -module-to-print=Newtype > %t.printed.A.txt
1+
// RUN: %target-swift-ide-test(mock-sdk: %clang-importer-sdk) -I %t -I %S/Inputs/custom-modules -enable-swift-newtype -print-module -source-filename %s -module-to-print=Newtype > %t.printed.A.txt
22
// RUN: FileCheck %s -check-prefix=PRINT -strict-whitespace < %t.printed.A.txt
33
// REQUIRES: objc_interop
44

@@ -32,7 +32,7 @@
3232
// PRINT-NEXT: static let thirdEntry: ClosedEnum
3333
// PRINT-NEXT: }
3434

35-
// RUN: %target-parse-verify-swift -I %S/Inputs/custom-modules
35+
// RUN: %target-parse-verify-swift -I %S/Inputs/custom-modules -enable-swift-newtype
3636
import Newtype
3737

3838
func tests() {
@@ -50,4 +50,4 @@ func tests() {
5050
let _ = ErrorDomain(rawValue: thirdEnum.rawValue)
5151
let _ = ClosedEnum(rawValue: errOne.rawValue)
5252

53-
}
53+
}

test/IRGen/newtype.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN// RUN: %target-swift-frontend -I %S/../IDE/Inputs/custom-modules %s -emit-ir | FileCheck %s
1+
// RUN// RUN: %target-swift-frontend -I %S/../IDE/Inputs/custom-modules %s -emit-ir -enable-swift-newtype | FileCheck %s
22
import Newtype
33

44
// REQUIRES: objc_interop

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,11 @@ InferImportAsMember("enable-infer-import-as-member",
300300
llvm::cl::desc("Infer when a global could be imported as a member"),
301301
llvm::cl::init(false));
302302

303+
static llvm::cl::opt<bool>
304+
HonorSwiftNewtypeAttr("enable-swift-newtype",
305+
llvm::cl::desc("Enable swift_newtype import"),
306+
llvm::cl::init(false));
307+
303308
static llvm::cl::opt<bool>
304309
StripNSPrefix("enable-strip-ns-prefix",
305310
llvm::cl::desc("Strip the NS prefix from Foundation et al"),
@@ -2680,11 +2685,14 @@ int main(int argc, char *argv[]) {
26802685
options::CodeCompleteInitsInPostfixExpr;
26812686
InitInvok.getLangOptions().Swift3Migration |= options::Swift3Migration;
26822687
InitInvok.getLangOptions().InferImportAsMember |=
2683-
options::InferImportAsMember; InitInvok.getLangOptions().StripNSPrefix |= options::StripNSPrefix;
2688+
options::InferImportAsMember;
2689+
InitInvok.getLangOptions().StripNSPrefix |= options::StripNSPrefix;
26842690
InitInvok.getClangImporterOptions().ImportForwardDeclarations |=
26852691
options::ObjCForwardDeclarations;
26862692
InitInvok.getClangImporterOptions().InferImportAsMember |=
26872693
options::InferImportAsMember;
2694+
InitInvok.getClangImporterOptions().HonorSwiftNewtypeAttr |=
2695+
options::HonorSwiftNewtypeAttr;
26882696
if (!options::ResourceDir.empty()) {
26892697
InitInvok.setRuntimeResourcePath(options::ResourceDir);
26902698
}

0 commit comments

Comments
 (0)