Commit Graph

859 Commits

Author SHA1 Message Date
Slava Pestov
1dc4c4699f RequirementMachine: Fix warning in noassert build 2023-08-15 13:51:27 -04:00
swift-ci
1969199a8e Merge remote-tracking branch 'origin/main' into rebranch 2023-07-26 23:13:08 -07:00
Evan Wilde
309aed4925 Add SmallSetVector replacement
llvm::SmallSetVector changed semantics
(https://reviews.llvm.org/D152497) resulting in build failures in Swift.
The old semantics allowed usage of types that did not have an
`operator==` because `SmallDenseSet` uses `DenseSetInfo<T>::isEqual` to
determine equality. The new implementation switched to using
`std::find`, which internally uses `operator==`. This type is used
pretty frequently with `swift::Type`, which intentionally deletes
`operator==` as it is not the canonical type and therefore cannot be
compared in normal circumstances.

This patch adds a new type-alias to the Swift namespace that provides
the old semantic behavior for `SmallSetVector`. I've also gone through
and replaced usages of `llvm::SmallSetVector` with the
`Swift::SmallSetVector` in places where we're storing a type that
doesn't implement or explicitly deletes `operator==`. The changes to
`llvm::SmallSetVector` should improve compile-time performance, so I
left the `llvm::SmallSetVector` where possible.
2023-07-25 12:28:27 -07:00
Slava Pestov
7ce6f37729 RequirementMachine: Correct concrete type unification with pack expansion on both sides 2023-07-25 02:47:46 -04:00
Slava Pestov
747b060389 RequirementMachine: Correct desugaring of same-type requirements with pack expansion on both sides 2023-07-25 02:47:45 -04:00
swift_jenkins
fe5227c427 Merge remote-tracking branch 'origin/main' into next 2023-07-20 18:47:26 -07:00
Becca Royal-Gordon
1b81c3de7b [NFC] Adopt ValueDecl * for decl_declared_here 2023-07-19 13:08:12 -07:00
Evan Wilde
26a974e772 [NFC] Headers headers headers!
Including headers that were being transitively included from LLVM
before. Also pointing them at the new locations for some of them.
2023-07-17 10:55:55 -07:00
Slava Pestov
ba924edf61 RequirementMachine: Don't suffix pack parameter symbols with ellipsis since ASTPrinter already prints 'each' 2023-07-03 15:41:09 -04:00
Slava Pestov
69fb9871d4 RequirementMachine: Simplify desugarSameShapeRequirement() 2023-07-03 15:41:09 -04:00
Slava Pestov
0c6f6db980 RequirementMachine: Add missing closing brace in debug output 2023-07-03 15:41:09 -04:00
Slava Pestov
7dea282810 RequirementMachine: Better validation in RequirementMachine::getReducedShapeTerm() 2023-07-03 15:41:09 -04:00
Slava Pestov
dca00debec RequirementMachine: Same-type requirements imply same-shape requirements
We want `T.A == U.B` to imply `shape(T) == shape(U)` if T (and thus U)
is a parameter pack.

To do this, we introduce some new rewrite rules:

1) For each associated type symbol `[P:A]`, a rule `([P:A].[shape] => [P:A])`.
2) For each non-pack generic parameter `τ_d_i`, a rule `τ_d_i.[shape] => [shape]`.

Now consider a rewrite rule `(τ_d_i.[P:A] => τ_D_I.[Q:B])`. The left-hand
side overlaps with the rule `([P:A].[shape] => [shape])` on the term
`τ_d_i.[P:A].[shape]`. Resolving the overlap gives us a new rule

    t_d_i.[shape] => T_D_I.[shape]

If T is a term corresponding to some type parameter, we say that `T.[shape]` is
a shape term. If `T'.[shape]` is a reduced term, we say that T' is the reduced
shape of T.

Recall that shape requirements are represented as rules of the form:

    τ_d_i.[shape] => τ_D_I.[shape]

Now, the rules of the first kind reduce our shape term `T.[shape]` to
`τ_d_i.[shape]`, where `τ_d_i` is the root generic parameter of T.

If `τ_d_i` is not a pack, a rule of the second kind reduces it to `[shape]`,
so the reduced shape of a non-pack parameter T is the empty term.

Otherwise, if `τ_d_i` is a pack, `τ_d_i.[shape]` might reduce to `τ_D_I.[shape]`
via a shape requirement. In this case, `τ_D_I` is the reduced shape of T.

Fixes rdar://problem/101813873.
2023-07-03 15:41:09 -04:00
Slava Pestov
8afff61699 AST: Replace TypeArrayView<GenericTypeParamType> with ArrayRef<GenericTypeParamType *>
This basically undoes 3da6fe9c0d, which in hindsight was wrong.

There were no other usages of TypeArrayView anywhere else except for
GenericSignature::getGenericParams(), and it was almost never what
you want, so callers had to convert back and forth to an ArrayRef.
Remove it.
2023-06-29 19:23:44 -04:00
Evan Wilde
250082df25 [NFC] Reformat all the LLVMs
Reformatting everything now that we have `llvm` namespaces. I've
separated this from the main commit to help manage merge-conflicts and
for making it a bit easier to read the mega-patch.
2023-06-27 09:03:52 -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
Holly Borla
cd752cca22 [NameLookup] Plumb source location arguments through all name lookup APIs.
This source location will be used to determine whether to add a name lookup
option to exclude macro expansions when the name lookup request is constructed.
Currently, the source location argument is unused.
2023-06-11 23:09:47 -07:00
Nate Chandler
32156b5a99 [RequirementMachine] Used loc in sig reqs.
`InferredGenericSignatureRequest` creates `StructuralRequirement`s for
the requirements of the generic signature that is passed to it (if one
is).

Previously, it used invalid `SourceLoc`s for these requirements.  The
result was that when errors that were emitted as a result of those
`StructuralRequirement`s (during concrete type contraction), they would
also have invalid `SourceLoc`s.  The effect was that those errors were
ignored during `diagnoseRequirementErrors`.

Here, use the available loc for those requirements.

rdar://108963047
2023-05-17 15:16:23 -07:00
Nate Chandler
db823e4aa0 [RequirementMachine] NFC: Hoisted loc definition.
In preparation for using the value in the `StructuralRequirement`s
created for the `parentSig`.
2023-05-17 15:16:23 -07:00
Slava Pestov
80f8487d32 RequirementMachine: Use Requirement::checkRequirement() to desugar layout requirements 2023-04-05 23:42:17 -04:00
Slava Pestov
8d219442c8 RequirementMachine: Use Requirement::checkRequirement() to desugar superclass requirements 2023-04-05 23:42:17 -04:00
Slava Pestov
58822544bc RequirementMachine: Use Requirement::checkRequirement() to desugar conformance requirements 2023-04-05 23:42:17 -04:00
Slava Pestov
e14ac9e482 RequirementMachine: Clean up desugarRequirement() a bit 2023-04-05 23:42:17 -04:00
Rose
8d06ca7f4e Prefer std::copy_n over std::copy where appropriate.
std::copy_n saves us from having to do the addition manually.
2023-03-16 16:25:51 -04:00
Slava Pestov
a5cf7d3298 RequirementMachine: The reduced type of a PackExpansionType has a reduced *shape* for the count type
I don't have a test case for this but it was bound to
come up eventually.
2023-03-15 23:04:50 -04:00
Slava Pestov
75d25c6175 RequirementMachine: Ensure that isValidTypeParameter() checks the generic parameter type too 2023-03-13 19:04:15 -04:00
Slava Pestov
5cbdf7ad6d RequirementMachine: Tweak some comments 2023-03-07 23:19:25 -05:00
Holly Borla
74cc62735b [RequirementMachine] Diagnose same-element requirements.
The rewrite rules are not quite right yet for same-element requirements, so
let's ban them for now.
2023-03-06 21:32:30 -08:00
swift-ci
cfa0d635d8 Merge pull request #62721 from valeriyvan/RequirementMachineRequests
[Gardening] Simplify excessive conditional expression: (A && !B) || (!A && B) is equivalent to bool(A) != bool(B)
2023-01-25 10:15:38 -08:00
Valeriy Van
f3e7407a62 [Gardening] Simplify excessive conditional expression: (A && !B) || (!A && B) is equivalent to bool(A) != bool(B) 2023-01-25 16:59:04 +02:00
Holly Borla
679825063a [RequirementMachine] Only skip Sendable requirements inferred from preconcurrency
decls if the decl we're inferring the generic signature for is not itself
preconcurrency.
2023-01-19 20:28:50 -08:00
Holly Borla
d526e4c0e7 [RequirementMachine] Skip Sendable conformance requirements from preconcurrency
declarations in requirement inference.
2023-01-19 20:28:30 -08:00
Slava Pestov
c11a4240df RequirementMachine: Fix a typo 2022-11-28 23:58:06 -05:00
Holly Borla
c225fc428b Merge pull request #62228 from hborla/pack-element-generic-environment
[GenericEnvironment] Include original parameter packs in opened pack element signatures.
2022-11-28 15:34:35 -05:00
Holly Borla
5766b560e7 [RequirementMachine] Don't produced concrete type parameter diagnostics for
same-type requirements where one side is a parameter pack.
2022-11-23 15:28:10 -05: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
Slava Pestov
3f1e80c8a1 Revert "RequirementMachine: Remove completion procedure hacks for old minimal conformances algorithm"
This reverts commit d3b7aab725.
2022-11-01 19:10:47 -04:00
Slava Pestov
23abf74c0a RequirementMachine: Better error recovery from invalid protocol inheritance clauses
getInheritedProtocols() skips type resolution and directly resolves
TypeReprs to TypeDecls.

