Skip to content

[SILGen]: extend uninhabited parameter checks to self param #75795

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
36 changes: 22 additions & 14 deletions lib/SILGen/SILGenProlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1382,20 +1382,28 @@ void SILGenFunction::emitProlog(

emitExpectedExecutorProlog();

// IMPORTANT: This block should be the last one in `emitProlog`,
// since it terminates BB and no instructions should be insterted after it.
// Emit an unreachable instruction if a parameter type is
// uninhabited
if (paramList) {
for (auto *param : *paramList) {
if (param->getTypeInContext()->isStructurallyUninhabited()) {
SILLocation unreachableLoc(param);
unreachableLoc.markAsPrologue();
B.createUnreachable(unreachableLoc);
break;
}
}
}
// Emits an unreachable instruction if a parameter type (including `self`) is
// uninhabited. Returns `true` if an unreachable location was created.
auto handleUninhabitedParam = [&](ParamDecl *param) -> bool {
if (!param->getTypeInContext()->isStructurallyUninhabited())
return false;

SILLocation unreachableLoc(param);
unreachableLoc.markAsPrologue();
B.createUnreachable(unreachableLoc);
return true;
};

// IMPORTANT: An unreachable block should be the last one in `emitProlog`,
// since it is a terminator and no instructions should be insterted after it.
if (selfParam)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

with this change, at least one part of the standard library now emits a warning when built – the Never conformance to Identifiable. should that be changed? i think if this change is also merged, then the implementation could perhaps just be removed.

if (handleUninhabitedParam(selfParam))
return;

if (paramList)
for (auto *param : *paramList)
if (handleUninhabitedParam(param))
return;
}

static void emitIndirectPackParameter(SILGenFunction &SGF,
Expand Down
39 changes: 39 additions & 0 deletions test/SILGen/functions_uninhabited_param.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,42 @@ func empty_custom_product(_ xs: (E, Int)) { // expected-note {{'xs' is of type '
print() // expected-warning{{will never be executed}}
}

//===--- Uninhabited self parameters

extension Never {
var unreachableComputed: Int { // expected-note{{'self' is of type 'Never' which cannot be constructed because it is an enum with no cases}}
42 // expected-warning{{will never be executed}}
}

subscript(_ i: Int) -> Int { // expected-note{{'self' is of type 'Never' which cannot be constructed because it is an enum with no cases}}
42 // expected-warning{{will never be executed}}
}

func unreachableMethod() -> Int { // expected-note{{'self' is of type 'Never' which cannot be constructed because it is an enum with no cases}}
42 // expected-warning{{will never be executed}}
}

func unreachableMethodWithParam(_ x: Int) -> Int { // expected-note{{'self' is of type 'Never' which cannot be constructed because it is an enum with no cases}}
42 // expected-warning{{will never be executed}}
}

func unreachableMethodWithUninhabitedParam(_ p0: Int, _ p1: Self, _ p2: Never) -> Int { // expected-note{{'p1' is of type 'Never' which cannot be constructed because it is an enum with no cases}}
42 // expected-warning{{will never be executed}}
}

func unreachableWithNestedDecls() -> Int { // expected-note{{'self' is of type 'Never' which cannot be constructed because it is an enum with no cases}}
func g() -> Int { 42 }
let c = { g() } // expected-warning{{will never be executed}}
return c()
}
}

protocol P {
var prop: Int { get }
func uncallable(_ n: Never)
}

extension Never: P {
var prop: Int { fatalError() }
func uncallable(_ n: Never) {}
}