Skip to content

Commit ecbf057

Browse files
author
Greg Titus
committed
Always add specialize constrain, add note if it fails
1 parent 3810950 commit ecbf057

File tree

5 files changed

+20
-23
lines changed

5 files changed

+20
-23
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4628,8 +4628,6 @@ NOTE(duplicated_key_declared_here, none,
46284628
// Generic specializations
46294629
ERROR(cannot_explicitly_specialize_generic_function,none,
46304630
"cannot explicitly specialize a generic function", ())
4631-
ERROR(not_a_generic_definition,none,
4632-
"cannot specialize a non-generic definition", ())
46334631
ERROR(not_a_generic_type,none,
46344632
"cannot specialize non-generic type %0", (Type))
46354633
ERROR(protocol_declares_unknown_primary_assoc_type,none,

lib/Sema/CSGen.cpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1937,26 +1937,19 @@ namespace {
19371937

19381938
Type visitUnresolvedSpecializeExpr(UnresolvedSpecializeExpr *expr) {
19391939
auto baseTy = CS.getType(expr->getSubExpr());
1940-
Type specializeTy = nullptr;
1940+
Type specializeTy = baseTy;
19411941

1942-
if (baseTy->isTypeVariableOrMember() || baseTy->is<AnyFunctionType>()) {
1943-
specializeTy = baseTy;
1944-
} else if (AnyMetatypeType *meta = baseTy->getAs<AnyMetatypeType>()) {
1942+
if (AnyMetatypeType *meta = baseTy->getAs<AnyMetatypeType>()) {
19451943
specializeTy = meta->getInstanceType();
1946-
} else {
1947-
// FIXME: If the base type is a type variable, constrain it to a
1948-
// metatype of a bound generic type.
1949-
auto &de = CS.getASTContext().Diags;
1950-
de.diagnose(expr->getSubExpr()->getLoc(),
1951-
diag::not_a_generic_definition);
1952-
de.diagnose(expr->getLAngleLoc(),
1953-
diag::while_parsing_as_left_angle_bracket);
1954-
return Type();
19551944
}
19561945

19571946
auto *overloadLocator = CS.getConstraintLocator(expr->getSubExpr());
19581947
if (addSpecializationConstraint(overloadLocator, specializeTy,
19591948
expr->getUnresolvedParams())) {
1949+
// Diagnosis already happened, but add a bit of context
1950+
auto &de = CS.getASTContext().Diags;
1951+
de.diagnose(expr->getLAngleLoc(),
1952+
diag::while_parsing_as_left_angle_bracket);
19601953
return Type();
19611954
}
19621955

test/Parse/enum_element_pattern_swift4.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ enum E {
99

1010
static func testE(e: E) {
1111
switch e {
12-
case A<UndefinedTy>(): // expected-error {{cannot specialize a non-generic definition}}
12+
case A<UndefinedTy>(): // expected-error {{cannot find type 'UndefinedTy' in scope}}
1313
// expected-note@-1 {{while parsing this '<' as a type parameter bracket}}
1414
break
15-
case B<Int>(): // expected-error {{cannot specialize a non-generic definition}} expected-note {{while parsing this '<' as a type parameter bracket}}
15+
case B<Int>(): // expected-error {{cannot specialize non-generic type 'E'}}
16+
// expected-error@-1 {{cannot call value of non-function type 'E'}}
1617
break
1718
default:
1819
break;
@@ -22,10 +23,11 @@ enum E {
2223

2324
func testE(e: E) {
2425
switch e {
25-
case E.A<UndefinedTy>(): // expected-error {{cannot specialize a non-generic definition}}
26+
case E.A<UndefinedTy>(): // expected-error {{cannot find type 'UndefinedTy' in scope}}
2627
// expected-note@-1 {{while parsing this '<' as a type parameter bracket}}
2728
break
28-
case E.B<Int>(): // expected-error {{cannot specialize a non-generic definition}} expected-note {{while parsing this '<' as a type parameter bracket}}
29+
case E.B<Int>(): // expected-error {{cannot specialize non-generic type 'E'}}
30+
// expected-error@-1 {{cannot call value of non-function type 'E'}}
2931
break
3032
case .C(): // expected-error {{pattern with associated values does not match enum case 'C'}}
3133
// expected-note@-1 {{remove associated values to make the pattern match}} {{10-12=}}

test/Parse/generic_disambiguation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ var a, b, c, d : Int
2626
_ = a < b
2727
_ = (a < b, c > d)
2828
// Parses as generic because of lparen after '>'
29-
(a < b, c > (d)) // expected-error{{cannot specialize a non-generic definition}}
29+
(a < b, c > (d)) // expected-error{{cannot find type 'b' in scope}}
3030
// expected-note@-1 {{while parsing this '<' as a type parameter bracket}}
3131
// Parses as generic because of lparen after '>'
32-
(a<b, c>(d)) // expected-error{{cannot specialize a non-generic definition}}
32+
(a<b, c>(d)) // expected-error{{cannot find type 'b' in scope}}
3333
// expected-note@-1 {{while parsing this '<' as a type parameter bracket}}
3434
_ = a>(b)
3535
_ = a > (b)

test/Sema/generic-arg-list.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ func test(i: Int) {
2525
let _ = i.gen<Int> // expected-error {{'>' is not a postfix unary operator}}
2626
// expected-error@-1 {{binary operator '<' cannot be applied to operands of type '() -> ()' and '()'}}
2727
// expected-note@-2 {{overloads for '<' exist with these partially matching parameter lists:}}
28-
// expected-error@-1 {{function produces expected type 'Int'; did you mean to call it with '()'?}}
2928
let _ = i.bar<Int> // expected-error {{'>' is not a postfix unary operator}}
30-
3129
let _ = 0.bar<Int> // expected-error {{cannot specialize non-generic type 'Int'}}
3230
}
3331

@@ -37,3 +35,9 @@ extension Bool {
3735

3836
let _: () -> Bool = false.foo<Int> // expected-error {{cannot explicitly specialize a generic function}}
3937

38+
func foo(_ x: Int) {
39+
_ = {
40+
_ = x<String> // expected-error {{cannot specialize non-generic type 'Int'}}
41+
}
42+
}
43+

0 commit comments

Comments
 (0)