Skip to content

Commit 175acc6

Browse files
authored
Merge pull request #23485 from johnno1962/Self-fallback-followup
Exclude properties of class with type Self
2 parents 28ecdea + 36ac824 commit 175acc6

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1063,8 +1063,10 @@ static Type diagnoseUnknownType(TypeResolution resolution,
10631063
AbstractFunctionDecl *methodDecl = dc->getInnermostMethodContext();
10641064
bool declaringMethod = methodDecl &&
10651065
methodDecl->getDeclContext() == dc->getParentForLookup();
1066+
bool isPropertyOfClass = insideClass &&
1067+
options.is(TypeResolverContext::PatternBindingDecl);
10661068

1067-
if (((!insideClass || !declaringMethod) &&
1069+
if (((!insideClass || !declaringMethod) && !isPropertyOfClass &&
10681070
!options.is(TypeResolverContext::GenericRequirement)) ||
10691071
options.is(TypeResolverContext::ExplicitCastExpr)) {
10701072
Type SelfType = nominal->getSelfInterfaceType();

test/type/self.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ class A<T> {
7070
// expected-warning@-1 {{conditional cast from 'Self' to 'Self' always succeeds}}
7171
// expected-warning@-2 {{conditional downcast from 'Self?' to 'A<T>' is equivalent to an implicit conversion to an optional 'A<T>'}}
7272
}
73+
func copy() -> Self {
74+
let copy = Self.init(a: 11)
75+
return copy
76+
}
77+
78+
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'?}}
79+
let copy = Self.init(a: 11)
80+
return copy
81+
}
7382
}
7483

7584
class B: A<Int> {
@@ -88,6 +97,14 @@ class B: A<Int> {
8897
override class func y() {
8998
print("override \(Self.self).\(#function)")
9099
}
100+
override func copy() -> Self {
101+
let copy = super.copy() as! Self // supported
102+
return copy
103+
}
104+
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'?}}
105+
let copy = super.copied as! Self // unsupported
106+
return copy
107+
}
91108
}
92109

93110
class C {
@@ -121,6 +138,15 @@ struct S2 {
121138
// expected-warning@-1 {{conditional cast from 'S2.S3<T>' to 'S2.S3<T>' always succeeds}}
122139
}
123140
}
141+
func copy() -> Self {
142+
let copy = Self.init()
143+
return copy
144+
}
145+
146+
var copied: Self {
147+
let copy = Self.init()
148+
return copy
149+
}
124150
}
125151

126152
extension S2 {

0 commit comments

Comments
 (0)