Skip to content

Add -emit-public-type-metadata-accessors to work around metadata linkage bug. #16616

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
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
3 changes: 3 additions & 0 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ namespace swift {
/// Enables key path resilience.
bool EnableKeyPathResilience = false;

/// Enables public emission of private metadata accessors.
bool EmitPublicTypeMetadataAccessors = false;

/// If set to true, the diagnosis engine can assume the emitted diagnostics
/// will be used in editor. This usually leads to more aggressive fixit.
bool DiagnosticsEditorMode = false;
Expand Down
5 changes: 5 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,11 @@ def warn_swift3_objc_inference : Flag<["-"], "warn-swift3-objc-inference">,
Alias<warn_swift3_objc_inference_complete>,
Flags<[FrontendOption, DoesNotAffectIncrementalBuild, HelpHidden]>;

def emit_public_type_metadata_accessors :
Flag<["-"], "emit-public-type-metadata-accessors">,
Flags<[FrontendOption]>,
HelpText<"Emit all type metadata accessors as public">;

def Rpass_EQ : Joined<["-"], "Rpass=">,
Flags<[FrontendOption]>,
HelpText<"Report performed transformations by optimization passes whose "
Expand Down
2 changes: 2 additions & 0 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ static void addCommonFrontendArgs(const ToolChain &TC, const OutputInfo &OI,
options::OPT_warn_swift3_objc_inference_minimal,
options::OPT_warn_swift3_objc_inference_complete);
inputArgs.AddLastArg(arguments, options::OPT_typo_correction_limit);
inputArgs.AddLastArg(arguments,
options::OPT_emit_public_type_metadata_accessors);
inputArgs.AddLastArg(arguments, options::OPT_enable_app_extension);
inputArgs.AddLastArg(arguments, options::OPT_enable_testing);
inputArgs.AddLastArg(arguments, options::OPT_g_Group);
Expand Down
3 changes: 3 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,9 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
}
}

Opts.EmitPublicTypeMetadataAccessors =
Args.hasArg(OPT_emit_public_type_metadata_accessors);

Opts.EnableNSKeyedArchiverDiagnostics =
Args.hasFlag(OPT_enable_nskeyedarchiver_diagnostics,
OPT_disable_nskeyedarchiver_diagnostics,
Expand Down
15 changes: 15 additions & 0 deletions lib/IRGen/MetadataRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,17 @@ bool irgen::isTypeMetadataAccessTrivial(IRGenModule &IGM, CanType type) {
return false;
}

/// Determine whether we should promote the type metadata access function
/// for the given nominal type to "public".
static bool promoteMetadataAccessFunctionToPublic(
const NominalTypeDecl *nominal) {
ASTContext &ctx = nominal->getASTContext();

// When -emit-public-type-metadata-accessors is provided, promote all
// of the metadata access functions to public.
return ctx.LangOpts.EmitPublicTypeMetadataAccessors;
}

/// Return the standard access strategy for getting a non-dependent
/// type metadata object.
MetadataAccessStrategy irgen::getTypeMetadataAccessStrategy(CanType type) {
Expand Down Expand Up @@ -621,8 +632,12 @@ MetadataAccessStrategy irgen::getTypeMetadataAccessStrategy(CanType type) {
case FormalLinkage::PublicUnique:
return MetadataAccessStrategy::PublicUniqueAccessor;
case FormalLinkage::HiddenUnique:
if (promoteMetadataAccessFunctionToPublic(nominal))
return MetadataAccessStrategy::PublicUniqueAccessor;
return MetadataAccessStrategy::HiddenUniqueAccessor;
case FormalLinkage::Private:
if (promoteMetadataAccessFunctionToPublic(nominal))
return MetadataAccessStrategy::PublicUniqueAccessor;
return MetadataAccessStrategy::PrivateAccessor;

case FormalLinkage::PublicNonUnique:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
func foo() -> Any {
return Wrapper()
}
17 changes: 17 additions & 0 deletions test/IRGen/emit_public_type_metadata_accessors.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %swift -module-name test -target x86_64-apple-macosx10.9 -emit-ir -parse-stdlib -emit-public-type-metadata-accessors -primary-file %s %S/Inputs/emit_public_type_metadata_accessors_other.swift | %FileCheck --check-prefix=CHECK-PUBLIC %s

// RUN: %swift -module-name test -target x86_64-apple-macosx10.9 -emit-ir -parse-stdlib -primary-file %s %S/Inputs/emit_public_type_metadata_accessors_other.swift | %FileCheck --check-prefix=CHECK-NONPUBLIC %s

private class C { }

// CHECK-PUBLIC: define swiftcc %swift.metadata_response @"$S4test3Foo33_DEC9477CC6E8E6E7A9CE422B1DBE7EA4LLVMa"
// CHECK-NONPUBLIC: define internal swiftcc %swift.metadata_response @"$S4test3Foo33_DEC9477CC6E8E6E7A9CE422B1DBE7EA4LLVMa"
private struct Foo {
private let c: C = C()
}

public struct Wrapper {
private let foo: Foo = Foo()
}