Skip to content

Compiler error when extending a typealias of a partially specialized generic type #68212

Open
@tapsns

Description

@tapsns

Description
When creating a typealias that partially specializes a generic type, the compiler generates an error when attempting to extend that type via the typealias.

Steps to reproduce
Create a Generic type with two type parameters.

struct Field<Tag,Value> {
  let tag: Tag
  let value: Value
}

Define a typealias which specializes one of the type parameters as such :

typealias IntField<Tag> = Field<Tag,Int>

Define an extension based on the typealias:

extension IntField {
  func adding(_ value: Int) -> Self {
    Field(tag: tag, value: self.value + value)
  }
}

Expected behavior
I would expect this code to compile.

Instead the compiler complained: "Binary operator '+' cannot be applied to operands of type 'Value' and 'Int'"

If instead of extending the typealias, I extend the type directly it compiles fine as such:

extension Field where Value == Int {
  func adding(_ value: Int) -> Self {
    Field(tag: tag, value: self.value + value)
  }
}

Environment

  • Swift compiler version info swift-driver version: 1.75.2 Apple Swift version 5.8.1 (swiftlang-5.8.0.124.5 clang-1403.0.22.11.100)
  • Xcode version info: Xcode 14.3.1 Build version 14E300c
  • Deployment target: macOS Playground

Metadata

Metadata

Assignees

Labels

TypeResolverbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfdeclarationsFeature: declarationsextensionFeature → declarations: `extension` declarationsgenericsFeature: generic declarations and typesgood first issueGood for newcomersswift 5.9type checkerArea → compiler: Semantic analysistypealiasFeature → type declarations: `typealias` declarationsunexpected errorBug: Unexpected error

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions