Commit Graph

315 Commits

Author SHA1 Message Date
Slava Pestov
6ff8f55634 Sema: Fix handling of Solution::typeBindings in replaySolution() 2024-11-20 13:35:21 -05:00
Slava Pestov
9a93a8a420 Sema: Fix latent bug in CSRanking.cpp
I believe openedType1 should be computed using innerDC1 not innerDC2.
2024-11-18 12:31:36 -05:00
Slava Pestov
09df48f9fb Sema: Remove DenseMap usage when opening types 2024-11-18 11:40:38 -05:00
Hamish Knight
2d7500eda6 [AST] Remove ParenType
Today ParenType is used:

1. As the type of ParenExpr
2. As the payload type of an unlabeled single
   associated value enum case (and the type of
   ParenPattern).
3. As the type for an `(X)` TypeRepr

For 1, this leads to some odd behavior, e.g the
type of `(5.0 * 5).squareRoot()` is `(Double)`. For
2, we should be checking the arity of the enum case
constructor parameters and the presence of
ParenPattern respectively. Eventually we ought to
consider replacing Paren/TuplePattern with a
PatternList node, similar to ArgumentList.

3 is one case where it could be argued that there's
some utility in preserving the sugar of the type
that the user wrote. However it's really not clear
to me that this is particularly desirable since a
bunch of diagnostic logic is already stripping
ParenTypes. In cases where we care about how the
type was written in source, we really ought to be
consulting the TypeRepr.
2024-10-31 11:32:40 +00:00
Slava Pestov
38fe0696a4 Sema: Factor out clearScore() and replayScore() 2024-10-09 11:48:07 -04:00
Slava Pestov
1e2d4fbc59 Sema: Record score increases in the trail 2024-10-08 16:16:31 -04:00
Allan Shortlidge
c868378d96 ConstraintSystem: Use scoring to implement MemberImportVisibility.
Previously, the constraint solver would first attempt member lookup that
excluded members from transitively imported modules. If there were no viable
candidates, it would perform a second lookup that included the previously
excluded members, treating any candidates as unviable. This meant that if the
member reference did resolve to one of the unviable candidates the resulting
AST would be broken, which could cause unwanted knock-on diagnostics.

Now, members from transitively imported modules are always returned in the set
of viable candidates. However, scoring will always prioritize candidates from
directly imported modules over members from transitive imports. This solves the
ambiguities that `MemberImportVisibility` is designed to prevent. If the only
viable candidates are from transitively imported modules, though, then the
reference will be resolved successfully and diagnosed later in
`MiscDiagnostics.cpp`. The resulting AST will not contain any errors, which
ensures that necessary access levels can be computed correctly for the imports
suggested by `MemberImportVisibility` fix-its.

Resolves rdar://126637855.
2024-09-10 09:47:42 -07:00
Slava Pestov
b601c294ac AST: Replace remaining uses of Type::transform() with transformRec() 2024-08-12 16:05:43 -04:00
Slava Pestov
375363a473 AST: Move global conformance lookup entry points to ConformanceLookup.h 2024-08-08 23:35:58 -04:00
Slava Pestov
86d567f95a AST: ModuleDecl::lookupConformance() is a static method 2024-07-06 12:05:47 -04:00
Tim Kientzle
1098054291 Merge branch 'main' into tbkka-assertions2 2024-06-18 17:52:00 -07:00
Hamish Knight
09a1f0b0c1 [CS] NFC: Factor out includingParentApply 2024-06-12 19:38:09 +01:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Holly Borla
d9aa8697ab [Concurrency] Teach the constraint system about .isolation on dynamically
isolated function values.
2024-03-13 22:23:31 -07:00
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08:00
Pavel Yaskevich
218346c285 [CSRanking] Adjust isDeclMoreConstrainedThan to account for invertible protocols
Use of invertible protocols doesn't make type parameter more
constrained.
2024-02-09 17:16:50 -08:00
Rick van Voorden
f8ae46b3f3 [inclusive-language] changed sanity to soundness 2024-01-25 18:18:02 -08:00
Alex Hoppen
485a06e503 [CodeCompletion] Don’t increase score for non-default literal types when performing member completion on a literal
Resolves rdar://118999423
2023-12-05 16:00:59 -08:00
Pavel Yaskevich
e24d18c8d4 [CSRanking] Specify whether specialization check allows missing conformances or not
We have two places where `CompareDeclSpecializationRequest` is used:

