-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
[Sema] Disallow stored properties to have uninhabited types #19992
Conversation
Reword comment Add tuple test case
@swift-ci please test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for picking this up!
lib/Sema/TypeCheckDecl.cpp
Outdated
@@ -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() && |
There was a problem hiding this comment.
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.
@@ -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", |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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", ...)
There was a problem hiding this comment.
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"
Build failed |
Build failed |
@jrose-apple how should we handle |
The condition @slavapestov Can speak about the empty member in |
I think we can still contrive an uninhabited local by using generics:
(Slava is out right now.) |
fix some tests
@jrose-apple how does this look? |
|
||
if (VD->getInterfaceType()->is<TupleType>()) { | ||
uninhabitedTypeDiag = diag::pattern_no_uninhabited_tuple_type; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add an assertion that the type is an enum otherwise? That way we'll know if we ever add any new ways for something to be structurally uninhabited.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heh. It could be a BoundGenericEnumType too…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops 😅
Add generic enum test case
Looks pretty good. I still don't love the diagnostic text, but I can't really think of something nicer, and this is probably pretty rare anyway. Any other onlookers have any remaining thoughts? @swift-ci Please smoke test |
@swift-ci Please test source compatibility |
@swift-ci Please test |
Build failed |
Build failed |
@jrose-apple is this ready for merge? |
Thanks for doing this! |
Maybe this makes sense as a warning, but it does not make sense IMO to make it an error to define properties with uninhabited types. The original motivating issue in https://bugs.swift.org/browse/SR-8811 indicates an issue with the unreachable code analysis, and while this makes it impossible to get into that situation, I think we should still look into the underlying issue, and reduce this to a warning. |
As a warning, I think it'd also make sense to follow the behavior of the |
@jrose-apple will do! |
@jckarter which warning are you referring to? I looked through the diagnostics to find something for |
This warning:
|
Yea, I had just expected void to be in the diagnostic name. Would it make sense to just reuse this warning? |
Resolves: SR-8811
cc: @jrose-apple @CodaFi