Skip to content

[AST] Ill-formed @objc protocols might have associated types. #19754

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
Oct 7, 2018
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
19 changes: 14 additions & 5 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3651,13 +3651,22 @@ ProtocolDecl::getInheritedProtocols() const {
llvm::TinyPtrVector<AssociatedTypeDecl *>
ProtocolDecl::getAssociatedTypeMembers() const {
llvm::TinyPtrVector<AssociatedTypeDecl *> result;
if (!isObjC()) {
for (auto member : getMembers()) {
if (auto ATD = dyn_cast<AssociatedTypeDecl>(member)) {
result.push_back(ATD);
}

// Clang-imported protocols never have associated types.
if (hasClangNode())
return result;

// Deserialized @objc protocols never have associated types.
if (!getParentSourceFile() && isObjC())
return result;

// Find the associated type declarations.
for (auto member : getMembers()) {
if (auto ATD = dyn_cast<AssociatedTypeDecl>(member)) {
result.push_back(ATD);
}
}

return result;
}

Expand Down
57 changes: 57 additions & 0 deletions validation-test/Runtime/sr8666.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// RUN: %target-run-simple-swift
// REQUIRES: executable_test

private protocol AnyChangeTracker {
var myVariable: Any? { get }
var isModified: Bool { get }
}

protocol ChangeTrackerType {
associatedtype Value
var originalValue: Value { get }
var value: Value { get set }
var isModified: Bool { get }
}

extension ChangeTrackerType where Value: Equatable {
var isModified: Bool {
return value != originalValue
}
}

struct ChangeTracker<T: Equatable>: ChangeTrackerType {
let originalValue: T
var value: T

init(value: T) {
originalValue = value
self.value = value
}
}

extension ChangeTracker: AnyChangeTracker where Value: OptionalType, Value.Wrapped: Equatable {
var myVariable: Any? {
return value
}
}

protocol OptionalType {
associatedtype Wrapped
var value: Wrapped? { get }
}

extension Optional: OptionalType {
var value: Wrapped? {
return self
}
}

let s: Any = ChangeTracker<String?>(value: "Foo")

guard let s = s as? AnyChangeTracker else {
fatalError("Does not comply to AnyChangeTracker")
}

let myVar = String(describing: s.myVariable ?? "nil")
assert(myVar == "Optional(\"Foo\")")

14 changes: 14 additions & 0 deletions validation-test/compiler_crashers_2_fixed/0176-sr-8094.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: not %target-swift-frontend -typecheck %s

@objc protocol Foo {
associatedtype Bar
var property: Generic<Bar> { get }
}

class Generic<Element> {
}

class FooImpl<T>: NSObject, Foo {
let property: Generic<T>
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

// REQUIRES: asserts
// RUN: not --crash %target-swift-frontend %s -emit-ir

// RUN: not %target-swift-frontend %s -emit-ir
class a:P@objc protocol P{{}func a:a{}{}typealias a