Commit Graph

70 Commits

Author SHA1 Message Date
Slava Pestov
1adfad0df2 IRGen: Terrible workaround for problem in searchNominalTypeMetadata()
This fixes a regression introduced by e3c8f423bc,
but the root cause was actually a subtle invariant violation
in IRGen.

FulfillmentMap's use of canonical types as keys assumes that
canonical type equality is sufficient for checking if two types
"are the same". However, this is not true when those types
contain type parameters, because two distinct type parameters
might belong to the same equivalence class.

Thus, when we take the type's context substitution map, and
apply it to each type parameter in our given list of
requirements, the substitution operation could output a
different but equivalent type parameter.

As a workaround for this problem, try to preserve the old
behavior of subst() in this specific case.

Fixes rdar://160649141.
2025-11-07 18:26:30 -05:00
Slava Pestov
f17baefd7a IRGen: Fix miscompile when a generic parameter is fixed to a tuple containing a pack
If you had something like:

    struct G<T> {
      func f<each U>(_: repeat each U) where T == (repeat each U) {}
    }

We would fulfill 'each U' from the metadata for 'G<(repeat each U)>',
by taking apart the tuple metadata for `(repeat each U)` and forming
a pack.

However this code path was only intended to kick in for a tuple
conformance witness thunk. In the general case, this optimization
is not correct, because if 'each U' is substituted with a
one-element pack, the generic argument of `G<(repeat each U)>` is
just that one element's metadata, and not a tuple. In fact, we
cannot distinguish the one-element tuple case, because the wrapped
element may itself be a tuple.

The fix is to just split off FulfillmentMap::searchTupleTypeMetadata()
from searchTypeMetadata(), and only call the former when we're in
the specific situation that requires it.

- Fixes https://github.com/swiftlang/swift/issues/78191.
- Fixes rdar://problem/135325886.
2025-05-16 17:34:11 -04:00
Erik Eckstein
d225c47d25 AST: rename OpenArchetypeType -> ExistentialArchetypeType
NFC
2025-03-11 20:21:46 +01:00
Alejandro Alonso
75c2cbf593 Implement value generics
Some requirement machine work

Rename requirement to Value

Rename more things to Value

Fix integer checking for requirement

some docs and parser changes

Minor fixes
2024-09-04 15:13:25 -07:00
Slava Pestov
977b444eb3 AST: Add a new overload of getContextSubstitutionMap() 2024-07-10 13:28:26 -04:00
Slava Pestov
fae01d9776 AST: Remove ModuleDecl parameter from more places 2024-07-06 12:05:46 -04: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
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
Slava Pestov
b2ffac54d3 IRGen: Fulfill pack metadata from tuple metadata
When the SelfMetadata in a witness thunk is the tuple type (repeat each Self),
we can fulfill the pack shape and type metadata for Pack{repeat each Self}
from tuple metadata.

The length is trivially projected; the type metadata pack is slightly more
involved, because tuple metadata stores a list of element/offset pairs, so
we must stack allocate a pack and fill it in.
2023-08-24 17:57:10 -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
Slava Pestov
c84a14fdc1 IRGen: Conditional conformances for variadic types 2023-06-21 13:42:57 -04:00
Slava Pestov
b2bc2c72ec AST: Introduce PackElementType 2023-05-25 11:17:30 -04:00
Slava Pestov
da1b210479 IRGen: Add missing return to getSingletonPackExpansionParameter() 2023-04-12 17:32:54 -04:00
Slava Pestov
7ad2365278 AST: Introduce PackType::unwrapSingletonPackExpansion() 2023-04-02 23:04:00 -04:00
John McCall
0e932329c3 Fix fulfillments for type metadata and witness table packs
A lot of the fixes here are adjustments to compensate in the
fulfillment and metadata-path subsystems for the recent pack
substitutions representation change.  I think these adjustments
really make the case for why the change was the right one to make:
the code was clearly not considering the possibility of packs
in these positions, and the need to handle packs makes everything
work out much more cleanly.

