pointfreeco / swift-macro-testing

Magical testing tools for Swift macros.
MIT License
263 stars 19 forks source link

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

Open gohanlon opened 7 months ago

gohanlon commented 7 months ago

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

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 commented 7 months ago

This can be addressed incrementally, but is also discussed in #18 (a comprehensive proposal).