Commit Graph

1018 Commits

Author SHA1 Message Date
Pavel Yaskevich
e6a6ecf44b [NameLookup] Lift synthesized protocol check into getDirectlyInheritedNominalTypeDecls
Some places use `getDirectlyInheritedNominalTypeDecls` directly
instead of fetching it through a request, so lift the check there
instead (direct use of `getDirectlyInheritedNominalTypeDecls`
could be refactored separately).
2023-11-16 10:58:34 -08:00
Pavel Yaskevich
e050e35c36 [AST] Teach InheritedProtocolsRequest about synthesized protocol attributes
ClangImporter converts NS_SWIFT_SENDABLE into `SynthesizedProtocolAttr`
which needs to be recognized by the request that collects all of the
protocols that the given protocol inherits.
2023-11-16 10:58:34 -08:00
Allan Shortlidge
b4c5cfd536 AST: Enumerate expanded attributes in TypeCheckAttr.
We need to avoid triggering semantic attribute requests in TypeCheckAttr
because it happens too early in type checking to trigger some semantic
attribute requests, and we only want to diagnose attributes that were written
in source anyways.
2023-11-01 10:44:28 -07:00
Allan Shortlidge
6d985e34a2 AST: Introduce Decl::getSemanticCustomAttrs().
In order to avoid triggering request cycles as `SemanticDeclAttrsRequest` grows
to cover a larger range of implicit attributes, we need a lighter-weight
accessor for just the CustomAttrs that have been expanded from macros.
Introduce `getSemanticCustomAttrs()` and adopt it where `getSemanticAttrs()`
had been previously used to enumerate macro expanded custom attributes.
2023-10-31 17:02:39 -07:00
Varun Dhand
f289c11814 Fix typo in NameLookup.cpp from paramter to parameter 2023-10-25 19:34:41 +05:30
Rintaro Ishizaki
03bf349778 [Macros] Improve visitation of auxiliary decls
Use the same pattern as 'getAllMembers()'. This supports nested macro
expansion:
```
std::function<void(Decl *)> visit;
visit = [&](Decl *d) {
  doIt(d);
  d->visitAuxiliaryDecls(visit);
};
for (auto *d : decls)
  visit(d);
```

