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

Property accesses in failed expectations sometimes include their value twice in expanded description #601

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

stmontgomery
Copy link
Contributor

This fixes a bug in which some property access expressions passed to #expect or other expectations will include their value twice, redundantly, in the expanded description if they fail.

Here are two examples showing the expanded description before and after this fix:

// BEFORE: ❌ Expectation failed: !([].isEmpty → true → true)
// AFTER:  ❌ Expectation failed: !([].isEmpty → true)
#expect(![].isEmpty)

struct Outer {
  struct Middle {
    struct Inner {
      var value: Int? = nil
    }
    var inner = Inner()
  }
  var middle = Middle()
}
let outer = Outer()

// BEFORE: ❌ Expectation failed: (outer.middle.inner → Inner(value: nil)).value → nil → nil
// AFTER:  ❌ Expectation failed: (outer.middle.inner → Inner(value: nil)).value → nil
_ = try #require(outer.middle.inner.value)

The problem is that the keyPath associated value of the __Expression.Kind.propertyAccess enum case is an __Expression and recursively expanding it includes its runtime value. However, the overall property access is an expression too, and when it gets expanded, it includes that same value. So storing keyPath as an expression and expanding it is redundant. The source code of keyPath needs to be stored, but it doesn't need to be an __Expression.

Conceptually, property accesses should be modeled more like function calls: the result of a function call expression is not stored as associated value of the functionCall enum case.

Modifications:

  • Change the keyPath associated value of __Expression.Kind.propertyAccess to be a String containing the name of the property, rather than an __Expression.
  • Update the logic for recursively expanding a failure description for a faled expectation as a result of the previous change.
  • Change the implementation of #expect and similar macros to emit the property name as a string literal rather than an __Expression factory function.
  • Add new tests.

Result:

The two examples given above no longer include the property value twice.

Checklist:

  • Code and documentation should follow the style of the Style Guide.
  • If public symbols are renamed or modified, DocC references should be updated.

@stmontgomery
Copy link
Contributor Author

@swift-ci please test

Copy link
Contributor

@grynspan grynspan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's discuss when you're back.

@stmontgomery
Copy link
Contributor Author

@swift-ci please test

@stmontgomery
Copy link
Contributor Author

@swift-ci please test

@stmontgomery stmontgomery added this to the Swift 6.1 milestone Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants