stackotter / swift-macro-toolkit

A powerful toolkit for creating concise and expressive Swift macros
Apache License 2.0
246 stars 15 forks source link

Implement type normalization #7

Open stackotter opened 1 year ago

stackotter commented 1 year ago

A major pain point for many Macro developers is handling all of the different ways that users can write types. For example, Void? is equivalent to Optional<Void> which is equivalent to (((((()))?))); what a nightmare!

If you're going to complete this issue you may need a good understanding of Swift itself, but the issue should still be relatively approachable for anyone new to Swift Macros.

This task contains four main steps; I've tried to give enough detail for someone to jump in and complete this without looking around the codebase for too long (specifically for Hacktoberfest!)

The following steps would likely improve Quality of Life (but could be completed separately);

NormalizedType (a rough plan)

enum NormalizedType {
    /// A composition of two types (e.g. `Encodable & Decodable`). Used to
    /// combine protocol requirements.
    case composition(NormalizedCompositionType)
    /// A some or any protocol type (e.g. `any T` or `some T`).
    case someOrAny(NormalizedSomeOrAnyType)
    /// A function type (e.g. `() -> ()`).
    case function(NormalizedFunctionType)
    /// An implicitly unwrapped optional type (e.g. `Int!`).
    case implicitlyUnwrappedOptional(NormalizedImplicitlyUnwrappedOptionalType)
    /// A member type (e.g. `Array<Int>.Element`).
    case member(NormalizedMemberType)
    /// A pack expansion type (e.g. `repeat each V`).
    case packExpansion(NormalizedPackExpansionType)
    /// A pack reference type (e.g. `each V`).
    case packReference(NormalizedPackReferenceType)
    /// A simple type (e.g. `Int` or `Box<Int>`).
    case simple(NormalizedSimpleType)
    /// A suppressed type in a conformance position (e.g. `~Copyable`).
    case suppressed(NormalizedSuppressedType)
    //// A tuple type (e.g. `(Int, String)`).
    case tuple(NormalizedTupleType)
}

Converting Type to NormalizedType as simply as possible will tackle most forms of normalization. However, there are another two normalization patterns that need to be considered and aren't encoded in this new type: () gets replaced by Void (a NormalizedSimpleType), and (_) gets replaced by _ (where _ is some arbitrary type).

alessionossa commented 8 months ago

I'm trying to implement this, with good results already, however I am not sure what classRestriction, metatype and missing cases of Type should be mapped to in NormalizedType.

stackotter commented 8 months ago

Awesome! I reckon classRestriction should map to the nominal type AnyObject (iirc it’s equivalent to that but I may be mistaken), metatype should be mapped to the member type X.Type (where X is the type that the metatype is of), and missing is a bit annoying. For missing it may turn out that macros are never invoked on malformed syntax (the only way that missing can appear in the AST afaik), in which case we could potentially fatalError on missing earlier on (when initialising a Type) since it’s meant to be unreachable; but for now maybe just add a missing case to NormalizedType and I’ll deal with that issue later.