Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declaration order of type-constrained typealias and struct implementing a generic protocol causes "invalid type alias" error #73154

Open
pkillian opened this issue Apr 19, 2024 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. generics Feature: generic declarations and types typealias Feature → type declarations: `typealias` declarations

Comments

@pkillian
Copy link

pkillian commented Apr 19, 2024

Description

When declaring a type-constrained typealias and implementing a struct that conforms to generic protocols, compilation can fail with an "invalid type alias" error if the typealias's declaration precedes the struct's implementation in the source file.

Reproduction

This example DOES NOT compile:

// Define two protocols; the second has an associated type constrained to the first
protocol Genericable {}
protocol Exampleable<Generic> where Generic: Genericable {
    associatedtype Generic

    // Define a typealias that requires a generic type, using the associatedtype above
    typealias GenericArray = Array<Generic>

    // Define a function that uses the above typealias to define a generic parameter
    func start(generics: GenericArray)
}

// Implement some structs and classes using the above protocols
struct ImplementedGeneric: Genericable {}
class ImplementedClass<Generic: Genericable, Example: Exampleable> where Example.Generic == Generic {}

// NOTE: MOVE THIS TYPEALIAS TO END OF FILE TO BUILD SUCCESSFULLY
typealias ImplementedClassType = ImplementedClass<ImplementedGeneric, ImplementedExample<ImplementedGeneric>>

struct ImplementedExample<Generic: Genericable>: Exampleable {

    // NOTE: Build failure here whenever the typealias of "ImplementedClassType"
    //   precedes "ImplementedExample"'s declaration in the source file
    func start(generics: GenericArray) {
        // do stuff
    }
}

The following compilation error is encountered:

Reference to invalid type alias 'GenericArray' of type 'ImplementedExample'

This example DOES compile, with no errors:

// Define two protocols; the second has an associated type constrained to the first
// Define two protocols; the second has an associated type constrained to the first
protocol Genericable {}
protocol Exampleable<Generic> where Generic: Genericable {
    associatedtype Generic

    // Define a typealias that requires a generic type, using the associatedtype above
    typealias GenericArray = Array<Generic>

    // Define a function that uses the above typealias to define a generic parameter
    func start(generics: GenericArray)
}

// Implement some structs and classes using the above protocols
struct ImplementedGeneric: Genericable {}
class ImplementedClass<Generic: Genericable, Example: Exampleable> where Example.Generic == Generic {}

struct ImplementedExample<Generic: Genericable>: Exampleable {

    // NOTE: Build failure here whenever the typealias of "ImplementedClassType"
    //   precedes "ImplementedExample"'s declaration in the source file
    func start(generics: GenericArray) {
        // do stuff
    }
}

// NOTE: MOVE THIS TYPEALIAS ABOVE ``ImplementedExample`` TO REPRODUCE BUG
typealias ImplementedClassType = ImplementedClass<ImplementedGeneric, ImplementedExample<ImplementedGeneric>>

Expected behavior

Compilation is expected to succeed regardless of which order the typealias is defined and the struct is implemented. This prevents compilation from succeeding if the typealias is declared in a separate Swift file from the struct's implementation.

Environment

swift-driver version: 1.87.3 Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
Target: arm64-apple-macosx14.0

Additional information

I did my best to reduce the repro case down to the fewest lines of code and types, but doing any of the following fails to reproduce the issue:

  • Removing func start(...) from the protocol's definition
  • Removing ImplementedClass's first generic type Generic: Genericable
@pkillian pkillian added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Apr 19, 2024
@hborla hborla added generics Feature: generic declarations and types typealias Feature → type declarations: `typealias` declarations and removed triage needed This issue needs more specific labels labels Apr 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. generics Feature: generic declarations and types typealias Feature → type declarations: `typealias` declarations
Projects
None yet
Development

No branches or pull requests

2 participants