Skip to content

AST: Make sure that an opaque type descriptor is weak-linked if its naming declaration is [5.5] #38483

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
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
6 changes: 6 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,9 @@ AvailabilityContext Decl::getAvailabilityForLinkage() const {
if (auto *accessor = dyn_cast<AccessorDecl>(this))
return accessor->getStorage()->getAvailabilityForLinkage();

if (auto *opaqueTypeDecl = dyn_cast<OpaqueTypeDecl>(this))
return opaqueTypeDecl->getNamingDecl()->getAvailabilityForLinkage();

if (auto *ext = dyn_cast<ExtensionDecl>(this))
if (auto *nominal = ext->getExtendedNominal())
return nominal->getAvailabilityForLinkage();
Expand All @@ -914,6 +917,9 @@ bool Decl::isAlwaysWeakImported() const {
if (auto *accessor = dyn_cast<AccessorDecl>(this))
return accessor->getStorage()->isAlwaysWeakImported();

if (auto *opaqueTypeDecl = dyn_cast<OpaqueTypeDecl>(this))
return opaqueTypeDecl->getNamingDecl()->isAlwaysWeakImported();

if (auto *ext = dyn_cast<ExtensionDecl>(this))
if (auto *nominal = ext->getExtendedNominal())
return nominal->isAlwaysWeakImported();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public protocol P {
func blah()
}

public struct S : P {
public func blah() {}
}

extension P {
@available(macOS 100, *)
@_weakLinked public func someAPI() -> some P { return S() }
}
11 changes: 11 additions & 0 deletions test/IRGen/Inputs/weak_import_opaque_result_type_helper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public protocol P {
func blah()
}

public struct S : P {
public func blah() {}
}

extension P {
@_weakLinked public func someAPI() -> some P { return S() }
}
16 changes: 16 additions & 0 deletions test/IRGen/weak_availability_opaque_result_type.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/weak_availability_opaque_result_type_helper.swiftmodule -parse-as-library %S/Inputs/weak_availability_opaque_result_type_helper.swift -enable-library-evolution
// RUN: %target-swift-frontend -disable-type-layout -primary-file %s -I %t -emit-ir | %FileCheck %s

// REQUIRES: OS=macosx

import weak_availability_opaque_result_type_helper

func useWeakImportedOpaqueResultType<T : P>(_ p: T) {
if #available(macOS 100, *) {
p.someAPI().blah()
}
}

// CHECK-LABEL: @"$s43weak_availability_opaque_result_type_helper1PPAAE7someAPIQryFQOMQ" = extern_weak global %swift.type_descriptor
// CHECK-LABEL: declare extern_weak {{.+}} void @"$s43weak_availability_opaque_result_type_helper1PPAAE7someAPIQryF"
14 changes: 14 additions & 0 deletions test/IRGen/weak_import_opaque_result_type.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/weak_import_opaque_result_type_helper.swiftmodule -parse-as-library %S/Inputs/weak_import_opaque_result_type_helper.swift -enable-library-evolution -disable-availability-checking
// RUN: %target-swift-frontend -disable-type-layout -disable-availability-checking -primary-file %s -I %t -emit-ir | %FileCheck %s

// UNSUPPORTED: OS=windows-msvc

import weak_import_opaque_result_type_helper

func useWeakImportedOpaqueResultType<T : P>(_ p: T) {
p.someAPI().blah()
}

// CHECK-LABEL: @"$s37weak_import_opaque_result_type_helper1PPAAE7someAPIQryFQOMQ" = extern_weak global %swift.type_descriptor
// CHECK-LABEL: declare extern_weak {{.+}} void @"$s37weak_import_opaque_result_type_helper1PPAAE7someAPIQryF"