Skip to content

Commit f085c28

Browse files
committed
Not just instance members & better diagnostic
fix some tests
1 parent 7991aec commit f085c28

File tree

7 files changed

+44
-13
lines changed

7 files changed

+44
-13
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,9 +1367,14 @@ ERROR(pattern_binds_no_variables,none,
13671367
"variables",
13681368
(unsigned))
13691369

1370-
ERROR(stored_property_no_uninhabited_type,none,
1371-
"stored property %0 cannot have uninhabited type %1",
1372-
(Identifier, Type))
1370+
ERROR(pattern_no_uninhabited_type,none,
1371+
"%select{%select{variable|constant}0|stored property}1 %2 cannot have "
1372+
"enum type %3 with no cases",
1373+
(bool, bool, Identifier, Type))
1374+
ERROR(pattern_no_uninhabited_tuple_type,none,
1375+
"%select{%select{variable|constant}0|stored property}1 %2 cannot have "
1376+
"tuple type %3 containing enum with no cases",
1377+
(bool, bool, Identifier, Type))
13731378

13741379
ERROR(nscoding_unstable_mangled_name,none,
13751380
"%select{private|fileprivate|nested|local}0 class %1 has an "

lib/Sema/TypeCheckDecl.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2461,12 +2461,18 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
24612461
}
24622462
}
24632463

2464-
// Reject variable if it is a stored property with a structurally
2465-
// uninhabited type
2466-
if (VD->isInstanceMember() && VD->hasStorage() &&
2464+
// Reject variable if it is a stored property with an uninhabited type
2465+
if (VD->hasStorage() &&
24672466
VD->getInterfaceType()->isStructurallyUninhabited()) {
2468-
TC.diagnose(VD->getLoc(), diag::stored_property_no_uninhabited_type,
2469-
VD->getName(), VD->getInterfaceType());
2467+
auto uninhabitedTypeDiag = diag::pattern_no_uninhabited_type;
2468+
2469+
if (VD->getInterfaceType()->is<TupleType>()) {
2470+
uninhabitedTypeDiag = diag::pattern_no_uninhabited_tuple_type;
2471+
}
2472+
2473+
TC.diagnose(VD->getLoc(), uninhabitedTypeDiag, VD->isLet(),
2474+
VD->isInstanceMember(), VD->getName(),
2475+
VD->getInterfaceType());
24702476
VD->markInvalid();
24712477
}
24722478

test/IRGen/generic_enums.swift

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

88
enum Foo<A>{
9+
case bar
910
}

test/Reflection/Inputs/TypeLowering.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,14 @@ public struct MetatypeStruct {
144144
public let abstractMetatype: MetadataHolder<BasicStruct.Type, BasicStruct>
145145
}
146146

147+
// We don't allow stored properties to have uninhabited types now, but make a
148+
// wrapper over one to continue testing this
147149
public enum EmptyEnum {}
148150

151+
public struct EmptyEnumWrapper<T> {
152+
public var value: T
153+
}
154+
149155
public enum NoPayloadEnum {
150156
case A
151157
case B
@@ -194,7 +200,7 @@ public enum MultiPayloadGenericDynamic<T, U> {
194200
}
195201

196202
public struct EnumStruct {
197-
public let empty: EmptyEnum
203+
public let empty: EmptyEnumWrapper<EmptyEnum>
198204
public let noPayload: NoPayloadEnum
199205
public let sillyNoPayload: SillyNoPayloadEnum
200206
public let singleton: SingletonEnum

test/Reflection/typeref_lowering.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,9 @@
926926
// CHECK-64: (struct TypeLowering.EnumStruct)
927927
// CHECK-64-NEXT: (struct size=81 alignment=8 stride=88 num_extra_inhabitants=[[PTR_XI]]
928928
// CHECK-64-NEXT: (field name=empty offset=0
929-
// CHECK-64-NEXT: (no_payload_enum size=0 alignment=1 stride=1 num_extra_inhabitants=0))
929+
// CHECK-64-NEXT: (struct size=0 alignment=1 stride=1 num_extra_inhabitants=0
930+
// CHECK-64-NEXT: (field name=value offset=0
931+
// CHECK-64-NEXT: (no_payload_enum size=0 alignment=1 stride=1 num_extra_inhabitants=0))))
930932
// CHECK-64-NEXT: (field name=noPayload offset=0
931933
// CHECK-64-NEXT: (no_payload_enum size=1 alignment=1 stride=1 num_extra_inhabitants=0))
932934
// CHECK-64-NEXT: (field name=sillyNoPayload offset=1

test/attr/attr_objc.swift

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

943943
var var_PlainEnum: PlainEnum
944-
// CHECK-LABEL: {{^}} var var_PlainEnum: PlainEnum
944+
// expected-error@-1 {{stored property 'var_PlainEnum' cannot have enum type 'PlainEnum' with no cases}}
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}}
949950

950951
var var_PlainProtocol: PlainProtocol
951952
// CHECK-LABEL: {{^}} var var_PlainProtocol: PlainProtocol
@@ -1271,6 +1272,7 @@ class infer_instanceVar1 {
12711272
// expected-error@-1 {{'unowned' may only be applied to class and class-bound protocol types, not 'PlainStruct'}}
12721273
unowned var var_Unowned_bad3: PlainEnum
12731274
// 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}}
12741276
unowned var var_Unowned_bad4: String
12751277
// expected-error@-1 {{'unowned' may only be applied to class and class-bound protocol types, not 'String'}}
12761278
// CHECK-NOT: @objc{{.*}}Unowned_fail

test/decl/var/properties.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,16 @@ class WeakFixItTest {
12721272
// Stored properties cannot have uninhabited types
12731273

12741274
struct SR8811 {
1275-
var x: Never // expected-error {{stored property 'x' cannot have uninhabited type 'Never'}}
1275+
var x: Never // expected-error {{stored property 'x' cannot have enum type 'Never' with no cases}}
12761276

1277-
var y: (Int, Never, Bool) // expected-error {{stored property 'y' cannot have uninhabited type '(Int, Never, Bool)'}}
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()
12781287
}

0 commit comments

Comments
 (0)