Skip to content

Commit c27f79e

Browse files
committed
Make Self available to instance member functions (SE-0068?)
1 parent 3172ff4 commit c27f79e

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,16 @@ resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *DC) {
551551
};
552552

553553
if (!isConfused) {
554+
if (Name.getBaseName().userFacingName() == "Self") {
555+
DeclName selfName(Context.getIdentifier("self"));
556+
auto selfInstance = lookupUnqualified(DC, selfName, Loc, lookupOptions);
557+
if (!selfInstance.empty()) {
558+
ValueDecl *D = selfInstance.front().getValueDecl();
559+
Expr *E = new (Context) DeclRefExpr(D, nameLoc, /*Implicit=*/false);
560+
return new (Context) DynamicTypeExpr(Loc, Loc, E, Loc, Type());
561+
}
562+
}
563+
554564
TypoCorrectionResults corrections(*this, Name, nameLoc);
555565
performTypoCorrection(DC, UDRE->getRefKind(), Type(),
556566
lookupOptions, corrections);

test/decl/func/dynamic_self.swift

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class C1 {
8989

9090
if b { return self.init(int: 5) }
9191

92-
return Self() // expected-error{{use of unresolved identifier 'Self'; did you mean 'self'?}}
92+
return Self() // expected-error{{non-nominal type 'Self.Type' does not support explicit initialization}}
9393
}
9494

9595
// This used to crash because metatype construction went down a
@@ -412,4 +412,46 @@ class SelfOperator {
412412
func useSelfOperator() {
413413
let s = SelfOperator()
414414
_ = s + s
415-
}
415+
}
416+
417+
// These references to Self are now possible
418+
419+
class A<T> {
420+
let b: Int
421+
required init(a: Int) {
422+
print("\(Self).\(#function)")
423+
b = a
424+
}
425+
static func z() {
426+
print("\(Self.self).\(#function)")
427+
}
428+
class func y() {
429+
print("\(Self).\(#function)")
430+
}
431+
func x() {
432+
print("\(Self.self).\(#function)")
433+
Self.y()
434+
Self.z()
435+
_ = Self.init(a: 77)
436+
}
437+
}
438+
439+
class B: A<Int> {
440+
let a: Int
441+
required convenience init(a: Int) {
442+
print("\(Self).\(#function)")
443+
self.init()
444+
}
445+
init() {
446+
print("\(Self.self).\(#function)")
447+
Self.y()
448+
Self.z()
449+
a = 99
450+
super.init(a: 88)
451+
}
452+
override class func y() {
453+
print("\(Self).\(#function)")
454+
}
455+
}
456+
457+
B().x()

0 commit comments

Comments
 (0)