Skip to content

Commit 1b51bc1

Browse files
committed
[Sema] Check existence of GenericSignature before getting GenericParamList from it
Fixes 2 compiler crasher.
1 parent 1325fdc commit 1b51bc1

File tree

4 files changed

+9
-3
lines changed

4 files changed

+9
-3
lines changed

lib/Sema/TypeCheckAttr.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1408,11 +1408,14 @@ void AttributeChecker::visitSpecializeAttr(SpecializeAttr *attr) {
14081408
auto *FD = cast<AbstractFunctionDecl>(D);
14091409
auto *genericSig = FD->getGenericSignature();
14101410

1411-
unsigned numTypes = genericSig->getGenericParams().size();
1411+
unsigned numTypes = 0;
1412+
if (genericSig)
1413+
numTypes = genericSig->getGenericParams().size();
14121414
if (numTypes != attr->getTypeLocs().size()) {
14131415
TC.diagnose(attr->getLocation(), diag::type_parameter_count_mismatch,
14141416
FD->getName(), numTypes, attr->getTypeLocs().size(),
14151417
numTypes > attr->getTypeLocs().size());
1418+
attr->setInvalid();
14161419
return;
14171420
}
14181421
// Initialize each TypeLoc in this attribute with a concrete type,

test/attr/attr_specialize.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public func twoGenericParams<T, U>(_ t: T, u: U) -> (T, U) {
2323
return (t, u)
2424
}
2525

26+
@_specialize(Int) // expected-error{{generic type 'nonGenericParam' specialized with too many type parameters (got 1, but expected 0)}}
27+
func nonGenericParam(x: Int) {}
28+
2629
// Specialize contextual types.
2730
// ----------------------------
2831

Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
// RUN: not --crash %target-sil-opt %s
1+
// RUN: not %target-sil-opt %s
22
// REQUIRES: asserts
33
@_specialize(h)func f
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
// See http://swift.org/LICENSE.txt for license information
66
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not --crash %target-swift-frontend %s -emit-ir
8+
// RUN: not %target-swift-frontend %s -emit-ir
99
@_specialize(s)func r

0 commit comments

Comments
 (0)