Commit Graph

1007 Commits

Author SHA1 Message Date
Frederick Kellison-Linn
71697c37ca Allow implicit self in escaping closures when self usage is unlikely to cause cycle (#23934)
* WIP implementation

* Cleanup implementation

* Install backedge rather than storing array reference

* Add diagnostics

* Add missing parameter to ResultFinderForTypeContext constructor

* Fix tests for correct fix-it language

* Change to solution without backedge, change lookup behavior

* Improve diagnostics for weak captures and captures under different names

* Remove ghosts of implementations past

* Address review comments

* Reorder member variable initialization

* Fix typos

* Exclude value types from explicit self requirements

* Add tests

* Add implementation for AST lookup

* Add tests

* Begin addressing review comments

* Re-enable AST scope lookup

* Add fixme

* Pull fix-its into a separate function

* Remove capturedSelfContext tracking from type property initializers

* Add const specifiers to arguments

* Address review comments

* Fix string literals

* Refactor implicit self diagnostics

* Add comment

* Remove trailing whitespace

* Add tests for capture list across multiple lines

* Add additional test

* Fix typo

* Remove use of ?: to fix linux build

* Remove second use of ?:

* Rework logic for finding nested self contexts
2019-12-20 02:38:41 +00:00
Robert Widmann
1a9bdaffa2 Refactor Direct Name Lookup
The old name lookup would frequently try to flush and rebuild the name lookup cache.  Instead, never flush the cache, and use the cache misses as an opportunity to load members and bring the lookup table up to date with any added extensions.
2019-12-18 22:36:27 -08:00
Robert Widmann
a1b451470e Teach loadNamedMembers to import inherited constructors 2019-12-18 16:17:10 -08:00
Robert Widmann
f22fb81326 Block additive lookup in directReferencesForQualifiedTypeLookup 2019-12-16 15:36:09 -08:00
Robert Widmann
e7170bd751 Add Qualified Lookup Requests 2019-12-16 15:36:09 -08:00
Brent Royal-Gordon
6a8598a99c [NFC] Remove DeclNameRef staging calls 2019-12-11 00:55:18 -08:00
Brent Royal-Gordon
addbe3e5ed [NFC] Thread DeclNameRef through most of the compiler
This huge commit contains as many of the mechanical changes as possible.
2019-12-11 00:55:18 -08:00
Brent Royal-Gordon
9a57554bdf [NFC] DeclNameRef-ify qualified and unqualified lookup 2019-12-11 00:55:17 -08:00
Brent Royal-Gordon
1df792ae9f [NFC] Convert TypeRepr to use DeclName(Loc)?
Replaces `ComponentIdentTypeRepr::getIdentifier()` and `getIdLoc()` with `getNameRef()` and `getNameLoc()`, which use `DeclName` and `DeclNameRef` respectively.
2019-12-11 00:45:08 -08:00
Brent Royal-Gordon
027e4b7941 [NFC] Make base-name behavior of lookup table loading explicit
We were unnecessarily converting back and forth between base names and full names, which seemed a bit silly.
2019-12-11 00:45:07 -08:00
Robert Widmann
acf282bc3d Merge pull request #28689 from hamishknight/lookup-requests
[NameLookup] Add requests for module and AnyObject lookup (master)
2019-12-10 23:54:51 -08:00
Robert Widmann
dd51251014 Move semantic member synthesis into the type checker's lookup utilities
Pull these calls out of lookup itself to prevent re-entrancy.  Synthesizing these members can force a conformance check that can ultimately wind up back in lookup.  Pull the forcing out of every single qualified lookup and just install these members in the TypeChecker's entrypoints to lookup.

The upside is we can localize this gross hack, the downside is, of course, that we have to smear it across all of the lookup entrypoints.
2019-12-06 14:17:34 -08:00
Robert Widmann
b8779f7df7 Fix getDirectlyInheritedNominalTypeDecls
getDirectlyInheritedNominalTypeDecls looks for inherited protocols in two places: the actual inheritance clause of a declaration and the trailing where clause.  This works fine for normal declarations, but deserialized protocols that have Self constraints have no trailing where clause and appear to have no super-protocol bounds.  This means clients can potentially disagree about the structure of the protocol, and any symbols derived from it.

The test case demonstrates this problem directly: We build a hollowed-out SwiftUI preview provider then try to dynamically replace a requirement in a Self-constrained protocol extension.  The SwiftUI module sees the where clause, but when we go to deserialize the module in the "Preview" the protocol extension sees the wrong inheritance bounds and mis-mangles the call to Self.view(for:ofType).

The fix is to ask the requirement signature for Self requirements that would normally appear on a trailing where clause.

Resolves rdar://57150777
2019-12-03 17:01:57 -08:00
Hamish Knight
6811586e40 Remove NumLookupQualifiedInAnyObject counter
We now have an equivalent counter for
AnyObjectLookupRequest.
2019-11-22 12:59:27 -08:00
Hamish Knight
256c129506 [NameLookup] Add AnyObjectLookupRequest 2019-11-22 12:59:27 -08:00
Hamish Knight
e04b56bd67 [NameLookup] Add LookupInModuleRequest 2019-11-22 12:59:26 -08:00
Slava Pestov
ef05accd31 Sema: Remove even more vestigial TypeChecker usages 2019-11-19 17:40:00 -05:00
Hamish Knight
2853615880 Add type to package inputs to UnqualifiedLookupRequest
This allows us use an OptionSet parameter for
the request (as currently we can't directly use it
as a parameter due to not having an == definition
for it). It also allows us to regain default
arguments for the source loc and flag parameters.
2019-11-15 16:35:14 -08:00
Hamish Knight
e8c30ca1b7 Remove UnqualifiedLookup
Now that we have UnqualifiedLookupRequest, this
class no longer serves much of a purpose. Inline
its constructor logic into the request.
2019-11-15 14:25:42 -08:00
Hamish Knight
3aa7158d6c Switch UnqualifiedLookup callers over to UnqualifiedLookupRequest 2019-11-15 14:25:42 -08:00
Hamish Knight
633de0241b Add UnqualifiedLookupRequest
This request performs raw unqualified lookup,
and will be used to replace the UnqualifiedLookup
type.
2019-11-15 14:25:42 -08:00
Hamish Knight
185c265ef6 NFC: Move LookupResult from Sema to Name Lookup
This will be used as the result type for the
unqualified lookup request.
2019-11-15 14:25:41 -08:00
Robert Widmann
2b08d1b834 [NFC] ASTContext::getLazyResolver -> ASTContext::getLegacyGlobalTypeChecker 2019-11-05 14:44:41 -08:00
Robert Widmann
d4906ac10b Rationalize Implicit Member Synthesis Somewhat
Codable's deep magic currently forces conformance checks in the middle
of name lookup in order to inject CodingKeys into lookup results.  This
is compounded by the fact that this lookup fixup is occuring
incrementally, meaning depending on order of requirements being looked
up, Decl::getMembers() will give you a different answer.

Compounding this, NameLookup relied on the LazyResolver to formalize
this layering violation, and relied on implicit laziness to guard
against re-entrancy.

The approach is multi-pronged:
1) Shift the layering violation into the request evaluator
2) Spell out the kinds of resolution we support explicitly (make them
easier to find and kill)
3) Remove the LazyResolver entrypoint this was relying on
4) Split off the property wrappers part into its own utility
2019-11-05 09:10:13 -08:00
Robert Widmann
a29bfb4434 Fold resolveImplicitConstructors into resolveImplicitMember 2019-11-01 18:11:24 -07:00
Brent Royal-Gordon
17169fc1fe Merge pull request #27950 from brentdax/dumpster-fire
[NFC] Standardize dump() methods in frontend
2019-10-31 20:36:26 -07:00
Brent Royal-Gordon
99faa033fc [NFC] Standardize dump() methods in frontend
By convention, most structs and classes in the Swift compiler include a `dump()` method which prints debugging information. This method is meant to be called only from the debugger, but this means they’re often unused and may be eliminated from optimized binaries. On the other hand, some parts of the compiler call `dump()` methods directly despite them being intended as a pure debugging aid. clang supports attributes which can be used to avoid these problems, but they’re used very inconsistently across the compiler.

