Skip to content

Commit a987ad6

Browse files
Merge pull request #40082 from AnthonyLatsis/invalid-ext-bind
AST: Make sure malscoped extensions get their extended nominal computed
2 parents 4f7f9f5 + 5215fe5 commit a987ad6

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
@@ -1255,9 +1255,14 @@ ExtensionDecl::takeConformanceLoaderSlow() {
12551255
}
12561256

12571257
NominalTypeDecl *ExtensionDecl::getExtendedNominal() const {
1258-
assert((hasBeenBound() || canNeverBeBound()) &&
1259-
"Extension must have already been bound (by bindExtensions)");
1260-
return ExtendedNominal.getPointer();
1258+
if (hasBeenBound()) {
1259+
return ExtendedNominal.getPointer();
1260+
} else if (canNeverBeBound()) {
1261+
return computeExtendedNominal();
1262+
}
1263+
1264+
llvm_unreachable(
1265+
"Extension must have already been bound (by bindExtensions)");
12611266
}
12621267

12631268
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)