Skip to content

Commit ec3af4d

Browse files
committed
---
yaml --- r: 347516 b: refs/heads/master c: 1180f23 h: refs/heads/master
1 parent e8b678d commit ec3af4d

File tree

4 files changed

+62
-19
lines changed

4 files changed

+62
-19
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 6e3a6ebd32e301ecda9d499896786d28bb659ab9
2+
refs/heads/master: 1180f23b9a4975ae16a3299bda3d6718d7b6153a
33
refs/heads/master-next: 203b3026584ecad859eb328b2e12490099409cd5
44
refs/tags/osx-passed: b6b74147ef8a386f532cf9357a1bde006e552c54
55
refs/tags/swift-2.2-SNAPSHOT-2015-12-01-a: 6bb18e013c2284f2b45f5f84f2df2887dc0f7dea

trunk/lib/Sema/TypeCheckType.cpp

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,44 @@ static std::string getDeclNameFromContext(DeclContext *dc,
10331033
}
10341034
}
10351035

1036+
//
1037+
// SE-0068 is "Expanding Swift Self to class members and value types"
1038+
// https://github.com/apple/swift-evolution/blob/master/proposals/0068-universal-self.md
1039+
//
1040+
static Type SelfAllowedBySE0068(TypeResolution resolution,
1041+
TypeResolutionOptions options) {
1042+
auto dc = resolution.getDeclContext();
1043+
ASTContext &ctx = dc->getASTContext();
1044+
DeclContext *nominalDC = nullptr;
1045+
NominalTypeDecl *nominal = nullptr;
1046+
if ((nominalDC = dc->getInnermostTypeContext()) &&
1047+
(nominal = nominalDC->getSelfNominalTypeDecl())) {
1048+
assert(!isa<ProtocolDecl>(nominal) && "Cannot be a protocol");
1049+
1050+
bool insideClass = nominalDC->getSelfClassDecl() != nullptr;
1051+
AbstractFunctionDecl *methodDecl = dc->getInnermostMethodContext();
1052+
bool declaringMethod = methodDecl &&
1053+
methodDecl->getDeclContext() == dc->getParentForLookup();
1054+
bool isMutablePropertyOrSubscriptOfClass = insideClass &&
1055+
(options.is(TypeResolverContext::PatternBindingDecl) ||
1056+
options.is(TypeResolverContext::FunctionResult));
1057+
bool isTypeAliasInClass = insideClass &&
1058+
options.is(TypeResolverContext::TypeAliasDecl);
1059+
1060+
if (((!insideClass || !declaringMethod) &&
1061+
!isMutablePropertyOrSubscriptOfClass && !isTypeAliasInClass &&
1062+
!options.is(TypeResolverContext::GenericRequirement)) ||
1063+
options.is(TypeResolverContext::ExplicitCastExpr)) {
1064+
Type SelfType = nominal->getSelfInterfaceType();
1065+
if (insideClass)
1066+
SelfType = DynamicSelfType::get(SelfType, ctx);
1067+
return resolution.mapTypeIntoContext(SelfType);
1068+
}
1069+
}
1070+
1071+
return Type();
1072+
}
1073+
10361074
/// Diagnose a reference to an unknown type.
10371075
///
10381076
/// This routine diagnoses a reference to an unknown type, and
@@ -1058,25 +1096,10 @@ static Type diagnoseUnknownType(TypeResolution resolution,
10581096
NominalTypeDecl *nominal = nullptr;
10591097
if ((nominalDC = dc->getInnermostTypeContext()) &&
10601098
(nominal = nominalDC->getSelfNominalTypeDecl())) {
1099+
// Attempt to refer to 'Self' within a non-protocol nominal
1100+
// type. Fix this by replacing 'Self' with the nominal type name.
10611101
assert(!isa<ProtocolDecl>(nominal) && "Cannot be a protocol");
10621102

1063-
bool insideClass = nominalDC->getSelfClassDecl() != nullptr;
1064-
AbstractFunctionDecl *methodDecl = dc->getInnermostMethodContext();
1065-
bool declaringMethod = methodDecl &&
1066-
methodDecl->getDeclContext() == dc->getParentForLookup();
1067-
bool isPropertyOfClass = insideClass &&
1068-
options.is(TypeResolverContext::PatternBindingDecl);
1069-
1070-
if (((!insideClass || !declaringMethod) && !isPropertyOfClass &&
1071-
!options.is(TypeResolverContext::GenericRequirement)) ||
1072-
options.is(TypeResolverContext::ExplicitCastExpr)) {
1073-
Type SelfType = nominal->getSelfInterfaceType();
1074-
if (insideClass)
1075-
SelfType = DynamicSelfType::get(SelfType, ctx);
1076-
return resolution.mapTypeIntoContext(SelfType);
1077-
}
1078-
1079-
// Attempt to refer to 'Self' within a non-protocol nominal type.
10801103
// Produce a Fix-It replacing 'Self' with the nominal type name.
10811104
auto name = getDeclNameFromContext(dc, nominal);
10821105
diags.diagnose(comp->getIdLoc(), diag::self_in_nominal, name)
@@ -1341,6 +1364,10 @@ resolveTopLevelIdentTypeComponent(TypeResolution resolution,
13411364
if (options.contains(TypeResolutionFlags::SilenceErrors))
13421365
return ErrorType::get(ctx);
13431366

1367+
if (id == ctx.Id_Self)
1368+
if (auto SelfType = SelfAllowedBySE0068(resolution, options))
1369+
return SelfType;
1370+
13441371
return diagnoseUnknownType(resolution, nullptr, SourceRange(), comp,
13451372
options, lookupOptions);
13461373
}

trunk/test/type/self.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ final class FinalMario : Mario {
4646
// These references to Self are now possible (SE-0068)
4747

4848
class A<T> {
49+
typealias _Self = Self
50+
// expected-error@-1 {{'Self' is only available in a protocol or as the result of a method in a class; did you mean 'A'?}}
4951
let b: Int
5052
required init(a: Int) {
5153
print("\(Self.self).\(#function)")
@@ -79,6 +81,13 @@ class A<T> {
7981
let copy = Self.init(a: 11)
8082
return copy
8183
}
84+
subscript (i: Int) -> Self { // expected-error {{'Self' is only available in a protocol or as the result of a method in a class; did you mean 'A'?}}
85+
get {
86+
return Self.init(a: i)
87+
}
88+
set(newValue) {
89+
}
90+
}
8291
}
8392

8493
class B: A<Int> {
@@ -164,6 +173,13 @@ extension S2 {
164173
return Self.init() as? Self
165174
// expected-warning@-1 {{conditional cast from 'S2' to 'S2' always succeeds}}
166175
}
176+
subscript (i: Int) -> Self {
177+
get {
178+
return Self.init()
179+
}
180+
set(newValue) {
181+
}
182+
}
167183
}
168184

169185
enum E {

trunk/utils/build-script-impl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2534,7 +2534,7 @@ for host in "${ALL_HOSTS[@]}"; do
25342534
cmake_options+=(
25352535
-DLLDB_BUILD_FRAMEWORK:BOOL=TRUE
25362536
-DLLDB_CODESIGN_IDENTITY=""
2537-
-DLLDB_USE_SYSTEM_DEBUGSERVER:BOOL=TRUE
2537+
-DLLDB_USE_SYSTEM_DEBUGSERVER:BOOL="${LLDB_USE_SYSTEM_DEBUGSERVER}"
25382538
-DLLDB_FRAMEWORK_TOOLS="darwin-debug;lldb-argdumper;lldb-server;repl_swift"
25392539
)
25402540
fi

0 commit comments

Comments
 (0)