Commit Graph

1556 Commits

Author SHA1 Message Date
Doug Gregor
b97ef02d85 Opaque opaque types and compute substitutions in the constraint system
Opaque opaque types and record them within the "opened types" of the
constraint system, then use that information to compute the set of
substitutions needed for the opaque type declaration using the normal
mechanism of the constraint solver. Record these substitutions within
the underlying-to-opaque conversion.

Use the recorded substitutions in the underlying-to-opaque conversion
to set the underlying substitutions for the opaque type declaration
itself, rather than reconstructing the substitutions in an ad hoc manner
that does not account for structural opaque result types.
2021-12-26 20:33:58 -08:00
Richard Wei
6645f0d33f Merge pull request #40630 from rxwei/capture-inference 2021-12-22 14:44:34 -08:00
Richard Wei
1b3c0b7a73 [Regex] Infer capture types of regex literals.
When parsing a regular expression literal, accept a serialized capture structure from the regex parser. During type checking, decode it and form Swift types.

Examples:
```swift
'/(.)(.)/' // ==> `Regex<(Substring, Substring)>`
'/(?<label>.)(.)/' // ==> `Regex<(label: Substring, Substring)`
'/((.))*((.)?)/' //==> `Regex<([Substring], [Substring], Substring, Substring?)>`
```

Also:
- Fix a bug where a regex literal parsing error is not returning an error parser result.

Note:
- This needs to land after apple/swift-experimental-string-processing#92 and after `dev/4` tag has been created.
- See apple/swift-experimental-string-processing#92 for regex parser changes and the capture structure encoding.
- The `RegexLiteralParsingFn` `CaptureStructureOut` pointer type change from `char *` to `void *` will not break builds due to implicit pointer conversion (SE-0324) and unchanged ABI.

Resolves rdar://83253511.
2021-12-22 02:58:21 -08:00
Robert Widmann
e5bfda7c6e Merge pull request #40587 from CodaFi/substitute-teacher
Initial Semantics for Variadic Generics
2021-12-20 11:25:25 -08:00
Michael Ilseman
7bff9da67d Revert "Revert "Merge pull request #40595 from hamishknight/straw-bales"" 2021-12-19 10:08:48 -07:00
Arnold Schwaighofer
9511994e52 Revert "Merge pull request #40595 from hamishknight/straw-bales"
This reverts commit a67a0436f7, reversing
changes made to 9965df76d0.