- A performance optimization that compares two generic overloads;
- Solution ranking that checks all of the selected overloads against
  another solution.

The former can allow missing conformances and shouldn't prevent
the solver from checking overloads that differ on `Sendable`
(because there is no information about what is passed as arguments)
but the latter, since it has a solution, should prefer Sendable
overloads over non-Sendable ones if possible
(i.e. `init<T: Sendable>(_: T)` is a subtype of `init<T>(_: T)`).
2023-10-31 13:47:23 -07:00
Alex Hoppen
655c8f4865 [Sema] Check cs.isForCodeCompletion() instead of checking for presence of CompletionCallback
This shouldn’t really change anything but is just a little cleaner.
2023-09-08 08:23:38 -07:00
Pavel Yaskevich
b98cd11f12 [CSRanking] Augment overload ranking to account for variadic generics
If one of the choices is variadic generic, let's use `matchCallArguments`
to find argument/parameter mappings and form pack expansions for arguments
when necessary.

Resolves: rdar://112029630
2023-07-20 10:15:08 -07:00
Alex Hoppen
48dd99b12d [IDE] Don't rank based on overload choices of function calls that contain the code completion token 2023-07-07 19:50:46 +02:00
Alex Hoppen
6cec68e302 [IDE] Ignore score kinds that represent implicit conversions when solving for code completion
Ignore conversion score increases during code completion to make sure we don't filter solutions that might start receiving the best score based on a choice of the code completion token.
2023-07-07 19:50:46 +02: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
fd060f5dde [ConstraintSystem] Add a locator to openType and some of its callers
`openType` didn't need a locator before it was simply replacing generic
parameters with corresponding type variables but now, with opening of
pack expansions types, a locator is needed for pack expansion variables.
2023-05-02 09:32:19 -07:00
Holly Borla
509188630b [ConstraintSystem] Implement type checking for converting a tuple to a
pack using the `.element` syntax.
2023-02-28 22:56:59 -08: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
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
Slava Pestov
7848b4f4e0 Sema: Remove usages of AbstractTypeParamDecl::getSuperclass()/getConformedProtocols() 2022-11-01 19:13:46 -04:00
Slava Pestov
7a16b0275b AST: Allow one-element tuple types to be constructed
These will never appear in the source language, but can arise
after substitution when the original type is a tuple type with
a pack expansion type.

Two examples:
- original type: (Int, T...), substitution T := {}
- original type: (T...), substitution T := {Int}

We need to model these correctly to maintain invariants.

Callers that previously used to rely on TupleType::get()
returning a ParenType now explicitly check for the one-element
case instead.
2022-08-23 11:12:00 -04: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
42135e27c4 Merge pull request #60387 from amritpan/improve-solution-printing
[ConstraintSystem] Improve solution printing in the type inference algorithm debug output
2022-08-10 09:03:29 -07:00
Amritpan Kaur
95e5440d66 [ConstraintSystem] Edit ScoreKind descriptions and print ScoreKind increase values. 2022-08-09 14:10:15 -07:00
Amritpan Kaur
6895b057b2 [ConstraintSystem] Refactor Score printing to use new method that also prints non-zero ScoreKind description. 2022-08-09 14:10:15 -07:00
Amritpan Kaur
d07d39045d [ConstraintSystem] Move getScoreKindName to ConstraintSystem for use across CS and create new function for printing non-zero ScoreKinds.
You are currently editing a commit while rebasing branch 'improve-solution-printing' on '538ee30efb5'.
2022-08-09 14:10:15 -07:00
Amritpan Kaur
2786f24b67 [TypeCheckConstraints.cpp] Remove printing of any empty constraint choice headings and fix minor spacing issues. 2022-08-09 14:09:39 -07:00
Hamish Knight
6e51841d14 [AST] Enforce that composeTuple drops parameter flags
Callers may either assert that the parameter flags
are empty, or ask for them to be dropped.
2022-08-02 13:56:31 +01:00
Anthony Latsis
0a2293fd41 CSRanking: Check for timeouts during solution comparisons, which are time-consuming 2022-07-12 02:33:48 +03:00
Anthony Latsis
a359f7745c Add type-checker performance tracking test for #43369
To make this test work, fix an issue in `ConstraintSystem::salvage` where a
threshold breach during solving went unnoticed due to exiting on ambiguity
before reaching the `isTooComplex` check. Address this by moving the
`isTooComplex` check to before we start processing solutions, and stick another
one in `findBestSolution` for short-circuiting while we're here.
2022-07-09 16:34:19 +03:00
Pavel Yaskevich
a4c17fa9a0 [CSRanking] Fix a bug in archetype vs. concrete type ranking
Code completion related changes introduced a bug which increased
`score2` regardless which type was an archetype, which surfaced
as a source compatibility regression in ReactiveKit.
2022-03-23 17:28:18 -07:00
Alex Hoppen
f538d33e5f [CodeCompletion][Sema] Migrate CallArgurment position completion to the solver-based implementation
This hooks up call argument position completion to the typeCheckForCodeCompletion API to generate completions from all the solutions the constraint solver produces (even those requiring fixes), rather than relying on a single solution being applied to the AST (if any).

