Skip to content

Commit 5bb6179

Browse files
committed
Mark decls that are illegal due to generic inside non-generic decl or non-generic inside generic decl with setInvalid(), and avoid assertions
trying to set the superclass on classes in such situations by setting the superclass of an invalid decl to the error type. This fixes a bunch of compiler crashes, and also changes some errors in other tests where the main error is the invalid declaration and now the downstream errors can be a bit different because the decl has been invalidated.
1 parent 1946f95 commit 5bb6179

15 files changed

+25
-21
lines changed

lib/AST/Decl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4757,7 +4757,7 @@ Type TypeBase::getSwiftNewtypeUnderlyingType() {
47574757
ClassDecl *ClassDecl::getSuperclassDecl() const {
47584758
if (auto superclass = getSuperclass())
47594759
return superclass->getClassOrBoundGenericClass();
4760-
return nullptr;
4760+
return nullptr;
47614761
}
47624762

47634763
void ClassDecl::setSuperclass(Type superclass) {

lib/Sema/ITCDecl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ void IterativeTypeChecker::processTypeCheckSuperclass(
153153
}
154154

155155
// Set the superclass type.
156+
if (classDecl->isInvalid())
157+
superclassType = ErrorType::get(getASTContext());
156158
classDecl->setSuperclass(superclassType);
157159
}
158160

lib/Sema/TypeCheckDecl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3418,6 +3418,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
34183418
NTD->getName(),
34193419
proto->getName());
34203420
}
3421+
NTD->setInvalid();
34213422
return true;
34223423
}
34233424

