Skip to content

Inlay hints may wrongly insert variadic type #2609

Description

@Steffeeen

Swift version

swiftlang-6.2.3.3.21

Platform

macOS 15

Editor

Visual Studio Code, main branch of SourceKit-LSP

Description

For a function like this:

func test(x: Int...) {
  let y = x
}

SourceKit-LSP shows the inferred type for y to be Int.... Because the shown type is also the one that is inserted by double-clicking, SourceKit-LSP inserts Int... as the type for y which is not valid as variadic types are only allowed in function parameters.

I know that this issue probably has to be fixed in SourceKit, as SourceKit-LSP just displays the types that source.request.variable.type returns. However, as the source.request.variable.type request may also be used by other tools I'm not sure if we can just change the way variadic types are printed in the result. As a workaround, we could also just detect variadic types in the inlay hint code in SourceKit-LSP and replace them by the corresponding array type.

I'm open to providing a PR to fix this directly in SourceKit or for the workaround in SourceKit-LSP.

Steps to Reproduce

Inlay hint test case that reproduces this issue:

func testInferredVariadicParameter() async throws {
  let (positions, hints) = try await performInlayHintRequest(
    markedText: """
      func test(x: Int...) {
        let y1️⃣ = x
      }
      """
  )
  assertHintsEqual(
    hints,
    [
      makeInlayHint(
        position: positions["1️⃣"],
        kind: .type,
        label: ": [Int]"
      ),
    ]
  )
}

Logging

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions