Skip to content

Conversation

@grynspan
Copy link
Contributor

@grynspan grynspan commented Sep 25, 2023

This PR adds an additional argument to the #expect() and #require() macros that, if specified, replaces the source location derived at macro expansion time and passed to __check(). This change is ABI-safe for anyone who has already adopted swift-testing because it does not affect the emitted code or symbols. It is source-safe.

In the typical use case, the existing/old code path is taken at compile time and this PR has no effect. If a developer wants to specify a precise source location, they can do so.

This PR also changes the default behaviour of #expect() to generate an instance of SourceLocation using default arguments rather than by asking the macro context to provide all info. This is a little bit faster and is more accurate when a freestanding macro expression is embedded in another macro expression, such as:

#expect(throws: FooError.self) {
  #expect(try foo())
}

Note

The arguments to SourceLocation.init() all currently have default values. We might want to change that in the future so that a developer cannot accidentally underspecify an instance. Where a default value is truly required, we can offer a .here() factory function that explicitly indicates intent.

Resolves rdar://112974442.
Resolves #15.

@grynspan
Copy link
Contributor Author

@swift-ci please test

1 similar comment
@grynspan
Copy link
Contributor Author

@swift-ci please test

@grynspan
Copy link
Contributor Author

@swift-ci please test

This PR adds an additional argument to the `#expect()` and `#require()` macros that, if specified, replaces the source location derived at macro expansion time and passed to `__check()`. This change is ABI-safe for anyone who has already adopted swift-testing because it does not affect the emitted code or symbols. It is source-safe.

In the typical use case, the existing/old code path is taken at compile time and this PR has no effect. If a developer wants to specify a precise source location, they can do so.

This PR also changes the default behaviour of `#expect()` to generate an instance of `SourceLocation` using default arguments rather than by asking the macro context to provide all info. This is a little bit faster and is more accurate when a freestanding macro expression is embedded in another macro expression, such as:

```swift
  #expect(try foo())
}
```

> [!NOTE]
> The arguments to `SourceLocation.init()` all currently have default values. We might want to change that in the future so that a developer cannot accidentally underspecify an instance. Where a default value is truly required, we can offer a `.here()` factory function that explicitly indicates intent.

Resolves rdar://112974442.
Resolves #15.
@grynspan grynspan force-pushed the jgrynspan/112974442-sourcelocation-arg branch from 581cd35 to b2eb738 Compare September 26, 2023 15:20
@grynspan
Copy link
Contributor Author

@swift-ci please test

Copy link
Contributor

@stmontgomery stmontgomery left a comment

Choose a reason for hiding this comment

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

One small thing, but LGTM!

@grynspan
Copy link
Contributor Author

@swift-ci please test

@grynspan grynspan merged commit 03afefd into main Sep 26, 2023
@grynspan grynspan deleted the jgrynspan/112974442-sourcelocation-arg branch September 26, 2023 16:01
@stmontgomery stmontgomery added the enhancement New feature or request label Oct 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide an API to supply explicit file/line locations

4 participants