Skip to content

Commit 3396510

Browse files
committed
---
yaml --- r: 341119 b: refs/heads/rxwei-patch-1 c: 8684bae h: refs/heads/master i: 341117: 07c6069 341115: 27dbf4f 341111: 4cf900a 341103: d045017 341087: c08bd81 341055: a2705cb 340991: 70f7c1d
1 parent 2359f4f commit 3396510

File tree

7 files changed

+46
-33
lines changed

7 files changed

+46
-33
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ refs/tags/swift-DEVELOPMENT-SNAPSHOT-2018-08-18-a: b10b1fce14385faa6d44f6b933e95
10151015
refs/heads/rdar-43033749-fix-batch-mode-no-diags-swift-5.0-branch: a14e64eaad30de89f0f5f0b2a782eed7ecdcb255
10161016
refs/heads/revert-19006-error-bridging-integer-type: 8a9065a3696535305ea53fe9b71f91cbe6702019
10171017
refs/heads/revert-19050-revert-19006-error-bridging-integer-type: ecf752d54b05dd0a20f510f0bfa54a3fec3bcaca
1018-
refs/heads/rxwei-patch-1: 8f22da205a579f6fac312d59a7ae52d997d3ec67
1018+
refs/heads/rxwei-patch-1: 8684baedcc932c1dbb2a6e62f7a371191657942a
10191019
refs/heads/shahmishal-patch-1: e58ec0f7488258d42bef51bc3e6d7b3dc74d7b2a
10201020
refs/heads/typelist-existential: 4046359efd541fb5c72d69a92eefc0a784df8f5e
10211021
refs/tags/swift-4.2-DEVELOPMENT-SNAPSHOT-2018-08-20-a: 4319ba09e4fb8650ee86061075c74a016b6baab9

branches/rxwei-patch-1/include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2588,7 +2588,7 @@ ERROR(self_in_mutable_subscript,none,
25882588
ERROR(self_in_parameter,none,
25892589
"'Self' cannot be the type of a function argument in a class", ())
25902590
ERROR(self_in_nested_return,none,
2591-
"'Self' cannot be the type of a nested return value", ())
2591+
"'Self' can only appear at the top level of a method result type", ())
25922592

25932593
//------------------------------------------------------------------------------
25942594
// MARK: Type Check Attributes

branches/rxwei-patch-1/lib/Sema/TypeCheckDecl.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3204,9 +3204,21 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
32043204
else
32053205
diagnoseSelfTypedParameters(Param->getInterfaceType(), Loc, 1);
32063206
}
3207-
TypeLoc RTyLoc = FD->getBodyResultTypeLoc();
3208-
if (RTyLoc.getType())
3209-
diagnoseSelfTypedParameters(RTyLoc.getType(), RTyLoc.getLoc(), 1);
3207+
3208+
auto ResultTy = FD->getResultInterfaceType();
3209+
3210+
// For now, DynamicSelfType can only appear at the top level of a
3211+
// function result type, possibly wrapped in an optional type.
3212+
if (ResultTy->hasDynamicSelfType()) {
3213+
if (auto ObjectTy = ResultTy->getOptionalObjectType())
3214+
ResultTy = ObjectTy;
3215+
if (!ResultTy->is<DynamicSelfType>()) {
3216+
auto loc = FD->getBodyResultTypeLoc().getLoc();
3217+
if (loc.isInvalid())
3218+
loc = FD->getLoc();
3219+
TC.diagnose(loc, diag::self_in_nested_return);
3220+
}
3221+
}
32103222
}
32113223
}
32123224

