Skip to content

Commit 0d87c2d

Browse files
authored
Merge pull request #7608 from slavapestov/more-generic-subscript-pre-gardening
More preliminary gardening for generic subscripts
2 parents 2b8f47e + ea0f271 commit 0d87c2d

File tree

5 files changed

+21
-64
lines changed

5 files changed

+21
-64
lines changed

include/swift/AST/Decl.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,13 +1399,13 @@ class GenericContext : public DeclContext {
13991399
: DeclContext(Kind, Parent) { }
14001400

14011401
public:
1402-
/// \brief Retrieve the set of parameters to a generic subscript, or null if
1403-
/// this subscript is not generic.
1402+
/// \brief Retrieve the set of parameters to a generic context, or null if
1403+
/// this context is not generic.
14041404
GenericParamList *getGenericParams() const { return GenericParams; }
14051405

14061406
void setGenericParams(GenericParamList *GenericParams);
14071407

1408-
/// \brief Determine whether this subscript has generic parameters
1408+
/// \brief Determine whether this context has generic parameters
14091409
/// of its own.
14101410
bool isGeneric() const { return GenericParams != nullptr; }
14111411

@@ -1419,10 +1419,10 @@ class GenericContext : public DeclContext {
14191419
TrailingWhere = trailingWhereClause;
14201420
}
14211421

1422-
/// Retrieve the generic signature for this subscript.
1422+
/// Retrieve the generic signature for this context.
14231423
GenericSignature *getGenericSignature() const;
14241424

1425-
/// Retrieve the generic context for this subscript.
1425+
/// Retrieve the generic context for this context.
14261426
GenericEnvironment *getGenericEnvironment() const;
14271427

14281428
/// Retrieve the innermost generic parameter types.
@@ -1446,7 +1446,7 @@ class GenericContext : public DeclContext {
14461446
GenericSignature *genericSig,
14471447
uint64_t genericEnvData);
14481448

1449-
/// Set the generic context of this subscript.
1449+
/// Set the generic context of this context.
14501450
void setGenericEnvironment(GenericEnvironment *genericEnv);
14511451
};
14521452

lib/Sema/TypeCheckDecl.cpp

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4812,35 +4812,12 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
48124812
gp->setOuterParameters(FD->getDeclContext()->getGenericParamsOfContext());
48134813

48144814
auto *sig = TC.validateGenericFuncSignature(FD);
4815-
4816-
// Create a fresh generic signature builder.
4817-
GenericSignatureBuilder builder(
4818-
FD->getASTContext(),
4819-
LookUpConformanceInModule(FD->getModuleContext()));
4820-
auto *parentSig = FD->getDeclContext()->getGenericSignatureOfContext();
4821-
TC.checkGenericParamList(&builder, gp, parentSig, nullptr);
4822-
4823-
// Infer requirements from parameter patterns.
4824-
for (auto pattern : FD->getParameterLists()) {
4825-
builder.inferRequirements(pattern, gp);
4826-
}
4827-
4828-
// Infer requirements from the result type.
4829-
if (!FD->getBodyResultTypeLoc().isNull()) {
4830-
unsigned depth = gp->getDepth();
4831-
builder.inferRequirements(FD->getBodyResultTypeLoc(),
4832-
/*minDepth=*/depth,
4833-
/*maxDepth=*/depth);
4834-
}
4815+
auto *env = sig->createGenericEnvironment(*FD->getModuleContext());
4816+
FD->setGenericEnvironment(env);
48354817

