Skip to content

Commit 9d55400

Browse files
author
Greg Titus
committed
Recheck UnresolvedSpecializeExpr after type variables are gone.
1 parent 0350023 commit 9d55400

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

lib/Sema/CSApply.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3225,8 +3225,18 @@ namespace {
32253225
}
32263226

32273227
Expr *visitUnresolvedSpecializeExpr(UnresolvedSpecializeExpr *expr) {
3228-
// Our specializations should have resolved the subexpr to the right type.
3229-
return expr->getSubExpr();
3228+
// Our specializations should have resolved the subexpr to the right type,
3229+
// recheck for validity.
3230+
auto subExpr = expr->getSubExpr();
3231+
auto ty = subExpr->getType();
3232+
if (ty && !ty->is<AnyMetatypeType>()) {
3233+
auto &de = cs.getASTContext().Diags;
3234+
de.diagnose(subExpr->getLoc(), diag::not_a_generic_definition);
3235+
de.diagnose(expr->getLAngleLoc(),
3236+
diag::while_parsing_as_left_angle_bracket);
3237+
return nullptr;
3238+
}
3239+
return subExpr;
32303240
}
32313241

32323242
Expr *visitMemberRefExpr(MemberRefExpr *expr) {

test/Sema/generic-arg-list.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
extension Int {
4+
func foo() -> Int {}
5+
var bar: Int {
6+
get {}
7+
}
8+
9+
func baz() -> Int {}
10+
func baz(_ x: Int = 0) -> Int {}
11+
}
12+
13+
func test(i: Int) {
14+
// Error
15+
let _ = i.foo<Int>() // expected-error {{cannot explicitly specialize a generic function}}
16+
// expected-note@-1 {{while parsing this '<' as a type parameter bracket}}
17+
let _ = i.bar<Int> // expected-error {{'>' is not a postfix unary operator}}
18+
19+
// Now also Error
20+
let _ = 0.foo<Int>() // expected-error {{cannot specialize a non-generic definition}}
21+
// expected-note@-1 {{while parsing this '<' as a type parameter bracket}}
22+
let _ = 0.bar<Int> // expected-error {{'>' is not a postfix unary operator}}
23+
24+
let _ = i.baz<Int> // expected-error {{cannot specialize a non-generic definition}}
25+
// expected-note@-1 {{while parsing this '<' as a type parameter bracket}}
26+
}

0 commit comments

Comments
 (0)