Skip to content

Commit

Permalink
Make Self available to instance member functions (SE-0068?)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnno1962 committed Feb 25, 2019
1 parent 3172ff4 commit c27f79e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
10 changes: 10 additions & 0 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,16 @@ resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *DC) {
};

if (!isConfused) {
if (Name.getBaseName().userFacingName() == "Self") {
DeclName selfName(Context.getIdentifier("self"));
auto selfInstance = lookupUnqualified(DC, selfName, Loc, lookupOptions);
if (!selfInstance.empty()) {
ValueDecl *D = selfInstance.front().getValueDecl();
Expr *E = new (Context) DeclRefExpr(D, nameLoc, /*Implicit=*/false);
return new (Context) DynamicTypeExpr(Loc, Loc, E, Loc, Type());
}
}

TypoCorrectionResults corrections(*this, Name, nameLoc);
performTypoCorrection(DC, UDRE->getRefKind(), Type(),
lookupOptions, corrections);
Expand Down
46 changes: 44 additions & 2 deletions test/decl/func/dynamic_self.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class C1 {

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

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

// This used to crash because metatype construction went down a
Expand Down Expand Up @@ -412,4 +412,46 @@ class SelfOperator {
func useSelfOperator() {
let s = SelfOperator()
_ = s + s
}
}

// These references to Self are now possible

class A<T> {
let b: Int
required init(a: Int) {
print("\(Self).\(#function)")
b = a
}
static func z() {
print("\(Self.self).\(#function)")
}
class func y() {
print("\(Self).\(#function)")
}
func x() {
print("\(Self.self).\(#function)")
Self.y()
Self.z()
_ = Self.init(a: 77)
}
}

class B: A<Int> {
let a: Int
required convenience init(a: Int) {
print("\(Self).\(#function)")
self.init()
}
init() {
print("\(Self.self).\(#function)")
Self.y()
Self.z()
a = 99
super.init(a: 88)
}
override class func y() {
print("\(Self).\(#function)")
}
}

B().x()

0 comments on commit c27f79e

Please sign in to comment.