Skip to content

[SILGen] Insert an unreachable if a function declaration's parameter is uninhabited #20528

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 33 commits into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
a71adb6
[sema] Check if a function param type is uninhabited and emit a diagn…
theblixguy Nov 12, 2018
ec208d6
[test] Add tests
theblixguy Nov 12, 2018
812a1cf
[sema] update indentation, remove unused index param, use getInterfac…
theblixguy Nov 13, 2018
f632e0b
[silgen] Update prolog code to emit unreachable if a func param is un…
theblixguy Nov 14, 2018
2158ed1
[ast] [sema] remove previous changes
theblixguy Nov 15, 2018
baa201e
[test] add missing newline
theblixguy Nov 15, 2018
0d02829
[silgen] move unreachable code emission to void emitProlog()
theblixguy Nov 16, 2018
54616ec
[silgen] Removes whitespace
theblixguy Nov 16, 2018
2e75ca5
[test] Updates warning message
theblixguy Nov 16, 2018
2b04c10
Merge branch 'master' into fix/SR-8435
Nov 16, 2018
1d8c2c2
[test] added another test case
theblixguy Nov 16, 2018
ba4b743
[test] remove tests for now
theblixguy Nov 16, 2018
8c39e27
[test] Adds tests for functions with uninhabited params
Nov 16, 2018
297a35d
[test] add missing newline
theblixguy Nov 16, 2018
15d2b4c
[ast] [diag] [sil] add note for unreachable function body
theblixguy Nov 17, 2018
b7df15c
[silgen] emit diagnostic (note) about unreachable function body
theblixguy Nov 17, 2018
a149cb0
[test] update test case
theblixguy Nov 17, 2018
ffd56d9
[test] add missing expected-note
theblixguy Nov 17, 2018
01fa184
[silgen] emit a diagnostic note only if the body is not empty
theblixguy Nov 18, 2018
032142c
[test] update tests
theblixguy Nov 18, 2018
4b4bb4c
[test] move test file to SILGen/
theblixguy Nov 18, 2018
4e3278e
[ast] [diag] [sil] update diagnostic note
theblixguy Nov 19, 2018
f249139
[silgen] check if the body is not null before accessing it
theblixguy Nov 19, 2018
e8d3caa
[test] update expected-note in test
theblixguy Nov 19, 2018
4f39e3b
[silgen] [ast] adds param name to diagnostics
Nov 26, 2018
174092a
Merge branch 'master' into fix/SR-8435
Nov 26, 2018
3b8128c
[silgen] Move diagnostic note creation to SILGenStmt
Nov 26, 2018
430d8a5
[ast] [diag] fix indentation
theblixguy Nov 26, 2018
4a26a74
[silgen] fix indentation
Nov 26, 2018
09923b8
Merge branch 'fix/SR-8435' of https://github.com/theblixguy/swift int…
Nov 26, 2018
ce40755
[silgen] fix brace indent
theblixguy Nov 26, 2018
59a5d71
[silgen] run git-clang-format to fix all indentation issues
Nov 26, 2018
5587edf
[silgen] fix brace indentation (again)
Nov 26, 2018
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: 2 additions & 0 deletions include/swift/AST/DiagnosticsSIL.def
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ ERROR(guard_body_must_not_fallthrough,none,
"'guard' body must not fall through, consider using a 'return' or 'throw'"
" 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))
NOTE(unreachable_code_branch,none,
"condition always evaluates to %select{false|true}0", (bool))
NOTE(call_to_noreturn_note,none,
Expand Down
13 changes: 13 additions & 0 deletions lib/SILGen/SILGenProlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,19 @@ void SILGenFunction::emitProlog(AnyFunctionRef TheClosure,
Type resultType, bool throws) {
uint16_t ArgNo = emitProlog(paramList, selfParam, resultType,
TheClosure.getAsDeclContext(), throws);

// Emit an unreachable instruction if a parameter type is
// uninhabited
if (paramList) {
for (auto *param : *paramList) {
if (param->getType()->isStructurallyUninhabited()) {
SILLocation unreachableLoc(param);
unreachableLoc.markAsPrologue();
B.createUnreachable(unreachableLoc);
break;
}
}
}

// Emit the capture argument variables. These are placed last because they
// become the first curry level of the SIL function.
Expand Down
10 changes: 10 additions & 0 deletions lib/SILGen/SILGenStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,16 @@ void StmtEmitter::visitBraceStmt(BraceStmt *S) {
} else {
diagnose(getASTContext(), ESD.getStartLoc(),
diag::unreachable_code);
if (!S->getElements().empty()) {
for (auto *arg : SGF.getFunction().getArguments()) {
if (arg->getType().getASTType()->isStructurallyUninhabited()) {
diagnose(getASTContext(), S->getStartLoc(),
diag::unreachable_code_uninhabited_param_note,
arg->getDecl()->getBaseName().userFacingName());
break;
}
}
}
}
return;
}
Expand Down
10 changes: 10 additions & 0 deletions test/SILGen/functions_uninhabited_param.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %target-swift-emit-sil -enable-sil-ownership %s -o /dev/null -verify

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

func foo(baz: Never) -> Int { // expected-note {{'baz' is uninhabited, so this function body can never be executed}}
print("I can't be called!") // expected-warning{{will never be executed}}
return 0
}

func bar(baz: Never) -> Int {} // ok