This commit adds `SWIFT_DEBUG_DUMP` and `SWIFT_DEBUG_DUMPER(<name>(<params>))` macros to declare `dump()` methods with the appropriate set of attributes and adopts this macro throughout the frontend. It does not pervasively adopt this macro in SILGen, SILOptimizer, or IRGen; these components use `dump()` methods in a different way where they’re frequently called from debugging code. Nor does it adopt it in runtime components like swiftRuntime and swiftReflection, because I’m a bit worried about size.

Despite the large number of files and lines affected, this change is NFC.
2019-10-31 18:37:42 -07:00
Robert Widmann
0267384e11 Fixup SourceKit and Tests
Patch up all the places that are making a syntactic judgement about the
isInvalid() bit in a ValueDecl.  They may continue to use that query,
but most guard themselves on whether the interface type has been set.
2019-10-30 15:09:14 -07:00
Doug Gregor
190d96535c [Property wrappers] Fix source compatibility issue with attribute lookup.
Swift 5.1's lookup for custom attributes skipped associated type
members, which allowed code like the given example to compile. To
maintain source compatibility, identify the narrow case that happens
in practice---the property wrapper is at module scope but is now
shadowed by an associated type---warn about it, and accept it.

Fixes rdar://problem/56213175.
2019-10-11 22:44:50 -07:00
Slava Pestov
52cf3659b5 Sema: Simplify validateDecl()
Remove the early return in the case where one of our parent contexts was
being validated, and replace it with a simpler check that is only
performed in some callers related to associated type inference; we want
to bail out in one specific case only, which is that the declaration
is inside an extension whose generic signature is in the process of
being computed.
2019-10-07 22:20:18 -04:00
Slava Pestov
51e4792008 AST: Types nested inside a subclass shadow types in the superclass with same name
The existing rules sometimes behaved this way, because validateDecl()
bails out silently without error on circularity.

