Skip to content

Uninhabited != NoReturn #4365

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ class alignas(1 << TypeAlignInBits) TypeBase {
/// semantics?
bool hasReferenceSemantics();

/// Is this an uninhabited type, such as 'Never'?
/// Is this 'Never'?
bool isNever();

/// Is this the 'Any' type?
Expand Down
4 changes: 1 addition & 3 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ bool TypeBase::hasReferenceSemantics() {
bool TypeBase::isNever() {
if (auto nominalDecl = getAnyNominal())
if (auto enumDecl = dyn_cast<EnumDecl>(nominalDecl))
if (enumDecl->getAllElements().empty())
return true;

return enumDecl == getASTContext().getNeverDecl();
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/GenClangType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ GenClangType::visitBoundGenericType(CanBoundGenericType type) {
clang::CanQualType GenClangType::visitEnumType(CanEnumType type) {
// Special case: Uninhabited enums are not @objc, so we don't
// know what to do below, but we can just convert to 'void'.
if (type->isNever())
if (type->getDecl()->getAllElements().empty())
return Converter.convert(IGM, IGM.Context.TheEmptyTupleType);

assert(type->getDecl()->isObjC() && "not an @objc enum?!");
Expand Down
2 changes: 1 addition & 1 deletion test/IRGen/dllexport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public var ci : c = c()

open class d {
private func m() -> Never {
fatalError()
return fatalError()
}
}

Expand Down
5 changes: 5 additions & 0 deletions test/SILOptimizer/return.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ func singleBlock2() -> Int {
y += 1
} // expected-error {{missing return in a function expected to return 'Int'}}

enum NoCasesButNotNever {}

func diagnoseNoCaseEnumMissingReturn() -> NoCasesButNotNever {
Copy link
Contributor

Choose a reason for hiding this comment

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

Note that this is meant to be an error even with a 'Never' return. a Never-returning function should end with a call to some other Never-returning function, or the return should be unreachable.

} // expected-error {{missing return in a function expected to return 'NoCasesButNotNever'}}

class MyClassWithClosure {
var f : (_ s: String) -> String = { (_ s: String) -> String in } // expected-error {{missing return in a closure expected to return 'String'}}
}
Expand Down