Skip to content

Commit

Permalink
Merge pull request #23613 from slavapestov/Self-fallback-followup-5.1
Browse files Browse the repository at this point in the history
Exclude properties of class with type Self [5.1]
  • Loading branch information
slavapestov authored Apr 2, 2019
2 parents cb67e8b + ed11057 commit 779cf83
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1063,8 +1063,10 @@ static Type diagnoseUnknownType(TypeResolution resolution,
AbstractFunctionDecl *methodDecl = dc->getInnermostMethodContext();
bool declaringMethod = methodDecl &&
methodDecl->getDeclContext() == dc->getParentForLookup();
bool isPropertyOfClass = insideClass &&
options.is(TypeResolverContext::PatternBindingDecl);

if (((!insideClass || !declaringMethod) &&
if (((!insideClass || !declaringMethod) && !isPropertyOfClass &&
!options.is(TypeResolverContext::GenericRequirement)) ||
options.is(TypeResolverContext::ExplicitCastExpr)) {
Type SelfType = nominal->getSelfInterfaceType();
Expand Down
26 changes: 26 additions & 0 deletions test/type/self.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ class A<T> {
// expected-warning@-1 {{conditional cast from 'Self' to 'Self' always succeeds}}
// expected-warning@-2 {{conditional downcast from 'Self?' to 'A<T>' is equivalent to an implicit conversion to an optional 'A<T>'}}
}
func copy() -> Self {
let copy = Self.init(a: 11)
return copy
}

var copied: Self { // expected-error {{'Self' is only available in a protocol or as the result of a method in a class; did you mean 'A'?}}
let copy = Self.init(a: 11)
return copy
}
}

class B: A<Int> {
Expand All @@ -88,6 +97,14 @@ class B: A<Int> {
override class func y() {
print("override \(Self.self).\(#function)")
}
override func copy() -> Self {
let copy = super.copy() as! Self // supported
return copy
}
override var copied: Self { // expected-error {{'Self' is only available in a protocol or as the result of a method in a class; did you mean 'B'?}}
let copy = super.copied as! Self // unsupported
return copy
}
}

class C {
Expand Down Expand Up @@ -121,6 +138,15 @@ struct S2 {
// expected-warning@-1 {{conditional cast from 'S2.S3<T>' to 'S2.S3<T>' always succeeds}}
}
}
func copy() -> Self {
let copy = Self.init()
return copy
}

var copied: Self {
let copy = Self.init()
return copy
}
}

extension S2 {
Expand Down

0 comments on commit 779cf83

Please sign in to comment.