Skip to content

[Sema] Disallow stored properties to have uninhabited types #19992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1367,6 +1367,10 @@ ERROR(pattern_binds_no_variables,none,
"variables",
(unsigned))

ERROR(stored_property_no_uninhabited_type,none,
"stored property %0 cannot have uninhabited type %1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love this diagnostic because I don't think "uninhabited" is a great user-facing term, but I can't think of anything really better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I was looking for ways to reword this, but I couldn't come up with anything.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The compiler seems to define uninhabited types as either enums with no cases, or tuples containing uninhabited types. Could we just call it a "caseless enum" or "enum with no cases"? I suppose that might require a separate phrasing for tuples:

ERROR(stored_property_no_uninhabited_enum,none,
      "stored property %0 cannot have caseless enum %1", ...)
 ERROR(stored_property_no_uninhabited_tuple,none,
      "stored property %0 cannot have tuple type %1 containing caseless enum", ...)

Copy link
Contributor Author

@Azoy Azoy Oct 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this sound?

// Non-structurally
"%select{%select{variable|constant}0|property}1 %2 cannot have "
"enum type %3 with no cases"

// structurally
"%select{%select{variable|constant}0|property}1 %2 cannot have "
"tuple type %3 containing enum with no cases"

(Identifier, Type))

ERROR(nscoding_unstable_mangled_name,none,
"%select{private|fileprivate|nested|local}0 class %1 has an "
"unstable name when archiving via 'NSCoding'",
Expand Down
9 changes: 9 additions & 0 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2461,6 +2461,15 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
}
}

// Reject variable if it is a stored property with a structurally
// uninhabited type
if (VD->isInstanceMember() && VD->hasStorage() &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is limited to members; any stored type properties or globals or even locals would have the same problem.

VD->getInterfaceType()->isStructurallyUninhabited()) {
TC.diagnose(VD->getLoc(), diag::stored_property_no_uninhabited_type,
VD->getName(), VD->getInterfaceType());
VD->markInvalid();
}

if (!checkOverrides(VD)) {
// If a property has an override attribute but does not override
// anything, complain.
Expand Down
9 changes: 9 additions & 0 deletions test/decl/var/properties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1267,3 +1267,12 @@ class WeakFixItTest {
// expected-error @+1 {{'weak' variable should have optional type '(WFI_P1 & WFI_P2)?'}} {{18-18=(}} {{33-33=)?}}
weak var bar : WFI_P1 & WFI_P2
}

// SR-8811
// Stored properties cannot have uninhabited types

struct SR8811 {
var x: Never // expected-error {{stored property 'x' cannot have uninhabited type 'Never'}}

var y: (Int, Never, Bool) // expected-error {{stored property 'y' cannot have uninhabited type '(Int, Never, Bool)'}}
}