Co-authored-by: Nathan Hawes <nathan.john.hawes@gmail.com>
2022-03-17 15:15:54 +01: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
Hamish Knight
6c6d3c9f10 [CS] Add special case to preserve compatibility for rdar://84279742
When ranking constructor parameter lists, we
compose them as tuples or parens, and check if
they are subtypes or unlabeled versions of each
other. Previously this was done with the parameter
flags intact, but recently I changed the logic to
explicitly strip parameter flags in preparation
for no longer storing the flags on these types.

This caused a slight behavior change, as it turns
out we have a special case in `TupleType::get`
that allows an unlabeled single parameter to be
composed as a tuple type if its variadic bit is
set. With the parameter flags now stripped, we
produce a paren type. This means that when
comparing the parameter lists e.g `(x: Int...)`
and `(Int...)`, instead of comparing two tuple
types end up comparing a tuple with a paren and
fail.

To preserve the old behavior, implement a special
case for when we have an unlabeled and labeled
variadic comparison for a single parameter. In
this case, add the parameter types directly to the
type diff, and track which one had the label. The
ranking logic can then use this to prefer the
unlabeled variant. This is only needed in the
single parameter case, as other cases will compare
as tuples the same as before. In cases where
variadics aren't used, we may end up trying to
compare parens with tuples, but that's consistent
with what we previously did.

rdar://84279742
2021-10-21 13:55:03 +01:00
Pavel Yaskevich
1f7623d630 [CSRanking] Allow worse (score-wise) solutions to be removed by filtering
The removed condition was incorrect because first of all it would
always be true since `losers` are populated with `false` based on
number of viable solutions and secondly it would result in a mix
of solutions with and without fixes.

Instead, in ambiguity cases, let's remove all of the solutions
that are worse than others based on score to avoid doing any
extra work in the future steps or during diagnostics.
2021-10-18 14:50:29 -07:00
Hamish Knight
ac50dfd1d4 [CS] Fix crasher caught by stress tester
We need to be more lenient checking here as the
type variable may not be bound yet.
2021-10-12 09:51:45 +01: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
Holly Borla
1737303602 [ConstraintSystem] Increase the score when attempting overload choices
for unapplied references when the choice is a function declaration.

This will allow the solver to prune those overload choices when it
has already found a solultion with a property (all else equal in the
score). This is already done as an ambiguity tie-breaker in solution
ranking, but adding this bit to the score will prune a lot of search
space within the solver.
2021-09-22 13:33:44 -07:00
Robert Widmann
d86551de67 Lift Requirement and Parameter Accessors up to GenericSignature
Start treating the null {Can}GenericSignature as a regular signature
with no requirements and no parameters. This not only makes for a much
safer abstraction, but allows us to simplify a lot of the clients of
GenericSignature that would previously have to check for null before
using the abstraction.
2021-07-22 23:27:05 -07:00
Rintaro Ishizaki
848031825e [CodeCompletion] Peform complete filterSolutions in code completion
Not-filtering solutions causes unacceptable slownesses in some cases.
For now, filter solutions as normal typechecking does to restore the
performance.

rdar://76714968
2021-04-26 13:03:32 -07:00