Skip to content

[SIL] Fix Diagnosing Never-returning functions that don't call other Never-returning functions #4393

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 2 commits into from
Aug 19, 2016
Merged
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
6 changes: 5 additions & 1 deletion include/swift/AST/DiagnosticsSIL.def
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,11 @@ ERROR(assignment_to_immutable_value,none,
ERROR(missing_return,none,
"missing return in a %select{function|closure}1 expected to return %0",
(Type, unsigned))
ERROR(non_exhaustive_switch,none,
ERROR(missing_never_call,none,
"%select{function|closure}1 with uninhabited return type %0 is missing "
"call to another never-returning function on all paths",
(Type, unsigned))
ERROR(non_exhaustive_switch,none,
"switch must be exhaustive, consider adding a default clause", ())
ERROR(guard_body_must_not_fallthrough,none,
"'guard' body may not fall through, consider using 'return' or 'break'"
Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ class alignas(1 << TypeAlignInBits) TypeBase {
bool hasReferenceSemantics();

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

/// Is this the 'Any' type?
bool isAny();
Expand Down
3 changes: 1 addition & 2 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,11 @@ bool TypeBase::hasReferenceSemantics() {
return getCanonicalType().hasReferenceSemantics();
}

bool TypeBase::isNever() {
bool TypeBase::isUninhabited() {
if (auto nominalDecl = getAnyNominal())
if (auto enumDecl = dyn_cast<EnumDecl>(nominalDecl))
if (enumDecl->getAllElements().empty())
return true;

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->isUninhabited())
return Converter.convert(IGM, IGM.Context.TheEmptyTupleType);

assert(type->getDecl()->isObjC() && "not an @objc enum?!");
Expand Down
2 changes: 1 addition & 1 deletion lib/SIL/SILType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ bool SILType::isReferenceCounted(SILModule &M) const {

bool SILType::isNoReturnFunction() const {
if (auto funcTy = dyn_cast<SILFunctionType>(getSwiftRValueType()))
return funcTy->getSILResult().getSwiftRValueType()->isNever();
return funcTy->getSILResult().getSwiftRValueType()->isUninhabited();

return false;
}
Expand Down
9 changes: 3 additions & 6 deletions lib/SILOptimizer/Mandatory/DataflowDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,13 @@ static void diagnoseMissingReturn(const UnreachableInst *UI,
llvm_unreachable("unhandled case in MissingReturn");
}

// No action required if the function returns 'Void' or that the
// function is marked 'noreturn'.
if (ResTy->isVoid() || F->isNoReturnFunction())
return;

SILLocation L = UI->getLoc();
assert(L && ResTy);
auto diagID = F->isNoReturnFunction() ? diag::missing_never_call
: diag::missing_return;
diagnose(Context,
L.getEndSourceLoc(),
diag::missing_return, ResTy,
diagID, ResTy,
FLoc.isASTNode<ClosureExpr>() ? 1 : 0);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,7 @@ bool TypeChecker::typeCheckCatchPattern(CatchStmt *S, DeclContext *DC) {

static bool isDiscardableType(Type type) {
return (type->is<ErrorType>() ||
type->isNever() ||
type->isUninhabited() ||
type->lookThroughAllAnyOptionalTypes()->isVoid());
}

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

enum NoCasesButNotNever {}

func diagnoseNoCaseEnumMissingReturn() -> NoCasesButNotNever {
} // expected-error {{function with uninhabited return type 'NoCasesButNotNever' is missing call to another never-returning function on all paths}}

func diagnoseNeverMissingBody() -> Never {
} // expected-error {{function with uninhabited return type 'Never' is missing call to another never-returning function on all paths}}

_ = { () -> Never in
}() // expected-error {{closure with uninhabited return type 'Never' is missing call to another never-returning function on all paths}}-

func diagnoseNeverWithBody(i : Int) -> Never {
if (i == -1) {
print("Oh no!")
} else {
switch i {
case 0:
exit()
case 1:
fatalError()
default:
repeat { } while true
}
}
} // expected-error {{function with uninhabited return type 'Never' is missing call to another never-returning function on all paths}}

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