Skip to content

Sema: Type aliases referenced from @inlinable functions must be public or @usableFromInline #17257

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
5 changes: 5 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -3849,6 +3849,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
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);
}

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
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