There's still some work that needs to happen around type packs;
in particular, we're not caching them or fulfilling them as a
whole, and we do have the setup to do that properly now.
2023-03-10 12:52:37 -05:00
Slava Pestov
7d779a9b36 IRGen: Fulfillment of pack shapes from nominal type metadata 2023-03-10 12:15:55 -05:00
Slava Pestov
6e31af6dda IRGen: Simplify enumerateFulfillments() 2022-12-13 11:56:33 -05:00
Slava Pestov
0c3a9d8d55 IRGen: Replace FulfillmentKey with GenericRequirement 2022-12-11 22:14:37 -05:00
Slava Pestov
68514b10b5 AST: Introduce ElementArchetypeType 2022-10-21 21:55:35 -04:00
Holly Borla
9bb837a241 [AST] Rename SequenceArchetype to PackArchetype. 2022-10-10 16:25:26 -07:00
Slava Pestov
c1b8690401 AST: Introduce special Builtin.TheTupleType singleton 2022-09-10 00:26:42 -04:00
Slava Pestov
aa67c8bf8a Parametrized => parameterized 2022-02-03 13:27:24 -05:00
Slava Pestov
e7e536705e AST: Introduce ParametrizedProtocolType 2022-01-26 00:11:38 -05:00
Doug Gregor
452eccab83 Remove NestedArchetypeType.
Nested archetypes are represented by their base archetype kinds (primary,
opened, or opaque type) with an interface type that is a nested type,
as represented by a DependentMemberType. This provides a more uniform
representation of archetypes throughout the frontend.
2022-01-14 21:28:21 -08:00
Robert Widmann
06bc38f5a2 Model PackExpansion Types
A PackExpansionType is the interface type of the explicit expansion of a
corresponding set of variadic generic parameters.
Pack expansions are spelled as single-element tuples with a single variadic
component in most contexts except functions where they are allowed to appear without parentheses to match normal variadic declaration syntax.

```
func expand<T...>(_ xs: T...) -> (T...)
                        ~~~~     ~~~~~~
```

A pack expansion type comes equipped with a pattern type spelled before
the ellipses - `T` in the examples above. This pattern type is the subject
of the expansion of the pack that is tripped when its variadic generic
parameter is substituted for a `PackType`.
2021-12-16 00:34:09 -08:00
Robert Widmann
746aa1fb58 Model Pack Types
A pack type looks a lot like a tuple in the surface language, except there
is no way for the user to spell a pack. Pack types are created by the solver
when it encounters an apply of a variadic generic function, as in

```
func print<T...>(_ xs: T...) {}
// Creates a pack type <String, Int, String>
print("Macs say Hello in", 42, " different languages")
```

Pack types substituted into the variadic generic arguments of a
PackExpansionType "trip" the pack expansion and cause it to produce a
new pack type with the pack expansion pattern applied.

```
typealias Foo<T...> = (T?...)
Foo<Int, String, Int> // Forces expansion to (Int?, String?, Int?)
```
2021-12-16 00:25:34 -08:00
Holly Borla
445a856652 [Type System] Introduce a dedicated type to represent existential types.
The new type, called ExistentialType, is not yet used in type resolution.
Later, existential types written with `any` will resolve to this type, and
bare protocol names will resolve to this type depending on context.
2021-12-09 23:14:50 -08:00
Robert Widmann
e7e11df927 Model Sequence Archetypes 2021-11-16 11:38:57 -08:00
Robert Widmann
3e1a61f425 [NFC] Fold The Tri-State In Optional<ProtocolConformanceRef>
ProtocolConformanceRef already has an invalid state.  Drop all of the
uses of Optional<ProtocolConformanceRef> and just use
ProtocolConformanceRef::forInvalid() to represent it.  Mechanically
translate all of the callers and callsites to use this new
representation.
2019-10-29 16:55:56 -07:00
Joe Groff
dd2b51d6dc Add an OpaqueTypeArchetypeType subclass. 2019-04-17 14:43:32 -07:00
Joe Groff
f1648a1b3e Split subclasses out of ArchetypeType.
Context archetypes and opened existential archetypes differ in a number of details, and this simplifies the overlapping storage of the kind-specific fields. This should be NFC; for now, this doesn't change the interface of ArchetypeType, but should allow some refinements of how the special handling of certain archetypes are handled.
2018-12-12 08:55:56 -08:00
Arnold Schwaighofer
14ff9c1984 Adjust comment 2018-11-28 08:43:16 -08:00
Arnold Schwaighofer
6a54c365e1 Lift the restriction to archetypes and consider all interesting types (checked earlier) 2018-11-28 08:38:56 -08:00
Arnold Schwaighofer
e2a83006ea IRGen: Consider the superclass bound of archetypes in searchTypeMetadata
We need to search the type metadata of superclass bounds to handle signatures like:

class K<T> {}

 $@convention(witness_method: A2) <τ_0_0><τ_1_0 where τ_0_0 : K<τ_1_0>, τ_1_0 : C> (@in_guaranteed τ_0_0) -> ()

rdar://46173958
SR-9305
2018-11-28 07:48:00 -08:00
Saleem Abdulrasool
d281b98220 litter the tree with llvm_unreachable
This silences the instances of the warning from Visual Studio about not all
codepaths returning a value.  This makes the output more readable and less
likely to lose useful warnings.  NFC.
2018-09-13 15:26:14 -07:00
Jordan Rose
84f471b031 [IRGen] Handle ProtocolInfo for protocols whose members aren't used (#18692)
Certain uses of protocols only formally need the requirement
signature, not any of the method requirements. This results in IRGen
seeing a protocol where none of the members have been validated except
the associated types. Account for this by allowing ProtocolInfo to
only contain the layout for the base protocols and associated types,
if requested.

