-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[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
Conversation
@swift-ci please test |
Build failed |
There was a problem hiding this 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 |
There was a problem hiding this comment.
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?
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
@swift-ci please test |
Build failed |
Build failed |
There was a problem hiding this 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)) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
@swift-ci please smoke test and merge |
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.