Skip to content

Implement re-declaration checking for declarations in local context #34125

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

Conversation

slavapestov
Copy link
Contributor

@slavapestov slavapestov commented Sep 30, 2020

Currently parse-time lookup diagnoses these, so we need to implement it in Sema to prepare for disabling parse-time lookup.

There are two cases:

  • Local declarations inside a BraceStmt are now handled via the same CheckRedeclarationRequest used for type and global members, because we visit them as part of typeCheckDecl(). For this, I added a new finishLookupInBraceStmt flag to ASTScope::lookupLocalDecls(); when set, this stops the lookup at the innermost BraceStmt, because for purposes of re-declaration checking we only want to consider other declarations with the same name contained in the same BraceStmt. This is because shadowing of local declarations from outer scopes is, in fact, permitted.
  • Generic parameters, function parameters and pattern bindings in statements get their own bespoke check. This one is simpler, since we have all the declarations in a sequence; we just iterate over sequence looking for duplicate names.

While we're here, delete a couple of bits of dead code as well.

…calDecls()

This will be used to implement re-declaration checking for local
declarations. Currently this is handled by parse-time lookup.

To make it work with ASTScope, we need to perform lookups that
look into the innermost local scope only; for example, this is
an invalid redeclaration:

do {
  let x = 321
  let x = 123
}

But the following is fine, even though both VarDecls are in the same
*DeclContext*:

do {
  let x = 321
  do {
    let x = 123
  }
}
…parser lookup is off

The existing redeclaration checking can be extended for declarations in
local scope, but it won't catch these.
NamedDeclConsumer::foundDecl(VD, Reason, dynamicLookupInfo);
unsigned after = results.size();
if (after > before)
factory->addedResult(results.back());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@davidungar This is another debugging code path that you added, which has since become dead code. Notice how the Consumer instance variable was never used, and in fact we call addedResult() elsewhere, so we don't lose anything by deleting this code here.

@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test

@slavapestov
Copy link
Contributor Author

@swift-ci Please test source compatibility

@slavapestov
Copy link
Contributor Author

@swift-ci Please test Linux

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.

1 participant