Skip to content

Commit 410fa12

Browse files
committed
[AST] Create opaque type from tuple type repr
1 parent 4240b00 commit 410fa12

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

lib/AST/NameLookup.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2774,10 +2774,10 @@ static bool declsAreProtocols(ArrayRef<TypeDecl *> decls) {
27742774

27752775
bool TypeRepr::isProtocol(DeclContext *dc){
27762776
auto &ctx = dc->getASTContext();
2777-
DirectlyReferencedTypeDecls d = directReferencesForTypeRepr(ctx.evaluator, ctx, this, dc);
2778-
return declsAreProtocols(d);
2777+
return findIf([&ctx, dc](TypeRepr *ty) { return declsAreProtocols(directReferencesForTypeRepr(ctx.evaluator, ctx, ty, dc)); });
27792778
}
27802779

2780+
27812781
static GenericParamList *
27822782
createExtensionGenericParams(ASTContext &ctx,
27832783
ExtensionDecl *ext,
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// RUN: %target-typecheck-verify-swift -disable-availability-checking -warn-redundant-requirements -enable-experimental-implicit-some
2+
3+
// Access Return Type Constraint
4+
protocol Cafe {
5+
func breakfast()
6+
func treats()
7+
}
8+
9+
class SeasonalMenu : Cafe {
10+
func breakfast() {}
11+
func treats() {}
12+
}
13+
14+
func getCurrentMenu () -> some Cafe {
15+
return SeasonalMenu()
16+
}
17+
18+
var cafe = getCurrentMenu()
19+
cafe.breakfast()
20+
cafe.treats()
21+
22+
// Type alias + custom protocols
23+
typealias Snack = Shop & Cafe
24+
typealias Meal = Eatery
25+
26+
struct CoffeeBar: Snack {
27+
func coffee(){ }
28+
func breakfast() { }
29+
func treats(){ }
30+
}
31+
32+
class Best: Eatery {
33+
func lunch() { };
34+
func dinner() { }
35+
func dessert() { }
36+
}
37+
38+
class TopTier: Eatery {
39+
func lunch() { };
40+
func dinner() { }
41+
func dessert() { }
42+
}
43+
44+
func list(_: Snack) {}
45+
46+
func find( _: Snack) -> Snack { } // expected-error {{function declares an opaque return type, but has no return statements in its body from which to infer an underlying type}}
47+
48+
// tuple types
49+
func list(_: (Meal, Meal)) {}
50+
51+
func highestRated() -> (Eatery, Eatery) {
52+
return (Best(), TopTier())
53+
}
54+
55+
func highestRated() -> (some Snack, some Snack) { } // expected-error {{function declares an opaque return type, but has no return statements in its body from which to infer an underlying type}}
56+
57+
// opaque compostion types
58+
func find() -> Shop & Cafe {
59+
return CoffeeBar()
60+
}
61+
62+
func find() -> AnyObject {
63+
return CoffeeBar() // expected-error {{Return expression of type 'CoffeeBar' expected to be an instance of a class or class-constrained type}}
64+
}
65+
66+
func find() -> Any {
67+
return CoffeeBar()
68+
}
69+
70+
// protocol with associated type constraint
71+
protocol Basket {
72+
associatedtype Fruit
73+
associatedtype MiniBasket: Basket where MiniBasket.Fruit == Fruit
74+
75+
var fruits: [Fruit] { get set }
76+
var minibaskets: [MiniBasket] { get set }
77+
}
78+
79+
struct MyFruit : Basket {
80+
var fruits: [String]
81+
var minibaskets: [FruitforFriend]
82+
}
83+
84+
struct FruitforFriend : Basket {
85+
var fruits: [String]
86+
var minibaskets: [FruitforFriend]
87+
}
88+
89+
func eat(_ myfruit: inout Basket) -> Basket {
90+
myfruit.fruits.removeLast()
91+
myfruit.minibaskets.removeLast()
92+
return myfruit
93+
}
94+

0 commit comments

Comments
 (0)