Skip to content
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
5 changes: 5 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -3835,6 +3835,11 @@ ERROR(resilience_decl_unavailable,
"cannot be referenced from " FRAGILE_FUNC_KIND "3",
(DescriptiveDeclKind, DeclName, AccessLevel, unsigned))

WARNING(resilience_decl_unavailable_warn,
none, "%0 %1 is %select{private|fileprivate|internal|%error|%error}2 and "
"should not be referenced from " FRAGILE_FUNC_KIND "3",
(DescriptiveDeclKind, DeclName, AccessLevel, unsigned))

#undef FRAGILE_FUNC_KIND

NOTE(resilience_decl_declared_here_public,
Expand Down
34 changes: 18 additions & 16 deletions lib/Sema/DerivedConformanceRawRepresentable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,22 @@ static void deriveBodyRawRepresentable_raw(AbstractFunctionDecl *toRawDecl) {
toRawDecl->setBody(body);
}

static void maybeMarkAsInlinable(DerivedConformance &derived,
AbstractFunctionDecl *afd) {
ASTContext &C = derived.TC.Context;
auto parentDC = derived.getConformanceContext();
if (parentDC->getParentModule()->getResilienceStrategy() !=
ResilienceStrategy::Resilient) {
AccessScope access =
afd->getFormalAccessScope(nullptr,
/*treatUsableFromInlineAsPublic*/true);
if (auto *attr = afd->getAttrs().getAttribute<UsableFromInlineAttr>())
attr->setInvalid();
if (access.isPublic())
afd->getAttrs().add(new (C) InlinableAttr(/*implicit*/false));
}
}

static VarDecl *deriveRawRepresentable_raw(DerivedConformance &derived) {
ASTContext &C = derived.TC.Context;

Expand All @@ -143,14 +159,7 @@ static VarDecl *deriveRawRepresentable_raw(DerivedConformance &derived) {

// If the containing module is not resilient, make sure clients can get at
// the raw value without function call overhead.
if (parentDC->getParentModule()->getResilienceStrategy() !=
ResilienceStrategy::Resilient) {
AccessScope access =
enumDecl->getFormalAccessScope(nullptr,
/*treatUsableFromInlineAsPublic*/true);
if (access.isPublic())
getterDecl->getAttrs().add(new (C) InlinableAttr(/*implicit*/false));
}
maybeMarkAsInlinable(derived, getterDecl);

derived.addMembersToConformanceContext({getterDecl, propDecl, pbDecl});

Expand Down Expand Up @@ -350,14 +359,7 @@ deriveRawRepresentable_init(DerivedConformance &derived) {

// If the containing module is not resilient, make sure clients can construct
// an instance without function call overhead.
if (parentDC->getParentModule()->getResilienceStrategy() !=
ResilienceStrategy::Resilient) {
AccessScope access =
enumDecl->getFormalAccessScope(nullptr,
/*treatUsableFromInlineAsPublic*/true);
if (access.isPublic())
initDecl->getAttrs().add(new (C) InlinableAttr(/*implicit*/false));
}
maybeMarkAsInlinable(derived, initDecl);

C.addSynthesizedDecl(initDecl);

Expand Down
24 changes: 19 additions & 5 deletions lib/Sema/ResilienceDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ void TypeChecker::diagnoseInlinableLocalType(const NominalTypeDecl *NTD) {
}
}

/// A uniquely-typed boolean to reduce the chances of accidentally inverting
/// a check.
enum class DowngradeToWarning: bool {
No,
Yes
};

bool TypeChecker::diagnoseInlinableDeclRef(SourceLoc loc,
const ValueDecl *D,
const DeclContext *DC,
Expand Down Expand Up @@ -119,11 +126,18 @@ bool TypeChecker::diagnoseInlinableDeclRef(SourceLoc loc,
if (D->isDynamic())
return false;

// FIXME: Figure out what to do with typealiases
if (isa<TypeAliasDecl>(D))
return false;
DowngradeToWarning downgradeToWarning = DowngradeToWarning::No;

// Swift 4.2 did not perform any checks for type aliases.
if (!Context.isSwiftVersionAtLeast(5) &&
isa<TypeAliasDecl>(D))
downgradeToWarning = DowngradeToWarning::Yes;

auto diagID = diag::resilience_decl_unavailable;
if (downgradeToWarning == DowngradeToWarning::Yes)
diagID = diag::resilience_decl_unavailable_warn;

diagnose(loc, diag::resilience_decl_unavailable,
diagnose(loc, diagID,
D->getDescriptiveKind(), D->getFullName(),
D->getFormalAccessScope().accessLevelForDiagnostics(),
static_cast<unsigned>(Kind));
Expand All @@ -136,6 +150,6 @@ bool TypeChecker::diagnoseInlinableDeclRef(SourceLoc loc,
D->getDescriptiveKind(), D->getFullName());
}

return true;
return (downgradeToWarning == DowngradeToWarning::No);
}

4 changes: 1 addition & 3 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1977,9 +1977,7 @@ void AttributeChecker::visitUsableFromInlineAttr(UsableFromInlineAttr *attr) {

// On internal declarations, @inlinable implies @usableFromInline.
if (VD->getAttrs().hasAttribute<InlinableAttr>()) {
if (attr->isImplicit())
attr->setInvalid();
else
if (TC.Context.isSwiftVersionAtLeast(4,2))
diagnoseAndRemoveAttr(attr, diag::inlinable_implies_usable_from_inline);
return;
}
Expand Down
3 changes: 3 additions & 0 deletions test/Compatibility/attr_inlinable_old_spelling_4.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@

@_inlineable public func oldInlinableFunction() {}
@_versioned func oldVersionedFunction() {}

// No warning here
@_inlineable @_versioned func redundantAttribute() {}
3 changes: 3 additions & 0 deletions test/Compatibility/attr_inlinable_old_spelling_42.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@

@_versioned func oldVersionedFunction() {}
// expected-warning@-1 {{'@_versioned' has been renamed to '@usableFromInline'}}{{2-12=usableFromInline}}

@inlinable @usableFromInline func redundantAttribute() {}
// expected-warning@-1 {{'@inlinable' declaration is already '@usableFromInline'}}
23 changes: 23 additions & 0 deletions test/Compatibility/attr_inlinable_typealias.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %target-typecheck-verify-swift -swift-version 4

private typealias PrivateAlias = Int
// expected-note@-1 {{type alias 'PrivateAlias' is not '@usableFromInline' or public}}

internal typealias InternalAlias = Int
// expected-note@-1 {{type alias 'InternalAlias' is not '@usableFromInline' or public}}

@usableFromInline typealias UsableFromInlineAlias = Int

public typealias PublicAlias = Int

@inlinable public func f() {
_ = PrivateAlias.self
// expected-warning@-1 {{type alias 'PrivateAlias' is private and should not be referenced from an '@inlinable' function}}

_ = InternalAlias.self
// expected-warning@-1 {{type alias 'InternalAlias' is internal and should not be referenced from an '@inlinable' function}}

_ = UsableFromInlineAlias.self

_ = PublicAlias.self
}
11 changes: 4 additions & 7 deletions test/SIL/Serialization/Inputs/def_generic.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
@_fixed_layout
public class A<T> {
typealias Element = T
@usableFromInline
@inlinable
func convertFromArrayLiteral(_ elements: Element...) -> A {
@usableFromInline typealias Element = T

@inlinable func convertFromArrayLiteral(_ elements: Element...) -> A {
return A()
}

@usableFromInline
@inlinable
init() {}
@inlinable init() {}

@inlinable public subscript<U>(value: T) -> U? {
return nil
Expand Down
8 changes: 4 additions & 4 deletions test/attr/attr_inlinable.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %target-typecheck-verify-swift -swift-version 4
// RUN: %target-typecheck-verify-swift -swift-version 4 -enable-testing
// RUN: %target-typecheck-verify-swift -swift-version 4 -enable-resilience
// RUN: %target-typecheck-verify-swift -swift-version 4 -enable-resilience -enable-testing
// RUN: %target-typecheck-verify-swift -swift-version 4.2
// RUN: %target-typecheck-verify-swift -swift-version 4.2 -enable-testing
// RUN: %target-typecheck-verify-swift -swift-version 4.2 -enable-resilience
// RUN: %target-typecheck-verify-swift -swift-version 4.2 -enable-resilience -enable-testing
@inlinable struct TestInlinableStruct {}
// expected-error@-1 {{'@inlinable' attribute cannot be applied to this declaration}}

Expand Down
9 changes: 5 additions & 4 deletions test/attr/attr_inlinable_typealias.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
// RUN: %target-typecheck-verify-swift

// None of this is enforced for now, but make sure we don't crash or
// do anything stupid when a typealias is annotated with @usableFromInline.
// RUN: %target-typecheck-verify-swift -swift-version 5

private typealias PrivateAlias = Int
// expected-note@-1 {{type alias 'PrivateAlias' is not '@usableFromInline' or public}}

internal typealias InternalAlias = Int
// expected-note@-1 {{type alias 'InternalAlias' is not '@usableFromInline' or public}}

@usableFromInline typealias UsableFromInlineAlias = Int

public typealias PublicAlias = Int

@inlinable public func f() {
_ = PrivateAlias.self
// expected-error@-1 {{type alias 'PrivateAlias' is private and cannot be referenced from an '@inlinable' function}}

_ = InternalAlias.self
// expected-error@-1 {{type alias 'InternalAlias' is internal and cannot be referenced from an '@inlinable' function}}

_ = UsableFromInlineAlias.self

Expand Down