Commit Graph

295 Commits

Author SHA1 Message Date
Alex Hoppen
c403fab547 Merge pull request #63860 from ahoppen/ahoppen/report-solution-callback
[IDE] Report solutions from a result builder transform to a SolutionCallback
2023-02-27 19:23:02 +01:00
Alex Hoppen
9cc300878f [IDE] Report solutions from a result builder transform to a SolutionCallback 2023-02-23 14:31:21 +01:00
swift-ci
815e8cc8e7 Merge pull request #63714 from ahoppen/ahoppen/ideinspectiontarget-charsourcerange
[IDE] Check whether an AST node contains the IDE inspection point based on `CharSourceRange`
2023-02-20 10:20:45 -08:00
Alex Hoppen
8bdf68d483 [IDE] Check whether an AST node contains the IDE inspection point based on CharSourceRange
This fixes an issue if the range ends with a string literal that contains the IDE inspection target. In that case the end of the range will point to the start of the string literal but the IDE inspection target is inside the string literal and thus after the range’s end.
2023-02-20 15:37:38 +01:00
Hamish Knight
eac3fb4506 [CS] NFC: Default the allowFreeTypeVariables parameter
All but one client wants to set this to anything
other than `FreeTypeVariableBinding::Disallow`.
2023-02-17 20:58:46 +00:00
Hamish Knight
b885da4721 [CS] NFC: Fix some bad formatting 2023-02-17 20:58:44 +00:00
Pavel Yaskevich
5ce8c72366 [BuilderTransform] NFC: Fix a typo in debug output header for transformed body 2023-02-16 01:15:04 -08:00
Pavel Yaskevich
f27369d93a [BuilderTransform] Add contextual type to transformation information
This information would be used to decide whether conjunction that
represents transformed closure should be delayed or not.
2023-02-14 16:30:42 -08:00
Hamish Knight
c87b1b8bef Merge pull request #63022 from hamishknight/express-yourself 2023-02-03 16:40:09 +00: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
0ea42cf41f [CS] NFC: Factor out brace element handling in a couple of cases
Split out brace element handling for a couple of
walkers into a new function. The diff for this is
best viewed without whitespace changes. This
should be NFC.
2023-02-01 15:30:16 +00:00
Alex Hoppen
2c5b193892 [Sema] Type check multi-statement closures inside result builders 2023-02-01 10:29:56 +01:00
Pavel Yaskevich
7fb1ea05aa [BuilderTransform] Increase impact of unhandled statement fix
The impact should be very high because the whole body is skipped
if the builder does not support some of the operations, otherwise
the fix would interfere with result builders that do support
operations but have syntactic issues in the body.

Resolves: rdar://89880662
2023-01-23 10:58:27 -08:00
Pavel Yaskevich
43d83bb461 [BuilderTransform] All if sequences without else should call buildOptional
In cases like:

```
if (...) {
  <body>
} else if (...) {
  <body>
} else if (...) {
  <body>
}
```

Each branch should end with `buildOptional` wrapping `buildEither`
because there is no covering unconditional `else` in the sequence:

```
var $__builder: <<Type>>

if (...) {
  $__builder = buildOptional(.some(buildEither(...)))
} else {
  if (...) {
    $__builder = buildOptional(.some(buildEither(...)))
  } else {
    if (...) {
      $__builder = buildOptional(.some(buildEither(...)))
    } else {
      $__builder = buildOptional(.none)
    }
  }
}
```

Resolves: rdar://104345754
2023-01-18 13:26:20 -08:00
Pavel Yaskevich
945f35f7ca [BuilderTransform] Make sure that synthesized if elements have correct locations
Placeholder variable that represents result of `if` should be placed
at the beginning of the statement, same goes for `Optional(.some(...))`
that wraps the expression in "then" branch.

Resolves: https://github.com/apple/swift/issues/62848
2023-01-10 12:33:30 -08:00
Pavel Yaskevich
b01d21b1ec [BuilderTransform] NFC: Correctly indent transformed body debug printing 2022-12-21 12:18:02 -08:00
Pavel Yaskevich
529269946d [AST] Drop ResultBuilderASTTransform feature flag 2022-12-21 12:18:02 -08:00
Alex Hoppen
3c90c892e9 [CodeCompletion] Drop ForCodeCompletion constraint system option for syntactic elements that don't contain completion token 2022-12-21 12:17:56 -08:00
Alex Hoppen
c03addb1e0 [CodeCompletion] Insert code completion token into build block call to retrieve contextual type
rdar://94224692
2022-12-21 10:31:46 -08:00
Alex Hoppen
fe2ae72ad2 [IDE] Rename CodeCompletion to IDEInspection in cases where the code path no longer exclusively applies to code completion
The code completio infrastructure is also being used for cursor info now, so it should no longer be called code completion.

rdar://103251187
2022-12-13 11:41:05 +01:00
Pavel Yaskevich
6b3776456c Merge pull request #62053 from abdulowork/improve-type-checker-debugging-indentation
Improve indentation in type checker debugging output
2022-11-30 12:11:27 -08:00
Pavel Yaskevich
2e0cc04ba1 Merge pull request #62227 from xedin/result-builder-ast-transform-direct-buildBlock
[ResultBuilder] ASTTransform: Don't capture buildBlock into a separat…
2022-11-28 10:18:53 -08:00
Pavel Yaskevich
8060fa0844 [ResultBuilder] ASTTransform: Don't capture buildBlock into a separate variable
Inject `buildBlock` call into `return`, `buildFinalResult`, and
`build{Optional, Either}` to take advantage of contextual information
and maintain compatibility with current inference behavior.
2022-11-22 11:42:18 -08:00
Erik Eckstein
ab1b343dad use new llvm::Optional API
`getValue` -> `value`
`getValueOr` -> `value_or`
`hasValue` -> `has_value`
`map` -> `transform`

The old API will be deprecated in the rebranch.
To avoid merge conflicts, use the new API already in the main branch.

rdar://102362022
2022-11-21 19:44:24 +01:00
Timofey Solonin
3f366947e4 Improve indentation in debugging output 2022-11-17 23:25:31 +08:00
Hamish Knight
1846276a13 [CS] Factor out brace element handling in a couple of cases
Split out brace element handling for a couple of
walkers into a new function. The diff for this is
best viewed without whitespace changes. This
should be NFC.
2022-11-14 20:03:41 +00:00
Pavel Yaskevich
480bccae55 [BuilderTransform] Lift all restrictions from variable decls when AST transform is enabled
Result builders would be able to declare:
  - uninitialized variables
  - variables with property wrappers (with and without explicit initializer)
  - default initializable variables i.e. `var x: Int?`
  - computed variables
  - `lazy` variables
2022-10-04 13:28:17 -07:00
Ben Barham
79b0cd60a8 [Sema] Skip adding solutions to completion callback if needed
If `CompletionContextFinder` fails to find a `CompletionNode`, skip
trying to filter and add solutions to the completion callback. This
prevents an assertion/crash in `filterSolutionsForCodeCompletion` which
assumes `CompletionNode` is non-null (either an expression or keypath).

Resolves rdar://99966094.
2022-09-16 11:39:39 -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
Allan Shortlidge
f5f1d3c028 AST: Parse #_hasSymbol
Introduce the compiler directive `#_hasSymbol` which will be used to detect whether weakly linked symbols are present at runtime. It is intended for use in combination with `@_weakLinked import` or `-weak-link-at-target`.

```
if #_hasSymbol(foo(_:)) {
  foo(42)
}
```

Parsing only; SILGen is coming in a later commit.

Resolves rdar://99342017
2022-09-08 17:15:29 -07:00
Pavel Yaskevich
87816457c1 [BuilderTransform] AST Transform: Inject buildOptional to top-level if else conditions
Fixes a bug in `buildOptional` injection for `if else` branches
without unconditional `else` which leaves type-join with just `.some(...)`
and results in optionality mismatch if `buildOptional` don't not produce
an optional type.
2022-09-01 13:31:00 -07:00
Hamish Knight
db2a9a470b Merge pull request #60803 from hamishknight/part-and-partial 2022-08-31 14:57:53 +01:00
Holly Borla
ca08b56fd3 Merge pull request #60844 from hborla/generic-result-builder
[ConstraintSystem] Before applying the result builder transform to a function body, map the result builder type into context.
2022-08-30 08:06:58 -07:00
Holly Borla
8ba51888c6 [ConstraintSystem] Before applying the result builder transform to a
function body, map the result builder type into context.

This was already done for inferred result builder attributes; now,
the constraint system will map the builder type into context for all
result builder attributes applied to computed properties/functions.
2022-08-29 22:13:13 -07:00
Hamish Knight
cebcbb0767 [CS] Improve diagnostics when buildPartialBlock is unavailable
If `buildBlock` is also unavailable, or the
builder itself is unavailable, continue to solve
using `buildPartialBlock` to get better
diagnostics.

This behavior technically differs from what is
specified in SE-0348, but only affects the invalid
case where no builder methods are available to use.

