Commit Graph

1344 Commits

Author SHA1 Message Date
Pavel Yaskevich
ac6ea62cec [CSGen] NFC: Extract var reference collector to be used in more places 2023-07-07 19:50:46 +02:00
Pavel Yaskevich
e337a3947d [ConstraintSystem] Implement tap expression checking in the solver
Generate a conjunction for each tap expression body as soon as it
gets a contextual type instead of separate post-factum type-checking
via `typeCheckTapBody`.
2023-07-07 19:50:46 +02:00
Sophia Poirier
63e30b5525 [Variadic Generics] add tracking of pack environments for pack elements to Constraint System 2023-07-06 13:44:10 -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
Pavel Yaskevich
199616a49b Merge pull request #66708 from xedin/generalize-default-closure-type-constraint
[ConstraintSystem] Use fallback type constraint to default pack expansion
2023-06-19 00:47:18 -07:00
Pavel Yaskevich
2ee646f47b [ConstraintSystem] NFC: Generalize DefaultClosureType constraint
This constraint is useful in more places than closures because
it gives a way to provide a "fallback type" without it having
effect on inference.
2023-06-16 11:15:15 -07:00
Hamish Knight
f10d0df62f Merge pull request #66593 from hamishknight/pattern-error 2023-06-16 18:06:47 +01:00
Holly Borla
612a2e79fa Merge pull request #66657 from hborla/variadic-macro-specialization
[ConstraintSystem] Use an explicit generic argument constraint for `UnresolvedSpecializeExpr`.
2023-06-15 09:35:25 -07:00
Holly Borla
d357315661 [ConstraintSystem] Handle typealiases in ExplicitGenericArguments
constraint simplification.
2023-06-15 00:27:18 -07:00
Holly Borla
eb858d938e [ConstraintSystem] Use an explicit generic argument constraint for
UnresolvedSpecializeExpr.

The eager generic argument matching code in CSGen for unresolved
specializations was incorrect because it didn't account for parameter
packs. Fortunately, a constraint to delay explicit generic argument
matching already exists for macros. Use it here.
2023-06-14 18:24:58 -07:00
Hamish Knight
c6dd3ad839 [CS] Diagnose UnresolvedPatternExprs as part of constraint solving
Instead of diagnosing in CSApply, let's create a
fix and diagnose in the solver instead.
Additionally, make sure we assign ErrorTypes to
any VarDecls bound by the invalid pattern, which
fixes a crash.

rdar://110638279
2023-06-13 12:14:25 +01:00
Hamish Knight
762cd4d10e Merge pull request #66565 from hamishknight/connect-two 2023-06-13 08:45:53 +01:00
Hamish Knight
1616e167b9 [CS] Walk into patterns when looking for closure referenced vars
Previously we would skip over ExprPatterns, but
we need to ensure that we walk them, as they may
have interesting variable references that the
closure needs to be connected to the type
variables for.

rdar://110617471
2023-06-12 18:27:24 +01: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
2943064511 [Macros] Always exclude macro expansions in TypeChecker::lookupMacros. 2023-06-11 23:09:47 -07:00
Hamish Knight
b43d351197 [CS] Improve diagnostics a bit for pattern mismatch
There's still plenty of more work to do here for
pattern diagnostics, including introducing a
bunch of new locator elements, and handling things
like argument list mismatches. This at least lets
us fall back to a generic mismatch diagnostic.
2023-06-07 00:35:02 +01:00
Hamish Knight
7a137d6756 [CS] Allow ExprPatterns to be type-checked in the solver
Previously we would wait until CSApply, which
would trigger their type-checking in
`coercePatternToType`. This caused a number of
bugs, and hampered solver-based completion, which
does not run CSApply. Instead, form a conjunction
of all the ExprPatterns present, which preserves
some of the previous isolation behavior (though
does not provide complete isolation).

We can then modify `coercePatternToType` to accept
a closure, which allows the solver to take over
rewriting the ExprPatterns it has already solved.

This then sets the stage for the complete removal
of `coercePatternToType`, and doing all pattern
type-checking in the solver.
2023-06-07 00:35:01 +01:00
Hamish Knight
2a3b15c3e8 [CS] Remove null Type handling from getTypeForPattern
We should never CSGen a null Type for patterns.
2023-06-07 00:35:01 +01:00
Hamish Knight
7c514fcea4 [CS] Remove null pattern handling from getTypeForPattern
Push the only null case that can occur up into the
caller.
2023-06-07 00:35:01 +01:00
Hamish Knight
8d2f2baff0 [CS] Remove external type logic from getTypeForPattern
This shouldn't be necessary, we should be able to
solve with type variables instead. This makes sure
we don't end up with weird special cases that only
occur when an external type is present.
2023-06-07 00:35:00 +01:00
Hamish Knight
23dbf6f106 [CS] Reverse the type order in a couple of pattern equality constraints
Order them such that if they were changed to
conversions, they would be sound. This shouldn't
make a difference, but unfortunately it turns out
pattern equality is not symmetric. As such, tweak
the pattern equality logic to account for the
reversed types. This allows us to remove a special
case from function matching.
2023-06-07 00:35:00 +01:00
Hamish Knight
f3b16fd7ad [CS] Fix a couple of constraints in getTypeForPattern
The TypedPattern and IsPattern constraints were
incorrectly written, with conversions propagating
out of the patterns, when really conversions
ought to propagate into patterns. In any case, it
seems like we really want equality here. Fix the
constraints to use equality, and have the cast
constraint operate on the external pattern type
instead of the subpattern type.
2023-06-07 00:35:00 +01:00
Sophia Poirier
93864f6c15 [Variadic Generics] drop requirement of .element for tuple expansion rdar://107160966 2023-05-30 11:37:55 -04:00
Michael Gottesman
67fcb1c86e [consume-operator] Change consume so that its result is not considered an lvalue.
Before this change it was possible to:

