Skip to content

Commit 7991aec

Browse files
committed
Disallow stored properties to have uninhabited types
Reword comment Add tuple test case
1 parent c858372 commit 7991aec

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,6 +1367,10 @@ 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))
1373+
13701374
ERROR(nscoding_unstable_mangled_name,none,
13711375
"%select{private|fileprivate|nested|local}0 class %1 has an "
13721376
"unstable name when archiving via 'NSCoding'",

lib/Sema/TypeCheckDecl.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2461,6 +2461,15 @@ 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() &&
2467+
VD->getInterfaceType()->isStructurallyUninhabited()) {
2468+
TC.diagnose(VD->getLoc(), diag::stored_property_no_uninhabited_type,
2469+
VD->getName(), VD->getInterfaceType());
2470+
VD->markInvalid();
2471+
}
2472+
24642473
if (!checkOverrides(VD)) {
24652474
// If a property has an override attribute but does not override
24662475
// anything, complain.

test/decl/var/properties.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,3 +1267,12 @@ class WeakFixItTest {
12671267
// expected-error @+1 {{'weak' variable should have optional type '(WFI_P1 & WFI_P2)?'}} {{18-18=(}} {{33-33=)?}}
12681268
weak var bar : WFI_P1 & WFI_P2
12691269
}
1270+
1271+
// SR-8811
1272+
// Stored properties cannot have uninhabited types
1273+
1274+
struct SR8811 {
1275+
var x: Never // expected-error {{stored property 'x' cannot have uninhabited type 'Never'}}
1276+
1277+
var y: (Int, Never, Bool) // expected-error {{stored property 'y' cannot have uninhabited type '(Int, Never, Bool)'}}
1278+
}

0 commit comments

Comments
 (0)