Skip to content

Redo The Notes For "Uninhabited" Parameter Types #39477

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
Sep 28, 2021
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
4 changes: 3 additions & 1 deletion include/swift/AST/DiagnosticsSIL.def
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,9 @@ ERROR(guard_body_must_not_fallthrough,none,
" to exit the scope", ())
WARNING(unreachable_code,none, "will never be executed", ())
NOTE(unreachable_code_uninhabited_param_note,none,
"'%0' is uninhabited, so this function body can never be executed", (StringRef))
"'%0' is of type %1 which cannot be constructed because %select{it "
"contains %3 which is an enum with no cases|it is an enum with no cases}2",
(StringRef, Type, bool, Type))
NOTE(unreachable_code_branch,none,
"condition always evaluates to %select{false|true}0", (bool))
NOTE(call_to_noreturn_note,none,
Expand Down
13 changes: 11 additions & 2 deletions lib/SILGen/SILGenStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,19 @@ void StmtEmitter::visitBraceStmt(BraceStmt *S) {
diag::unreachable_code);
if (!S->getElements().empty()) {
for (auto *arg : SGF.getFunction().getArguments()) {
if (arg->getType().getASTType()->isStructurallyUninhabited()) {
auto argTy = arg->getType().getASTType();
if (argTy->isStructurallyUninhabited()) {
// Use the interface type in this diagnostic because the SIL type
// unpacks tuples. But, the SIL type being exploded means it
// points directly at the offending tuple element type and we can
// use that to point the user at problematic component(s).
auto argIFaceTy = arg->getDecl()->getInterfaceType();
diagnose(getASTContext(), S->getStartLoc(),
diag::unreachable_code_uninhabited_param_note,
arg->getDecl()->getBaseName().userFacingName());
arg->getDecl()->getBaseName().userFacingName(),
argIFaceTy,
argIFaceTy->is<EnumType>(),
argTy);
break;
}
}
Expand Down
16 changes: 12 additions & 4 deletions test/SILGen/functions_uninhabited_param.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

//===--- Function declaration with uninhabited parameter type

func foo(baz: Never) -> Int { // expected-note {{'baz' is uninhabited, so this function body can never be executed}}
func foo(baz: Never) -> Int { // expected-note {{'baz' is of type 'Never' which cannot be constructed because it is an enum with no cases}}
print("I can't be called!") // expected-warning{{will never be executed}}
return 0
}
Expand All @@ -11,11 +11,11 @@ func bar(baz: Never) -> Int {} // ok

// SR-13432
func map<T>(_ block: (Never) -> T) {}
map { arg in // expected-note {{'arg' is uninhabited, so this function body can never be executed}}
map { arg in // expected-note {{'arg' is of type 'Never' which cannot be constructed because it is an enum with no cases}}
5 // expected-warning {{will never be executed}}
}

map { arg in // expected-note {{'arg' is uninhabited, so this function body can never be executed}}
map { arg in // expected-note {{'arg' is of type 'Never' which cannot be constructed because it is an enum with no cases}}
return 5 // expected-warning {{will never be executed}}
}

Expand All @@ -26,4 +26,12 @@ enum E {

let _: (E.Type) -> (E) -> () = { s in { e in s.f(e) } }
// expected-warning@-1 {{will never be executed}}
// expected-note@-2 {{'e' is uninhabited, so this function body can never be executed}}
// expected-note@-2 {{'e' is of type 'E' which cannot be constructed because it is an enum with no cases}}

func empty_product(_ xs: (Int, String, Never)) { // expected-note {{'xs' is of type '(Int, String, Never)' which cannot be constructed because it contains 'Never' which is an enum with no cases}}
print() // expected-warning{{will never be executed}}
}
func empty_custom_product(_ xs: (E, Int)) { // expected-note {{'xs' is of type '(E, Int)' which cannot be constructed because it contains 'E' which is an enum with no cases}}
print() // expected-warning{{will never be executed}}
}