Skip to content

[Name lookup] Enable shadowing for type lookup and of the Swift module. #21370

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 5 commits into from
Dec 17, 2018

Conversation

DougGregor
Copy link
Member

Tweak the shadowing rules in two ways:

  1. For unqualified type lookup, apply shadowing rules. Without this, we
    would always get an ambiguity if there were two types with the same name,
    so this should be a strict improvement.
  2. Allow a name introduced in any other module to shadow a name in the
    Swift standard library. This is (another) weak form of a more sensible,
    generalized rule that would use the import graph to describe shadowing.

Together, these tweaks allow the Result type that was recently introduced in
the standard library to exist without breaking source compatibility for
Swift code that is already using a Result type. The user Result type will
shadow (hide) the Swift one. The latter can be spelled Swift.Result if it
is needed by such code.

Fixes rdar://problem/46767892.

… rather than co-opting NULL, which is a valid CanType type.
Tweak the shadowing rules in two ways:

1) For unqualified type lookup, apply shadowing rules. Without this, we
would always get an ambiguity if there were two types with the same name,
so this should be a strict improvement.
2) Allow a name introduced in any other module to shadow a name in the
Swift standard library. This is (another) weak form of a more sensible,
generalized rule that would use the import graph to describe shadowing.

Together, these tweaks allow the Result type that was recently introduced in
the standard library to exist without breaking source compatibility for
Swift code that is already using a Result type. The user Result type will
shadow (hide) the Swift one. The latter can be spelled Swift.Result if it
is needed by such code.

Fixes rdar://problem/46767892.
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

1 similar comment
@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

We’re going to need to use this in GenericSignature.cpp. NFC
Narrow the recently-introduced shadowing rule for types so that it only
applies to non-member types. Member types lack a reasonable syntax for
specifying precisely which module to look into, and there are a few use
cases where the type checker will pick a type that would be shadowed by
the new rule.

Fixes a source-compatibility regression introduced by the shadowing rule.
When substituting type parameters to compute a canonical type within a
given generic context, also handle the case where the type parameter is
directly known to be a concrete type. The type checker won’t do this, but 
SourceKit does. 

Fixes a regression in SourceKit/DocSupport/doc_stdlib.swift.
@DougGregor
Copy link
Member Author

DougGregor commented Dec 17, 2018

The test failure was real (a bug in GenericSignature::getCanonicalTypeInContext nonetheless). On the source-compatibility side, there was a real failure in ProcedureKit due to my over-application of the new shadowing rule to member types, and well as a bunch of UPASS's for projects that have their own Result types. The new commits address these issues.

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

@@ -1169,6 +1191,11 @@ UnqualifiedLookup::UnqualifiedLookup(DeclName Name, DeclContext *DC,
lookupInModule(&M, {}, Name, CurModuleResults, NLKind::UnqualifiedLookup,
resolutionKind, TypeResolver, DC, extraImports);

// Always perform name shadowing for type lookup.
if (options.contains(Flags::TypeLookup)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is iffy—it means that expression context lookup will behave differently than type context lookup. Or does that happen elsewhere?

Copy link
Member Author

Choose a reason for hiding this comment

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

It does happen elsewhere. Most unqualified lookup goes through TypeChecker::lookupUnqualified, which (separately) triggers shadowing, so this is bringing things closer together in a duct-tape-and-paper-clip sort of way. There's more refactoring to do, but I'm holding out hope for this to be safe enough for Swift 5.0, hence the narrow applicability (to type lookup, to the Swift module, to non-member types);.

Copy link
Contributor

@jrose-apple jrose-apple Dec 17, 2018

Choose a reason for hiding this comment

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

All right, makes sense. Maybe worth a FIXME, though?

@@ -44,6 +44,7 @@
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/SaveAndRestore.h"
#include <algorithm>
#include "GenericSignatureBuilderImpl.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: LLVM style says local headers go at the top.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, of course. Committing locally to fix at some point, but I'm not going to re-start CI for that right now.

@DougGregor
Copy link
Member Author

All of the source-compatibility failures are UPASS's.

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.

2 participants