Skip to content

Commit 9202120

Browse files
authored
Merge pull request #5600 from slavapestov/remove-nested-generics-flag
Remove -enable-experimental-nested-generic-types flag
2 parents 9d954f2 + b3bc749 commit 9202120

32 files changed

+265
-420
lines changed

Diff for: CHANGELOG.md

+20
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,26 @@ CHANGELOG
2020
Swift 3.1
2121
---------
2222

23+
* [SR-1446](https://bugs.swift.org/browse/SR-1446)
24+
25+
Nested types may now appear inside generic types, and nested types may have their own generic parameters:
26+
27+
```swift
28+
struct OuterNonGeneric {
29+
struct InnerGeneric<T> {}
30+
}
31+
32+
struct OuterGeneric<T> {
33+
struct InnerNonGeneric {}
34+
35+
struct InnerGeneric<T> {}
36+
}
37+
38+
extension OuterNonGeneric.InnerGeneric {}
39+
extension OuterGeneric.InnerNonGeneric {}
40+
extension OuterGeneric.InnerGeneric {}
41+
```
42+
2343
* [SR-1009](https://bugs.swift.org/browse/SR-1009):
2444

2545
Constrained extensions allow same-type constraints between generic parameters and concrete types. This enables you to create extensions, for example, on `Array` with `Int` elements:

Diff for: include/swift/AST/DiagnosticsSema.def

-9
Original file line numberDiff line numberDiff line change
@@ -1144,15 +1144,6 @@ ERROR(pattern_binds_no_variables,none,
11441144

11451145

11461146
// Generic types
1147-
ERROR(unsupported_generic_nested_in_type,none,
1148-
"generic type %0 cannot be nested in type %1",
1149-
(Identifier, Identifier))
1150-
ERROR(unsupported_type_nested_in_generic_type,none,
1151-
"type %0 cannot be nested in generic type %1",
1152-
(Identifier, Identifier))
1153-
ERROR(unsupported_type_nested_in_generic_extension,none,
1154-
"type %0 cannot be nested in extension of generic type %1",
1155-
(Identifier, Identifier))
11561147
ERROR(unsupported_type_nested_in_generic_function,none,
11571148
"type %0 cannot be nested in generic function %1",
11581149
(Identifier, Identifier))

Diff for: include/swift/Basic/LangOptions.h

-3
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,6 @@ namespace swift {
141141
/// \brief Enable experimental property behavior feature.
142142
bool EnableExperimentalPropertyBehaviors = false;
143143

144-
/// \brief Enable experimental nested generic types feature.
145-
bool EnableExperimentalNestedGenericTypes = false;
146-
147144
/// \brief Staging flag for class resilience, which we do not want to enable
148145
/// fully until more code is in place, to allow the standard library to be
149146
/// tested with value type resilience only.

Diff for: include/swift/Option/FrontendOptions.td

-4
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,6 @@ def enable_experimental_property_behaviors :
240240
Flag<["-"], "enable-experimental-property-behaviors">,
241241
HelpText<"Enable experimental property behaviors">;
242242

243-
def enable_experimental_nested_generic_types :
244-
Flag<["-"], "enable-experimental-nested-generic-types">,
245-
HelpText<"Enable experimental support for nested generic types">;
246-
247243
def disable_availability_checking : Flag<["-"],
248244
"disable-availability-checking">,
249245
HelpText<"Disable checking for potentially unavailable APIs">;

Diff for: lib/Basic/Demangle.cpp

+15-5
Original file line numberDiff line numberDiff line change
@@ -2396,6 +2396,11 @@ class NodePrinter {
23962396
case Node::Kind::VariadicTuple:
23972397
return true;
23982398

2399+
case Node::Kind::ProtocolList:
2400+
if (pointer->getChild(0)->getNumChildren() <= 1)
2401+
return true;
2402+
return false;
2403+
23992404
case Node::Kind::Allocator:
24002405
case Node::Kind::ArgumentTuple:
24012406
case Node::Kind::AssociatedTypeMetadataAccessor:
@@ -2473,7 +2478,6 @@ class NodePrinter {
24732478
case Node::Kind::PrefixOperator:
24742479
case Node::Kind::ProtocolConformance:
24752480
case Node::Kind::ProtocolDescriptor:
2476-
case Node::Kind::ProtocolList:
24772481
case Node::Kind::ProtocolWitness:
24782482
case Node::Kind::ProtocolWitnessTable:
24792483
case Node::Kind::ProtocolWitnessTableAccessor:
@@ -2686,8 +2690,6 @@ class NodePrinter {
26862690
} // end anonymous namespace
26872691

26882692
static bool isExistentialType(NodePointer node) {
2689-
assert(node->getKind() == Node::Kind::Type);
2690-
node = node->getChild(0);
26912693
return (node->getKind() == Node::Kind::ExistentialMetatype ||
26922694
node->getKind() == Node::Kind::ProtocolList);
26932695
}
@@ -3004,7 +3006,10 @@ void NodePrinter::print(NodePointer pointer, bool asContext, bool suppressType)
30043006
if (!pointer->hasChildren())
30053007
need_parens = true;
30063008
else {
3007-
Node::Kind child0_kind = pointer->getChild(0)->getChild(0)->getKind();
3009+
Node::Kind child0_kind = pointer->getChild(0)->getKind();
3010+
if (child0_kind == Node::Kind::Type)
3011+
child0_kind = pointer->getChild(0)->getChild(0)->getKind();
3012+
30083013
if (child0_kind != Node::Kind::VariadicTuple &&
30093014
child0_kind != Node::Kind::NonVariadicTuple)
30103015
need_parens = true;
@@ -3410,8 +3415,13 @@ void NodePrinter::print(NodePointer pointer, bool asContext, bool suppressType)
34103415
Printer << " ";
34113416
Idx++;
34123417
}
3413-
NodePointer type = pointer->getChild(Idx);
3418+
NodePointer type = pointer->getChild(Idx)->getChild(0);
3419+
bool needs_parens = !isSimpleType(type);
3420+
if (needs_parens)
3421+
Printer << "(";
34143422
print(type);
3423+
if (needs_parens)
3424+
Printer << ")";
34153425
if (isExistentialType(type)) {
34163426
Printer << ".Protocol";
34173427
} else {

Diff for: lib/Frontend/CompilerInvocation.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -804,9 +804,6 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
804804
Opts.EnableExperimentalPropertyBehaviors |=
805805
Args.hasArg(OPT_enable_experimental_property_behaviors);
806806

807-
Opts.EnableExperimentalNestedGenericTypes |=
808-
Args.hasArg(OPT_enable_experimental_nested_generic_types);
809-
810807
Opts.EnableClassResilience |=
811808
Args.hasArg(OPT_enable_class_resilience);
812809

Diff for: lib/Sema/TypeCheckDecl.cpp

-27
Original file line numberDiff line numberDiff line change
@@ -3969,33 +3969,6 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
39693969
return true;
39703970
}
39713971

3972-
if (!TC.Context.LangOpts.EnableExperimentalNestedGenericTypes) {
3973-
if (auto parent = dyn_cast<NominalTypeDecl>(DC)) {
3974-
if (NTD->getGenericParams())
3975-
TC.diagnose(NTD->getLoc(), diag::unsupported_generic_nested_in_type,
3976-
NTD->getName(),
3977-
parent->getName());
3978-
else
3979-
TC.diagnose(NTD->getLoc(),
3980-
diag::unsupported_type_nested_in_generic_type,
3981-
NTD->getName(),
3982-
parent->getName());
3983-
NTD->setInvalid();
3984-
return true;
3985-
} else if (auto ED = dyn_cast<ExtensionDecl>(DC)) {
3986-
auto *parent = ED->getAsNominalTypeOrNominalTypeExtensionContext();
3987-
if (parent == nullptr) {
3988-
/* Invalid extension -- diagnosed elsewhere */
3989-
return true;
3990-
}
3991-
3992-
TC.diagnose(NTD->getLoc(),
3993-
diag::unsupported_type_nested_in_generic_extension,
3994-
NTD->getName(),
3995-
parent->getName());
3996-
}
3997-
}
3998-
39993972
if (DC->isLocalContext() && DC->isGenericContext()) {
40003973
// A local generic context is a generic function.
40013974
if (auto AFD = dyn_cast<AbstractFunctionDecl>(DC)) {

Diff for: stdlib/public/core/HashedCollections.swift.gyb

+14-19
Original file line numberDiff line numberDiff line change
@@ -451,9 +451,6 @@ public struct Set<Element : Hashable> :
451451
internal typealias _VariantBuffer = _VariantSetBuffer<Element>
452452
internal typealias _NativeBuffer = _NativeSetBuffer<Element>
453453

454-
/// The index type for subscripting the set.
455-
public typealias Index = SetIndex<Element>
456-
457454
internal var _variantBuffer: _VariantBuffer
458455

459456
/// Creates a new, empty set with at least the specified number of elements'
@@ -1642,9 +1639,6 @@ public struct Dictionary<Key : Hashable, Value> :
16421639
/// key-value pair.
16431640
public typealias Element = (key: Key, value: Value)
16441641

1645-
/// The index type of a dictionary.
1646-
public typealias Index = DictionaryIndex<Key, Value>
1647-
16481642
internal var _variantBuffer: _VariantBuffer
16491643

16501644
/// Creates an empty dictionary.
@@ -4705,6 +4699,7 @@ internal enum ${Self}IndexRepresentation<${TypeParametersDecl}> {
47054699
case _cocoa(_CocoaIndex)
47064700
}
47074701

4702+
extension ${Self} {
47084703
%{
47094704
if Self == 'Set':
47104705
SubscriptingWithIndexDoc = """\
@@ -4726,12 +4721,7 @@ elif Self == 'Dictionary':
47264721
}%
47274722

47284723
${SubscriptingWithIndexDoc}
4729-
public struct ${Self}Index<${TypeParametersDecl}> :
4730-
Comparable {
4731-
// FIXME(ABI)#34 (Nesting types in generics): `DictionaryIndex` and `SetIndex` should
4732-
// be nested types (Dictionary.Index and Set.Index).
4733-
// rdar://problem/17002096
4734-
4724+
public struct Index : Comparable {
47354725
// Index for native buffer is efficient. Index for bridged NS${Self} is
47364726
// not, because neither NSEnumerator nor fast enumeration support moving
47374727
// backwards. Even if they did, there is another issue: NSEnumerator does
@@ -4750,12 +4740,12 @@ public struct ${Self}Index<${TypeParametersDecl}> :
47504740
internal var _value: ${Self}IndexRepresentation<${TypeParameters}>
47514741

47524742
@_versioned
4753-
internal static func _native(_ index: _NativeIndex) -> ${Self}Index {
4743+
internal static func _native(_ index: _NativeIndex) -> Index {
47544744
return ${Self}Index(_value: ._native(index))
47554745
}
47564746
#if _runtime(_ObjC)
47574747
@_versioned
4758-
internal static func _cocoa(_ index: _CocoaIndex) -> ${Self}Index {
4748+
internal static func _cocoa(_ index: _CocoaIndex) -> Index {
47594749
return ${Self}Index(_value: ._cocoa(index))
47604750
}
47614751
#endif
@@ -4789,10 +4779,15 @@ public struct ${Self}Index<${TypeParametersDecl}> :
47894779
#endif
47904780
}
47914781

4792-
extension ${Self}Index {
4782+
}
4783+
4784+
public typealias ${Self}Index<${TypeParametersDecl}> =
4785+
${Self}<${TypeParameters}>.Index
4786+
4787+
extension ${Self}.Index {
47934788
public static func == (
4794-
lhs: ${Self}Index<${TypeParameters}>,
4795-
rhs: ${Self}Index<${TypeParameters}>
4789+
lhs: ${Self}<${TypeParameters}>.Index,
4790+
rhs: ${Self}<${TypeParameters}>.Index
47964791
) -> Bool {
47974792
if _fastPath(lhs._guaranteedNative) {
47984793
return lhs._nativeIndex == rhs._nativeIndex
@@ -4813,8 +4808,8 @@ extension ${Self}Index {
48134808
}
48144809

48154810
public static func < (
4816-
lhs: ${Self}Index<${TypeParameters}>,
4817-
rhs: ${Self}Index<${TypeParameters}>
4811+
lhs: ${Self}<${TypeParameters}>.Index,
4812+
rhs: ${Self}<${TypeParameters}>.Index
48184813
) -> Bool {
48194814
if _fastPath(lhs._guaranteedNative) {
48204815
return lhs._nativeIndex < rhs._nativeIndex

0 commit comments

Comments
 (0)