Skip to content

Commit 654dd1f

Browse files
committed
---
yaml --- r: 284607 b: refs/heads/swift-5.1-branch-04-24-2019 c: f135577 h: refs/heads/master i: 284605: ffc1ba9 284603: 0654ebd 284599: 70ddc65 284591: d2b8b61 284575: 70457ea 284543: ff7d609
1 parent c4076be commit 654dd1f

File tree

9 files changed

+110
-43
lines changed

9 files changed

+110
-43
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1319,4 +1319,4 @@ refs/heads/revert-24065-raj-es-perf-bench: acd6c09b6bd8c5aa922845ee4465bf1333e4f
13191319
refs/heads/rxwei-patch-3: d5f349dedfb5fcaf84c5ac788ad33ea47e502d11
13201320
refs/heads/rxwei-patch-4: d60ed33a03d398b956e1aa81f37b04d0b1cf101e
13211321
refs/heads/rxwei-patch-4-1: b7adfa58fd761906363d280fe323578f61097822
1322-
refs/heads/swift-5.1-branch-04-24-2019: cb2fcb6e5624163cc7f7526991eda23e10f195e4
1322+
refs/heads/swift-5.1-branch-04-24-2019: f13557735cabc8edd9a18d58fac74e63f400ae0f

branches/swift-5.1-branch-04-24-2019/include/swift/AST/Decl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4391,6 +4391,8 @@ class AbstractStorageDecl : public ValueDecl {
43914391
}
43924392

43934393
void computeIsValidKeyPathComponent();
4394+
4395+
OpaqueTypeDecl *OpaqueReturn = nullptr;
43944396

43954397
public:
43964398

