Skip to content
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

Exclude properties of class with type Self #23485

Merged
merged 5 commits into from
Mar 27, 2019
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
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