Commit Graph

845 Commits

Author SHA1 Message Date
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
Holly Borla
f4bfce6448 [RequirementMachine] Map same-shape requriements to rewrite rules.
A same-shape requirement 'length(T...) == length(U...)' becomes a rewrite
rule 'T.[shape] => U.[shape]'. Reduced shape rules will drop the [shape]
term from each side of the rule, and create a same-shape requirement
between the two type parameter packs.
2022-10-06 20:38:31 -07:00
Holly Borla
0f13eda26c [RequirementMachine] Add a new symbol kind for the shape of a parameter
pack.
2022-10-06 20:38:30 -07:00
Holly Borla
ae5ebba2c1 [RequirementMachine] Add same-length requirement inference for pack
expansion types.
2022-10-06 20:38:30 -07:00
Slava Pestov
658ba01741 RequirementMachine: Redo concrete contraction after splitting concrete equivalence classes
This fixes an edge case where we start with the following requirements:

    - U : P
    - T : P
    - T.[P]A == C
    - T == G<T.[P]A>
    - U.[P]A == T.[P]A

and end up with the following set of minimal rules (where the type
witness for [P]A in the conformance G<C> : P is C):

    - U.[P] => U
    - U.[P:A] => T.[P:A]
    - T.[concrete: G<C>] => T
    - T.[concrete: G<C> : P] => T

Since U.[P]A and T.[P]A are concrete, we split the abstract same-type
requirement into two requirements, and re-run minimization:

    - U : P
    - T.[P]A == C
    - U.[P]A == C
    - T == G<C>

The concrete conformance rule T.[concrete: G<C> : P] => T does not
correspond to a requirement, so it was simply dropped, and the above
rules violate post-contraction invariants; T.[P]A is not a valid
type parameter because there is no conformance requirement T : P in
the minimized signature.

We can fix this by re-running concrete contraction after splitting
concrete equivalence classes. After contraction, the above requirements
turn into

    - U : P
    - C == C
    - U.[P]A == C
    - T == G<C>

Which correctly minimizes to

    - U : P
    - U.[P]A == C
    - T == G<C>

Both concrete contraction and concrete equivalence classes are hacks,
and we should think of a way to directly express the transformations
they perform with the rewrite system.

Fixes https://github.com/apple/swift/issues/61192.
2022-09-19 23:50:21 -04:00
Slava Pestov
6bd817c507 RequirementMachine: Add -debug-requirement-machine=split-concrete-equiv-class flag 2022-09-19 23:50:21 -04:00
Slava Pestov
5c32f2136e AST: Introduce RequirementKind::SameCount 2022-08-23 11:12:00 -04:00
Slava Pestov
7d8f3e6b63 AST: Change return type of Requirement::subst() to Requirement
Instead of returning None, let callers check hasError() if they need to.

Fixes rdar://problem/98565072.
2022-08-12 14:03:57 -04:00
Slava Pestov
9339443e79 RequirementMachine: Diagnose recursive requirements
Note the test cases in abstract_type_witnesses used to pass but are now
rejected. This is fine, because doing anything more complicated used to
crash, and the GSB would crash or misbehave with these examples.
2022-08-11 14:12:06 -04:00
Slava Pestov
42407bba3b RequirementMachine: Convert recursive requirements into ErrorTypes 2022-08-11 14:10:01 -04:00
Slava Pestov
3432a1f20c RequirementMachine: Don't filter out ErrorType as aggressively 2022-08-11 14:10:01 -04:00
Slava Pestov
e3ab64afa7 RequirementMachine: Compute recursive rules 2022-08-11 14:10:01 -04:00
Slava Pestov
15b99ae416 RequirementMachine: Concrete contraction gives up on recursive same-type and superclass requirements
A requirement of the form T == G<T> is never valid, and T == G<T.A>
will leave behind a term T.[P:A] once the conformance T : P is
dropped in contraction. So just ignore recursive requirements
here, allowing them to be properly diagnosed later.
2022-08-11 14:10:01 -04:00
Slava Pestov
5a49a337d9 RequirementMachine: Fix some comments 2022-08-11 14:10:01 -04:00
Slava Pestov
7eeb56309b RequirementMachine: Small concrete contraction cleanup 2022-08-11 14:10:01 -04:00