Skip to content

Commit e0ed4a1

Browse files
committed
Sema: Diagnose types nested inside closures in generic context
Neither of the following is supported: func g<T>(_: T) { _ = { struct S {} } } struct G<T> { let fn = { struct S {} } } Even though nested generic types are supported, the second example is more like a type inside a function than a type inside a type, because 'S' has no parent type. Technically this is source-breaking but since neither SILGen nor IRGen knew how to generate code for these I doubt anything worked.
1 parent f509330 commit e0ed4a1

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,9 @@ ERROR(pattern_binds_no_variables,none,
11781178
ERROR(unsupported_type_nested_in_generic_function,none,
11791179
"type %0 cannot be nested in generic function %1",
11801180
(Identifier, Identifier))
1181+
ERROR(unsupported_type_nested_in_generic_closure,none,
1182+
"type %0 cannot be nested in closure in generic context",
1183+
(Identifier))
11811184
ERROR(unsupported_type_nested_in_protocol,none,
11821185
"type %0 cannot be nested in protocol %1",
11831186
(Identifier, Identifier))

lib/Sema/TypeCheckDecl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3974,6 +3974,10 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
39743974
diag::unsupported_type_nested_in_generic_function,
39753975
NTD->getName(),
39763976
AFD->getName());
3977+
} else {
3978+
TC.diagnose(NTD->getLoc(),
3979+
diag::unsupported_type_nested_in_generic_closure,
3980+
NTD->getName());
39773981
}
39783982
}
39793983
}

test/decl/nested/type_in_function.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ func outerGenericFunction<T>(_ t: T) {
2121
func nonGenericMethod(_ t: T, u: U) {}
2222
func genericMethod<V>(_ t: T, u: U) -> V where V : Racoon, V.Stripes == T {}
2323
}
24+
25+
_ = {
26+
struct ConcreteInClosure { // expected-error{{type 'ConcreteInClosure' cannot be nested in closure in generic context}}
27+
}
28+
29+
struct GenericInClosure<U> { // expected-error{{type 'GenericInClosure' cannot be nested in closure in generic context}}
30+
}
31+
}
2432
}
2533

2634
class OuterNonGenericClass {

0 commit comments

Comments
 (0)