branches/rxwei-patch-1/lib/Sema/TypeCheckGeneric.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -567,15 +567,12 @@ void TypeChecker::validateGenericFuncOrSubscriptSignature(
567567
decl->getDeclContext()->getGenericSignatureOfContext();
568568

569569
auto params = func ? func->getParameters() : subscr->getIndices();
570-
bool hasDynamicSelf = false;
571570
TypeLoc emptyLoc;
572571
TypeLoc &resultTyLoc = [&]() -> TypeLoc& {
573572
if (subscr)
574573
return subscr->getElementTypeLoc();
575-
if (auto fn = dyn_cast<FuncDecl>(func)) {
576-
hasDynamicSelf = fn->hasDynamicSelf();
574+
if (auto fn = dyn_cast<FuncDecl>(func))
577575
return fn->getBodyResultTypeLoc();
578-
}
579576
return emptyLoc;
580577
}();
581578

@@ -605,9 +602,8 @@ void TypeChecker::validateGenericFuncOrSubscriptSignature(
605602
if (!resultTyLoc.isNull() &&
606603
!(resultTyLoc.getTypeRepr() &&
607604
isa<OpaqueReturnTypeRepr>(resultTyLoc.getTypeRepr())))
608-
validateType(resultTyLoc, resolution, hasDynamicSelf
609-
? TypeResolverContext::DynamicSelfResult
610-
: TypeResolverContext::FunctionResult);
605+
validateType(resultTyLoc, resolution,
606+
TypeResolverContext::FunctionResult);
611607

612608
// Infer requirements from it.
613609
if (resultTyLoc.getTypeRepr()) {
@@ -658,9 +654,8 @@ void TypeChecker::validateGenericFuncOrSubscriptSignature(
658654
resultTyLoc.setType(
659655
getOrCreateOpaqueResultType(resolution, decl, opaqueTy));
660656
} else {
661-
validateType(resultTyLoc, resolution, hasDynamicSelf
662-
? TypeResolverContext::DynamicSelfResult
663-
: TypeResolverContext::FunctionResult);
657+
validateType(resultTyLoc, resolution,
658+
TypeResolverContext::FunctionResult);
664659
}
665660
}
666661

branches/rxwei-patch-1/lib/Sema/TypeCheckType.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,18 +1284,25 @@ resolveTopLevelIdentTypeComponent(TypeResolution resolution,
12841284
auto DC = resolution.getDeclContext();
12851285

12861286
// Dynamic 'Self' in the result type of a function body.
1287-
if (options.getBaseContext() == TypeResolverContext::DynamicSelfResult &&
1288-
comp->getIdentifier() == ctx.Id_Self) {
1289-
auto func = cast<FuncDecl>(DC);
1290-
assert(func->hasDynamicSelf() && "Not marked as having dynamic Self?");
1291-
1292-
// FIXME: The passed-in TypeRepr should get 'typechecked' as well.
1293-
// The issue is though that ComponentIdentTypeRepr only accepts a ValueDecl
1294-
// while the 'Self' type is more than just a reference to a TypeDecl.
1287+
if (comp->getIdentifier() == ctx.Id_Self) {
1288+
switch (options.getBaseContext()) {
1289+
case TypeResolverContext::FunctionResult: {
1290+
if (auto *typeDC = DC->getInnermostTypeContext()) {
1291+
// FIXME: The passed-in TypeRepr should get 'typechecked' as well.
1292+
// The issue is though that ComponentIdentTypeRepr only accepts a ValueDecl
1293+
// while the 'Self' type is more than just a reference to a TypeDecl.
1294+
auto selfType = resolution.mapTypeIntoContext(
1295+
typeDC->getSelfInterfaceType());
1296+
if (!typeDC->getSelfClassDecl())
1297+
return selfType;
1298+
return DynamicSelfType::get(selfType, ctx);
1299+
}
12951300

1296-
auto selfType = resolution.mapTypeIntoContext(
1297-
func->getDeclContext()->getSelfInterfaceType());
1298-
return DynamicSelfType::get(selfType, ctx);
1301+
break;
1302+
}
1303+
default:
1304+
break;
1305+
}
12991306
}
13001307

13011308
auto id = comp->getIdentifier();
@@ -3081,7 +3088,6 @@ Type TypeResolver::resolveImplicitlyUnwrappedOptionalType(
30813088
break;
30823089
case TypeResolverContext::FunctionInput:
30833090
case TypeResolverContext::FunctionResult:
3084-
case TypeResolverContext::DynamicSelfResult:
30853091
case TypeResolverContext::PatternBindingDecl:
30863092
doDiag = !isDirect;
30873093
break;

branches/rxwei-patch-1/lib/Sema/TypeCheckType.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,6 @@ enum class TypeResolverContext : uint8_t {
9494
/// tuple return values. See also: TypeResolutionFlags::Direct
9595
FunctionResult,
9696

97-
/// Whether we are in the result type of a function body that is
98-
/// known to produce dynamic Self.
99-
DynamicSelfResult,
100-
10197
/// Whether we are in a protocol's where clause
10298
ProtocolWhereClause,
10399

@@ -211,7 +207,6 @@ class TypeResolutionOptions {
211207
case Context::FunctionInput:
212208
case Context::VariadicFunctionInput:
213209
case Context::FunctionResult:
214-
case Context::DynamicSelfResult:
215210
case Context::ProtocolWhereClause:
216211
case Context::ExtensionBinding:
217212
case Context::SubscriptDecl:

branches/rxwei-patch-1/test/type/self.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,15 @@ class C {
129129
}
130130
func h(j: () -> Self) -> () -> Self {
131131
// expected-error@-1 {{'Self' cannot be the type of a function argument in a class}}
132-
// expected-error@-2 {{'Self' cannot be the type of a nested return value}}
132+
// expected-error@-2 {{'Self' can only appear at the top level of a method result type}}
133133
return { return self }
134134
// expected-error@-1 {{cannot convert value of type 'C' to closure result type 'Self'}}
135135
}
136+
func i() -> (Self, Self) {}
137+
// expected-error@-1 {{'Self' can only appear at the top level of a method result type}}
138+
139+
func j() -> Self.Type {}
140+
// expected-error@-1 {{'Self' can only appear at the top level of a method result type}}
136141

137142
let p0: Self?
138143
var p1: Self? // expected-error {{'Self' is not available as the type of a mutable property}}

0 commit comments

Comments
 (0)