Commit Graph

75 Commits

Author SHA1 Message Date
Owen Voorhees
43e2d107e1 [SE-0276] Implement multi-pattern catch clauses
Like switch cases, a catch clause may now include a comma-
separated list of patterns. The body will be executed if any
one of those patterns is matched.

This patch replaces `CatchStmt` with `CaseStmt` as the children
of `DoCatchStmt` in the AST. This necessitates a number of changes
throughout the compiler, including:
- Parser & libsyntax support for the new syntax and AST structure
- Typechecking of multi-pattern catches, including those which
  contain bindings.
- SILGen support
- Code completion updates
- Profiler updates
- Name lookup changes
2020-04-04 09:28:26 -07:00
Robert Widmann
987cd55f50 [NFC] Drop llvm::Expected from Evaluation Points
A request is intended to be a pure function of its inputs. That function could, in theory, fail. In practice, there were basically no requests taking advantage of this ability - the few that were using it to explicitly detect cycles can just return reasonable defaults instead of forwarding the error on up the stack.

This is because cycles are checked by *the Evaluator*, and are unwound by the Evaluator.

Therefore, restore the idea that the evaluate functions are themselves pure, but keep the idea that *evaluation* of those requests may fail. This model enables the best of both worlds: we not only keep the evaluator flexible enough to handle future use cases like cancellation and diagnostic invalidation, but also request-based dependencies using the values computed at the evaluation points. These aforementioned use cases would use the llvm::Expected interface and the regular evaluation-point interface respectively.
2020-03-26 23:08:02 -07:00
Doug Gregor
f0530d0a77 [Function builders] Support buildOptional(_:) in lieu of buildIf(_:).
Line up with the function builders pitch, which uses buildOptional(_:)
to build optional values.
2020-03-09 21:48:15 -07:00
Doug Gregor
ce97d22f21 [Function builders] Add support for buildFinalResult().
When present in a function builder, buildFinalResult() will be called on
the value of the outermost block to form the final result of the closure.
This allows one to collapse the full function builder computation into
a single result without having to do it in each buildBlock() call.
2020-03-09 00:10:07 -07:00
Doug Gregor
3887498e55 [Constraint system] Diagnose @unknown cases in function builders. 2020-03-04 22:46:36 -08:00
Holly Borla
e3239ba5fc [ConstraintSystem] Bail out of applyFunctionBuilderBodyTransform if
there is a failure in constraint generation.
2020-03-03 19:32:06 -08:00
Doug Gregor
12f8b51921 Merge pull request #30189 from DougGregor/function-builders-switch-exhaustive
[Constraint solver] Check switch exhaustiveness in function builders
2020-03-03 18:48:39 -08:00
Doug Gregor
7ee33a920d [Constraint solver] Check switch exhaustiveness in function builders 2020-03-03 13:44:36 -08:00
Doug Gregor
8191aa4924 Merge pull request #30174 from DougGregor/function-builder-switch
[Constraint system] Implement switch support for function builders.
2020-03-03 12:34:08 -08:00
Doug Gregor
ea8d143f64 [Constraint system] Introduce a one-way constraint for a switch subject.
The normal type checking of switch statements checks the switch subject
first, without context, then evaluates the cases. Introduce a one-way
constraint into the type checking of switch statements within function
builders to provide this same behavior. The difference can be observed
in code such as:

    enum E {
      case a
      case b(Int, String?)
    }

    enum E2 {
      case b(Int, String?)
    }

    func getSomeEnumOverloaded(_: Double) -> E { return .a }
    func getSomeEnumOverloaded(_: Int) -> E2 { return .b(0, nil) }

    func f() {
      switch getSomeEnumOverloaded(17) {
      case .a:  // error: no member named "a" in E2
        print("a")
      default:
        print("default")
      }
    }

When the subject expression `getSomeEnumOverloaded(17)` is resolved
without consider cases, it will select the second
`getSomeEnumOverloaded(_:)`, because the literal 17 prefers to be
an `Int`. The type checking of the first case would then fail because E2
does not contain a member named "a".

Prior to this change, the same expression within a function
builder would succeed in type checking, because the lack of case named
"a" within "E2" would make the second getSomeEnumOverloaded() unusable.

Making this code work by considering the cases along with the subject
expression is not unreasonable, and may be the right long term
direction for the language. However, it's a feature that should be
discussed separately, and the semantics should agree between function
builders and normal statements.

Big thanks to John McCall for noting the missing one-way constraint!
2020-03-03 08:51:26 -08:00
Doug Gregor
4b43573693 [Constraint system] Implement switch support for function builders.
Implement support for switch statements within function builders. Cases can
perform arbitrary pattern matches, e.g.,

    tuplify(true) { c in
      "testSwitchCombined"
      switch e {
      case .a:
        "a"
      case .b(let i, _?), .b(let i, nil):
        i + 17
      }
    }

subject to the normal rules of switch statements. Cases within function
builders cannot, however, include “fallthrough” statements, because those
(like “break” and “continue”) are control flow.

The translation of performed for `switch` statements is similar to that of
`if` statements, using `buildEither(first:)` and `buildEither(second:)` on
the function builder type.

This is the bulk of switch support, tracked by rdar://problem/50426203.
2020-03-02 17:25:25 -08:00
Holly Borla
87bb7755c2 Merge pull request #30101 from hborla/dynamic-replacement-type-erasure
[Sema] Implement type erasure for dynamic replacement.
2020-02-28 09:37:33 -08:00
Holly Borla
3cdc30ffeb [Sema] Support type erasure for dynamic replacement in function
builders.
2020-02-27 09:01:16 -08:00
Doug Gregor
8482516412 [Constraint system] Properly deal with "as" patterns.
Teach pattern matching involving "as" patterns to work properly in
function builders. The code almost handled this, but prematurely
prechecking expressions in patterns broke it.
2020-02-25 16:29:07 -08:00
Doug Gregor
141f3e7f07 [Constraint system] Expand SolutionApplicationTarget to StmtConditions.
Handle StmtCondition as part of SolutionApplicationTarget, so we can
generate constraints from it and rewrite directly as part of a solution,
rather than open-coding the operation in the function builder transform.
2020-02-25 13:47:26 -08:00
Doug Gregor
4830c48960 [Constraint system] Support if let / if case in function builders.
Use the generalized constraint generation and binding for patterns to
introduce support for if-let and if-case in function builders, handling
arbitrary patterns.

Part of function builder generalization, rdar://problem/50426203.
2020-02-25 09:44:06 -08:00
Doug Gregor
3f2f79a699 [Constraint system] Fold pattern variable binding into constraint gen.
Rather than re-walk the pattern to create type bindings for the variables
that show up in the pattern, assign types to each of the variables as part
of constraint generation for the pattern. Only do this in contexts
where we will need the types, e.g., function builders.
2020-02-25 08:51:56 -08:00
Hamish Knight
1a9764e6d7 [CS] Remove SubExpressionDiagnostics option 2020-02-19 07:43:59 -08:00
Doug Gregor
5e28c7c886 [Constraint System] Move bindVariablesInPattern into the constraint system.
This general notion of wiring up the types of variables that occur
within a pattern to the types in the produced pattern type is useful
outside of function builders, too.
2020-02-13 10:51:26 -08:00
Doug Gregor
08315b102d Merge pull request #29786 from DougGregor/function-builders-let-decls
Allow initialized let/var declarations in function builders.
2020-02-12 20:50:28 -08:00
Doug Gregor
d4cd5e0ebc Merge pull request #29801 from DougGregor/constraint-solver-rewrite-target
[Constraint system] Factor out application to a SolutionApplicationTarget
2020-02-12 20:24:41 -08:00
Doug Gregor
2347829324 [Constraint System] Allow initialized let/var declarations in function builders.
Introduce support for initialized let/var declarations within function
builder closures, e.g.,

    let (a, b) = c()

We generate constraints for the declarations as elsewhere, but the types of
the declared variables (a and b in this case) are bound to the type of the
pattern by one-way constraints, to describe the flow of type information
through the closure.

Implements rdar://problem/57330696.
2020-02-12 17:51:48 -08:00
Doug Gregor
7f9029071d [Constraint system] Adopt rewriteTarget for function builder transform.
When applying a function builder to a closure to produce the final,
type-checked closure, use the new rewriteTarget() so it’s performed on
a per-target basis. Use this to eliminate some duplicating in the handling
of return types.
2020-02-12 17:51:47 -08:00
Doug Gregor
151fc79264 Merge pull request #29770 from DougGregor/builder-transform-node-types
[Constraint solver] Fix handling of node types for function builder application
2020-02-11 17:21:16 -08:00
Doug Gregor
4c98b31031 [Constraint solver] Apply solution to the system before builder transform.
This makes sure we can find information in the constraint system during
application. Fixes rdar://problem/59239224
2020-02-11 15:14:44 -08:00
Robert Widmann
d2360d2e8c [Gardening] dyn_cast -> isa 2020-02-07 16:09:31 -08:00
Holly Borla
778f8941ee [MiscDiagnostics] Walk into the body of a non single-statement closure
if the closure had a function builder transform applied.

This way, function builder closures can have syntactic restrictions
diagnosed the same way as other expressions.
2020-02-04 16:29:02 -08:00
Doug Gregor
3981a68a76 Merge pull request #29626 from DougGregor/function-builders-single-expr-closures-return
[Constraint solver] Fix function builders with single-expression closures
2020-02-03 23:10:51 -08:00
Doug Gregor
f8eee50310 [Constraint solver] Fix function builders with single-expression closures
Fix a few related issues involving the interaction with
single-expression closures:

* A single-expression closure can have a "return" in it; in such
cases, disable the function-builder transform.
* Have the function builder constraint generator look through the
"return" statement in a single-expression closure the same way as
solution application does