Don't visit auxiliary decls in `PrintAST::visit(Decl *)` this function
is only intended for single decl printing. The caller should visit them
separately. For that, add
`ModuleDecl::getTopLevelDeclsWithAuxiliaryDecls()`
2023-10-23 14:51:12 -07:00
Harlan Haskins
4ac34a40ea @retroactive conformance syntax and checking (#36068) 2023-10-20 14:27:03 -07:00
Kavon Farvardin
c01360d02e [Sema] reimplement ~C as an general inverse constraint 2023-09-20 09:34:06 -07:00
Slava Pestov
973a5065e2 Merge pull request #68282 from slavapestov/sema-tuple-conformances
(More) Declaration checker support for tuple conformances
2023-09-07 11:57:05 -04:00
Allan Shortlidge
0dd8f4c492 AST: Introduce abstraction for extension/type decl inheritance clauses.
Wrap the `InheritedEntry` array available on both `ExtensionDecl` and
`TypeDecl` in a new `InheritedTypes` class. This class will provide shared
conveniences for working with inherited type clauses. NFC.
2023-09-06 10:41:57 -07:00
Slava Pestov
d5cdfb2cf2 AST: Clone generic parameter list of tuple extension from extended typealias 2023-09-05 23:21:27 -04:00
Slava Pestov
fd06bd87aa AST: Allow the extended type of an extension to refer to be a tuple type
Either directly, or via a type alias.
2023-09-05 23:21:26 -04:00
Doug Gregor
b7bfaf3522 [Macros] Fix handling of extension macro conformances and witnesses
Fix two inter-related issues with extension macros that provide
conformances to a protocol, the combined effect of which is that one
cannot meaningfully provide extension macros that implement
conformances to a protocol like Equatable or Hashable that also
supports auto-synthesis.

The first issue involves name lookup of operators provided by macro
expansions. The logic for performing qualified lookup in addition to
unqualified lookup (for operators) did not account for extension
macros in the same manner as it did for member macros, so we would not
find a macro-produced operator (such as operator==) in witness
matching.

The second issue is more fundamental, which is that the conformance
lookup table would create `NormalProtocolConformance` instances for
pre-macro-expansion conformance entries, even though these should
always have been superseded by explicit conformances within the macro
expansion buffers. The end result is that we could end up with two
`NormalProtocolConformance` records for the same conformance. Some
code was taught to ignore the pre-expansion placeholder conformances,
other code was not. Instead, we now refuse to create a
`NormalProtocolConformance` for the pre-expansion entries, and remove
all of the special-case checks for this, so we always using the
superseding explicit conformances produced by the macro expansions (or
error if the macros don't produce them).

Fixes rdar://113994346 / https://github.com/apple/swift/issues/66348
2023-08-16 19:18:36 -07:00
Slava Pestov
59bdf6630c Sema: Move tuple conformance diagnostic to TypeCheckDeclPrimary.cpp 2023-08-09 17:42:25 -04:00
Doug Gregor
6ac69d8c3c Remove findMacroForCustomAttr in favor of namelookup::lookupMacros
The former was effectively a copy of the latter.
2023-07-25 14:16:50 -07:00
Doug Gregor
f0f74e6c5e [Macros] Don't allow types to shadow freestanding macros.
When performing name lookup for freestanding macros (e.g., after the
`#`), don't allow types to shadow macros from imported libraries.

Fixes rdar://110429368.
2023-07-25 11:34:25 -07:00
Doug Gregor
6265f0c542 [SE-0395] Rename _Observation module to Observation
The review of SE-0395 is down to small details at this point that won't
affect the overall shape of the API much. Rename the model in
anticipation of that.
2023-06-30 11:01:02 -07:00
Holly Borla
7a46466b65 Merge pull request #66967 from hborla/extension-macro
[Macros] Generalize `conformance` macros as `extension` macros
2023-06-29 08:57:04 -07:00
Doug Gregor
81acd82cf7 [Name lookup] Shadow the _Observation model as we do with other stdlib modules
Fixes rdar://111464071.
2023-06-28 14:30:03 -07:00
Holly Borla
725374e0d8 [Macros] Implement attached extension macros. 2023-06-27 21:22:12 -07:00
Keith Smiley
e9ff334778 Fix missing indexing data with overloaded type (#65729)
When you have a type that's ambiguous because it's defined in 2 imported
modules, but you don't have to disambiguate by using the module name,
previously no index references were produced. Now most are for the
common case, but notably nested type constructors and generics still
aren't emitted, partially because of https://github.com/apple/swift/issues/65726

Fixes: https://github.com/apple/swift/issues/64598
2023-06-23 09:58:17 -07:00
Rintaro Ishizaki
096d8ea142 [CodeCompletion] Suggest synthesized declarations from macros
* Don't invalidate the lookup cache in 'getOrCreateSynthesizedFile()'
  Adding a synthesized file itself doesn't introduce any decls. Instead,
  we should invalidate the right after the actual declrations are added
  in the file
* Remove 'SourceLookupCache::invalidate()' method. It was just used
  right before the destruction. It was just not necessary
* Include auxiliary decls in 'SourceLookupCache::lookupVisibleDecls()'
  Previously, global symbol completion didn't include decls synthesized
  by peer macros or freestanding decl macros
* Include "auxiliary decls" in visible member lookup, and visible local
  decl lookup
* Hide macro unique names

rdar://110535113
2023-06-22 13:18:46 -07:00
Holly Borla
393b4ceb95 [NameLookup] Move macro-related name lookup operations into the namelookup
namespace.

This moves the `isInMacroArgument` predicate and `lookupMacros` into `namelookup`.
ASTScope still encapsulates the scope tree and contains the operation to lookup
the enclosing macro scope, which then invokes a callback to determine whether a
potential macro scope is indeed a macro, because answering this question requires
name lookup.
2023-06-11 23:10:43 -07:00
Holly Borla
cd752cca22 [NameLookup] Plumb source location arguments through all name lookup APIs.
This source location will be used to determine whether to add a name lookup
option to exclude macro expansions when the name lookup request is constructed.
Currently, the source location argument is unused.
2023-06-11 23:09:47 -07:00
Doug Gregor
a72fb83034 Requestify AbstractStorageDecl::hasStorage().
The `hasStorage()` computation is used in many places to determine the
signatures of other declarations. It currently needs to expand accessor
macros, which causes a number of cyclic references. Provide a
simplified request to determine `hasStorage` without expanding or
resolving macros, breaking a common pattern of cycles when using
macros.

Fixes rdar://109668383.
2023-06-10 08:28:06 -07:00
Doug Gregor
234534e0ce [Member name lookup] Eliminate non-lazy member loading.
Lazy member loading has been in use and the default for several years
now. However, the lazy loading was disabled for any type whose primary
definition was parsed even though some of its extensions could have
been deserialized, e.g., from a Clang module. Moreover, the non-lazy
path walked all of the extensions of such a type for all member name
lookup operations. Faced with a large number of extensions to the same
type (in my example, 6,000), this walk of the list of the extensions
could dominate type-checking time.

Eliminate all effects of the `-disable-named-lazy-member-loading`
flag, and always use the "lazy" path, which effectively does no work
for parsed type definitions and extensions thereof. The example with
6,000 extensions of a single type goes from type checking in 6 seconds
down to type checking in 0.6 seconds, and name lookup completely
disappears from the profiling trace.

The deleted tests relied on the flag that is now inert. They aren't by
themselves providing much value nowadays, and it's better to have the
simpler (and more efficient) implementation of member name lookup be
the only one.
2023-06-02 14:56:03 -07:00
Slava Pestov
7499c222ee AST: Requestify lookup of protocol referenced by ImplementsAttr
Direct lookup relied in primary file checking to have filled in the
protocol type stored in the ImplementsAttr. This was already wrong
with multi-file test cases in non-WMO mode, and crashed in the
ASTPrinter if printing a declaration in a non-primary file.

I don't have a standalone test case that is independent of my
upcoming ASTPrinter changes, but this is a nice cleanup regardless.
2023-06-02 13:17:25 -04:00
Doug Gregor
2f41499bde [Name lookup] Make sure to update macro-expanded list when an extension is added 2023-05-31 17:12:43 -07:00
Doug Gregor
54d2ea0a17 [Member name lookup] Only look to expand macros in types/extensions that matter
Keep track of those types and extensions that have any macro expansions,
so that when we need to check whether to expand macros to satisfy name
lookup, we only look through that list---rather than looking at every
extension again.

This improves compile times significantly for a source file with a
large number of extensions on a single type. However, there is still
much to do here.
2023-05-31 16:06:09 -07:00
Doug Gregor
af3ea4d142 [Name lookup] Only walk types/extensions to expand macros when we need to
Keep track of which types/extensions have members that could be produced by
a macro expansion, including the names of those members. Use this to
avoid walking into extensions or type definitions to expand macros
when they don't have any related macros.
2023-05-31 15:37:46 -07:00
Doug Gregor
a690fc4abb [Macros] Fix lazy expansion of freestanding macros within types/extensions
An incorrectly-specified starting context for name lookup meant that
macro lookup itself was failing, and therefore did not trigger
expansion of the freestanding macro or introduction of the
macro-expanded declarations into the context.

Fixes rdar://108824784.
2023-05-04 12:26:12 -07:00
Holly Borla
336da919fe [Features] Add a dedicated experimental feature flag for tuple conformances. 2023-04-15 17:23:25 -07:00
Doug Gregor
527a4cdd42 [Macros] Suppress lookups into macro expansions in direct lookup in types/extensions
Eliminate a source of cyclic dependencies by not expanding macros when
we are resolving macro arguments within a type or extension context.
This extends the scheme introduced for module-scope lookup to also
apply to lookup within types.
2023-04-06 16:04:43 -07:00
Doug Gregor
f7e479759d Merge pull request #64854 from DougGregor/top-level-macro-lookup 2023-04-03 06:50:39 -07:00
Doug Gregor
828de17b00 [Macros] Resolve macro names using unqualified lookup that ignores expansions
The macro name resolution in the source lookup cache was only looking at
macros in the current module, meaning that any names introduced by peer
or declaration macros declared in one module but used in another would
not be found by name lookup.

Switch the source lookup cache over to using the same
`forEachPotentialResolvedMacro` API that is used by lookup within
types, so we have consistent name-lookup-level macro resolution in both
places.

... except that would be horribly cyclic, of course, so introduce name
lookup flags to ignore top-level declarations introduced by macro
expansions. This is semantically correct because macro expansions are
not allowed to introduce new macros anyway, because that would have
been a terrible idea.

Fixes rdar://107321469. Peer and declaration macros at module scope
should work a whole lot better now.
2023-04-02 23:15:38 -07:00
Doug Gregor
7dca692c01 Merge pull request #64840 from DougGregor/macro-lookup-in-types 2023-04-02 07:23:58 -07:00
Doug Gregor
706b3e1d62 [Name lookup] Match macro introduced names based on their base names.
Some lookups, such as the ones used for protocol conformance checking,
provide full names (with arguments). Drop everything down to simple names
so they we don't skip macro expansions when we might need them.
2023-04-01 21:42:46 -07:00
Doug Gregor
e4365535e3 [Macros] Make sure to avoid full macro resolution in name lookup.
When populating the name lookup tables with macro-expanded declarations,
we were careful to avoid full macro resolution for attached macros, but
freestanding macros were will doing full resolution. Switch
freestanding macros over to the same partial resolution mechanism to
avoid reference cycles.
2023-04-01 00:36:42 -07:00
Doug Gregor
9af2de42f2 [Macros] Only expand member macros that can affect qualified name lookup.
Rather than eagerly expanding all member macros when doing any name
lookup into a type, use the same introduced-name-tracking logic we
apply to peer macros to only expand those member macros that can
produce the requested name. This makes the compiler lazier.
2023-04-01 00:25:26 -07:00
Doug Gregor
5e4118ad97 Allow expansion of member macros on extensions and make those names visible.
Ensure that name lookup triggers the expansion of member macros on
extensions, so we can find those names. And fix a bug that caused
expansion of member macros to crash.
2023-03-31 23:29:40 -07:00
Doug Gregor
467f8f5e0f [Macros] Ensure that we can find peer macros for members of extensions.
When populating the name lookup tables for a nominal type, we were only
visiting declarations with peer macros within the nominal type
declaration itself. Also visit the members of extensions of the
nominal type.

Without this change, peer-macro-defined members of extensions are not
visible.
2023-03-31 23:05:18 -07:00
Richard Wei
eb8e984b97 [Macros] Private discriminators for outermost-private MacroExpansionDecl (#64813)
Add a private discriminator to the mangling of an outermost-private `MacroExpansionDecl` so that declaration macros in different files won't have colliding macro expansion buffer names.

rdar://107462515
2023-03-31 20:36:29 -07:00
QuietMisdreavus
e45030214d add a source location for GenericTypeParamDecls (#64688)
rdar://105982860
2023-03-29 10:17:46 -06:00
Angela Laar
3a20ff0826 [NameLookup] Rename opaque type collector function 2023-03-15 18:07:26 -07:00
Angela Laar
ac00a8cd29 [NameLookup] Collect implicit opaque GenericParams
Plain protocols with generic type paramters should
be collected as opaque types.
2023-03-15 16:00:33 -07:00
Doug Gregor
6ab3bc27f3 Ensure that we lazily find macro-unique names in nominal type context 2023-03-07 12:30:37 -08:00
Richard Wei
31d73b81a2 Merge pull request #64071 from rxwei/macro-aux-decl
Always use `Decl::visitAuxiliaryDecls` to visit decls produced by macros, including peer macros and declaration macros. Use name-driven expansion for peer macros. Remove `MacroExpansionDecl::getRewritten()`.

Also make `ExpandMacroExpansionDeclRequest` cache the buffer ID (similar to other macros) instead of an array of decls.
2023-03-05 03:19:58 -08:00
Richard Wei
98c2a837d2 [Macros] Always visit macro-produced decls as auxiliary decls
Always use `Decl::visitAuxiliaryDecls` to visit decls produced by macros, including peer macros and declaration macros. Use name-driven expansion for peer macros. Remove `MacroExpansionDecl::getRewritten()`.

Also make `ExpandMacroExpansionDeclRequest` cache the buffer ID (similar to other macros) instead of an array of decls.
2023-03-04 23:48:21 -08:00
Alastair Houghton
44783e72c6 [Frontend] Add support for implicit import of _Backtracing
Once the API has gone through Swift Evolution, we will want to implicitly
import the _Backtracing module.  Add code to do that, but set it to off
by default for now.

rdar://105394140
2023-03-04 08:00:06 +00:00
Doug Gregor
bd35cdb865 Merge pull request #63985 from DougGregor/expression-macros-effects 2023-03-01 06:52:43 -08:00