48364818
// Revert the types within the signature so it can be type-checked with
48374819
// archetypes below.
48384820
TC.revertGenericFuncSignature(FD);
4839-
4840-
// Assign archetypes.
4841-
builder.finalize(FD->getLoc(), sig->getGenericParams());
4842-
auto *env = sig->createGenericEnvironment(*FD->getModuleContext());
4843-
FD->setGenericEnvironment(env);
48444821
} else if (FD->getDeclContext()->getGenericSignatureOfContext()) {
48454822
(void)TC.validateGenericFuncSignature(FD);
48464823
// Revert all of the types within the signature of the function.
@@ -6458,24 +6435,12 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
64586435
gp->setOuterParameters(CD->getDeclContext()->getGenericParamsOfContext());
64596436

64606437
auto *sig = TC.validateGenericFuncSignature(CD);
6461-
6462-
GenericSignatureBuilder builder(
6463-
CD->getASTContext(),
6464-
LookUpConformanceInModule(CD->getModuleContext()));
6465-
auto *parentSig = CD->getDeclContext()->getGenericSignatureOfContext();
6466-
TC.checkGenericParamList(&builder, gp, parentSig, nullptr);
6467-
6468-
// Infer requirements from the parameters of the constructor.
6469-
builder.inferRequirements(CD->getParameterList(1), gp);
6438+
auto *env = sig->createGenericEnvironment(*CD->getModuleContext());
6439+
CD->setGenericEnvironment(env);
64706440

64716441
// Revert the types within the signature so it can be type-checked with
64726442
// archetypes below.
64736443
TC.revertGenericFuncSignature(CD);
6474-
6475-
// Assign archetypes.
6476-
builder.finalize(CD->getLoc(), sig->getGenericParams());
6477-
auto *env = sig->createGenericEnvironment(*CD->getModuleContext());
6478-
CD->setGenericEnvironment(env);
64796444
} else if (CD->getDeclContext()->getGenericSignatureOfContext()) {
64806445
(void)TC.validateGenericFuncSignature(CD);
64816446

test/Constraints/same_types.swift

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,10 @@ func fail4<T: Barrable>(_ t: T) -> (Y, Z)
9595
return (t.bar, t.bar.foo) // expected-error{{cannot convert return expression of type 'X' to return type 'Z'}}
9696
}
9797

98-
// TODO: repeat diagnostic
9998
func fail5<T: Barrable>(_ t: T) -> (Y, Z)
10099
where
101100
T.Bar.Foo == Z,
102-
T.Bar == Y { // expected-error 2{{generic parameter 'T.Bar.Foo' cannot be equal to both 'Z' and 'X'}}
101+
T.Bar == Y { // expected-error{{generic parameter 'T.Bar.Foo' cannot be equal to both 'Z' and 'X'}}
103102
return (t.bar, t.bar.foo) // expected-error{{cannot convert return expression of type 'X' to return type 'Z'}}
104103
}
105104

@@ -110,7 +109,7 @@ func testAssocTypeEquivalence<T: Fooable>(_ fooable: T) -> X.Type
110109
return T.Foo.self
111110
}
112111