Let's make this explicit instead, and get it working in all cases.

Fixes <rdar://https://bugs.swift.org/browse/SR-3492> and
<rdar://problem/45889192>.
2019-09-25 22:17:56 -04:00
Robert Widmann
ed18f47d05 Port getInterfaceType() patterns in libAST 2019-09-23 16:49:09 -07:00
Robert Widmann
2df956a4ca Make decl shadowing ask the right question
If it's going to validate the declaration, it wants to know if there's an interface type.  Computing the generic signature can be done on demand.
2019-09-20 20:38:38 -07:00
Robert Widmann
38bde33067 Merge pull request #27172 from CodaFi/aliasing-artifacts-and-noise-reduction-techniques
Kill validateDeclForNameLookup Harder
2019-09-18 15:00:51 -07:00
Jordan Rose
8d7f1b7c5d [AST] Separate SourceFile from FileUnit.h
Like the last commit, SourceFile is used a lot by Parse and Sema, but
less so by the ClangImporter and (de)Serialization. Split it out to
cut down on recompilation times when something changes.

This commit does /not/ split the implementation of SourceFile out of
Module.cpp, which is where most of it lives. That might also be a
reasonable change, but the reason I was reluctant to is because a
number of SourceFile members correspond to the entry points in
ModuleDecl. Someone else can pick this up later if they decide it's a
good idea.

No functionality change.
2019-09-17 17:54:41 -07:00
Jordan Rose
853caa66d4 [AST] Split FileUnit and its subclasses out of Module.h
Most of AST, Parse, and Sema deal with FileUnits regularly, but SIL
and IRGen certainly don't. Split FileUnit out into its own header to
cut down on recompilation times when something changes.

No functionality change.
2019-09-17 17:54:41 -07:00
Robert Widmann
5e34169aca Separate computing interface types and underlying types
Computing the interface type of a typealias used to push validation forward and recompute the interface type on the fly.  This was fragile and inconsistent with the way interface types are computed in the rest of the decls.  Separate these two notions, and plumb through explicit interface type computations with the same "computeType" idiom.  This will better allow us to identify the places where we have to force an interface type computation.

Also remove access to the underlying type loc.  It's now just a cache location the underlying type request will use.  Push a type repr accessor to the places that need it, and push the underlying type accessor for everywhere else.  Getting the structural type is still preferred for pre-validated computations.

This required the resetting of a number of places where we were - in many cases tacitly - asking the question "does the interface type exist".  This enables the removal of validateDeclForNameLookup
2019-09-17 08:20:55 -07:00
David Ungar
c7be2def90 Merge pull request #27143 from davidungar/A-9-12-eager-off
[NFC NameLookup ASTScope] Fixes for large app, eager primary tree creation, memberCount fix.
2019-09-14 18:46:17 -07:00
David Ungar
2ede6f2b82 Silence unused warnings 2019-09-13 17:16:19 -07:00
Slava Pestov
52479cadd5 AST: Fix source break with new shadowing rules
My recent refactoring of top-level lookup replaced the old
shadowing done as part of top-level lookup with two separate
rules:

- If all paths from the current source file to a module 'B'
  go through a module 'A', then declarations in 'A' shadow
  declarations in 'B'.

- If a declaration in module 'A' was found via a scoped
  import and a declaration with the same name in module 'B'
  was found via an unscoped import, prefer the declaration
  from module 'A'.

However this caused a source break when you have a scenario
like the following:

- A source file imports 'A', 'B', and 'B.Foo'.

- 'A' re-exports 'B'.

- Both 'A' and 'B' define a type named 'Foo'.

The problem is that the scoped import 'B.Foo' can actually
find both 'A.Foo' and 'B.Foo', since 'B' re-exports 'A'.

Furthermore, since the source file explicitly imports 'A',
'B' does not shadow 'A' in the import graph.

As a result neither shadowing rule would eliminate the
ambiguity.

The new rule combines the scoped import check and the
shadowing check by considering all access paths to 'A'
that are not shadowed by 'B'. Using this rule, 'A.Foo'
is only seen via the access path 'A', whereas 'B.Foo'
is seen under both 'B' and 'B.Foo'. Since 'B.Foo' is seen
via a scoped import and 'A.Foo' is only seen via an
unscoped import, we can conclude that 'B.Foo' shadows
'A.Foo'.

Fixes <rdar://problem/55205050>.
2019-09-12 21:30:43 -04:00
Robert Widmann
2161fd3778 Fix the declcontext used to find the extended nominal type
This should not start searching at the extension.  Start looking one
level up.  Breaks a potential cycle where an extension's generic
parameters were subject to lookup while computing the nominal type.
2019-09-06 17:22:31 -07:00
swift-ci
e011c67631 Merge pull request #27059 from CodaFi/find-my-friends 2019-09-06 12:33:53 -07:00
Robert Widmann
c25e21840a Convert some TypeLoc computations to helpers
These aren't requirements of the (Simple)Request interface.  Downgrade
them to helper functions.
2019-09-06 11:16:17 -07:00
Varun Gandhi
f2f63882e2 [NFC] Always use syntactic information in ExtendedNominalRequest.
Since we started saving the nominal in the .swiftmodule, changing the other
call-sites in ClangImporter to save the value means that we can rely on
the typeRepr being non-null except when the syntax tree was ill-formed.
2019-09-05 15:33:02 -07:00
Varun Gandhi
0dcb936531 Serialize extended nominal separately when serializing an extension.
Instead of computing it from the extended type after deserialization --
which is tricky to do, due to potential presence of protocol
compositions -- we obtain the extended nominal directly.

Fixes SR-11227 and linked rdar://problem/53712389.
2019-09-05 15:33:02 -07:00
Slava Pestov
a09382ce2b AST: Completely remove NominalTypeDecl::LookupDirectFlags::IgnoreNewExtensions 2019-09-03 22:53:52 -04:00
Slava Pestov
3196b5e1ac AST: If lazy member loading fails in an extension, don't revert the type to eager loading
We can just load all members of the extension and proceed.
2019-09-03 22:53:52 -04:00
Slava Pestov
220bc64064 ClangImporter: Directly track constructors imported into a nominal type
This avoids a re-entrant lookup while doing lazy member loading,
and eliminates a usage of LookupDirectFlags::IgnoreNewExtensions,
and the last usage of NominalTypeDecl::makeMemberVisible().
2019-09-03 22:39:36 -04:00
Slava Pestov
7f6ef1e12d AST: Don't visit all extensions of a type when doing lazy member lookup
Instead only do it while preparing the lookup table.
2019-09-03 22:39:35 -04:00
Suyash Srijan
d2198294f8 Merge pull request #26795 from theblixguy/fix/SR-11288
[Typechecker] Check conforming protocols of context when resolving a custom attribute
2019-08-30 20:22:33 +01:00