@@ -4676,6 +4678,14 @@ class AbstractStorageDecl : public ValueDecl {
46764678

46774679
bool hasDidSetOrWillSetDynamicReplacement() const;
46784680

4681+
OpaqueTypeDecl *getOpaqueResultTypeDecl() const {
4682+
return OpaqueReturn;
4683+
}
4684+
void setOpaqueResultTypeDecl(OpaqueTypeDecl *decl) {
4685+
assert(!OpaqueReturn && "already has opaque type decl");
4686+
OpaqueReturn = decl;
4687+
}
4688+
46794689
// Implement isa/cast/dyncast/etc.
46804690
static bool classof(const Decl *D) {
46814691
return D->getKind() >= DeclKind::First_AbstractStorageDecl &&

branches/swift-5.1-branch-04-24-2019/include/swift/AST/DiagnosticsParse.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ ERROR(sil_box_expected_r_angle,none,
786786

787787
// Opaque types
788788
ERROR(opaque_mid_composition,none,
789-
"'opaque' should appear at the beginning of a composition", ())
789+
"'some' should appear at the beginning of a composition", ())
790790

791791
//------------------------------------------------------------------------------
792792
// MARK: Layout constraint diagnostics

branches/swift-5.1-branch-04-24-2019/include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3558,7 +3558,10 @@ ERROR(unreferenced_generic_parameter,none,
35583558

35593559
// Opaque types
35603560
ERROR(unsupported_opaque_type,none,
3561-
"'opaque' types are only implemented for the declared type of properties and subscripts and the return type of functions", ())
3561+
"'some' types are only implemented for the declared type of properties and subscripts and the return type of functions", ())
3562+
3563+
ERROR(opaque_type_unsupported_pattern,none,
3564+
"'some' type can only be declared on a single property declaration", ())
35623565

35633566
// SIL
35643567
ERROR(opened_non_protocol,none,

branches/swift-5.1-branch-04-24-2019/lib/AST/Decl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2319,6 +2319,8 @@ void ValueDecl::setOverriddenDecls(ArrayRef<ValueDecl *> overridden) {
23192319
OpaqueTypeDecl *ValueDecl::getOpaqueResultTypeDecl() const {
23202320
if (auto func = dyn_cast<FuncDecl>(this)) {
23212321
return func->getOpaqueResultTypeDecl();
2322+
} else if (auto storage = dyn_cast<AbstractStorageDecl>(this)) {
2323+
return storage->getOpaqueResultTypeDecl();
23222324
} else {
23232325
return nullptr;
23242326
}
@@ -2327,6 +2329,8 @@ OpaqueTypeDecl *ValueDecl::getOpaqueResultTypeDecl() const {
23272329
void ValueDecl::setOpaqueResultTypeDecl(OpaqueTypeDecl *D) {
23282330
if (auto func = dyn_cast<FuncDecl>(this)) {
23292331
func->setOpaqueResultTypeDecl(D);
2332+
} else if (auto storage = dyn_cast<AbstractStorageDecl>(this)){
2333+
storage->setOpaqueResultTypeDecl(D);
23302334
} else {
23312335
llvm_unreachable("decl does not support opaque result types");
23322336
}

branches/swift-5.1-branch-04-24-2019/lib/Sema/TypeCheckGeneric.cpp

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,9 @@ static void revertDependentTypeLoc(TypeLoc &tl) {
173173

174174
/// Get the opaque type representing the return type of a declaration, or
175175
/// create it if it does not yet exist.
176-
static Type getOpaqueResultType(TypeChecker &tc,
177-
TypeResolution resolution,
178-
ValueDecl *originatingDecl,
179-
OpaqueReturnTypeRepr *repr) {
176+
Type TypeChecker::getOrCreateOpaqueResultType(TypeResolution resolution,
177+
ValueDecl *originatingDecl,
178+
OpaqueReturnTypeRepr *repr) {
180179
// If the decl already has an opaque type decl for its return type, use it.
181180
if (auto existingDecl = originatingDecl->getOpaqueResultTypeDecl()) {
182181
return existingDecl->getDeclaredInterfaceType();
@@ -188,16 +187,16 @@ static Type getOpaqueResultType(TypeChecker &tc,
188187
TypeLoc constraintTypeLoc(repr->getConstraint());
189188
// Pass along the error type if resolving the repr failed.
190189
bool validationError
191-
= tc.validateType(constraintTypeLoc, resolution, options);
190+
= validateType(constraintTypeLoc, resolution, options);
192191
auto constraintType = constraintTypeLoc.getType();
193192
if (validationError)
194193
return constraintType;
195194

196195
// Error out if the constraint type isn't a class or existential type.
197196
if (!constraintType->getClassOrBoundGenericClass()
198197
&& !constraintType->isExistentialType()) {
199-
tc.diagnose(repr->getConstraint()->getLoc(),
200-
diag::opaque_type_invalid_constraint);
198+
diagnose(repr->getConstraint()->getLoc(),
199+
diag::opaque_type_invalid_constraint);
201200
return constraintTypeLoc.getType();
202201
}
203202

@@ -222,7 +221,7 @@ static Type getOpaqueResultType(TypeChecker &tc,
222221
if (!interfaceGenericParams.empty())
223222
returnTypeDepth = interfaceGenericParams.back()->getDepth() + 1;
224223
auto returnTypeParam = GenericTypeParamType::get(returnTypeDepth, 0,
225-
tc.Context);
224+
Context);
226225
interfaceGenericParams.push_back(returnTypeParam);
227226

228227
if (constraintType->getClassOrBoundGenericClass()) {
@@ -252,14 +251,17 @@ static Type getOpaqueResultType(TypeChecker &tc,
252251
// It has the same parent context and generic environment as the originating
253252
// decl.
254253
auto dc = originatingDecl->getDeclContext();
255-
// TODO: generalize
256-
auto genericParams = cast<FuncDecl>(originatingDecl)->getGenericParams();
257-
258-
auto opaqueDecl = new (tc.Context) OpaqueTypeDecl(originatingDecl,
259-
genericParams,
260-
dc,
261-
interfaceSignature,
262-
returnTypeParam);
254+
255+
auto originatingGenericContext = originatingDecl->getAsGenericContext();
256+
GenericParamList *genericParams = originatingGenericContext
257+
? originatingGenericContext->getGenericParams()
258+
: nullptr;
259+
260+
auto opaqueDecl = new (Context) OpaqueTypeDecl(originatingDecl,
261+
genericParams,
262+
dc,
263+
interfaceSignature,
264+
returnTypeParam);
263265
opaqueDecl->copyFormalAccessFrom(originatingDecl);
264266
if (auto originatingEnv = originatingDC->getGenericEnvironmentOfContext()) {
265267
opaqueDecl->setGenericEnvironment(originatingEnv);
@@ -315,7 +317,7 @@ static void checkGenericFuncSignature(TypeChecker &tc,
315317
// checking.
316318
if (resolution.getStage() != TypeResolutionStage::Structural) {
317319
resultTypeLoc.setType(
318-
getOpaqueResultType(tc, resolution, fn, opaqueType));
320+
tc.getOrCreateOpaqueResultType(resolution, fn, opaqueType));
319321
}
320322
} else {
321323
TypeResolutionOptions options(fn->hasDynamicSelf()
@@ -676,9 +678,17 @@ static void checkGenericSubscriptSignature(TypeChecker &tc,
676678
}
677679

678680
// Check the element type.
679-
tc.validateType(subscript->getElementTypeLoc(), resolution,
680-
TypeResolverContext::FunctionResult);
681-
681+
// It's allowed to be an opaque return type.
682+
if (auto opaqueReturn = dyn_cast<OpaqueReturnTypeRepr>(
683+
subscript->getElementTypeLoc().getTypeRepr())) {
684+
auto opaqueTy = tc.getOrCreateOpaqueResultType(resolution, subscript,
685+
opaqueReturn);
686+
subscript->getElementTypeLoc().setType(opaqueTy);
687+
} else {
688+
tc.validateType(subscript->getElementTypeLoc(), resolution,
689+
TypeResolverContext::FunctionResult);
690+
}
691+
682692
// Infer requirements from it.
683693
if (builder) {
684694
auto source =

branches/swift-5.1-branch-04-24-2019/lib/Sema/TypeCheckPattern.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,28 @@ static bool validateTypedPattern(TypeChecker &TC,
684684
return TP->getType()->hasError();
685685

686686
TypeLoc TL = TP->getTypeLoc();
687-
bool hadError = TC.validateType(TL, resolution, options);
687+
688+
bool hadError;
689+
690+
// If the pattern declares an opaque type, and applies to a single
691+
// variable binding, then we can bind the opaque return type from the
692+
// property definition.
693+
if (auto opaqueRepr = dyn_cast<OpaqueReturnTypeRepr>(TL.getTypeRepr())) {
694+
auto named = dyn_cast<NamedPattern>(
695+
TP->getSubPattern()->getSemanticsProvidingPattern());
696+
if (named) {
697+
auto opaqueTy = TC.getOrCreateOpaqueResultType(resolution,
698+
named->getDecl(),
699+
opaqueRepr);
700+
TL.setType(opaqueTy);
701+
hadError = opaqueTy->hasError();
702+
} else {
703+
TC.diagnose(TP->getLoc(), diag::opaque_type_unsupported_pattern);
704+
hadError = true;
705+
}
706+
} else {
707+
hadError = TC.validateType(TL, resolution, options);
708+
}
688709

689710
if (hadError) {
690711
TP->setType(ErrorType::get(TC.Context));

branches/swift-5.1-branch-04-24-2019/lib/Sema/TypeChecker.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,6 +2113,10 @@ class TypeChecker final : public LazyResolver {
21132113
/// Check if the given decl has a @_semantics attribute that gives it
21142114
/// special case type-checking behavior.
21152115
DeclTypeCheckingSemantics getDeclTypeCheckingSemantics(ValueDecl *decl);
2116+
2117+
Type getOrCreateOpaqueResultType(TypeResolution resolution,
2118+
ValueDecl *originatingDecl,
2119+
OpaqueReturnTypeRepr *repr);
21162120
};
21172121

21182122
/// Temporary on-stack storage and unescaping for encoded diagnostic

branches/swift-5.1-branch-04-24-2019/test/type/opaque.swift

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,23 @@ class D: C, P, Q { func paul() {}; func priscilla() {}; func quinn() {} }
1414

1515
// TODO: Should be valid
1616

17-
let foo: some P = 1 // FIXME expected-error{{'opaque' types are only implemented}}
18-
var computedFoo: some P { // FIXME expected-error{{'opaque' types are only implemented}}
19-
get { return 1 }
20-
set { _ = newValue + 1 }
17+
let property: some P = 1 // TODO expected-error{{cannot convert}}
18+
19+
struct GenericProperty<T: P> {
20+
var x: T
21+
var property: some P {
22+
return x // TODO expected-error{{cannot convert}}
23+
}
24+
}
25+
26+
let (bim, bam): some P = (1, 2) // expected-error{{'some' type can only be declared on a single property declaration}}
27+
var computedProperty: some P {
28+
get { return 1 } // TODO expected-error{{cannot convert}}
29+
set { _ = newValue + 1 } // TODO expected-error{{}} expected-note{{}}
2130
}
2231
struct SubscriptTest {
23-
subscript(_ x: Int) -> some P { // expected-error{{'opaque' types are only implemented}}
24-
return x
32+
subscript(_ x: Int) -> some P {
33+
return x // TODO expected-error{{cannot convert}}
2534
}
2635
}
2736

@@ -64,23 +73,23 @@ struct Test {
6473
let inferredOpaqueStructural2 = (bar(), bas()) // expected-error{{inferred type}}
6574
}
6675

67-
//let zingle = {() -> some P in 1 } // FIXME ex/pected-error{{'opaque' types are only implemented}}
76+
//let zingle = {() -> some P in 1 } // FIXME ex/pected-error{{'some' types are only implemented}}
6877

6978
// Invalid positions
7079

71-
typealias Foo = some P // expected-error{{'opaque' types are only implemented}}
80+
typealias Foo = some P // expected-error{{'some' types are only implemented}}
7281

73-
func blibble(blobble: some P) {} // expected-error{{'opaque' types are only implemented}}
82+
func blibble(blobble: some P) {} // expected-error{{'some' types are only implemented}}
7483

75-
let blubble: () -> some P = { 1 } // expected-error{{'opaque' types are only implemented}}
84+
let blubble: () -> some P = { 1 } // expected-error{{'some' types are only implemented}}
7685

77-
func blib() -> P & some Q { return 1 } // expected-error{{'opaque' should appear at the beginning}}
78-
func blab() -> (P, some Q) { return (1, 2) } // expected-error{{'opaque' types are only implemented}}
79-
func blob() -> (some P) -> P { return { $0 } } // expected-error{{'opaque' types are only implemented}}
80-
func blorb<T: some P>(_: T) { } // expected-error{{'opaque' types are only implemented}}
81-
func blub<T>() -> T where T == some P { return 1 } // expected-error{{'opaque' types are only implemented}} expected-error{{cannot convert}}
86+
func blib() -> P & some Q { return 1 } // expected-error{{'some' should appear at the beginning}}
87+
func blab() -> (P, some Q) { return (1, 2) } // expected-error{{'some' types are only implemented}}
88+
func blob() -> (some P) -> P { return { $0 } } // expected-error{{'some' types are only implemented}}
89+
func blorb<T: some P>(_: T) { } // expected-error{{'some' types are only implemented}}
90+
func blub<T>() -> T where T == some P { return 1 } // expected-error{{'some' types are only implemented}} expected-error{{cannot convert}}
8291

83-
protocol OP: some P {} // expected-error{{'opaque' types are only implemented}}
92+
protocol OP: some P {} // expected-error{{'some' types are only implemented}}
8493

8594
func foo() -> some P {
8695
let x = (some P).self // expected-error*{{}}
@@ -89,9 +98,9 @@ func foo() -> some P {
8998

9099
// Invalid constraints
91100

92-
let zug: some Int = 1 // FIXME expected-error{{'opaque' types are only implemented}}
93-
let zwang: some () = () // FIXME expected-error{{'opaque' types are only implemented}}
94-
let zwoggle: some (() -> ()) = {} // FIXME expected-error{{'opaque' types are only implemented}}
101+
let zug: some Int = 1 // FIXME expected-error{{must specify only}}
102+
let zwang: some () = () // FIXME expected-error{{must specify only}}
103+
let zwoggle: some (() -> ()) = {} // FIXME expected-error{{must specify only}}
95104

96105
// Type-checking of expressions of opaque type
97106

@@ -251,6 +260,12 @@ struct DoesNotConform {}
251260
func doesNotConform() -> some P {
252261
return DoesNotConform()
253262
}
263+
264+
var doesNotConformProp: some P = DoesNotConform()
265+
266+
var DoesNotConformComputedProp: some P {
267+
return DoesNotConform()
268+
}
254269
*/
255270

256271

0 commit comments

Comments
 (0)