In particular, this improves diagnostics for
RegexComponentBuilder when the deployment target
is too low. Previously we would try to solve using
`buildBlock` (as `buildPartialBlock` is unavailable),
but RegexComponentBuilder only defines `buildBlock`
for the empty body case, leading to unhelpful
diagnostics that ultimately preferred not to use
the result builder at all.

rdar://97533700
2022-08-26 19:45:03 +01:00
Hamish Knight
dd1da22b16 [CS] Separate availability checking from SupportedOps cache
Previously we would cache the result of the first
query, with any further query of
`ResultBuilder::supports` ignoring the
`checkAvailability` parameter. Separate out the
availability checking such that we compute the
information up front, and adjust the result
depending on `checkAvailability`.
2022-08-26 19:45:02 +01:00
Suyash Srijan
390d8cc44b [IDE] Add result builder code completion for buildPartialBlock (#60669) 2022-08-20 15:36:15 +01:00
Pavel Yaskevich
93417d9f94 Merge pull request #60495 from amritpan/refactor-solver-state-indents
[ConstraintSystem] NFC: Refactor solver state depth to its own function fo…
2022-08-11 00:30:09 -07:00
Ben Barham
a59fa46813 Merge pull request #60426 from bnbarham/include-smallvec
[Basic] Include SmallVector.h for Clang <= 5
2022-08-10 15:54:48 -07:00
Amritpan Kaur
fd41a39294 [ConstraintSystem] Refactor solver state depth to its own function for easier indent editing in future. 2022-08-10 13:29:02 -07:00
Pavel Yaskevich
2ebc7a266c Merge pull request #60449 from xedin/dont-erase-result-during-transform
[ResultBuilder] AST transform: don't try type erasure of result expressions
2022-08-09 11:30:06 -07:00
Pavel Yaskevich
422dbe493a [ResultBuilder] AST transform: don't try type erasure of result expressions
Constraint generation is going to erase when appropriate and rewrite the target,
no need to do that explicitly during result builder transform.
2022-08-08 13:43:28 -07:00
Pavel Yaskevich
b39302a585 Merge pull request #60203 from xedin/rdar-96997534
[CSGen] Allow `_` pattern to be a hole
2022-08-08 10:14:18 -07:00
Ben Barham
6de34f37e5 [NFC] Revert SmallVector<T> -> SmallVector<T, N> fixes
With the change to include `SmallVector.h` directly in `LLVM.h` rather
than forward declaring in the only case it matters (ie. Clang <= 5),
these fixes are no longer needed. Since defaulted version is preferred
when there's no better choice (which is presumably the case if that's
how they were originally added), use it instead. Some uses were instead
changed to add `llvm::` so remove that too.
2022-08-05 21:25:55 -07:00
Pavel Yaskevich
f43aad2222 [BuilderTransform] Use correct locator for case subject expressions
Instead of `CTP_Initialization` transform should use `CTP_CaseStmt`
to identify the root correctly.
2022-08-03 15:50:33 -07:00
Meghana Gupta
d58560e65c Fix source to use template <typename T, unsigned N> class SmallVector
The SmallVector version with default template does not compile with older compiler versions
2022-08-03 12:00:57 -07:00
Alex Hoppen
a368f024bb [TypeChecker] Use the brace statement as the start location of a buildLimitedAvailability call
Otherwise, the call to buildLimitedAvailability has a bigger source range than the brace statement that surrounds it, causing a verification failure.
2022-08-02 11:59:03 -07:00
Pavel Yaskevich
186bf0ab3b [BuilderTransform] Cache previously applied transforms
Since result builder is just an AST transformation, the result
of a successful transform could be cache and reused with a different
`$__builderSelf` type.
2022-08-02 11:59:03 -07:00
Pavel Yaskevich
04952fdc0a [TypeChecker] Implement result builder transform via AST modification
The transform changes closure body into a multi-statement closure
with all of the implicit result builder calls and type-checks it
like a regular closure.

There are a couple of result builder specific changes mentioned below,
otherwise the logic to generate constraints and apply solutions is
unchanged:

- Placeholder variable: A variable declaration that doesn't have a
  type deduced and infers it from its first use. If such a variable
  has an initializer, it would be type-checked during solution application.

- TypeJoinExpr - an implicit expression that refers to a "join" variable
  and a set of expressions that should all produce the same type that
  becomes a type of a "join" variable.
2022-08-02 11:59:03 -07:00
Pavel Yaskevich
8850dcfea4 [BuilderTransform] Move some of the common logic into a base class
The base class is intended to facilitate logic sharing between
existing one-way constraint based transform and the new AST one.
2022-08-02 11:03:26 -07:00