On the other hand, when building a protocol requirement signature,
we use type resolution to resolve inheritance clause entries so
that we can properly support parameterized protocol types, and
protocol compositions that contain classes.

Since a TypeRepr with an invalid sub-component resolves to an
ErrorType, this meant that in invalid code, the first list of
protocols might contain protocols that don't appear in the second.

This broke rewrite system invariants. Fix this by checking if
type resolution failed when building the requirement signature of
a protocol, and if so, also look at getInheritedProtocols().

Fixes https://github.com/apple/swift/issues/61020.
2022-11-01 11:05:36 -04:00
Holly Borla
45722bf277 [RequirementMachine] Use pack expansion types as a source for requirement inference.
Previously, only the pattern type was added as a requirement inference source, but
the pack expansion type is necessary for inferring same-shape requirements for
packs that are expanded in parallel.
2022-10-23 19:51:05 -07:00
Holly Borla
cf38d55e3a [AST] Plumb reduced shape types through local requirements into pack archetypes. 2022-10-23 11:42:46 -07:00
Slava Pestov
d4136e981b RequirementMachine: Fix unused variable warning 2022-10-16 21:37:25 -04:00
Holly Borla
c4b946195e [AST] Replace the "type sequence" terminology with "parameter pack". 2022-10-10 16:28:13 -07:00
Holly Borla
be619c62c9 [RequirementMachine] Add a generic signature query that determines whether
two type parameter packs have the same shape.
2022-10-07 10:35:49 -07:00
Holly Borla
c7c1a2074e [RequirementMachine] Store the singleton 'Storage' instance for shape
symbols in RewriteContext.
2022-10-07 10:35:49 -07:00
Holly Borla
abb501db74 [RequirementMachine] Add verification for rules involving shape symbols. 2022-10-06 20:48:40 -07:00
Holly Borla
313138c7db [NFC][RequirementMachine] Minor syntax change for MutableTerm initialization. 2022-10-06 20:48:40 -07:00
Holly Borla
41ce264a7f [RequirementMachine] Simplify same-shape requirement inference from pack
expansion types by equating the pack expansion count/shape type with each
shape of the referenced packs in the expansion.
2022-10-06 20:48:40 -07:00
Holly Borla
38a2c8218b [Requirement] Rename RequirementKind::SameCount to SameShape. 2022-10-06 20:48:40 -07:00
Holly Borla
fab5b7d364 [RequirementMachine] Add a generic signature query that returns the
reduced shape of a given type parameter pack.
2022-10-06 20:38:31 -07:00
Holly Borla
60f80006fc [RequirementMachine] Generalize connected components to process same
shape requirements.
2022-10-06 20:38:31 -07:00