Skip to content

Commit e773f68

Browse files
committed
Sema: Diagnose nested type in tuple extension
1 parent 4b8bd6a commit e773f68

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2363,7 +2363,8 @@ ERROR(tuple_extension_extra_requirement,none,
23632363
(Type, unsigned, Type))
23642364
ERROR(tuple_extension_missing_requirement,none,
23652365
"tuple extension must require that %0 conforms to %1", (Type, Type))
2366-
2366+
ERROR(tuple_extension_nested_type,none,
2367+
"type %0 cannot be nested in tuple extension", (const NominalTypeDecl *))
23672368
ERROR(extension_metatype,none,
23682369
"cannot extend a metatype %0", (Type))
23692370
ERROR(extension_placeholder,none,

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,27 +2655,37 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
26552655

26562656
void checkUnsupportedNestedType(NominalTypeDecl *NTD) {
26572657
auto *DC = NTD->getDeclContext();
2658+
2659+
// We don't allow nested types inside inlinable contexts.
26582660
auto kind = DC->getFragileFunctionKind();
26592661
if (kind.kind != FragileFunctionKind::None) {
26602662
NTD->diagnose(diag::local_type_in_inlinable_function, NTD->getName(),
26612663
kind.getSelector());
26622664
}
26632665

2664-
// We don't support protocols outside the top level of a file.
2665-
if (isa<ProtocolDecl>(NTD) &&
2666-
!NTD->getParent()->isModuleScopeContext()) {
2667-
NTD->diagnose(diag::unsupported_nested_protocol, NTD);
2668-
NTD->setInvalid();
2669-
return;
2670-
}
2666+
if (auto *parentDecl = DC->getSelfNominalTypeDecl()) {
2667+
// We don't allow types to be nested within a tuple extension.
2668+
if (isa<BuiltinTupleDecl>(parentDecl)) {
2669+
NTD->diagnose(diag::tuple_extension_nested_type, NTD);
2670+
return;
2671+
}
26712672

2672-
// We don't support nested types in protocols.
2673-
if (auto proto = DC->getSelfProtocolDecl()) {
2674-
if (DC->getExtendedProtocolDecl()) {
2675-
NTD->diagnose(diag::unsupported_type_nested_in_protocol_extension, NTD,
2676-
proto);
2677-
} else {
2678-
NTD->diagnose(diag::unsupported_type_nested_in_protocol, NTD, proto);
2673+
// We don't support protocols outside the top level of a file.
2674+
if (isa<ProtocolDecl>(NTD) &&
2675+
!DC->isModuleScopeContext()) {
2676+
NTD->diagnose(diag::unsupported_nested_protocol, NTD);
2677+
NTD->setInvalid();
2678+
return;
2679+
}
2680+
2681+
// We don't support nested types in protocols.
2682+
if (auto proto = dyn_cast<ProtocolDecl>(parentDecl)) {
2683+
if (DC->getExtendedProtocolDecl()) {
2684+
NTD->diagnose(diag::unsupported_type_nested_in_protocol_extension, NTD,
2685+
proto);
2686+
} else {
2687+
NTD->diagnose(diag::unsupported_type_nested_in_protocol, NTD, proto);
2688+
}
26792689
}
26802690
}
26812691

test/Generics/tuple-conformances.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
extension () {
77
// expected-error@-1 {{tuple extension must be written as extension of '(repeat each Element)'}}
88
// expected-error@-2 {{tuple extension must declare conformance to exactly one protocol}}
9+
10+
struct Nested {}
11+
// expected-error@-1 {{type 'Nested' cannot be nested in tuple extension}}
912
}
1013

1114
typealias Tuple<each Element> = (repeat each Element)

0 commit comments

Comments
 (0)