Skip to content

Commit 19fc174

Browse files
committed
Don't create fake GTPDs for SIL generic signatures
1 parent e0f2b81 commit 19fc174

File tree

4 files changed

+35
-37
lines changed

4 files changed

+35
-37
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5324,9 +5324,7 @@ void PrintAST::visitMacroExpansionExpr(MacroExpansionExpr *expr) {
53245324
}
53255325

53265326
void PrintAST::visitTypeValueExpr(TypeValueExpr *expr) {
5327-
// Explicitly don't use 'printType' with 'getParamType' because that will
5328-
// print the preceeding 'let N' which is illegal in source.
5329-
expr->getParamTypeRepr()->print(Printer, Options);
5327+
expr->getType()->print(Printer, Options);
53305328
}
53315329

53325330
void PrintAST::visitBraceStmt(BraceStmt *stmt) {

lib/Sema/TypeCheckType.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,13 @@ namespace {
755755
if (!paramType->isValue())
756756
return true;
757757

758+
// Both of these generic type parameters are values, but they may not have
759+
// underlying value types associated with them. This can occur when a
760+
// parameter doesn't declare a value type and we're going to diagnose it
761+
// later.
762+
if (!paramType->getValueType() || !secondType->getValueType())
763+
return false;
764+
758765
// Otherwise, these are both value parameters and check that both their
759766
// value types are the same.
760767
return paramType->getValueType()->isEqual(secondType->getValueType());

lib/Serialization/Deserialization.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,21 +1595,11 @@ ModuleFile::getGenericSignatureChecked(serialization::GenericSignatureID ID) {
15951595
auto paramTy = getType(rawParamIDs[i+1])->castTo<GenericTypeParamType>();
15961596

15971597
if (!name.empty()) {
1598-
auto *paramDecl = GenericTypeParamDecl::createDeserialized(
1599-
getAssociatedModule(), name, paramTy->getDepth(),
1600-
paramTy->getIndex(), paramTy->getParamKind());
1601-
1602-
// If we're dealing with a value generic, the parameter type already
1603-
// serializes the value type. Inform the request evaluator that we don't
1604-
// need to recompute this value for the param decl.
1605-
if (paramTy->isValue()) {
1606-
paramDecl->getASTContext().evaluator.cacheOutput(
1607-
GenericTypeParamDeclGetValueTypeRequest{paramDecl},
1608-
paramTy->getValueType());
1609-
}
1610-
1611-
paramTy = paramDecl->getDeclaredInterfaceType()
1612-
->castTo<GenericTypeParamType>();
1598+
paramTy = GenericTypeParamType::get(name, paramTy->getParamKind(),
1599+
paramTy->getDepth(),
1600+
paramTy->getIndex(),
1601+
paramTy->getValueType(),
1602+
getContext());
16131603
}
16141604

16151605
paramTypes.push_back(paramTy);

test/Sema/value_generics.swift

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
protocol P {}
44

55
func invalid<let N>() {} // expected-error {{value generic 'N' must have an explicit value type declared}}
6+
// expected-error@-1 {{generic parameter 'N' is not used in function signature}}
7+
func invalid<let N>(_: A<N>) {} // expected-error {{value generic 'N' must have an explicit value type declared}}
8+
// expected-error@-1 {{cannot pass type 'N' as a value for generic value 'N'}}
69

710
struct A<let N: Int> {
811
var int: Int {
@@ -21,11 +24,11 @@ struct A<let N: Int> {
2124
N.self // OK
2225
}
2326

24-
var n: N { // expected-error {{using value generic 'let N' here is not allowed}}
27+
var n: N { // expected-error {{using value generic 'N' here is not allowed}}
2528
fatalError()
2629
}
2730

28-
var nType: N.Type { // expected-error {{using value generic 'let N' here is not allowed}}
31+
var nType: N.Type { // expected-error {{using value generic 'N' here is not allowed}}
2932
fatalError()
3033
}
3134

@@ -34,47 +37,47 @@ struct A<let N: Int> {
3437
}
3538
}
3639

37-
extension A where N: P {} // expected-error {{value generic type 'let N' cannot conform to protocol 'P'}}
40+
extension A where N: P {} // expected-error {{value generic type 'N' cannot conform to protocol 'P'}}
3841

39-
extension A where N == Int {} // expected-error {{cannot constrain value parameter 'let N' to be type 'Int'}}
42+
extension A where N == Int {} // expected-error {{cannot constrain value parameter 'N' to be type 'Int'}}
4043

4144
func b(with a: A<123>) {} // OK
4245
func c<let M: Int>(with a: A<M>) {} // OK
43-
func d<T>(with a: A<T>) {} // expected-error {{cannot pass type 'T' as a value for generic value 'let N'}}
44-
func e(with a: A<Int>) {} // expected-error {{cannot pass type 'Int' as a value for generic value 'let N'}}
46+
func d<T>(with a: A<T>) {} // expected-error {{cannot pass type 'T' as a value for generic value 'N'}}
47+
func e(with a: A<Int>) {} // expected-error {{cannot pass type 'Int' as a value for generic value 'N'}}
4548

4649
struct Generic<T: ~Copyable & ~Escapable> {}
4750
struct GenericWithIntParam<T: ~Copyable & ~Escapable, let N: Int> {}
4851

4952
func f(_: Generic<123>) {} // expected-error {{integer unexpectedly used in a type position}}
50-
func g<let N: Int>(_: Generic<N>) {} // expected-error {{cannot use value type 'let N' for generic argument 'T'}}
53+
func g<let N: Int>(_: Generic<N>) {} // expected-error {{cannot use value type 'N' for generic argument 'T'}}
5154
func h(_: (Int, 123)) {} // expected-error {{integer unexpectedly used in a type position}}
5255
func i(_: () -> 123) {} // expected-error {{integer unexpectedly used in a type position}}
5356
func j(_: (A<123>) -> ()) {} // OK
5457
func k(_: some 123) {} // expected-error {{integer unexpectedly used in a type position}}
55-
func l(_: GenericWithIntParam<123, Int>) {} // expected-error {{cannot pass type 'Int' as a value for generic value 'let N'}}
58+
func l(_: GenericWithIntParam<123, Int>) {} // expected-error {{cannot pass type 'Int' as a value for generic value 'N'}}
5659
func m(_: GenericWithIntParam<Int, 123>) {} // OK
5760

5861
typealias One = 1 // expected-error {{integer unexpectedly used in a type position}}
5962

60-
struct B<let N: UInt8> {} // expected-error {{'UInt8' is not a supported value type for 'let N'}}
63+
struct B<let N: UInt8> {} // expected-error {{'UInt8' is not a supported value type for 'N'}}
6164

6265
struct C<let N: Int, let M: Int> {}
6366

64-
extension C where N == 123 { // expected-note {{where 'let N' = '0'}}
65-
// expected-note@-1 {{where 'let N' = '0'}}
66-
// expected-note@-2 {{where 'let N' = 'let T'}}
67+
extension C where N == 123 { // expected-note {{where 'N' = '0'}}
68+
// expected-note@-1 {{where 'N' = '0'}}
69+
// expected-note@-2 {{where 'N' = 'T'}}
6770
func nIs123() {}
6871
}
6972

70-
extension C where M == 321 { // expected-note {{where 'let M' = '0'}}
71-
// expected-note@-1 {{where 'let M' = '0'}}
72-
// expected-note@-2 {{where 'let M' = 'let T'}}
73+
extension C where M == 321 { // expected-note {{where 'M' = '0'}}
74+
// expected-note@-1 {{where 'M' = '0'}}
75+
// expected-note@-2 {{where 'M' = 'T'}}
7376
func mIs123() {}
7477
}
7578

76-
extension C where N == M { // expected-note {{where 'let N' = '123', 'let M' = '0'}}
77-
// expected-note@-1 {{where 'let N' = '0', 'let M' = '321'}}
79+
extension C where N == M { // expected-note {{where 'N' = '123', 'M' = '0'}}
80+
// expected-note@-1 {{where 'N' = '0', 'M' = '321'}}
7881
func nAndMAreBothEqual() {}
7982
}
8083

@@ -97,8 +100,8 @@ func testC3(with c: C<0, 321>) {
97100
}
98101

99102
func testC4<let T: Int>(with c: C<T, T>) {
100-
c.nIs123() // expected-error {{referencing instance method 'nIs123()' on 'C' requires the types 'let T' and '123' be equivalent}}
101-
c.mIs123() // expected-error {{referencing instance method 'mIs123()' on 'C' requires the types 'let T' and '321' be equivalent}}
103+
c.nIs123() // expected-error {{referencing instance method 'nIs123()' on 'C' requires the types 'T' and '123' be equivalent}}
104+
c.mIs123() // expected-error {{referencing instance method 'mIs123()' on 'C' requires the types 'T' and '321' be equivalent}}
102105
c.nAndMAreBothEqual() // OK
103106
}
104107

0 commit comments

Comments
 (0)