Skip to content

Make unary builder return Regex type consistently #503

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

Merged
merged 1 commit into from
Jun 20, 2022

Conversation

rxwei
Copy link
Contributor

@rxwei rxwei commented Jun 20, 2022

Currently, unary regex component builders simply forward the component's base type. However, this is inconsistent with non-unary builder results. The current behavior may lead to surprising results when the user marks a property with @RegexComponentBuilder.

This patch makes RegexComponentBuilder.buildPartialBlock<R>(first: R) return a Regex<R.RegexOutput> rather than R itself.


Before:

// error: cannot convert value of type 'OneOrMore<Substring>' to specified type 'Regex<Substring>'
@RegexComponentBuilder
var r: Regex<Substring> {
  OneOrMore("a")
  // Adding other components below will make the error go away.
}

struct MyCustomRegex: RegexComponent {
  // error: cannot convert value of type 'OneOrMore<Substring>' to specified type 'Regex<Substring>'
  @RegexComponentBuilder
  var regex: Regex<Substring> {
    OneOrMore("a")
  }
}

After: No errors.

@rxwei rxwei requested review from milseman and natecook1000 June 20, 2022 07:32
@rxwei
Copy link
Contributor Author

rxwei commented Jun 20, 2022

@swift-ci please test

Copy link
Member

@milseman milseman left a comment

Choose a reason for hiding this comment

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

LGTM

Currently, unary regex component builder simply forwards the component's base type. However, this is inconsistent with non-unary builder results. The current behavior may lead to surprising results when the user marks a property with `@RegexComponentBuilder`.

This patch makes `RegexComponentBuilder.buildPartialBlock<R>(first: R)` return a `Regex<R.RegexOutput>` rather than `R` itself.

---

Before:

```swift
// error: cannot convert value of type 'OneOrMore<Substring>' to specified type 'Regex<Substring>'
@RegexComponentBuilder
var r: Regex<Substring> {
  OneOrMore("a")
  // Adding other components below will make the error go away.
}

struct MyCustomRegex: RegexComponent {
  // error: cannot convert value of type 'OneOrMore<Substring>' to specified type 'Regex<Substring>'
  var regex: Regex<Substring> {
    OneOrMore("a")
  }
}
```

After: No errors.
@rxwei rxwei force-pushed the unary-buildpartialblock branch from bf273c7 to e6f3896 Compare June 20, 2022 18:35
@rxwei
Copy link
Contributor Author

rxwei commented Jun 20, 2022

@swift-ci please test

@rxwei rxwei merged commit cc9efb9 into swiftlang:main Jun 20, 2022
@rxwei rxwei deleted the unary-buildpartialblock branch June 20, 2022 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants