Skip to content

AST: Weak-link declarations in extensions of weak-linked types #29184

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 3 commits into from
Jan 14, 2020
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
10 changes: 6 additions & 4 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -4446,21 +4446,23 @@ NOTE(availability_protocol_requirement_here, none,
"protocol requirement here", ())

WARNING(public_decl_needs_availability, none,
"public declarations should have an availability attribute with -require-explicit-availability", ())
"public declarations should have an availability attribute when building "
"with -require-explicit-availability", ())

// This doesn't display as an availability diagnostic, but it's
// implemented there and fires when these subscripts are marked
// unavailable, so it seems appropriate to put it here.
ERROR(availabilty_string_subscript_migration, none,
"subscripts returning String were obsoleted in Swift 4; explicitly construct a String from subscripted result", ())
"subscripts returning String were obsoleted in Swift 4; explicitly "
"construct a String from subscripted result", ())

//------------------------------------------------------------------------------
// MARK: @discardableResult
//------------------------------------------------------------------------------

WARNING(discardable_result_on_void_never_function, none,
"@discardableResult declared on a function returning %select{Never|Void}0 is unnecessary",
(bool))
"@discardableResult declared on a function returning %select{Never|Void}0 "
"is unnecessary", (bool))

//------------------------------------------------------------------------------
// MARK: Resilience diagnostics
Expand Down
8 changes: 8 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,10 @@ AvailabilityContext Decl::getAvailabilityForLinkage() const {
if (auto *accessor = dyn_cast<AccessorDecl>(this))
return accessor->getStorage()->getAvailabilityForLinkage();

if (auto *ext = dyn_cast<ExtensionDecl>(this))
if (auto *nominal = ext->getExtendedNominal())
return nominal->getAvailabilityForLinkage();

auto *dc = getDeclContext();
if (auto *ext = dyn_cast<ExtensionDecl>(dc))
return ext->getAvailabilityForLinkage();
Expand All @@ -806,6 +810,10 @@ bool Decl::isAlwaysWeakImported() const {
if (auto *accessor = dyn_cast<AccessorDecl>(this))
return accessor->getStorage()->isAlwaysWeakImported();

if (auto *ext = dyn_cast<ExtensionDecl>(this))
if (auto *nominal = ext->getExtendedNominal())
return nominal->isAlwaysWeakImported();

auto *dc = getDeclContext();
if (auto *ext = dyn_cast<ExtensionDecl>(dc))
return ext->isAlwaysWeakImported();
Expand Down
2 changes: 2 additions & 0 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ static void addCommonFrontendArgs(const ToolChain &TC, const OutputInfo &OI,
inputArgs.AddLastArg(arguments, options::OPT_typo_correction_limit);
inputArgs.AddLastArg(arguments, options::OPT_enable_app_extension);
inputArgs.AddLastArg(arguments, options::OPT_enable_library_evolution);
inputArgs.AddLastArg(arguments, options::OPT_require_explicit_availability);
inputArgs.AddLastArg(arguments, options::OPT_require_explicit_availability_target);
inputArgs.AddLastArg(arguments, options::OPT_enable_testing);
inputArgs.AddLastArg(arguments, options::OPT_enable_private_imports);
inputArgs.AddLastArg(arguments, options::OPT_enable_cxx_interop);
Expand Down
8 changes: 8 additions & 0 deletions test/IRGen/Inputs/weak_import_extension_helper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@available(macOS 10.60, *)
public struct Foo {
public init() {}
}

extension Foo {
public func extensionMethod() {}
}
22 changes: 22 additions & 0 deletions test/IRGen/weak_import_extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %empty-directory(%t)
//
// RUN: %target-swift-frontend -enable-library-evolution -emit-module -target x86_64-apple-macosx10.60 -emit-module-path %t/weak_import_extension_helper.swiftmodule -parse-as-library %S/Inputs/weak_import_extension_helper.swift
// RUN: %target-swift-frontend -primary-file %s -I %t -emit-ir -target x86_64-apple-macosx10.50 | %FileCheck %s --check-prefix=CHECK-OLD
// RUN: %target-swift-frontend -primary-file %s -I %t -emit-ir -target x86_64-apple-macosx10.60 | %FileCheck %s --check-prefix=CHECK-NEW
//
// REQUIRES: OS=macosx

import weak_import_extension_helper

@available(macOS 10.60, *)
public func callsExtensionMethod() {
Foo().extensionMethod()
}

// CHECK-OLD: declare extern_weak swiftcc %swift.metadata_response @"$s28weak_import_extension_helper3FooVMa"
// CHECK-OLD: declare extern_weak swiftcc void @"$s28weak_import_extension_helper3FooVACycfC"
// CHECK-OLD: declare extern_weak swiftcc void @"$s28weak_import_extension_helper3FooV0C6MethodyyF"

// CHECK-NEW: declare swiftcc %swift.metadata_response @"$s28weak_import_extension_helper3FooVMa"
// CHECK-NEW: declare swiftcc void @"$s28weak_import_extension_helper3FooVACycfC"
// CHECK-NEW: declare swiftcc void @"$s28weak_import_extension_helper3FooV0C6MethodyyF"
26 changes: 13 additions & 13 deletions test/attr/require_explicit_availability.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// RUN: %swift -typecheck -parse-stdlib -target x86_64-apple-macosx10.10 -verify -require-explicit-availability -require-explicit-availability-target "macOS 10.10" %s
// RUN: %swift -typecheck -parse-stdlib -target x86_64-apple-macosx10.10 -warnings-as-errors %s
// RUN: %swiftc_driver -typecheck -parse-stdlib -target x86_64-apple-macosx10.10 -Xfrontend -verify -require-explicit-availability -require-explicit-availability-target "macOS 10.10" %s
// RUN: %swiftc_driver -typecheck -parse-stdlib -target x86_64-apple-macosx10.10 -warnings-as-errors %s

public struct S { // expected-warning {{public declarations should have an availability attribute with -require-explicit-availability}}
public struct S { // expected-warning {{public declarations should have an availability attribute when building with -require-explicit-availability}}
public func method() { }
}

public func foo() { bar() } // expected-warning {{public declarations should have an availability attribute with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}
public func foo() { bar() } // expected-warning {{public declarations should have an availability attribute when building with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}

@usableFromInline
func bar() { } // expected-warning {{public declarations should have an availability attribute with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}
func bar() { } // expected-warning {{public declarations should have an availability attribute when building with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}

@available(macOS 10.1, *)
public func ok() { }
Expand All @@ -17,10 +17,10 @@ public func ok() { }
public func unavailableOk() { }

@available(macOS, deprecated: 10.10)
public func missingIntro() { } // expected-warning {{public declarations should have an availability attribute with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}
public func missingIntro() { } // expected-warning {{public declarations should have an availability attribute when building with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}

@available(iOS 9.0, *)
public func missingTargetPlatform() { } // expected-warning {{public declarations should have an availability attribute with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}
public func missingTargetPlatform() { } // expected-warning {{public declarations should have an availability attribute when building with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}

func privateFunc() { }

Expand All @@ -35,19 +35,19 @@ struct SOk {
precedencegroup MediumPrecedence {}
infix operator + : MediumPrecedence

public func +(lhs: S, rhs: S) -> S { } // expected-warning {{public declarations should have an availability attribute with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}
public func +(lhs: S, rhs: S) -> S { } // expected-warning {{public declarations should have an availability attribute when building with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}

public enum E { } // expected-warning {{public declarations should have an availability attribute with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}
public enum E { } // expected-warning {{public declarations should have an availability attribute when building with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}

public class C { } // expected-warning {{public declarations should have an availability attribute with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}
public class C { } // expected-warning {{public declarations should have an availability attribute when building with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}

public protocol P { } // expected-warning {{public declarations should have an availability attribute with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}
public protocol P { } // expected-warning {{public declarations should have an availability attribute when building with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}

extension S { // expected-warning {{public declarations should have an availability attribute with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}
extension S { // expected-warning {{public declarations should have an availability attribute when building with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}
func ok() { }
}

open class OpenClass { } // expected-warning {{public declarations should have an availability attribute with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}
open class OpenClass { } // expected-warning {{public declarations should have an availability attribute when building with -require-explicit-availability}} {{1-1=@available(macOS 10.10, *)\n}}

private class PrivateClass { }

Expand Down