Skip to content

Commit 5215fe5

Browse files
committed
AST: Make sure malscoped extensions get their extended nominal computed
1 parent 3290833 commit 5215fe5

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

lib/AST/Decl.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,9 +1245,14 @@ ExtensionDecl::takeConformanceLoaderSlow() {
12451245
}
12461246

12471247
NominalTypeDecl *ExtensionDecl::getExtendedNominal() const {
1248-
assert((hasBeenBound() || canNeverBeBound()) &&
1249-
"Extension must have already been bound (by bindExtensions)");
1250-
return ExtendedNominal.getPointer();
1248+
if (hasBeenBound()) {
1249+
return ExtendedNominal.getPointer();
1250+
} else if (canNeverBeBound()) {
1251+
return computeExtendedNominal();
1252+
}
1253+
1254+
llvm_unreachable(
1255+
"Extension must have already been bound (by bindExtensions)");
12511256
}
12521257

12531258
NominalTypeDecl *ExtensionDecl::computeExtendedNominal() const {

test/decl/ext/extensions.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ protocol NestingTest5 {
3737
func nestingTest6() {
3838
extension Foo {} // expected-error {{declaration is only valid at file scope}}
3939
}
40+
extension Array {
41+
func foo() {
42+
extension Array { // expected-error {{declaration is only valid at file scope}}
43+
// FIXME: Confusing error
44+
// expected-error@-2 {{constrained extension must be declared on the unspecialized generic type 'Array' with constraints specified by a 'where' clause}}
45+
}
46+
}
47+
}
4048

4149
//===--- Test that we only allow extensions only for nominal types.
4250

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: not %target-swift-frontend -typecheck %s
2+
3+
public extension Array {
4+
func chunked(into size: Int) -> [[Element]] {
5+
return stride(from: 0, to: count, by: size).map { elt in
6+
self[elt
7+
8+
9+
public extension Array where Element == Item {
10+
mutating func toggle(item: Item) -> Bool {
11+
_ = contains(item)
12+
}
13+
}

0 commit comments

Comments
 (0)