@@ -3432,6 +3433,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
34323433
diag::unsupported_type_nested_in_generic_type,
34333434
NTD->getName(),
34343435
parent->getName());
3436+
NTD->setInvalid();
34353437
return true;
34363438
} else if (auto ED = dyn_cast<ExtensionDecl>(DC)) {
34373439
auto *parent = ED->getAsNominalTypeOrNominalTypeExtensionContext();

test/Parse/matching_patterns.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ struct ContainsEnum {
151151

152152
func member(_ n: Possible<Int>) {
153153
switch n {
154-
case ContainsEnum.Possible<Int>.Naught,
154+
case ContainsEnum.Possible<Int>.Naught, // expected-error{{cannot specialize a non-generic definition}} expected-note {{while parsing this '<' as a type parameter bracket}}
155155
ContainsEnum.Possible.Naught,
156156
Possible<Int>.Naught,
157157
Possible.Naught,
@@ -163,7 +163,7 @@ struct ContainsEnum {
163163

164164
func nonmemberAccessesMemberType(_ n: ContainsEnum.Possible<Int>) {
165165
switch n {
166-
case ContainsEnum.Possible<Int>.Naught,
166+
case ContainsEnum.Possible<Int>.Naught, // expected-error{{cannot specialize a non-generic definition}} expected-note {{while parsing this '<' as a type parameter bracket}}
167167
.Naught:
168168
()
169169
}

test/Parse/type_expr.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ func genQualifiedType() {
105105
let _ : () = Gen<Foo>.Bar.meth()
106106
_ = Gen<Foo>.Bar.instMeth
107107

108-
_ = Gen<Foo>.Bar // expected-error{{expected member name or constructor call after type name}} expected-note{{add arguments}} {{19-19=()}} expected-note{{use '.self'}} {{19-19=.self}}
109-
_ = Gen<Foo>.Bar.dynamicType // expected-error{{'.dynamicType' is not allowed after a type name}} {{20-31=self}}
108+
_ = Gen<Foo>.Bar
109+
_ = Gen<Foo>.Bar.dynamicType
110110
}
111111

112112
func archetype<T: Zim>(_: T) {

test/decl/func/default-values.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ struct Outer<T> {
4848
}
4949
}
5050
}
51-
Outer<Int>.Inner.VeryInner() // expected-warning{{unused}}
52-
Outer<Int>.Inner.VeryInner(i: 12) // expected-warning{{unused}}
53-
Outer<Int>.Inner.VeryInner(f:12.5) // expected-warning{{unused}}
51+
Outer<Int>.Inner.VeryInner()
52+
Outer<Int>.Inner.VeryInner(i: 12)
53+
Outer<Int>.Inner.VeryInner(f:12.5)
5454
Outer<Int>.Inner.VeryInner.f()
5555
Outer<Int>.Inner.VeryInner.f(i: 12)
5656
Outer<Int>.Inner.VeryInner.f(f:12.5)

test/decl/nested/type_in_type.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ struct AnyStream<T : Sequence> {
128128
// Conform to the enumerable protocol.
129129
typealias Elements = StreamRange<T.Iterator>
130130
func getElements() -> Elements {
131-
return Elements(index: 0, elements: input.makeIterator())
131+
return Elements(index: 0, elements: input.makeIterator()) // expected-error {{'AnyStream<T>.StreamRange<T.Iterator>' cannot be constructed because it has no accessible initializers}}
132132
}
133133
}
134134

@@ -177,7 +177,7 @@ extension Bar {
177177
class X6<T> {
178178
let d: D<T>
179179
init(_ value: T) {
180-
d = D(value)
180+
d = D(value) // expected-error{{'<<error type>>' cannot be constructed because it has no accessible initializers}}
181181
}
182182
class D<T2> { // expected-error{{generic type 'D' cannot be nested in type 'X6'}}
183183
init(_ value: T2) {}
@@ -203,7 +203,7 @@ struct GS<T> {
203203
struct NestedGeneric<U> { // expected-note{{generic type 'NestedGeneric' declared here}} // expected-error{{generic type 'NestedGeneric' cannot be nested in type 'GS'}}
204204
func fff() -> (GS, NestedGeneric) {
205205
let gs = GS()
206-
let ns = NestedGeneric()
206+
let ns = NestedGeneric() // expected-error {{'GS<T>.NestedGeneric<U>' cannot be constructed because it has no accessible initializers}}
207207
return (gs, ns)
208208
}
209209
}
@@ -245,7 +245,7 @@ func useNested(_ ii: Int, hni: HasNested<Int>,
245245
typealias InnerI = HasNested<Int>.Inner
246246
var innerI = InnerI(5)
247247
typealias InnerF = HasNested<Float>.Inner
248-
var innerF : InnerF = innerI // expected-error{{cannot convert value of type 'InnerI' (aka 'HasNested<Int>.Inner') to specified type 'InnerF' (aka 'HasNested<Float>.Inner')}}
248+
var innerF : InnerF = innerI
249249

250250
_ = innerI.identity(i)
251251
i = innerI.identity(i)
@@ -265,6 +265,6 @@ func useNested(_ ii: Int, hni: HasNested<Int>,
265265
var ids = xis.g(1, u: "Hello", v: 3.14159)
266266
ids = (2, "world", 2.71828)
267267

268-
xis = xfs // expected-error{{cannot assign value of type 'HasNested<Float>.InnerGeneric<String>' to type 'HasNested<Int>.InnerGeneric<String>'}}
268+
xis = xfs
269269
}
270270

validation-test/compiler_crashers/28291-swift-constraints-constraintsystem-comparesolutions.swift renamed to validation-test/compiler_crashers_fixed/28291-swift-constraints-constraintsystem-comparesolutions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
// See http://swift.org/LICENSE.txt for license information
66
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not --crash %target-swift-frontend %s -parse
8+
// RUN: not %target-swift-frontend %s -parse
99
// REQUIRES: asserts
1010
class T,struct A{func b{}struct A{class B<T}func b:A.B{var _=b<T

validation-test/compiler_crashers/28319-swift-typechecker-checkconformance.swift renamed to validation-test/compiler_crashers_fixed/28319-swift-typechecker-checkconformance.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
// See http://swift.org/LICENSE.txt for license information
66
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not --crash %target-swift-frontend %s -parse
8+
// RUN: not %target-swift-frontend %s -parse
99
// REQUIRES: asserts
1010
protocol A{enum B:A{let:e var _=B}typealias e

validation-test/compiler_crashers/28331-swift-createdesignatedinitoverride.swift renamed to validation-test/compiler_crashers_fixed/28331-swift-createdesignatedinitoverride.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
// See http://swift.org/LICENSE.txt for license information
66
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not --crash %target-swift-frontend %s -parse
8+
// RUN: not %target-swift-frontend %s -parse
99
let:{{class a{enum S<U:a{class B:a{init}}class a

0 commit comments

Comments
 (0)