Note that this relies on the layout of a witness table always putting
the "requirement signature part" at the front, or at least at offsets
that aren't affected by function requirements.

rdar://problem/43260117
2018-08-14 11:10:02 -07:00
John McCall
aceb2fd5ce Ensure the transitive completion of type arguments and the superclass
before declaring nominal type metadata complete.

Also, future-proof MetadataState.
2018-03-29 13:52:36 -04:00
John McCall
a906f43329 Allow type metadata to be incomplete.
Most of the work of this patch is just propagating metadata states
throughout the system, especially local-type-data caching and
metadata-path resolution.  It took a few design revisions to get both
DynamicMetadataRequest and MetadataResponse to a shape that felt
right and seemed to make everything easier.

The design is laid out pretty clearly (I hope) in the comments on
DynamicMetadataRequest and MetadataResponse, so I'm not going to
belabor it again here.  Instead, I'll list out the work that's still
outstanding:

- I'm sure there are places we're asking for complete metadata where
  we could be asking for something weaker.

- I need to actually test the runtime behavior to verify that it's
  breaking the cycles it's supposed to, instead of just not regressing
  anything else.

- I need to add something to the runtime to actually force all the
  generic arguments of a generic type to be complete before reporting
  completion.  I think we can get away with this for now because all
  existing types construct themselves completely on the first request,
  but there might be a race condition there if another asks for the
  type argument, gets an abstract metadata, and constructs a type with
  it without ever needing it to be completed.

- Non-generic resilient types need to be switched over to an IRGen
  pattern that supports initialization suspension.

- We should probably space out the MetadataStates so that there's some
  space between Abstract and Complete.

- The runtime just calmly sits there, never making progress and
  permanently blocking any waiting threads, if you actually form an
  unresolvable metadata dependency cycle.  It is possible to set up such
  a thing in a way that Sema can't diagnose, and we should detect it at
  runtime.  I've set up some infrastructure so that it should be
  straightforward to diagnose this, but I haven't actually implemented
  the diagnostic yet.

- It's not clear to me that swift_checkMetadataState is really cheap
  enough that it doesn't make sense to use a cache for type-fulfilled
  metadata in associated type access functions.  Fortunately this is not
  ABI-affecting, so we can evaluate it anytime.

- Type layout really seems like a lot of code now that we sometimes
  need to call swift_checkMetadataState for generic arguments.  Maybe
  we can have the runtime do this by marking low bits or something, so
  that a TypeLayoutRef is actually either (1) a TypeLayout, (2) a known
  layout-complete metadata, or (3) a metadata of unknown state.  We could
  do that later with a flag, but we'll need to at least future-proof by
  allowing the runtime functions to return a MetadataDependency.
2018-03-26 12:18:04 -04:00
Huon Wilson
4f53475dd9 [IRGen] Support fulfilling conformances from conditional requirements.
A concrete conformance may involve conditional conformances, which are witness
tables that we can access from the original conformance's one. We need to track
metadata and be able to follow it in a metadata path.
2017-11-08 17:02:50 -08:00
Slava Pestov
15bbf2c246 IRGen: Fulfill generic parameters from class-constrained archetypes 2017-09-25 22:15:46 -07:00
Slava Pestov
5068baf9bb IRGen: FulfillmentMap::Everything was only used in one place, make it a local type 2017-09-25 22:15:45 -07:00
Slava Pestov
b949c67050 IRGen: Slightly more accurate isLeafTypeMetadata() for nested nominal types 2017-09-25 22:15:45 -07:00
Slava Pestov
453a5bc431 IRGen: Remove Fulfillment::searchParentTypeMetadata()
This is no longer needed now that we store arguments for all
nesting depths in type metadata.
2017-09-25 15:45:16 -07:00
John McCall
5b4c2cfc1c Minor improvements to MetadataPath for base conformances. 2017-03-14 03:21:51 -04:00
Doug Gregor
042e6510c3 [AST] Drop ProtocolDecl's "inherited protocols" list.
The list of directly inherited protocols of a ProtocolDecl is already
encoded in the requirement signature, as conformance constraints where
the subject is Self. Gather the list from there rather than separately
computing/storing the list of "inherited protocols".
2017-02-20 09:41:00 -08:00
Slava Pestov
5296d02485 AST: More include-what-you-use gardening 2017-02-12 00:51:26 -08:00
Doug Gregor
aa42045d66 [AST] “Error” types aren’t really unchecked; deal with them explicitly. 2017-02-11 00:04:13 -08:00
Slava Pestov
c90a77df31 IRGen: Use SubstitutionMap instead of ArrayRef<Substitution> in a few places
The crazy indexing logic is gone.
2017-02-03 19:55:41 -08:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00