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

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

merged 1 commit into from
Nov 2, 2020

Conversation

guitard0g
Copy link

@guitard0g guitard0g commented Oct 29, 2020

Local types can be used earlier within a scope than they were declared. We currently diagnose these type declarations as unreachable when they appear after a return, even though they may have been used prior to the return statement. This change stops that diagnostic.

Resolves SR-13639, rdar://69845495

Note on local function declarations:
Currently we emit an unreachable code diagnostic when a function appears after a return statement. Because functions can't be used before they are declared (except inside of another local function), this is fine. However, if the proposal to tweak local function scoping were to be implemented, then we would similarly need to skip emitting a diagnostic here when a local function is defined after a return.

@guitard0g
Copy link
Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - b9076f9cf68c5051a594611ff5928eecf81716e8

Copy link
Contributor

@slavapestov slavapestov left a comment

Choose a reason for hiding this comment

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

Nice fix! I have a small follow-up suggestion too.

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?

@slavapestov slavapestov self-assigned this Oct 29, 2020
Local types can be used earlier within a scope than they were declared.
We currently diagnose these type declarations as unreachable when they
appear after a return, even though they may have been used prior to
the return statement. This change stops that diagnostic.

Resolves SR-13639, rdar://69845495
@guitard0g guitard0g requested a review from slavapestov October 29, 2020 22:54
@guitard0g
Copy link
Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 45b670c

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 45b670c

Copy link
Contributor

@ravikandhadai ravikandhadai left a comment

Choose a reason for hiding this comment

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

Thanks Josh! Looks good to me.

} 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.

@guitard0g
Copy link
Author

@swift-ci please smoke test and merge

@swift-ci swift-ci merged commit 37bbfbf into swiftlang:main Nov 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants