Commit Graph

1378 Commits

Author SHA1 Message Date
Slava Pestov
9c1bb7189b Sema: Don't type check one-element labeled tuples when forming dynamic member lookups
These are going to be banned shortly, so construct them by other means.
2018-12-15 00:07:04 -05:00
Joe Groff
89979137fc Push ArchetypeType's API down to subclasses.
And clean up code that conditionally works only with certain kinds of archetype along the way.
2018-12-12 19:45:40 -08:00
Suyash Srijan
fa82965a7d [CSApply] Fix an issue with the @objc selector fix-it incorrectly being applied (#20949)
Special-case the diagnostic for members of non-@objc protocols that are referenced in #selector expressions.
2018-12-11 13:19:46 -05:00
Slava Pestov
392071e702 Sema: Peel off more code from the TypeChecker instance 2018-12-10 00:00:49 -05:00
Slava Pestov
06c2e980cb Merge pull request #21133 from slavapestov/lazy-implicit-inits
Lazy synthesis of implicit constructors in non-primary files
2018-12-07 22:45:40 -05:00
Slava Pestov
6c012b2aec AST: Remove some unnecessary LazyResolver * parameters from ASTContext methods 2018-12-07 20:39:27 -05:00
Slava Pestov
aa747dcd81 Remove property behaviors 2018-12-07 20:38:33 -05:00
Adrian Prantl
ff63eaea6f Remove \brief commands from doxygen comments.
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.

Patch produced by

      for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
2018-12-04 15:45:04 -08:00
Azoy
65217a71de Cache bool builtin init
remove newline
2018-12-02 22:38:24 -06:00
Azoy
28fc1ca296 tc.Context is shorter 2018-12-01 18:57:02 -06:00
Azoy
a2dafc3d26 Remove _getBool 2018-12-01 18:51:25 -06:00
Slava Pestov
0a8ee10621 AST: Refactor AbstractStorageDecl::getAccessStrategy() for keypath resilience
Instead of passing in a DeclContext, which we don't have when emitting a keypath
accessor, pass in a ModuleDecl and ResilienceExpansion.

Keypaths now work well enough in inlinable contexts that we can check in an
end-to-end resilience test.
2018-11-16 23:18:30 -05:00
Mark Lacey
018498fb2f Merge pull request #16942 from bjhomer/bjhomer/optional-try-flattening
Flatten nested optionals resulting from try? and optional sub-expressions
2018-11-16 11:14:05 -08:00
John McCall
05c951fdc9 Compute layout when emitting an other-constructor reference.
Fixes SE-9233, a crash due to a failure to validate a storage
declaration within a protocol when generating a call to an
initiializer.
2018-11-13 16:35:26 -05:00
Dan Zheng
2863b6cc64 Gardening for @dynamicMemberLookup. (#20498)
* Gardening for `@dynamicMemberLookup`.

- Unify code style of `@dynamicMemberLookup` and `@dynamicCallable` implementations.
  - Use consistent variable names, diagnostic messages, doc comments, etc.
- Move `@dynamicMemberLookup` test to `test/attr` directory.
2018-11-11 21:54:19 -05:00
Pavel Yaskevich
e50537d66a [CSApply] Handle @autoclosure arg/param conversions in Swift versions < 5
Since it was allowed to pass function types to @autoclosure
parameters in Swift versions < 5, it has to be handled as
a regular function coversion by `coerceToType`.
2018-11-10 11:59:29 -08:00
Pavel Yaskevich
d113b4ae1f [CSApply] Refactor ExprRewriter to handle @autoclosure from parameter flag 2018-11-10 11:59:29 -08:00
Pavel Yaskevich
d9504ef919 [TypeChecker] Add typeCheckParameterDefault method
`typeCheckParameterDefault` is used to type-check default value
expression associated with a given parameter. It makes it possible
to look-through `@autoclosure` function to use its result as
contextual type, and later build implicit autoclosure expression
if needed.
2018-11-10 11:59:28 -08:00
Dan Zheng
2a4e1b83fd Implement @dynamicCallable. (#20305)
* Implement dynamically callable types (`@dynamicCallable`).

- Implement dynamically callable types as proposed in SE-0216.
  - Dynamic calls are resolved based on call-site syntax.
  - Use the `withArguments:` method if it's defined and there are no
    keyword arguments.
  - Otherwise, use the `withKeywordArguments:` method.
- Support multiple `dynamicallyCall` methods.
  - This enables two scenarios:
    - Overloaded `dynamicallyCall` methods on a single
      `@dynamicCallable` type.
    - Multiple `dynamicallyCall` methods from a `@dynamicCallable`
      superclass or from `@dynamicCallable` protocols.
  - Add `DynamicCallableApplicableFunction` constraint. This, used with
    an overload set, is necessary to support multiple `dynamicallyCall`
    methods.
2018-11-09 09:49:14 -08:00
BJ Homer
dda367638b Add comment to clarify Swift 4/5 behavioral difference for 'try?' 2018-11-06 23:31:02 -07:00
BJ Homer
df6be36457 Add backwards-compatible behavior for swift-version < 5 2018-11-06 23:31:02 -07:00
BJ Homer
3ae807d4bf Make 'try?' flatten optional chaining like optional-chaining does.
If the sub-expression of the 'try?' is optional, the result will be the same level of optional-ness.
If the sub-expression is non-optional, the result is optional.

Thus, the following lines all end up with the same type of 'Int?'
 - let x = try? 3 as Int
 - let x = try? 3 as? Int
 - let x = try? 3 as Int?
2018-11-06 23:31:02 -07:00
Michael Ilseman
95ef4bc3a8 [String] Emit literals as UTF-8 rather than UTF-16 2018-11-04 10:42:40 -08:00
Brent Royal-Gordon
9bd1a26089 Implementation for SE-0228: Fix ExpressibleByStringInterpolation (#20214)
* [CodeCompletion] Restrict ancestor search to brace

This change allows ExprParentFinder to restrict certain searches for parents to just AST nodes within the nearest surrounding BraceStmt. In the string interpolation rework, BraceStmts can appear in new places in the AST; this keeps code completion from looking at irrelevant context.

NFC in this commit, but keeps code completion from crashing once TapExpr is introduced.

* Remove test relying on ExpressibleByStringInterpolation being deprecated

Since soon enough, it won’t be anymore.

* [AST] Introduce TapExpr

TapExpr allows a block of code to to be inserted between two expressions, accessing and potentially mutating the result of its subexpression before giving it to its parent expression. It’s roughly equivalent to this function:

  func _tap<T>(_ value: T, do body: (inout T) throws -> Void) rethrows -> T {
    var copy = value
    try body(&copy)
    return copy
  }

Except that it doesn’t use a closure, so no variables are captured and no call frame is (even notionally) added.

This commit does not include tests because nothing in it actually uses TapExpr yet. It will be used by string interpolation.

* SE-0228: Fix ExpressibleByStringInterpolation

This is the bulk of the implementation of the string interpolation rework. It includes a redesigned AST node, new parsing logic, new constraints and post-typechecking code generation, and new standard library types and members.

* [Sema] Rip out typeCheckExpressionShallow()

With new string interpolation in place, it is no longer used by anything in the compiler.

* [Sema] Diagnose invalid StringInterpolationProtocols

StringInterpolationProtocol informally requires conforming types to provide at least one method with the base name “appendInterpolation” with no (or a discardable) return value and visibility at least as broad as the conforming type’s. This change diagnoses an error when a conforming type does not have a method that meets those criteria.

* [Stdlib] Fix map(String.init) source break

Some users, including some in the source compatibility suite, accidentally used init(stringInterpolationSegment:) by writing code like `map(String.init)`. Now that these intializers have been removed, the remaining initializers often end up tying during overload resolution. This change adds several overloads of `String.init(describing:)` which will break these ties in cases where the compiler previously selected `String.init(stringInterpolationSegment:)`.

* [Sema] Make callWitness() take non-mutable arrays

It doesn’t actually need to mutate them.

* [Stdlib] Improve floating-point interpolation performance

This change avoids constructing a String when interpolating a Float, Double, or Float80. Instead, we write the characters to a fixed-size buffer and then append them directly to the string’s storage.

This seems to improve performance for all three types, but especially for Double and Float80, which cannot always fit into a small string when stringified.

* [NameLookup] Improve MemberLookupTable invalidation

In rare cases usually involving generated code, an overload added by an extension in the middle of a file would not be visible below it if the type had lazy members and the same base name had already been referenced above the extension. This change essentially dirties a type’s member lookup table whenever an extension is added to it, ensuring the entries in it will be updated.

This change also includes some debugging improvements for NameLookup.

* [SILOptimizer] XFAIL dead object removal failure

The DeadObjectRemoval pass in SILOptimizer does not currently remove reworked string interpolations as well as the old design because their effects cannot be described by @_effects(readonly). That causes a test failure on Linux. This change temporarily silences that test. The SILOptimizer issue has been filed as SR-9008.

* Confess string interpolation’s source stability sins

* [Parser] Parse empty interpolations

Previously, the parser had an odd asymmetry which caused the same function to accept foo(), but reject “\()”. This change fixes the issue.

Already tested by test/Parse/try.swift, which uses this construct in one of its throwing interpolation tests.

* [Sema] Fix batch-mode-only lazy var bug

The temporary variable used by string interpolation needs to be recontextualized when it’s inserted into a synthesized getter. Fixes a compilation failure in Alamofire.

I’ll probably follow up on this bug a bit more after merging.
2018-11-02 19:16:03 -07:00
John McCall
abdba1d3f4 Change the integer-literal type from Int2048 to IntLiteral.
Part of SR-290.
2018-10-31 23:14:58 -04:00
John McCall
cf511445e2 Basic support for Builtin.IntegerLiteral. 2018-10-31 18:42:34 -04:00
Pavel Yaskevich
4197cfd649 [ConstraintSystem] Properly cache type for literal initialization coercions
Since original implicit coercion expression is preserved in AST
it needs to have its simplified type cached in the constraint
system in order for AST to get the correct type when solution
is fully applied.

Resolves: rdar://problem/45415874
2018-10-21 20:12:38 -07:00
Pavel Yaskevich
e20723f6c8 [ConstraintSystem] Replace curry level with a boolean flag
Arbitrary currying is no longer allowed so level could be switched
to a boolean flag for methods like `computeDefaultMap` to identify
if they need to look through curried self type or not.
2018-10-18 17:50:05 -07:00
Harlan Haskins
8e4379ec9d [Sema] Don't unwrap CoerceExpr from literal coercion
When converting an initialization to a coercion like:

```swift
_ = Float(90)
```

the outer coercion is given the startLoc and endLoc of the CallExpr in
source.

However, once we unwrap it and pull the implicit CallExpr to
`init(_builtinIntegerLiteral:)`, we lose the original source range.

Instead, leave the outer CoerceExpr, as it's ignored in SILGen anyway.
2018-10-01 11:15:32 -07:00
Joe Groff
5414747873 Merge pull request #19613 from jckarter/enable-consuming
Make `__consuming` meaningful for code generation.
2018-10-01 10:06:29 -07:00
Joe Groff
9c5432c0dd Make __consuming meaningful for code generation.
Previously, the `__consuming` decl modifier failed to get propagated to the value ownership of the
method's `self` parameter, causing it to effectively be a no-op. Fix this, and address some of the
downstream issues this exposes:

- `coerceCallArguments` in the type checker failing to handle the single `__owned` parameter case
- Various places in SILGen and optimizer passes that made inappropriate assertions that `self`
  was always passed guaranteed
2018-09-28 14:09:59 -07:00
Pavel Yaskevich
effd86f1ad [ConstraintSystem] Remove rudimental TMF_ApplyingOperatorParameter flag
It was useful when logic related to `BridgingConstraint` was part of
`Conversion` constraint, which could be generated as a result of implicit
conversion for an operator parameter.
2018-09-27 09:54:20 -07:00
Slava Pestov
3b60ae153d AST: Rename AnyFunctionType::Param::getType() to getOldType() 2018-09-26 11:05:23 -07:00
Slava Pestov
dc8a0acf22 Merge pull request #19217 from hamishknight/optional-to-archetype-swift5
[Sema] Restrict new optional-to-archetype casting behaviour to Swift 5 mode
2018-09-26 00:12:28 -07:00
Greg Titus
a37c1f05e2 Merge pull request #19490 from gregomni/closure_typevars
[ConstraintSystem] [NFC] Clean up type variables for closure parameters.
2018-09-25 08:44:37 -07:00
gregomni
6deb401cc6 Previously we were sharing a single type variable for a param of a closure's function type and it's paramDecl's type inside the closure itself. With inout parameters this caused vardecls of inout type and some hacks to deal with them.
Instead, create two TVs and constrain them appropriately.
2018-09-23 17:41:18 -07:00
Michael Gottesman
c62f31f5dc Inject llvm::SmallBitVector into namespace swift;
I also eliminated all llvm:: before SmallBitVector in the code base.
2018-09-21 09:49:25 -07:00
Hamish Knight
43cfc33914 Merge remote-tracking branch 'upstream/master' into optional-to-archetype-swift5 2018-09-21 17:16:29 +01:00
Joe Groff
93b5de61e7 Implement the final approved syntax for SE-227 identity key paths.
`\.self` is the final chosen syntax. Implement support for this syntax, and remove the stopgap builtin and `WritableKeyPath._identity` property that were in place before.
2018-09-19 11:45:13 -07:00
Doug Gregor
56cfd84b6a [Type checker] Move accessor creation out of validateDecl().
Creating accessors for a storage declaration within validateDecl() caused
circular dependencies detected by the request-evaluator. Separate out
accessor creation to break the dependency.

Fixes SR-8656 / rdar://problem/43951634.
2018-09-11 16:20:12 -07:00
Slava Pestov
3dfd8e3993 Sema: Set the type of ParenExpr to ParenType
This eliminates a hack I just added in coerceCallArguments().
2018-09-11 12:43:03 -07:00
Slava Pestov
fe7ac5daca Merge pull request #19065 from slavapestov/remove-paren-preserving-hacks
Remove ParenType-preserving hacks
2018-09-10 20:56:59 -07:00
Slava Pestov
498af63a63 Sema: Refactor coerceCallArguments() a bit 2018-09-10 17:22:31 -07:00
Slava Pestov
35ddd5a8d6 Sema: Remove TypeChecker::getSuperClassOf() 2018-09-10 12:30:47 -07:00
Hamish Knight
2e2e8f06e4 [Sema] Restrict new optional-to-archetype casting behaviour to Swift 5 mode
In #13910, the behaviour of optional-to-archetype casts changed such that we're now more conservative with the unwrapping of the operand at compile time in order to account for the fact that an optional type could be substituted at runtime. This brought such casting behaviour inline with that in a non-generic context, however it wasn't properly gated by Swift version, leading to compatibility issues.

This commit restricts the new behaviour to Swift 5 mode and above.

Resolves SR-8704.
2018-09-10 19:34:02 +01:00
Arnold Schwaighofer
73df12c09f Remove dead constant_string_literal
constant_string_literal was added to support a one word representation
of String that never materialized.
2018-09-05 12:13:57 -07:00
Slava Pestov
83c32da93c Sema: Refactor constraints::matchCallArguments() to take parameters and not input tuples 2018-08-28 22:36:02 -07:00
Slava Pestov
59827da70f Sema: Remove an unnecessary hack from ExprRewriter::coerceCallArguments() 2018-08-28 22:36:02 -07:00
Slava Pestov
383e93b428 Sema: Call matchCallArguments() directly instead of matchTypes()
Now that function types cannot have a naked type variable as
their input type it's no longer possible to have an unsolved
ArgumentTupleConversion constraint, so we can bypass most of
the logic in matchTypes() and call matchCallArguments() instead.
2018-08-28 14:40:56 -07:00
Slava Pestov
8928cb5b8a Sema: Stop using FunctionType::getOld() when generating constraints for SubscriptExpr
Previously we would generate the following constraint here, where
'index' and 'output' are concrete types and $input is a type
variable:

- ValueMember(base, $input -> output)
- ArgumentTupleConversion(index, $input)

The problem is that we built a function type where the entire input
was a type variable, which would then bind to an argument list.

We could instead generate this constraint:

- ValueMember(base, $member)
- ApplicableFunction(index -> output, $member)

I also had to redo how keypaths map locators back to key path
components. Previously the ArgumentTupleConversion was created
with a locator ending in KeyPathComponent.

Now the ApplicableFunction is created with this locator, which means
the argument match is performed with a locator ending in
ApplyArgument.

A consequence of this is that the SubscriptIndex and SubscriptResult
locator path elements are no longer used, so remove them.

This requires various mechanical changes in places we look at
locators to handle this change. Should be NFC.
2018-08-28 14:40:56 -07:00