Skip to content

Commit e7c7239

Browse files
committed
[Sema] Insert ErrorType same-type constraints for placeholder signatures
This helps avoid producing more downstream errors. This changes `GenericSignature::forInvalid` to produce the same signature as e.g `<T where T == Undefined>`. This subsumes the need to introduce conformance requirements for invertible protocols.
1 parent 71841bd commit e7c7239

File tree

7 files changed

+10
-23
lines changed

7 files changed

+10
-23
lines changed

lib/AST/ASTContext.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6012,16 +6012,16 @@ GenericSignature::forInvalid(ArrayRef<GenericTypeParamType *> params) {
60126012
ASSERT(!params.empty());
60136013
auto &ctx = params.front()->getASTContext();
60146014

6015+
// Add same type requirements that make each of the generic parameters
6016+
// concrete error types. This helps avoid downstream diagnostics and is
6017+
// handled the same as if the user wrote e.g `<T where T == Undefined>`.
60156018
SmallVector<Requirement, 2> requirements;
60166019
for (auto *param : params) {
60176020
if (param->isValue())
60186021
continue;
60196022

6020-
for (auto ip : InvertibleProtocolSet::allKnown()) {
6021-
auto *proto = ctx.getProtocol(getKnownProtocolKind(ip));
6022-
requirements.emplace_back(RequirementKind::Conformance, param,
6023-
proto->getDeclaredInterfaceType());
6024-
}
6023+
requirements.emplace_back(RequirementKind::SameType, param,
6024+
ErrorType::get(ctx));
60256025
}
60266026
return GenericSignature::get(params, requirements);
60276027
}

lib/AST/Decl.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,8 +1643,7 @@ bool GenericContext::isComputingGenericSignature() const {
16431643

16441644
/// If we hit a cycle while building the generic signature, we can't return
16451645
/// nullptr, since this breaks invariants elsewhere. Instead, build a dummy
1646-
/// signature where everything is Copyable and Escapable, to avoid spurious
1647-
/// downstream diagnostics concerning move-only types.
1646+
/// invalid signature to avoid spurious downstream diagnostics.
16481647
static GenericSignature getPlaceholderGenericSignature(
16491648
ASTContext &ctx, const DeclContext *DC) {
16501649
SmallVector<GenericParamList *, 2> gpLists;

test/Constraints/same_types.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,15 +282,11 @@ protocol P2 {
282282
}
283283

284284
// CHECK-LABEL: same_types.(file).structuralSameTypeRecursive1@
285-
// CHECK-NEXT: Generic signature: <T, U>
285+
// CHECK-NEXT: Generic signature: <T, U where T == <<error type>>, U == <<error type>>>
286286

287287
// expected-error@+2 {{cannot build rewrite system for generic signature; concrete type nesting limit exceeded}}
288288
// expected-note@+1 {{τ_0_0.[P2:Assoc1].[concrete: ((((((((((((((((((((((((((((((((τ_0_0.[P2:Assoc1], τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1), τ_0_1)] => τ_0_0.[P2:Assoc1]}}
289-
func structuralSameTypeRecursive1<T: P2, U>(_: T, _: U)
290-
where T.Assoc1 == Tuple2<T.Assoc1, U>
291-
// expected-error@-1 {{'Assoc1' is not a member type of type 'T'}}
292-
// expected-error@-2 {{'Assoc1' is not a member type of type 'T'}}
293-
{ }
289+
func structuralSameTypeRecursive1<T: P2, U>(_: T, _: U) where T.Assoc1 == Tuple2<T.Assoc1, U> {}
294290

295291
protocol P3 {
296292
}

test/attr/attr_specialize.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ class G<T> {
5555
@_specialize(where T == S<T>)
5656
// expected-error@-1 {{cannot build rewrite system for generic signature; concrete type nesting limit exceeded}}
5757
// expected-note@-2 {{failed rewrite rule is τ_0_0.[concrete: S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<τ_0_0>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>] => τ_0_0}}
58-
// expected-error@-3 {{too few generic parameters are specified in '_specialize' attribute (got 0, but expected 1)}}
59-
// expected-note@-4 {{missing constraint for 'T' in '_specialize' attribute}}
6058
@_specialize(where T == Int, U == Int) // expected-error{{cannot find type 'U' in scope}}
6159

6260
func noGenericParams() {}

test/decl/protocol/req/recursion.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ extension SomeProtocol where T == Optional<T> { }
1111
// rdar://problem/19840527
1212

1313
class X<T> where T == X {
14-
// expected-error@-1 {{cannot build rewrite system for generic signature; concrete type nesting limit exceeded}}
15-
// expected-note@-2 {{failed rewrite rule is τ_0_0.[concrete: X<X<X<X<X<X<X<X<X<X<X<X<X<X<X<X<X<X<X<X<X<X<X<X<X<X<X<X<X<X<X<X<τ_0_0>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>] => τ_0_0}}
16-
// expected-error@-3 {{generic class 'X' has self-referential generic requirements}}
17-
var type: T { return Swift.type(of: self) } // expected-error{{cannot convert return expression of type 'X<T>.Type' to return type 'T'}}
14+
// expected-error@-1 {{generic class 'X' has self-referential generic requirements}}
15+
var type: T { return Swift.type(of: self) }
1816
}
1917

2018
// FIXME: The "associated type 'Foo' is not a member type of 'Self'" diagnostic

validation-test/compiler_crashers_2_fixed/0161-issue-49119.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ extension Type: P where Param: P, Param.A == Type<Param> {
1313
// expected-error@-3 {{type 'Type<Param>' does not conform to protocol 'P'}}
1414
// expected-note@-4 {{add stubs for conformance}}
1515
typealias A = Param
16-
// expected-note@-1 {{possibly intended match 'Type<Param>.A' (aka 'Param') does not conform to 'P'}}
1716
}

validation-test/compiler_crashers_2_fixed/issue-52031.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ extension S: P where N: P {
1313
static func f<X: P>(_ x: X) -> S<X.A> where A == X, X.A == N {
1414
// expected-error@-1 {{cannot build rewrite system for generic signature; rule length limit exceeded}}
1515
// expected-note@-2 {{τ_0_0.[P:A].[P:A].[P:A].[P:A].[P:A].[concrete: S<S<S<S<S<S<τ_0_0>>>>>>] => τ_0_0.[P:A].[P:A].[P:A].[P:A].[P:A] [subst↓]}}
16-
// expected-error@-3 {{'A' is not a member type of type 'X'}}
17-
// expected-error@-4 {{'A' is not a member type of type 'X'}}
1816
return S<X.A>()
19-
// expected-error@-1 {{'A' is not a member type of type 'X'}}
2017
}
2118
}

0 commit comments

Comments
 (0)