This commit or the earlier commit this commit is based on (#40531) broke the
incremental bot.
2021-12-18 11:02:37 -08:00
Richard Wei
300cbaba31 Integrate experimental string processing modules and enable end-to-end regex.
- Checkout apple/swift-experimental-string-processing using a tag.
- Build `_MatchingEngine` as part of libswift (`ExperimentalRegex`) using sources from the package.
- Parse regex literals using the parser from `_MatchingEngine`.
- Build both `_MatchingEngine` and `_StringProcessing` as part of core libs using sources from the package.
- Use `Regex<DynamicCaptures>` as the default regex type until we finalize apple/swift-experimental-string-processing#68.
2021-12-17 10:33:07 +00:00
Robert Widmann
9c6acbec2c Implement Generic Argument Binding for Variadic Types
The algorithm is detailed below:

Suppose we encounter a type T<..., U, V..., W, ...> for V... the _sole_ variadic generic parameter.

When we encounter an argument list <A, B, C, D, E, F, ...> to T, we must work in three steps

- Bind all generic parameters up to U in parallel with the argument list until we encounter V...
- Measure the tail of parameters after V... and call it `t`. Assuming `m` type variables hav been bound already, we must bind `n - t - m` type arguments to V...
- Finally, bind the remaining `t` arguments.

This procedure is often called "Saturation" in the literature. Variadic generics have a special saturation property where unlike normal generic parameters it is possible to bind zero arguments to them. In such a case, the result is an empty pack type, which is semantically well-formed.
2021-12-16 01:16:45 -08:00
Robert Widmann
3b66a31d5c Model Pack Expressions and Pack Reifications
Pack expressions take a series of argument values and bundle them together as a pack - much like how a tuple expression bundles argument expressions into a tuple.

Pack reification represents the operation that converts packs to tuples/scalar types in the AST. This is important since we want pack types in return positions to resolve to tuples contextually.
2021-12-16 00:39:33 -08:00
Richard Wei
05363cd55a Regex literal runtime plumbing.
- Frontend: Implicitly import `_StringProcessing` when frontend flag `-enable-experimental-string-processing` is set.
- Type checker: Set a regex literal expression's type as `_StringProcessing.Regex<(Substring, DynamicCaptures)>`. `(Substring, DynamicCaptures)` is a temporary `Match` type that will help get us to an end-to-end working system. This will be replaced by actual type inference based a regex's pattern in a follow-up patch (soon).
- SILGen: Lower a regex literal expression to a call to `_StringProcessing.Regex.init(_regexString:)`.
- String processing runtime: Add `Regex`, `DynamicCaptures` (matching actual APIs in apple/swift-experimental-string-processing), and `Regex(_regexString:)`.

Upcoming:
- Build `_MatchingEngine` and `_StringProcessing` modules with sources from apple/swift-experimental-string-processing.
- Replace `DynamicCaptures` with inferred capture types.
2021-12-09 16:05:34 -08:00
Hamish Knight
37f16520e6 Prototype regex literal AST and emission
With `-enable-experimental-string-processing`,
start lexing `'` delimiters as regex literals (this
is just a placeholder delimiter for now). The
contents of which gets passed to the libswift
library, which can return an error string to be
emitted, or null for success.

The libswift side isn't yet hooked up to the Swift
regex parser, so for now just emit a dummy
diagnostic for regexes starting with quantifiers.

If successful, build an AST node which will be
emitted as an implicit call to an
`init(_regexString:)` initializer of an in-scope
`Regex` decl (which will eventually be a known
stdlib decl).
2021-12-06 21:16:14 +00:00
Pavel Yaskevich
0e6e058e7c [TypeChecker] Fix constraint solver to respect LeaveClosureBodyUnchecked flag 2021-12-03 10:54:07 -08:00
Luciano Almeida
5337b8d40f Merge pull request #40328 from LucianoPAlmeida/minor-nfc
[NFC] Remove duplicated dyn_cast
2021-11-30 19:32:18 -03:00
Saleem Abdulrasool
349af3707d Merge pull request #40305 from compnerd/semitruck
gardening: make c++98-compat-extra-semi an error
2021-11-30 08:18:36 -08:00
LucianoAlmeida
87498648d1 [NFC] Remove duplicated dyn_cast 2021-11-30 12:19:33 -03:00
Pavel Yaskevich
bc54bc6bb7 Revert "[TypeChecker] SE-0326: Enable multi-statement closure inference by default" 2021-11-29 17:26:08 -08:00
Xi Ge
6891f1769b Merge remote-tracking branch 'apple/main' into 85268028 2021-11-29 07:43:11 -08:00
Saleem Abdulrasool
910fbee14e gardening: make c++98-compat-extra-semi an error
This cleans up 90 instances of this warning and reduces the build spew
when building on Linux.  This helps identify actual issues when
building which can get lost in the stream of warning messages.  It also
helps restore the ability to build the compiler with gcc.
2021-11-27 11:40:17 -08:00
Xi Ge
03c76bd32d sema: diagnose passing a non-constant value into a constant parameter 2021-11-22 11:52:57 -08:00
Anthony Latsis
e48f4830dd TypeResolution: Replace more 'forContextual' uses with 'resolveContextualType' 2021-11-19 16:44:03 +03:00
Hamish Knight
237338b504 Merge pull request #40224 from hamishknight/super-tuple-shuffle 2021-11-18 09:50:09 +00:00
Pavel Yaskevich
63f355fde0 Merge pull request #39989 from xedin/more-diag-improvements-for-multi-stmt-closures
[TypeChecker] SE-0326: Enable multi-statement closure inference by default
2021-11-17 10:14:58 -08:00
Hamish Knight
da36a2cb88 [CS] Restore a type variable for compatibility with rdar://85263844
Despite being otherwise disconnected from the
constraint system, it's possible for it to affect
how we type-check tuple matches in certain cases.

This is due to the fact that:
- It can have a lower type variable ID than an
opened generic parameter type, so becomes the
representative when merged with it. And because it
has a different locator, this can influence
binding prioritization.
- Tuple subtyping is broken, as it's currently a
*weaker* relationship than conversion.

Therefore, temporarily restore this bit of logic
for language versions < 6. If possible, we should
try and fix tuple subtying in Swift 6 mode to not
accept label mismatches, so that it's not more
permissive than tuple conversion.

rdar://85263844
2021-11-17 17:06:19 +00:00
Pavel Yaskevich
14939cf467 [CSGen] Rework nil-coalescing operator handling in LinkedExprAnalyzer
Allow `LinkedExprAnalyzer` to capture `??` operator and walk into
its arguments because they could have valuable type information,
but don't attempt to favor or link operators if `??` is present in a chain.

Resolves: rdar://85277993
2021-11-16 18:00:11 -08:00
Pavel Yaskevich
83033198c3 [TypeChecker] Fix constraint solver to respect LeaveClosureBodyUnchecked flag 2021-11-15 16:42:05 -08:00
Robert Widmann
658de80ca8 Merge pull request #39627 from CodaFi/the-replacements
Suggest Replacement Types for Invalid Placeholders
2021-11-10 16:23:29 -08:00
Hamish Knight
6ef96c9e3d Merge pull request #40083 from hamishknight/dont-favor-tuple-elements 2021-11-08 21:32:45 +00:00
Pavel Yaskevich
0c70f5650f Merge pull request #39592 from Jumhyn/contextual-placeholder-followup
Remove default argument from getContextualType
2021-11-08 10:00:36 -08:00
Hamish Knight
691da86383 [CS] Don't favor based on tuple element types
Previously this check was guarding against this
case, however with the argument list refactoring,
it's now possible for regular tuples to have
ApplyExpr parents. As such, broaden the check to
handle any tuple expr.
2021-11-08 13:16:31 +00:00
Holly Borla
d584153f78 Merge pull request #40078 from hborla/wrong-nil-coalescing-favoring
[CSGen] Don't walk into `??` in `LinkedExprAnalyzer`.
2021-11-06 13:31:55 -07:00
Holly Borla
c62e219ba2 [CSGen] Don't walk into ?? in LinkedExprAnalyzer.
Attempting to favor the type of this expression based on operand
types is wrong, because this operator attempts to unwrap its
operand types.
2021-11-05 21:12:59 -07:00
Pavel Yaskevich
7f94bbf29b [AST] NFC: Drop Type from TypeBase::isCGFloatType
This makes naming consistent with other accessors e.g. `isDouble`
2021-11-03 18:09:14 -07:00
Robert Widmann
ffff35465c Restore Top-Level Placeholder Restrictions in Expression Position
Respect the text of https://github.com/apple/swift-evolution/blob/main/proposals/0315-placeholder-types.md#top-level-type-placeholders and re-restrict the usage of placeholders in top-level expressions.
2021-11-03 12:50:02 -07:00
Holly Borla
beaaee7af1 [ConstraintSystem] Don't increase the score for unapplied function references
if the expression is an argument to `#selector`.

For `#selector` arguments, functions and properties are syntactically distinct
with the getter/setter label, so the solver should not unconditionally prefer
properties to unapplied functions. A better fix for this is to port over the
`#selector` diagnostics from CSApply, and not attempt invalid disjunction choices
based on the selector kind on the valid code path.
2021-10-20 09:19:25 -07:00
Hamish Knight
fee31c69f0 [CS] Move constructor ranking rule into CSRanking
Previously we were introducing a type variable
to mark a constructor's parameter list as
`TVO_PrefersSubtypeBinding`. Unfortunately this
relies on representing the parameter list as a
tuple, which will no longer be properly supported
once param flags are removed from tuple types.

Move the logic into CSRanking such that we pick up
and compare the parameter lists when comparing
overload bindings. For now, this still relies on
comparing the parameter lists as tuples, as there's
some subtle tuple subtyping rules that could
potentially affect source compatibility here, but
at least we can explicitly strip the parameter
flags and localise the hack to CSRanking rather
than exposing it as a constraint.
2021-10-12 09:51:44 +01:00
Hamish Knight
a1abcee108 [CS] Introduce ConstructorMemberType locator elt
Use this new element to represent the overload type
for a constructor call, and have it store a bit
indicating whether the call is for a short-form
`X(...)` or self-delegating `self.init(...)` call.
2021-10-12 09:51:44 +01:00
Pavel Yaskevich
e3aa37700e [ConstraintSystem] Allow default initialized patterns
Add support for pattern bindings with default initializable patterns
e.g. `let x: Int?`.
2021-10-08 10:08:03 -07:00
Frederick Kellison-Linn
b433724735 Remove default argument from getContextualType 2021-10-05 17:04:54 -04:00
Pavel Yaskevich
88ad3b7955 Merge pull request #39412 from Jumhyn/sr-15192
Replace inferrable types before binding literals to contextual type
2021-10-05 09:45:24 -07:00
Frederick Kellison-Linn
f6b6f0cd2b Lift replaceInferableTypes call into getContextualType 2021-10-04 23:36:33 -04:00
Frederick Kellison-Linn
4dff94ada3 Replace inferrable types before binding literals to contextual type 2021-10-01 12:45:49 -04:00
Pavel Yaskevich
50e70fb3f5 [CSGen] Preserve labels during splat situations in pattern matching context
Tuple splat/implosion is (still) allowed for patterns (with a warning in Swift 5)
so we need to use `SingleApply` while looking up members to make sure that e.g.
`case test(x: Int, y: Int)` gets the labels preserved when matched with
`case let .test(tuple)` and `Compound` when associated values form a tuple pattern.
2021-09-25 11:41:11 -07:00
Pavel Yaskevich
d798d3f711 Merge pull request #39190 from xedin/diagnostics-for-weak-patterns
[ResultBuilders] Properly diagnose `weak` declarations with non-optional type
2021-09-08 16:08:25 -07:00
Pavel Yaskevich
3bbde55052 [ConstraintSystem] Detect and diagnose incorrectly typed weak declarations
All `weak` declarations are supposed to have an optional type.
Detect situations when is not an optional, wrap it as a fix,
and diagnose the problem.
2021-09-08 09:39:54 -07:00
Hamish Knight
b5bc5141e5 Merge pull request #39192 from hamishknight/a-range-of-arguments
[CS] Store argument list mappings on solutions
2021-09-08 12:00:48 +01:00
Pavel Yaskevich
021349f520 Merge pull request #39162 from xedin/dont-increase-weak-let-optionality
[CSGen] Don't increase optionality of `weak var` patterns
2021-09-06 10:30:11 -07:00
Hamish Knight
632bf41768 [CS] Make ArgumentLists a private member
Move associateArgumentList onto ConstraintSystem
and make ArgumentLists private in preparation for
having it roll back at the end of solver scopes.
2021-09-06 12:54:49 +01:00
Hamish Knight
dc60996f89 Merge pull request #38836 from hamishknight/toil-and-tuple 2021-09-04 19:30:04 +01:00
Pavel Yaskevich
b1a79df066 [CSGen] Refactor weak declaration/capture optionality enforcement
- Make sure that `varType` is wrapped into an optional type for `weak`
  declarations.
- Verify that `externalPatternType` is an optional type when one-way
  constraints are requested.
2021-09-03 18:11:53 -07:00
Pavel Yaskevich
e8a89cc1d1 [CSGen] Don't increase optionality of weak var patterns
If the externally-imposed type is already optional,
let's not add optionality to the pattern.
2021-09-03 10:59:30 -07:00