113-
func fail6<T>(_ t: T) -> Int where T == Int { // expected-error 2{{same-type requirement makes generic parameter 'T' non-generic}}
112+
func fail6<T>(_ t: T) -> Int where T == Int { // expected-error{{same-type requirement makes generic parameter 'T' non-generic}}
114113
return t
115114
}
116115

@@ -161,7 +160,7 @@ struct S1<T : P> {
161160
S1<Q>().foo(x: 1, y: 2)
162161

163162
struct S2<T : P> where T.A == T.B {
164-
func foo<X, Y>(x: X, y: Y) where X == T.A, Y == T.B { // expected-error 2{{same-type requirement makes generic parameters 'X' and 'Y' equivalent}}
163+
func foo<X, Y>(x: X, y: Y) where X == T.A, Y == T.B { // expected-error{{same-type requirement makes generic parameters 'X' and 'Y' equivalent}}
165164
print(X.self)
166165
print(Y.self)
167166
print(x)
@@ -171,7 +170,7 @@ struct S2<T : P> where T.A == T.B {
171170
S2<Q>().foo(x: 1, y: 2)
172171

173172
struct S3<T : P> {
174-
func foo<X, Y>(x: X, y: Y) where X == T.A, Y == T.A {} // expected-error 2{{same-type requirement makes generic parameters 'X' and 'Y' equivalent}}
173+
func foo<X, Y>(x: X, y: Y) where X == T.A, Y == T.A {} // expected-error{{same-type requirement makes generic parameters 'X' and 'Y' equivalent}}
175174
}
176175
S3<Q>().foo(x: 1, y: 2)
177176

@@ -211,33 +210,27 @@ protocol P1 {
211210
func structuralSameType1<A: P1, B: P1, T, U, V, W>(_: A, _: B, _: T, _: U, _: V, _: W)
212211
where A.Assoc == X1<T, U>, B.Assoc == X1<V, W>, A.Assoc == B.Assoc { }
213212
// expected-error@-1{{same-type requirement makes generic parameters 'T' and 'V' equivalent}}
214-
// expected-error@-2{{same-type requirement makes generic parameters 'T' and 'V' equivalent}}
215-
// expected-error@-3{{same-type requirement makes generic parameters 'U' and 'W' equivalent}}
216-
// expected-error@-4{{same-type requirement makes generic parameters 'U' and 'W' equivalent}}
213+
// expected-error@-2{{same-type requirement makes generic parameters 'U' and 'W' equivalent}}
217214

218215
typealias Tuple2<T, U> = (T, U)
219216

220217
func structuralSameType2<A: P1, B: P1, T, U, V, W>(_: A, _: B, _: T, _: U, _: V, _: W)
221218
where A.Assoc == Tuple2<T, U>, B.Assoc == Tuple2<V, W>, A.Assoc == B.Assoc { }
222219
// expected-error@-1{{same-type requirement makes generic parameters 'T' and 'V' equivalent}}
223-
// expected-error@-2{{same-type requirement makes generic parameters 'T' and 'V' equivalent}}
224-
// expected-error@-3{{same-type requirement makes generic parameters 'U' and 'W' equivalent}}
225-
// expected-error@-4{{same-type requirement makes generic parameters 'U' and 'W' equivalent}}
220+
// expected-error@-2{{same-type requirement makes generic parameters 'U' and 'W' equivalent}}
226221

227222
func structuralSameType3<T, U, V, W>(_: T, _: U, _: V, _: W)
228223
where X1<T, U> == X1<V, W> { }
229224
// expected-error@-1{{same-type requirement makes generic parameters 'T' and 'V' equivalent}}
230-
// expected-error@-2{{same-type requirement makes generic parameters 'T' and 'V' equivalent}}
231-
// expected-error@-3{{same-type requirement makes generic parameters 'U' and 'W' equivalent}}
232-
// expected-error@-4{{same-type requirement makes generic parameters 'U' and 'W' equivalent}}
225+
// expected-error@-2{{same-type requirement makes generic parameters 'U' and 'W' equivalent}}
233226

234227
protocol P2 {
235228
associatedtype Assoc1
236229
associatedtype Assoc2
237230
}
238231

239232
func structuralSameTypeRecursive1<T: P2, U>(_: T, _: U)
240-
where T.Assoc1 == Tuple2<T.Assoc1, U> // expected-error 2{{same-type constraint 'T.Assoc1' == '(T.Assoc1, U)' is recursive}}
233+
where T.Assoc1 == Tuple2<T.Assoc1, U> // expected-error{{same-type constraint 'T.Assoc1' == '(T.Assoc1, U)' is recursive}}
241234
{ }
242235

243236

test/Generics/function_defs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,4 @@ func badTypeConformance1<T>(_: T) where Int : EqualComparable {} // expected-err
295295
func badTypeConformance2<T>(_: T) where T.Blarg : EqualComparable { } // expected-error{{'Blarg' is not a member type of 'T'}}
296296

297297
func badSameType<T, U : GeneratesAnElement, V>(_ : T)
298-
where T == U.Element, U.Element == V {} // expected-error 2{{same-type requirement makes generic parameters 'T' and 'V' equivalent}}
298+
where T == U.Element, U.Element == V {} // expected-error{{same-type requirement makes generic parameters 'T' and 'V' equivalent}}

test/Generics/superclass_constraint.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ func f8<T : GA<A>>(_: T) where T : GA<B> {} // expected-error{{generic parameter
3232
func f9<T : GA<A>>(_: T) where T : GB<A> {}
3333
func f10<T : GB<A>>(_: T) where T : GA<A> {}
3434

35-
// FIXME: Extra diagnostics because we're re-building the archetype builder.
36-
func f11<T : GA<T>>(_: T) { } // expected-error 2{{superclass constraint 'T' : 'GA<T>' is recursive}}
37-
func f12<T : GA<U>, U : GB<T>>(_: T, _: U) { } // expected-error 2{{superclass constraint 'U' : 'GB<T>' is recursive}} // expected-error 2{{superclass constraint 'T' : 'GA<U>' is recursive}}
35+
func f11<T : GA<T>>(_: T) { } // expected-error{{superclass constraint 'T' : 'GA<T>' is recursive}}
36+
func f12<T : GA<U>, U : GB<T>>(_: T, _: U) { } // expected-error{{superclass constraint 'U' : 'GB<T>' is recursive}} // expected-error{{superclass constraint 'T' : 'GA<U>' is recursive}}
3837
func f13<T : U, U : GA<T>>(_: T, _: U) { } // expected-error{{inheritance from non-protocol, non-class type 'U'}}
3938

4039
// rdar://problem/24730536

0 commit comments

Comments
 (0)