Skip to content

[SR-13639][Diag] Don't diagnose local type declarations as unreachable #34493

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 1 commit into from
Nov 2, 2020
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
5 changes: 5 additions & 0 deletions lib/SILGen/SILGenStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@ void StmtEmitter::visitBraceStmt(BraceStmt *S) {
// Ignore all other implicit expressions.
continue;
}
} else if (auto D = ESD.dyn_cast<Decl*>()) {
// Local type declarations are not unreachable because they can appear
// after the declared type has already been used.
if (isa<TypeDecl>(D))
Copy link
Contributor

Choose a reason for hiding this comment

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

One more thing I realized: local functions (FuncDecl) need the same treatment, because they can also be forward-referenced. Do you mind adding that in this PR or a follow-up?

Copy link
Author

@guitard0g guitard0g Oct 30, 2020

Choose a reason for hiding this comment

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

Sure thing. On a related note, while testing I found out forward references in this scenario seem to crash the compiler. That bug may need to be fixed before we can add proper tests for the forward reference scenario.

continue;
}

if (StmtType != UnknownStmtType) {
Expand Down
4 changes: 2 additions & 2 deletions test/SILGen/local_types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
func function1() {
return

class UnreachableClass {} // expected-warning {{code after 'return' will never be executed}}
class LocalClass {}
}

func function2() {
Expand All @@ -19,4 +19,4 @@ func function2() {

// CHECK-LABEL: sil private [transparent] [ossa] @$s11local_types9function2yyFyycfU_1SL_V1xSivpfi : $@convention(thin) () -> Int

// CHECK-LABEL: sil_vtable UnreachableClass
// CHECK-LABEL: sil_vtable LocalClass
11 changes: 10 additions & 1 deletion test/SILGen/unreachable_code.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-swift-emit-sil %s -o /dev/null -verify
// RUN: %target-swift-emit-sil %s -verify | %FileCheck %s

func testUnreachableAfterReturn() -> Int {
var x: Int = 3
Expand Down Expand Up @@ -146,3 +146,12 @@ func testUnreachableCatchClause() {
print("some error")
}
}

func sr13639() -> Int {
return Foo.bar
struct Foo { // no-warning
static var bar = 0
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you define a method inside Foo and add a CHECK: line to ensure the method is actually emitted, too?

// CHECK: sil private @$s16unreachable_code7sr13639SiyF3FooL_V7fooFuncyyF : $@convention(method) (Foo) -> ()
func fooFunc() {}
}
}