1. Call mutating methods on a consume result.
2. assign into a consume (e.x.: var x = ...; (consume x) = value.

From an implementation perspective, this involved just taking the logic I
already used for the CopyExpr and reusing it for ConsumeExpr with some small
tweaks.

rdar://109479440
2023-05-17 22:42:42 -07:00
Michael Gottesman
e6f1691122 [copy-operator] Add support for the copy operator in preparation for making consuming and borrowing no implicit copy.
Some notes:

1. I implemented this as a contextual keyword that can only apply directly to
lvalues. This ensures that we can still call functions called copy, define
variables named copy, etc. I added tests for both the c++ and swift-syntax based
parsers to validate this. So there shouldn't be any source breaks.

2. I did a little bit of type checker work to ensure that we do not treat
copy_expr's result as an lvalue. Otherwise, one could call mutating functions on
it or assign to it, which we do not want since the result of copy_value is

3. As expected, by creating a specific expr, I was able to have much greater
control of the SILGen codegen and thus eliminate extraneous copies and other
weirdness than if we used a function and had to go through SILGenApply.

rdar://101862423
2023-05-17 22:42:42 -07:00
Pavel Yaskevich
e3d1317208 [ConstraintSystem] Detect and diagnose missing each for value pack reference
Detect that a value pack is missing 'each' keyword during constraint
generation and fix-it by injecting `PackElementExpr`.
2023-05-08 13:30:51 -07:00
Pavel Yaskevich
583649088a [AST/CSGen] Move expanded pack collection into constraint generator
This is going to help us identify and fix positions were 'each'
keyword is mising.
2023-05-08 13:30:51 -07:00
Hamish Knight
3087afc1f8 Merge pull request #65651 from hamishknight/placeholder
Resolves https://github.com/apple/swift/issues/65650
2023-05-04 19:50:12 +01:00
Hamish Knight
b07f7b336e [CS] Improve handling of holes for Named/AnyPatterns
Rather than eagerly binding them to holes if the
sequence element type ends up being Any, let's
record the CollectionElementContextualMismatch fix,
and then if the patterns end up becoming holes,
skip penalizing them if we know the fix was
recorded. This avoids prematurely turning type
variables for ExprPatterns into holes, which
should be able to get better bindings from the
expression provided. Also this means we'll apply
the logic to non-Any sequence types, which
previously we would give a confusing diagnostic
to.
2023-05-04 14:53:58 +01:00
Pavel Yaskevich
fdfedc62bd [ConstraintSystem] NFC: Reverse direction of ShapeOf constraint
First type is the reduced shape of the second type (the pack type).
2023-05-03 10:44:10 -07:00
Pavel Yaskevich
bff6a896b2 [CSGen] Detect and diagnose invalid pack expansion expressions
Detect that pack expansion expression doesn't have any pack
references during constraint generation.

Resolves: rdar://107835215
2023-05-02 09:32:19 -07:00
Pavel Yaskevich
20d7642ca4 [CSGen] Allow expression pattern types to be holes 2023-04-24 10:02:00 -07:00
Pavel Yaskevich
b081bdbf57 [CSGen] Allow is pattern types to be holes 2023-04-21 12:14:41 -07:00
Anthony Latsis
82ac9328ae [NFC] CS: Misc minor debug output tweaks
* Use fancy arrows (`→`) because they are distinct from and shorter than `->`,
  and fancier.
* We have two ways of demarcating locators: `@ <locator>` and `[[<locator>]];`.
  Stick to the first, which is shorter and clearer.
* 'attempting type variable' → 'attempting binding'. *Bindings* are attempted,
  not type variables.
* `considering ->` → `considering:`. I think a colon is semantically more fit
  and makes things easier to read if the considered constraint has arrows in its
  description or types. It’s also shorter this way.
2023-04-18 22:42:59 +03:00
Pavel Yaskevich
24625bb809 [ConstraintSystem] Fix support for a single pack expansion parameter in init references
In Swift 5 and earlier initializer references are handled in a special
way that uses a type variable to represent a type of the parameter
list. Such type variables should be allowed to bind to a pack expansion
type to support cases where initializer has a single unlabeled variadic
generic parameter - `init(_ data: repeat each T)`.
2023-03-24 09:03:19 -07:00
Holly Borla
c60b4860ab Merge pull request #64498 from hborla/diagnose-pack-outside-expansion-expr
[ConstraintSystem] Enforce `TVO_CanBindToPack`, and diagnose pack references outside of pack expansion expressions.
2023-03-21 14:00:52 -07:00
Holly Borla
cb19fc3a71 [ConstraintSystem] Enforce TVO_CanBindToPack, and diagnose pack references outside
of pack expansion expressions.
2023-03-20 20:13:48 -07:00
Pavel Yaskevich
40a85f7602 Merge pull request #64463 from xedin/rework-capture-list-csgen
[CSGen] Type-check capture list together with closure body
2023-03-20 15:36:14 -07:00
Pavel Yaskevich
1691f32d84 [CSGen] Type-check capture list together with closure body
Delay constraint generation for capture list until body of
the associated closure is resolved. This means that we can
unify capture checking with that of regular pattern bindings
for multi-statement closures.
2023-03-17 15:22:38 -07:00
Pavel Yaskevich
c19de063d2 [ConstraintSystem] NFC: Remove obsolete generateConstraints(CaseStmt *, ...) method 2023-03-17 09:57:04 -07:00
Luciano Almeida
4d981e3c0b Merge pull request #63904 from LucianoPAlmeida/ambiguous-overload
[Sema] Diagnose function coercion ambiguity
2023-03-15 23:18:00 -03:00
Holly Borla
9358f2fca5 [ConstraintSystem] Allow type variables for pack expansion patterns to bind
to noescape.
2023-03-15 10:53:14 -07:00
Luciano Almeida
b375c097a9 [Diagnostic] Fix regression in requirement diagnostics 2023-03-15 09:45:30 -03:00
Luciano Almeida
4ebedc8333 [Sema] Adjust getCalleeLocator to look into coercion operand 2023-03-14 23:06:11 -03:00
Luciano Almeida
d5a9d6ab83 [ConstraintSystem] Introduce new locator path elemet to identify coercion 2023-03-11 18:47:53 -03:00
Holly Borla
75c1b6e199 Merge pull request #64166 from hborla/parameter-pack-substitution 2023-03-07 06:39:50 -08:00
Holly Borla
e7f82bc6e7 [ConstraintSystem] Record both the UUID and the shape class for opened pack
element environments.

This allows the constraint system to ensure that for a given pack expansion locator,
the given shape class is always the same when requesting the element environment.
If the shape class differs, it means there's a same-shape requirement failure, which
will be diagnosed via the ShapeOf constraint simplification.
2023-03-06 21:22:35 -08:00
Hamish Knight
2976edbe20 [CS] Rename SolutionApplicationTarget -> SyntacticElementTarget 2023-03-06 20:54:06 +00:00
Holly Borla
34b720c0a3 [CSGen] Constraint generation for PackExpansionExpr should return a null type
if the constraint system has not recorded a type for an expanded pack element
type repr.
2023-03-04 13:14:16 -08:00
Andrew Trick
f1ff6958a3 Merge pull request #63825 from atrick/diagnose-implicit-raw-bitwise
Warn on implicit pointer conversion from nontrivial inout values.
2023-03-02 10:57:30 -08:00