Fixes rdar://problem/59045763, where we rejected some well-formed code
involving a single-expression closure with a "return" keyword.
2020-02-03 17:52:44 -08:00
Pavel Yaskevich
7453d53e48 [TypeChecker/BuilderTransform] Switch to use hasLValueType instead of is<LValueType> to cover all cases where load is expected 2020-02-03 12:40:28 -08:00
Pavel Yaskevich
6d671020b9 [TypeChecker/BuilderTransform] Make sure implicit load expression updates types in AST
Can't use `ConstraintSystem::addImplicitLoadExpr` because that would
only cache types in constraint system and wouldn't propagate them to AST,
since that happens in `ExprRewritter::finalize` during regular solution
application. `TypeChecker::addImplicitLoadExpr` should be used directly
in cases like that.

Resolves: rdar://problem/58972627
2020-02-03 11:45:55 -08:00
Doug Gregor
eb2862ec1e [Constraint system] Collapse applySolutionImpl() into applySolution().
Now that we have a unified notion of a SolutionApplicationTarget, use it
to collapse 3 applySolution-ish functions into one.
2020-01-30 22:01:12 -08:00
Doug Gregor
cc44f22f43 [Function builder] Eliminate unnecessary dependency on UnderlyingTypeForOpaqueReturnType. 2020-01-27 15:01:09 -08:00
Doug Gregor
23615ba55d [Function builders] Add support for "if #available".
Availability checks in if statements don't need any actual semantic
checking, so enable them within function builders.
2020-01-23 22:00:41 -08:00
Doug Gregor
7217cfa4a5 [Constraint system] Move statement condition constraint generation.
Move constraint generation for statement conditions onto the
constraint system; it doesn't really have any reason to be located
within the function builder transform.
2020-01-23 21:51:53 -08:00
Doug Gregor
6238923e15 [Function builders] Support multiple Boolean conditions in 'if' statements
Generalize support for function builders to allow 'if' statements that
include multiple Boolean conditions, e.g., "if a, b, c, { ... }".
2020-01-23 17:04:17 -08:00
Doug Gregor
70300ada57 [Function builders] Perform syntactic use checks within function builders. 2020-01-22 22:22:36 -08:00
Doug Gregor
2b70025772 [Builder transform] Give generated pattern bindings proper source locations.
ASTScopes completely skip implicit pattern bindings, so don't mark
generated ones as implicit. Instead, give them suitable source
location information.

Fixes rdar://problem/58710568.
2020-01-21 10:12:48 -08:00
Doug Gregor
8283a67648 Revert "Revert "Reimplement function builders as statement transformations."" 2020-01-21 10:07:20 -08:00
Doug Gregor
86c13d3c74 Revert "Reimplement function builders as statement transformations." 2020-01-17 15:52:49 -08:00
Doug Gregor
bfa6d7316d [Function builders] Fake Expr-based locators for now.
The right solution is to extend the notion of the "anchor" of a locator
to also cover statements (and TypeReprs, and Patterns, and more), so
this is a stop-gap.
2020-01-16 13:19:21 -08:00
Doug Gregor
cf96e6559a [Function builders] Minor fixes for lvalue-ness, interface types 2020-01-16 13:19:21 -08:00
Doug Gregor
a49f0091cb [Constraint application] Skip error expressions when applying builders.
If we encountered an error, just skip it; there's nothing more to do.
2020-01-16 13:19:21 -08:00
Doug Gregor
f959ace180 [Function builders] Minor fixes for "load" expr generation 2020-01-16 13:18:33 -08:00
Doug Gregor
c8acc0fc43 [Function builders] Minor fixes for builder transform refactoring.
A couple of trivial fixes as part of the builder transform refactoring:
* When recording captured expressions, record the source entity appropriately
* Make sure to check (and skip) #if declarations in application
* Check and diagnostic #warning/#error as part of application (not generation)
* Simplify the body result type for return coercion during application
* When checking for applicability of a function builder, don't do any
  constraint generation.
2020-01-16 13:18:33 -08:00
Doug Gregor
3a51d5b9b2 [WIP] Reimplement function builders as statement transformations. 2020-01-16 13:18:33 -08:00
Doug Gregor
71350bdb7b Generalize some mentions of closures in function builder application 2020-01-16 13:18:33 -08:00
Doug Gregor
9e6f6d8d3b Rename FunctionBuilderClosurePreCheck -> FunctionBuilderBodyPreCheck 2020-01-16 13:18:33 -08:00
Pavel Yaskevich
af82e6f11b [TypeChecker] Use special locator while matching function builder body to closure result type 2020-01-14 00:09:33 -08:00
Pavel Yaskevich
9dc2dfde98 [ConstraintSystem] Allow single-statement closures be used with function builders
While resolving closure use contextual locator information to
determine whether given contextual type comes from a "function builder"
parameter and if so, use special `applyFunctionBuilder` to open
closure body instead of regular constraint generation.
2020-01-14 00:09:32 -08:00