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

Make Self available to member functions (SE-0068?) #22863

Merged
merged 19 commits into from
Mar 16, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 19 additions & 0 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,25 @@ resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *DC) {
};

if (!isConfused) {
if (Name == Context.Id_Self) {
// return new (Context) TypeExpr(TypeLoc::withoutLoc(
// DynamicSelfType::get(
// DC->getInnermostTypeContext()
johnno1962 marked this conversation as resolved.
Show resolved Hide resolved
// ->getSelfTypeInContext()
// //->getDeclaredInterfaceType()
// , Context)));
auto selfs = lookupUnqualified(DC, Context.Id_self, Loc, lookupOptions);
if (!selfs.empty()) {
ValueDecl *D = selfs.front().getValueDecl();
Expr *E = new (Context) DeclRefExpr(D, nameLoc, /*Implicit=*/false);
if (auto func = dyn_cast<AbstractFunctionDecl>(
DC->getInnermostMethodContext()))
if (func->isStatic())
return E;
return new (Context) DynamicTypeExpr(Loc, Loc, E, Loc, Type());
johnno1962 marked this conversation as resolved.
Show resolved Hide resolved
}
}

TypoCorrectionResults corrections(*this, Name, nameLoc);
performTypoCorrection(DC, UDRE->getRefKind(), Type(),
lookupOptions, corrections);
Expand Down
2 changes: 2 additions & 0 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,8 @@ static Type diagnoseUnknownType(TypeResolution resolution,
// type. Fix this by replacing 'Self' with the nominal type name.
assert(!isa<ProtocolDecl>(nominal) && "Cannot be a protocol");

return nominal->getDeclaredInterfaceType();
johnno1962 marked this conversation as resolved.
Show resolved Hide resolved

// Produce a Fix-It replacing 'Self' with the nominal type name.
auto name = getDeclNameFromContext(dc, nominal);
diags.diagnose(comp->getIdLoc(), diag::self_in_nominal, name)
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
johnno1962 marked this conversation as resolved.
Show resolved Hide resolved

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()