Skip to content

Sema: Add simulation of buggy Swift 3 typealias accessibility checking #6234

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
11 changes: 10 additions & 1 deletion lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,7 @@ static void recordSelfContextType(AbstractFunctionDecl *func) {
namespace {

class AccessScopeChecker {
ASTContext &Context;
const SourceFile *File;
TypeChecker::TypeAccessScopeCacheMap &Cache;

Expand All @@ -1147,7 +1148,9 @@ class AccessScopeChecker {

AccessScopeChecker(const DeclContext *useDC,
decltype(TypeChecker::TypeAccessScopeCache) &caches)
: File(useDC->getParentSourceFile()), Cache(caches[File]),
: Context(useDC->getASTContext()),
File(useDC->getParentSourceFile()),
Cache(caches[File]),
Scope(AccessScope::getPublic()) {}

bool visitDecl(ValueDecl *VD) {
Expand All @@ -1161,6 +1164,12 @@ class AccessScopeChecker {
return true;
}

// Simulation for Swift 3 bug.
if (isa<TypeAliasDecl>(VD) &&
VD->getInterfaceType()->hasTypeParameter() &&
Context.isSwiftVersion3())
return true;

auto cached = Cache.find(VD);
if (cached != Cache.end()) {
Scope = Scope->intersectWith(cached->second);
Expand Down
42 changes: 42 additions & 0 deletions test/Compatibility/accessibility_typealias.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// RUN: %target-typecheck-verify-swift -swift-version 3

public protocol P {
associatedtype Element

func f() -> Element
}

struct S<T> : P {
func f() -> T { while true {} }
}

public struct G<T> {
typealias A = S<T>

public func foo<U : P>(u: U) where U.Element == A.Element {}
}

public final class ReplayableGenerator<S: Sequence> : IteratorProtocol {
typealias Sequence = S
public typealias Element = Sequence.Iterator.Element

public func next() -> Element? {
return nil
}
}

struct Generic<T> {
fileprivate typealias Dependent = T
}

var x: Generic<Int>.Dependent = 3

func internalFuncWithFileprivateAlias() -> Generic<Int>.Dependent {
return 3
}

private func privateFuncWithFileprivateAlias() -> Generic<Int>.Dependent {
return 3
}

var y = privateFuncWithFileprivateAlias()
18 changes: 0 additions & 18 deletions test/Sema/accessibility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -637,21 +637,3 @@ internal struct AssocTypeOuterProblem2 {
fileprivate typealias Assoc = Int // expected-error {{type alias 'Assoc' must be as accessible as its enclosing type because it matches a requirement in protocol 'AssocTypeProto'}} {{5-16=internal}}
}
}

// This code was accepted in Swift 3
public protocol P {
associatedtype Element

func f() -> Element
}

struct S<T> : P {
func f() -> T { while true {} }
}

public struct G<T> {
typealias A = S<T> // expected-note {{type declared here}}

public func foo<U : P>(u: U) where U.Element == A.Element {}
// expected-error@-1 {{instance method cannot be declared public because its generic requirement uses an internal type}}
}
21 changes: 0 additions & 21 deletions test/Sema/accessibility_private.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,24 +210,3 @@ fileprivate struct SR2579 {
private var outerProperty = Inner().innerProperty // expected-warning {{property should not be declared in this context because its type 'SR2579.Inner.InnerPrivateType' uses a private type}}
var outerProperty2 = Inner().innerProperty // expected-warning {{property should be declared private because its type 'SR2579.Inner.InnerPrivateType' uses a private type}}
}

// FIXME: Dependent member lookup of typealiases is not subject
// to accessibility checking.
struct Generic<T> {
fileprivate typealias Dependent = T
}

var x: Generic<Int>.Dependent = 3
// expected-error@-1 {{variable must be declared private or fileprivate because its type uses a fileprivate type}}

func internalFuncWithFileprivateAlias() -> Generic<Int>.Dependent {
// expected-error@-1 {{function must be declared private or fileprivate because its result uses a fileprivate type}}
return 3
}

private func privateFuncWithFileprivateAlias() -> Generic<Int>.Dependent {
return 3
}

// FIXME: No error here
var y = privateFuncWithFileprivateAlias()
46 changes: 46 additions & 0 deletions test/Sema/accessibility_typealias.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// RUN: %target-typecheck-verify-swift -swift-version 4

public protocol P {
associatedtype Element

func f() -> Element
}

struct S<T> : P {
func f() -> T { while true {} }
}

public struct G<T> {
typealias A = S<T> // expected-note {{type declared here}}

public func foo<U : P>(u: U) where U.Element == A.Element {}
// expected-error@-1 {{instance method cannot be declared public because its generic requirement uses an internal type}}
}

public final class ReplayableGenerator<S: Sequence> : IteratorProtocol {
typealias Sequence = S // expected-note {{type declared here}}
public typealias Element = Sequence.Iterator.Element // expected-error {{type alias cannot be declared public because its underlying type uses an internal type}}

public func next() -> Element? {
return nil
}
}

// FIXME: Dependent member lookup of typealiases is not subject
// to accessibility checking.
struct Generic<T> {
fileprivate typealias Dependent = T
}

var x: Generic<Int>.Dependent = 3 // expected-error {{variable must be declared private or fileprivate because its type uses a fileprivate type}}

func internalFuncWithFileprivateAlias() -> Generic<Int>.Dependent { // expected-error {{function must be declared private or fileprivate because its result uses a fileprivate type}}
return 3
}

private func privateFuncWithFileprivateAlias() -> Generic<Int>.Dependent {
return 3
}

// FIXME: No error here
var y = privateFuncWithFileprivateAlias()