Skip to content

Commit 18d2cc5

Browse files
committed
Uninhabited downgrade to warning
1 parent d62a018 commit 18d2cc5

File tree

9 files changed

+17
-69
lines changed

9 files changed

+17
-69
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,15 +1371,6 @@ ERROR(pattern_binds_no_variables,none,
13711371
"variables",
13721372
(unsigned))
13731373

1374-
ERROR(pattern_no_uninhabited_type,none,
1375-
"%select{%select{variable|constant}0|stored property}1 %2 cannot have "
1376-
"enum type %3 with no cases",
1377-
(bool, bool, Identifier, Type))
1378-
ERROR(pattern_no_uninhabited_tuple_type,none,
1379-
"%select{%select{variable|constant}0|stored property}1 %2 cannot have "
1380-
"tuple type %3 containing enum with no cases",
1381-
(bool, bool, Identifier, Type))
1382-
13831374
ERROR(nscoding_unstable_mangled_name,none,
13841375
"%select{private|fileprivate|nested|local}0 class %1 has an "
13851376
"unstable name when archiving via 'NSCoding'",

lib/Sema/TypeCheckDecl.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2541,25 +2541,6 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
25412541
}
25422542
}
25432543

2544-
// Reject variable if it is a stored property with an uninhabited type
2545-
if (VD->hasStorage() &&
2546-
VD->getInterfaceType()->isStructurallyUninhabited()) {
2547-
auto uninhabitedTypeDiag = diag::pattern_no_uninhabited_type;
2548-
2549-
if (VD->getInterfaceType()->is<TupleType>()) {
2550-
uninhabitedTypeDiag = diag::pattern_no_uninhabited_tuple_type;
2551-
} else {
2552-
assert((VD->getInterfaceType()->is<EnumType>() ||
2553-
VD->getInterfaceType()->is<BoundGenericEnumType>()) &&
2554-
"unknown structurally uninhabited type");
2555-
}
2556-
2557-
TC.diagnose(VD->getLoc(), uninhabitedTypeDiag, VD->isLet(),
2558-
VD->isInstanceMember(), VD->getName(),
2559-
VD->getInterfaceType());
2560-
VD->markInvalid();
2561-
}
2562-
25632544
if (!checkOverrides(VD)) {
25642545
// If a property has an override attribute but does not override
25652546
// anything, complain.

lib/Sema/TypeCheckPattern.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,11 +1091,12 @@ bool TypeChecker::coercePatternToType(Pattern *&P, TypeResolution resolution,
10911091
var->getTypeLoc().setType(var->getType());
10921092

10931093
// If we are inferring a variable to have type AnyObject.Type,
1094-
// "()", or optional thereof, emit a diagnostic. In the first 2 cases, the
1095-
// coder probably forgot a cast and expected a concrete type. In the later
1096-
// case, they probably didn't mean to bind to a variable, or there is some
1097-
// other bug. We always tell them that they can silence the warning with an
1098-
// explicit type annotation (and provide a fixit) as a note.
1094+
// "()", an uninhabited type, or optional thereof, emit a diagnostic.
1095+
// In the first 2 cases, the coder probably forgot a cast and expected a
1096+
// concrete type. In the later case, they probably didn't mean to bind to
1097+
// a variable, or there is some other bug. We always tell them that they
1098+
// can silence the warning with an explicit type annotation
1099+
// (and provide a fixit) as a note.
10991100
Type diagTy = type->getOptionalObjectType();
11001101
if (!diagTy) diagTy = type;
11011102

@@ -1108,6 +1109,8 @@ bool TypeChecker::coercePatternToType(Pattern *&P, TypeResolution resolution,
11081109
} else if (auto MTT = diagTy->getAs<AnyMetatypeType>()) {
11091110
if (MTT->getInstanceType()->isAnyObject())
11101111
shouldRequireType = true;
1112+
} else if (diagTy->isStructurallyUninhabited()) {
1113+
shouldRequireType = true;
11111114
}
11121115

11131116
if (shouldRequireType &&

test/IRGen/generic_enums.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,4 @@ struct Bar<A1, A2> {
55
var b: Foo<A1>
66
}
77

8-
enum Foo<A>{
9-
case bar
10-
}
8+
enum Foo<A> {}

test/Reflection/Inputs/TypeLowering.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,8 @@ public struct MetatypeStruct {
168168
public let abstractMetatype: MetadataHolder<BasicStruct.Type, BasicStruct>
169169
}
170170

171-
// We don't allow stored properties to have uninhabited types now, but make a
172-
// wrapper over one to continue testing this
173171
public enum EmptyEnum {}
174172

175-
public struct EmptyEnumWrapper<T> {
176-
public var value: T
177-
}
178-
179173
public enum NoPayloadEnum {
180174
case A
181175
case B
@@ -224,7 +218,7 @@ public enum MultiPayloadGenericDynamic<T, U> {
224218
}
225219

226220
public struct EnumStruct {
227-
public let empty: EmptyEnumWrapper<EmptyEnum>
221+
public let empty: EmptyEnum
228222
public let noPayload: NoPayloadEnum
229223
public let sillyNoPayload: SillyNoPayloadEnum
230224
public let singleton: SingletonEnum

test/attr/attr_noreturn.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ func noReturn3(_: Int)
3434
// expected-error@-2 {{'@noreturn' has been removed; functions that never return should have a return type of 'Never' instead}}{{1-11=}}{{53-56=Never}}
3535

3636
// Test that error recovery gives us the 'Never' return type
37-
let x: Never = noReturn1(0)
38-
// expected-error@-1 {{constant 'x' cannot have enum type 'Never' with no cases}}
37+
let x: Never = noReturn1(0) // No error
3938

4039
// @noreturn in function type declarations
4140
let valueNoReturn: @noreturn () -> ()

test/attr/attr_objc.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -941,12 +941,11 @@ class infer_instanceVar1 {
941941
// expected-note@-2 {{Swift structs cannot be represented in Objective-C}}
942942

943943
var var_PlainEnum: PlainEnum
944-
// expected-error@-1 {{stored property 'var_PlainEnum' cannot have enum type 'PlainEnum' with no cases}}
944+
// CHECK-LABEL: {{^}} var var_PlainEnum: PlainEnum
945945

946946
@objc var var_PlainEnum_: PlainEnum
947947
// expected-error@-1 {{property cannot be marked @objc because its type cannot be represented in Objective-C}}
948948
// expected-note@-2 {{non-'@objc' enums cannot be represented in Objective-C}}
949-
// expected-error@-3 {{stored property 'var_PlainEnum_' cannot have enum type 'PlainEnum' with no cases}}
950949

951950
var var_PlainProtocol: PlainProtocol
952951
// CHECK-LABEL: {{^}} var var_PlainProtocol: PlainProtocol
@@ -1272,7 +1271,6 @@ class infer_instanceVar1 {
12721271
// expected-error@-1 {{'unowned' may only be applied to class and class-bound protocol types, not 'PlainStruct'}}
12731272
unowned var var_Unowned_bad3: PlainEnum
12741273
// expected-error@-1 {{'unowned' may only be applied to class and class-bound protocol types, not 'PlainEnum'}}
1275-
// expected-error@-2 {{stored property 'var_Unowned_bad3' cannot have enum type 'PlainEnum' with no cases}}
12761274
unowned var var_Unowned_bad4: String
12771275
// expected-error@-1 {{'unowned' may only be applied to class and class-bound protocol types, not 'String'}}
12781276
// CHECK-NOT: @objc{{.*}}Unowned_fail

test/decl/var/properties.swift

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,24 +1268,8 @@ class WeakFixItTest {
12681268
weak var bar : WFI_P1 & WFI_P2
12691269
}
12701270

1271-
// SR-8811
1272-
// Stored properties cannot have uninhabited types
1271+
// SR-8811 (Warning)
12731272

1274-
struct SR8811 {
1275-
var x: Never // expected-error {{stored property 'x' cannot have enum type 'Never' with no cases}}
1276-
1277-
var y: (Int, Never, Bool) // expected-error {{stored property 'y' cannot have tuple type '(Int, Never, Bool)' containing enum with no cases}}
1278-
}
1279-
1280-
let sr8811x: Never // expected-error {{constant 'sr8811x' cannot have enum type 'Never' with no cases}}
1281-
1282-
var sr8811y: (Int, Never) // expected-error {{variable 'sr8811y' cannot have tuple type '(Int, Never)' containing enum with no cases}}
1283-
1284-
// Ok
1285-
var sr8811z: Never {
1286-
return fatalError()
1287-
}
1288-
1289-
enum SR8811EmptyGenericEnum<A> {}
1273+
let sr8811x = fatalError() // expected-warning {{variable 'sr8811x' inferred to have type 'Never', which may be unexpected}}
12901274

1291-
let sr8811z: SR8811EmptyGenericEnum<Int> // expected-error {{constant 'sr8811z' cannot have enum type 'SR8811EmptyGenericEnum<Int>' with no cases}}
1275+
let sr8811y: Never = fatalError() // Ok

test/expr/expressions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,9 @@ func conversionTest(_ a: inout Double, b: inout Int) {
584584
var pi_f3 = float.init(getPi()) // expected-error {{ambiguous use of 'init(_:)'}}
585585
var pi_f4 = float.init(pi_f)
586586

587-
var e = Empty(f) // expected-error {{variable 'e' cannot have enum type 'Empty' with no cases}}
587+
var e = Empty(f) // expected-warning {{variable 'e' inferred to have type 'Empty', which may be unexpected}} expected-note {{add an explicit type annotation to silence this warning}}
588588
var e2 = Empty(d) // expected-error{{cannot convert value of type 'Double' to expected argument type 'Float'}}
589-
var e3 = Empty(Float(d)) // expected-error {{variable 'e3' cannot have enum type 'Empty' with no cases}}
589+
var e3 = Empty(Float(d)) // expected-warning {{variable 'e3' inferred to have type 'Empty', which may be unexpected}} expected-note {{add an explicit type annotation to silence this warning}}
590590
}
591591

592592
struct Rule { // expected-note {{'init(target:dependencies:)' declared here}}

0 commit comments

Comments
 (0)