swift-macro-testing icon indicating copy to clipboard operation
swift-macro-testing copied to clipboard

Fix-it coverage lost when macros include diagnostics with and without fix-its

Open gohanlon opened this issue 6 months ago • 1 comments

Description

assertMacro only applies fix-its when all emitted diagnostics have one or more fix-its, so fix-it coverage is lost when macros include diagnostics with and without fix-its. Instead, fixes should be included if any diagnostic has fix-its, even though it’s probable that the fixed source won’t resolve all the diagnostics. (I say “probable” because it is possible, yet unusual, for one fix-it to resolve multiple diagnostics.)

Currently, the following test:

func testInvalidLabelAndDefaultOnMultipleBindings() {
  assertMacro {
    """
    @MemberwiseInit
    struct S {
      @Init(default: 0, label: "$foo") let x, y: T
    }
    """
  }
}

records with just diagnostics:

func testInvalidLabelAndDefaultOnMultipleBindings() {
  assertMacro {
    """
    @MemberwiseInit
    struct S {
      @Init(default: 0, label: "$foo") let x, y: T
    }
    """
  } diagnostics: {
    """
    @MemberwiseInit
    struct S {
      @Init(default: 0, label: "$foo") let x, y: T
                        ┬────────────
            │           ╰─ 🛑 Custom 'label' can't be applied to multiple bindings
            ┬──────────
            ╰─ 🛑 Custom 'default' can't be applied to multiple bindings
               ✏️ Remove 'default: 0'
    }
    """
  }
}

I'd expect that the following fixes closure would be included after diagnostics:

[…]
  } fixes: {
    """
    @MemberwiseInit
    struct S {
      @Init(label: "$foo") let x, y: T
    }
    """
  }

Checklist

  • [X] If possible, I've reproduced the issue using the main branch of this package.
  • [X] This issue hasn't been addressed in an existing GitHub issue or discussion.

Expected behavior

No response

Actual behavior

No response

Steps to reproduce

No response

swift-macro-testing version information

0.2.0, main (15916c0)

Destination operating system

macOS 14.2

Xcode version information

Xcode Version 15.1 (15C65)

Swift Compiler version information

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

gohanlon avatar Jan 12 '24 23:01 gohanlon