Commit Graph

116 Commits

Author SHA1 Message Date
Pavel Yaskevich
cf257aa64a [PreCheck] Filter out macro declarations from result set
Move logic from `ConstraintGenerator::visitOverloadedDeclRefExpr`
to pre-check to avoid including macro declarations referenced
without `#`. This means that pre-checking would synthesize
`TypeExpr` in situations when there is a type reference that
is shadowed by a stdlib macro.

Resolves: https://github.com/apple/swift/issues/67815
Resolves: rdar://114796811
2023-08-31 17:30:14 -07:00
Becca Royal-Gordon
fe6753485f [NFC] Adopt new diagnostic features across Sema 2023-07-20 15:23:47 -07:00
Becca Royal-Gordon
1b81c3de7b [NFC] Adopt ValueDecl * for decl_declared_here 2023-07-19 13:08:12 -07:00
Evan Wilde
f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -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
Pavel Yaskevich
2b8a39724c Merge pull request #66513 from xedin/init-accessor-diagnostics
[Sema/SIL] Improve diagnostics related to init accessors
2023-06-14 09:57:08 -07:00
Pavel Yaskevich
5613006944 [Sema] PreCheck: Diagnose standalone self within init accessors
'self' within init accessor could only be used to refer to
properties listed in `initializes` and `accesses` attributes.
2023-06-13 10:58:50 -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
db23cf5f32 [PreCheckExpr] Don't perform typo correction inside macro arguments. 2023-06-11 23:09:48 -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
Hamish Knight
3f7c3a2faf Handle #if for if/switch expressions
- Allow an if/switch expression to become an
implicit return of a function that has a `#if`
body with a single active element that is an `if`
or `switch`.
- Allow `#if` branches of an if/switch expression,
as long as there is a single active expression
element.

rdar://107487977
2023-04-12 14:54:22 +01:00
Hamish Knight
2976edbe20 [CS] Rename SolutionApplicationTarget -> SyntacticElementTarget 2023-03-06 20:54:06 +00:00
Holly Borla
2d20644d95 Merge pull request #64105 from hborla/pack-element-type-expr-folding
[PreCheckExpr] Allow pack references when resolving types during `TypeExpr` folding.
2023-03-04 18:42:42 -08:00
Holly Borla
a8ca989072 [PreCheckExpr] Allow pack references when resolving types during TypeExpr folding. 2023-03-04 12:35:01 -08:00
Doug Gregor
0b2fe8f9a9 [Macros] Ensure that overloading a type and macro doesn't break type references
Fixes rdar://106044286
2023-03-02 22:37:49 -08:00
Doug Gregor
200f2340d9 [Macros] Be deliberate about walking macro arguments vs. expansions
Provide ASTWalker with a customization point to specify whether to
check macro arguments (which are type checked but never emitted), the
macro expansion (which is the result of applying the macro and is
actually emitted into the source), or both. Provide answers for the
~115 different ASTWalker visitors throughout the code base.

Fixes rdar://104042945, which concerns checking of effects in
macro arguments---which we shouldn't do.
2023-02-28 17:48:23 -08:00
Anthony Latsis
107ae6d589 Merge pull request #63784 from AnthonyLatsis/sugar-type-members-4
PreCheckExpr: Recognize non-identifier type qualifiers in type expressions
2023-02-23 21:19:43 +03:00
Anthony Latsis
a0d1b4300d PreCheckExpr: Recognize non-identifier type qualifiers in type expressions 2023-02-22 21:32:16 +03:00
Slava Pestov
e0c341d0ac AST: Rename PackReferenceTypeRepr => PackElementTypeRepr 2023-02-15 22:37:07 -05:00
Slava Pestov
2f1a88ccb1 Sema: Fold PackElementExpr(TypeExpr(R)) => TypeExpr(PackReferenceTypeRepr(R)) 2023-02-15 21:59:22 -05:00
Hamish Knight
a40f1abaff Introduce if/switch expressions
Introduce SingleValueStmtExpr, which allows the
embedding of a statement in an expression context.
This then allows us to parse and type-check `if`
and `switch` statements as expressions, gated
behind the `IfSwitchExpression` experimental
feature for now. In the future,
SingleValueStmtExpr could also be used for e.g
`do` expressions.

For now, only single expression branches are
supported for producing a value from an
`if`/`switch` expression, and each branch is
type-checked independently. A multi-statement
branch may only appear if it ends with a `throw`,
and it may not `break`, `continue`, or `return`.

The placement of `if`/`switch` expressions is also
currently limited by a syntactic use diagnostic.
Currently they're only allowed in bindings,
assignments, throws, and returns. But this could
be lifted in the future if desired.
2023-02-01 15:30:18 +00:00
Hamish Knight
df2b3b2880 [Sema] Introduce PreCheckReturnStmtRequest
Factor out some type-checking logic for ReturnStmt,
including the conversion to FailStmt, into a
request. We can then invoke this request from
both the regular type-checking path, as well as
during a pre-check of an expression.
2023-02-01 15:30:17 +00:00
Holly Borla
6e28f996ce [ConstraintSystem] Create opened pack element generic environments lazily
in the constraint system.
2023-01-07 09:50:14 -08:00
Holly Borla
db37b4c855 [PreCheck] Remove PreCheckExpression::isPostfixEllipsisOperator. 2023-01-07 09:50:14 -08:00
Holly Borla
74bf2f3e7a [AST] Introduce a syntactic distinction betweek vararg type reprs and
pack expansion type reprs.

Classic variadic parameters still use the postfix ellipsis syntax, and
pack expansion types now use a prefix 'repeat' keyword.
2023-01-07 09:50:11 -08:00
Holly Borla
89fa17d74c [Parser] Replace postfix ellipsis with a prefix 'repeat' keyword for pack
expansion expressions, and create them in the parser instead of during
pre-checking in the constraint system.
2023-01-07 09:49:14 -08:00
Holly Borla
ab4ec2b6cb [AST] Remove trailing pack elements from PackExpansionExpr.
This wasn't used, and callers can instead use getExpandedPacks, which
also includes TypeReprs.
2023-01-07 09:49:14 -08:00
Anthony Latsis
ad5d55c36e [NFC] AST: Rename IdentTypeReprDeclRefTypeRepr 2023-01-07 07:14:44 +03:00
Holly Borla
8956de43a3 [ConstraintSystem] Audit remaining arguments for packElementOpener to type
resolution.
2022-12-21 07:01:45 -05:00
Holly Borla
58953567b3 [CSGen] Use PackExpansionExpr::getExpandedPacks to generate ShapeOf constraints
rather than relying on PackElementExprs collected by preCheck.

This handles pack element expressions and pack element type reprs, and enforces that
all packs expanded by a given pack expansion expression all have the same shape.

Once the parser creates PackExpansionExpr directly (based on a dedicated syntax instead
of postfix '...'), the code in preCheck for identifying and creating pack expansions
can simply be deleted.
2022-12-20 22:34:16 -05:00
Holly Borla
7b35e85d31 [PreCheckExpr] Consider postfix ellipsis expressions containing 'each' type
reprs to be pack expansion expressions.
2022-12-20 11:36:58 -05:00
Holly Borla
454913aeb5 [TypeResolution] Plumb a pack element opener function through type resolution. 2022-12-20 11:36:58 -05:00
Holly Borla
42020792c6 [Sema] Represent pack element references in pack expansion patterns as
PackElementExpr.
2022-12-14 20:45:52 -08:00
Slava Pestov
0a2b7ac8cd Sema: Form pack expansion types in simplifyTypeExpr() 2022-12-06 14:15:17 -05:00
Slava Pestov
6ecab8ac35 Sema: Refactor PreCheckExpr.cpp a bit 2022-12-06 14:15:07 -05:00
Holly Borla
8ec5405a30 [GenericEnvironment] Store a substitution map for the outer generic parameters
of an opened pack element generic environment.
2022-11-23 11:17:05 -05:00
Holly Borla
d09ea98ba6 [GenericEnvironment] Add helper methods to map pack interface types to
element archetypes, and element interface types to pack archetypes.
2022-10-26 00:04:56 -07:00
Holly Borla
1ab9f8f7c6 [AST] Add helper methods to GenericTypeParamType for converting a given type
parameter to and from a pack type parameter.
2022-10-26 00:04:56 -07:00
Holly Borla
e80ee92898 [PreCheckExpr] Gate PackExpansionExpr creation behind the variadic generics flag. 2022-10-24 22:45:50 -07:00
Holly Borla
7354ad30c3 [ConstraintSystem] Use opened element types for opaque values in pack expansions. 2022-10-24 18:11:00 -07:00
Holly Borla
dab3b64c99 [ConstraintSystem] Implement basic type checking for pack expressions.
During prechecking, postfix '...' expressions are rewritten to pack
expansion expressions if the operand contains pack references. References
to packs are replaced with opaque values, and the pack expansion stores
the opaque value bindings to decl refs.
2022-10-22 13:41:48 -07:00
Slava Pestov
fbab90b89a Sema: Fix crash when pre-checking expression of the form P(<<literal>>) where P is a protocol
Fixes https://github.com/apple/swift/issues/61517
2022-10-14 21:29:51 -04:00
Pavel Yaskevich
1a79d93121 Merge pull request #40702 from calda/cal--implicit-weak-self
[SE-0365] Allow implicit self for `weak self` captures
2022-09-30 10:30:57 -07:00
Cal Stephens
03322bf716 Move implicit self lookup from resolveDeclRefExpr into ASTScope::unqualifiedLookup implementation 2022-09-20 07:06:54 -07:00
Cal Stephens
95949cec47 Use ASTScope::lookupUnqualified instead of TypeChecker::resolveDeclRefExpr 2022-09-17 14:20:59 -07:00
Cal Stephens
9e9f7be0f6 Resolve UDRE to DRE in PreCheckExpr 2022-09-15 20:03:13 -07:00
Cal Stephens
84896edaf2 Use Ctx.Id_self and update comments 2022-09-15 20:01:40 -07:00
Hamish Knight
4716f61fba [AST] Introduce explicit actions for ASTWalker
Replace the use of bool and pointer returns for
`walkToXXXPre`/`walkToXXXPost`, and instead use
explicit actions such as `Action::Continue(E)`,
`Action::SkipChildren(E)`, and `Action::Stop()`.
There are also conditional variants, e.g
`Action::SkipChildrenIf`, `Action::VisitChildrenIf`,
and `Action::StopIf`.

There is still more work that can be done here, in
particular:

- SourceEntityWalker still needs to be migrated.
- Some uses of `return false` in pre-visitation
methods can likely now be replaced by
`Action::Stop`.
- We still use bool and pointer returns internally
within the ASTWalker traversal, which could likely
be improved.

But I'm leaving those as future work for now as
this patch is already large enough.
2022-09-13 10:35:29 +01:00
Cal Stephens
9167a177cc Merge branch 'main' into cal--implicit-weak-self 2022-09-11 10:24:55 -07:00
Slava Pestov
da76e1ee71 Sema: Allow one-element tuples when -enable-experimental-variadic-generics is on 2022-09-10 00:26:43 -04:00