generalize-pattern - #161606
generalize-pattern#161606pacak wants to merge 2 commits into
Conversation
|
cc @Amanieu, @folkertdev, @sayantn |
|
r? @JohnTitor rustbot has assigned @JohnTitor. Use Why was this reviewer chosen?The reviewer was selected based on:
|
I'm not sure what's the right approach here. Sync it after this lands? A separate pull request? |
|
This is fine, it's a tiny change, we'll handle the sync after it merges. |
|
r? @nia-e |
|
#161608 (review) - I'm going to try this |
9471adf to
0a4f4c4
Compare
This comment has been minimized.
This comment has been minimized.
0a4f4c4 to
ae668d2
Compare
This comment has been minimized.
This comment has been minimized.
ae668d2 to
ebc1513
Compare
This comment has been minimized.
This comment has been minimized.
ebc1513 to
7f1c215
Compare
7f1c215 to
ef9cc1d
Compare
| } | ||
| #[inline(always)] | ||
| unsafe fn get_unchecked(&self, range: Range<usize>) -> &Self { | ||
| // SAFETY: Caller promises position is a character boundary. |
There was a problem hiding this comment.
There are two positions here, start and end, and the str method has extra requirements:
https://doc.rust-lang.org/std/primitive.str.html#method.get_unchecked
| // SAFETY: Caller promises position is a character boundary. | |
| // SAFETY: Caller promises start and end positions are both on character boundaries, | |
| // start is not after end, and both are within the slice. |
| } | ||
|
|
||
| unsafe impl<'a> Searcher<'a> for CharSearcher<'a> { | ||
| unsafe impl<'a> Searcher<'a, str> for CharSearcher<'a> { |
There was a problem hiding this comment.
This is a pre-existing issue: there is no safety comment here. It would be easier to review any changes to the impl if the safety guarantees were documented explicitly. Maybe the safety comment should go on all the next*() methods, because that seems to be what the trait is concerned about?
Here there's no change except the type parameter (and anything that changed on the trait), so this seems more like a nitpick.
There are 14 of these in this file, so this might be best left to another PR. Let's check with an experienced libs reviewer?
| //! | ||
| //! For more details, see the traits [`Pattern`], [`Haystack`], [`Searcher`], | ||
| //! [`ReverseSearcher`] and [`DoubleEndedSearcher`]. Although this API is | ||
| //! unstable, it is exposed via stable methods on corresponding haystack types. |
There was a problem hiding this comment.
Note this exposure in stable APIs, which implies some kind of stability guarantee…
| /// assert_eq!("abcdef_z".find(|ch| ch > 'd' && ch < 'y'), Some(4)); | ||
| /// assert_eq!("abcddd_z".find(|ch| ch > 'd' && ch < 'y'), None); | ||
| /// ``` | ||
| pub trait Pattern<H: Haystack + ?Sized>: Sized { |
There was a problem hiding this comment.
So adding this generic could impact stable type inference, even though Pattern itself is unstable.
Do we need a crater run here? Or do we want to rely on the beta crater run later?
(This is a question for an experienced libs reviewer.)
| /// [`cursor_at_front()`][Self::cursor_at_front]), | ||
| /// - the back of the haystack (as returned by | ||
| /// [`cursor_at_back()`][Self::cursor_at_back]), or | ||
| /// - any cursor returned by a [`Searcher`] or [`ReverseSearcher`]. |
There was a problem hiding this comment.
This might help explain why those positions are always safe.
| /// - any cursor returned by a [`Searcher`] or [`ReverseSearcher`]. | |
| /// - any cursor returned by the [`Searcher`] or [`ReverseSearcher`] unsafe traits, | |
| /// which are guaranteed to return valid split positions from their `next()` methods. |
| /// | ||
| /// # Safety | ||
| /// | ||
| /// The range's start and end must be valid haystack split positions, |
There was a problem hiding this comment.
See the second safety requirement of https://doc.rust-lang.org/std/primitive.str.html#method.get_unchecked
This was implied, but it's better to make safety comments explicit.
| /// The range's start and end must be valid haystack split positions, | |
| /// The range's start and end must be valid split positions inside the haystack, |
coretests: Add more pattern tests. Right now things are undertested and underspecified. Some of the library code would get in a loop if searcher starts returning empty rejects. And there's no tests for backwards multi byte char matchers. Pull request I'm reviving had a problem implementing that, so making sure it's tested before the actual code lands. Right now it is possible to break both tests (and user code) without breaking anything else in the test suite I think. This commit is extracted from rust-lang#160971 with slight modifications. <!-- start jj-vine stack --> This PR is part of a stack containing 16 PRs: 1. `main` 2. **"coretests: Add more pattern tests." (this PR)** 3. rust-lang#161606 ([Compare](https://github.com/pacak/rust/compare/more-pat-tests..generalize-pattern)) 4. rust-lang#161754 ([Compare](https://github.com/pacak/rust/compare/generalize-pattern..push-qvvokrsvvqww)) 5. rust-lang#161755 ([Compare](https://github.com/pacak/rust/compare/push-qvvokrsvvqww..push-wlxvvvxzrsly)) 6. rust-lang#161756 ([Compare](https://github.com/pacak/rust/compare/push-wlxvvvxzrsly..push-lkvnuzsvuqzt)) 7. rust-lang#161757 ([Compare](https://github.com/pacak/rust/compare/push-lkvnuzsvuqzt..push-wuxtmysppkst)) 8. rust-lang#161758 ([Compare](https://github.com/pacak/rust/compare/push-wuxtmysppkst..push-tnkxmkmykopl)) 9. rust-lang#161759 ([Compare](https://github.com/pacak/rust/compare/push-tnkxmkmykopl..push-umzmopsxmwry)) 10. rust-lang#161760 ([Compare](https://github.com/pacak/rust/compare/push-umzmopsxmwry..push-ptqpwloxpyru)) 11. rust-lang#161761 ([Compare](https://github.com/pacak/rust/compare/push-ptqpwloxpyru..push-rksmzopsvzlk)) 12. rust-lang#161762 ([Compare](https://github.com/pacak/rust/compare/push-rksmzopsvzlk..push-mkxpywoqusmn)) 13. rust-lang#161608 ([Compare](https://github.com/pacak/rust/compare/push-mkxpywoqusmn..flavor-pattern)) 14. rust-lang#161763 ([Compare](https://github.com/pacak/rust/compare/flavor-pattern..push-xoslomrztmly)) 15. rust-lang#161764 ([Compare](https://github.com/pacak/rust/compare/push-xoslomrztmly..push-pqpumynltyml)) 16. rust-lang#161765 ([Compare](https://github.com/pacak/rust/compare/push-pqpumynltyml..push-svrvnxkpuqul)) 17. rust-lang#161610 ([Compare](https://github.com/pacak/rust/compare/push-svrvnxkpuqul..osstr-pattern-tip)) <!-- end jj-vine stack -->
coretests: Add more pattern tests. Right now things are undertested and underspecified. Some of the library code would get in a loop if searcher starts returning empty rejects. And there's no tests for backwards multi byte char matchers. Pull request I'm reviving had a problem implementing that, so making sure it's tested before the actual code lands. Right now it is possible to break both tests (and user code) without breaking anything else in the test suite I think. This commit is extracted from rust-lang#160971 with slight modifications. <!-- start jj-vine stack --> This PR is part of a stack containing 16 PRs: 1. `main` 2. **"coretests: Add more pattern tests." (this PR)** 3. rust-lang#161606 ([Compare](https://github.com/pacak/rust/compare/more-pat-tests..generalize-pattern)) 4. rust-lang#161754 ([Compare](https://github.com/pacak/rust/compare/generalize-pattern..push-qvvokrsvvqww)) 5. rust-lang#161755 ([Compare](https://github.com/pacak/rust/compare/push-qvvokrsvvqww..push-wlxvvvxzrsly)) 6. rust-lang#161756 ([Compare](https://github.com/pacak/rust/compare/push-wlxvvvxzrsly..push-lkvnuzsvuqzt)) 7. rust-lang#161757 ([Compare](https://github.com/pacak/rust/compare/push-lkvnuzsvuqzt..push-wuxtmysppkst)) 8. rust-lang#161758 ([Compare](https://github.com/pacak/rust/compare/push-wuxtmysppkst..push-tnkxmkmykopl)) 9. rust-lang#161759 ([Compare](https://github.com/pacak/rust/compare/push-tnkxmkmykopl..push-umzmopsxmwry)) 10. rust-lang#161760 ([Compare](https://github.com/pacak/rust/compare/push-umzmopsxmwry..push-ptqpwloxpyru)) 11. rust-lang#161761 ([Compare](https://github.com/pacak/rust/compare/push-ptqpwloxpyru..push-rksmzopsvzlk)) 12. rust-lang#161762 ([Compare](https://github.com/pacak/rust/compare/push-rksmzopsvzlk..push-mkxpywoqusmn)) 13. rust-lang#161608 ([Compare](https://github.com/pacak/rust/compare/push-mkxpywoqusmn..flavor-pattern)) 14. rust-lang#161763 ([Compare](https://github.com/pacak/rust/compare/flavor-pattern..push-xoslomrztmly)) 15. rust-lang#161764 ([Compare](https://github.com/pacak/rust/compare/push-xoslomrztmly..push-pqpumynltyml)) 16. rust-lang#161765 ([Compare](https://github.com/pacak/rust/compare/push-pqpumynltyml..push-svrvnxkpuqul)) 17. rust-lang#161610 ([Compare](https://github.com/pacak/rust/compare/push-svrvnxkpuqul..osstr-pattern-tip)) <!-- end jj-vine stack -->
coretests: Add more pattern tests. Right now things are undertested and underspecified. Some of the library code would get in a loop if searcher starts returning empty rejects. And there's no tests for backwards multi byte char matchers. Pull request I'm reviving had a problem implementing that, so making sure it's tested before the actual code lands. Right now it is possible to break both tests (and user code) without breaking anything else in the test suite I think. This commit is extracted from rust-lang#160971 with slight modifications. <!-- start jj-vine stack --> This PR is part of a stack containing 16 PRs: 1. `main` 2. **"coretests: Add more pattern tests." (this PR)** 3. rust-lang#161606 ([Compare](https://github.com/pacak/rust/compare/more-pat-tests..generalize-pattern)) 4. rust-lang#161754 ([Compare](https://github.com/pacak/rust/compare/generalize-pattern..push-qvvokrsvvqww)) 5. rust-lang#161755 ([Compare](https://github.com/pacak/rust/compare/push-qvvokrsvvqww..push-wlxvvvxzrsly)) 6. rust-lang#161756 ([Compare](https://github.com/pacak/rust/compare/push-wlxvvvxzrsly..push-lkvnuzsvuqzt)) 7. rust-lang#161757 ([Compare](https://github.com/pacak/rust/compare/push-lkvnuzsvuqzt..push-wuxtmysppkst)) 8. rust-lang#161758 ([Compare](https://github.com/pacak/rust/compare/push-wuxtmysppkst..push-tnkxmkmykopl)) 9. rust-lang#161759 ([Compare](https://github.com/pacak/rust/compare/push-tnkxmkmykopl..push-umzmopsxmwry)) 10. rust-lang#161760 ([Compare](https://github.com/pacak/rust/compare/push-umzmopsxmwry..push-ptqpwloxpyru)) 11. rust-lang#161761 ([Compare](https://github.com/pacak/rust/compare/push-ptqpwloxpyru..push-rksmzopsvzlk)) 12. rust-lang#161762 ([Compare](https://github.com/pacak/rust/compare/push-rksmzopsvzlk..push-mkxpywoqusmn)) 13. rust-lang#161608 ([Compare](https://github.com/pacak/rust/compare/push-mkxpywoqusmn..flavor-pattern)) 14. rust-lang#161763 ([Compare](https://github.com/pacak/rust/compare/flavor-pattern..push-xoslomrztmly)) 15. rust-lang#161764 ([Compare](https://github.com/pacak/rust/compare/push-xoslomrztmly..push-pqpumynltyml)) 16. rust-lang#161765 ([Compare](https://github.com/pacak/rust/compare/push-pqpumynltyml..push-svrvnxkpuqul)) 17. rust-lang#161610 ([Compare](https://github.com/pacak/rust/compare/push-svrvnxkpuqul..osstr-pattern-tip)) <!-- end jj-vine stack -->
60b9872 to
e2acde3
Compare
|
I guess I'll rebase one more time once tests are merged. Not sure what to do with stability guarantees. I can look into describing safety requirements for searchers, probably a separate commit/PR. |
coretests: Add more pattern tests. Right now things are undertested and underspecified. Some of the library code would get in a loop if searcher starts returning empty rejects. And there's no tests for backwards multi byte char matchers. Pull request I'm reviving had a problem implementing that, so making sure it's tested before the actual code lands. Right now it is possible to break both tests (and user code) without breaking anything else in the test suite I think. This commit is extracted from rust-lang#160971 with slight modifications. <!-- start jj-vine stack --> This PR is part of a stack containing 16 PRs: 1. `main` 2. **"coretests: Add more pattern tests." (this PR)** 3. rust-lang#161606 ([Compare](https://github.com/pacak/rust/compare/more-pat-tests..generalize-pattern)) 4. rust-lang#161754 ([Compare](https://github.com/pacak/rust/compare/generalize-pattern..push-qvvokrsvvqww)) 5. rust-lang#161755 ([Compare](https://github.com/pacak/rust/compare/push-qvvokrsvvqww..push-wlxvvvxzrsly)) 6. rust-lang#161756 ([Compare](https://github.com/pacak/rust/compare/push-wlxvvvxzrsly..push-lkvnuzsvuqzt)) 7. rust-lang#161757 ([Compare](https://github.com/pacak/rust/compare/push-lkvnuzsvuqzt..push-wuxtmysppkst)) 8. rust-lang#161758 ([Compare](https://github.com/pacak/rust/compare/push-wuxtmysppkst..push-tnkxmkmykopl)) 9. rust-lang#161759 ([Compare](https://github.com/pacak/rust/compare/push-tnkxmkmykopl..push-umzmopsxmwry)) 10. rust-lang#161760 ([Compare](https://github.com/pacak/rust/compare/push-umzmopsxmwry..push-ptqpwloxpyru)) 11. rust-lang#161761 ([Compare](https://github.com/pacak/rust/compare/push-ptqpwloxpyru..push-rksmzopsvzlk)) 12. rust-lang#161762 ([Compare](https://github.com/pacak/rust/compare/push-rksmzopsvzlk..push-mkxpywoqusmn)) 13. rust-lang#161608 ([Compare](https://github.com/pacak/rust/compare/push-mkxpywoqusmn..flavor-pattern)) 14. rust-lang#161763 ([Compare](https://github.com/pacak/rust/compare/flavor-pattern..push-xoslomrztmly)) 15. rust-lang#161764 ([Compare](https://github.com/pacak/rust/compare/push-xoslomrztmly..push-pqpumynltyml)) 16. rust-lang#161765 ([Compare](https://github.com/pacak/rust/compare/push-pqpumynltyml..push-svrvnxkpuqul)) 17. rust-lang#161610 ([Compare](https://github.com/pacak/rust/compare/push-svrvnxkpuqul..osstr-pattern-tip)) <!-- end jj-vine stack -->
Rust has a tool called "crater" to check ecosystem impact, I'm waiting on an experienced libs/types reviewer (which I am not) to see if it's worth doing for this PR specifically.
Let's also wait for a more experienced reviewer for this decision. |
coretests: Add more pattern tests. Right now things are undertested and underspecified. Some of the library code would get in a loop if searcher starts returning empty rejects. And there's no tests for backwards multi byte char matchers. Pull request I'm reviving had a problem implementing that, so making sure it's tested before the actual code lands. Right now it is possible to break both tests (and user code) without breaking anything else in the test suite I think. This commit is extracted from rust-lang#160971 with slight modifications. <!-- start jj-vine stack --> This PR is part of a stack containing 16 PRs: 1. `main` 2. **"coretests: Add more pattern tests." (this PR)** 3. rust-lang#161606 ([Compare](https://github.com/pacak/rust/compare/more-pat-tests..generalize-pattern)) 4. rust-lang#161754 ([Compare](https://github.com/pacak/rust/compare/generalize-pattern..push-qvvokrsvvqww)) 5. rust-lang#161755 ([Compare](https://github.com/pacak/rust/compare/push-qvvokrsvvqww..push-wlxvvvxzrsly)) 6. rust-lang#161756 ([Compare](https://github.com/pacak/rust/compare/push-wlxvvvxzrsly..push-lkvnuzsvuqzt)) 7. rust-lang#161757 ([Compare](https://github.com/pacak/rust/compare/push-lkvnuzsvuqzt..push-wuxtmysppkst)) 8. rust-lang#161758 ([Compare](https://github.com/pacak/rust/compare/push-wuxtmysppkst..push-tnkxmkmykopl)) 9. rust-lang#161759 ([Compare](https://github.com/pacak/rust/compare/push-tnkxmkmykopl..push-umzmopsxmwry)) 10. rust-lang#161760 ([Compare](https://github.com/pacak/rust/compare/push-umzmopsxmwry..push-ptqpwloxpyru)) 11. rust-lang#161761 ([Compare](https://github.com/pacak/rust/compare/push-ptqpwloxpyru..push-rksmzopsvzlk)) 12. rust-lang#161762 ([Compare](https://github.com/pacak/rust/compare/push-rksmzopsvzlk..push-mkxpywoqusmn)) 13. rust-lang#161608 ([Compare](https://github.com/pacak/rust/compare/push-mkxpywoqusmn..flavor-pattern)) 14. rust-lang#161763 ([Compare](https://github.com/pacak/rust/compare/flavor-pattern..push-xoslomrztmly)) 15. rust-lang#161764 ([Compare](https://github.com/pacak/rust/compare/push-xoslomrztmly..push-pqpumynltyml)) 16. rust-lang#161765 ([Compare](https://github.com/pacak/rust/compare/push-pqpumynltyml..push-svrvnxkpuqul)) 17. rust-lang#161610 ([Compare](https://github.com/pacak/rust/compare/push-svrvnxkpuqul..osstr-pattern-tip)) <!-- end jj-vine stack -->
Rollup merge of #161596 - pacak:more-pat-tests, r=nia-e coretests: Add more pattern tests. Right now things are undertested and underspecified. Some of the library code would get in a loop if searcher starts returning empty rejects. And there's no tests for backwards multi byte char matchers. Pull request I'm reviving had a problem implementing that, so making sure it's tested before the actual code lands. Right now it is possible to break both tests (and user code) without breaking anything else in the test suite I think. This commit is extracted from #160971 with slight modifications. <!-- start jj-vine stack --> This PR is part of a stack containing 16 PRs: 1. `main` 2. **"coretests: Add more pattern tests." (this PR)** 3. #161606 ([Compare](https://github.com/pacak/rust/compare/more-pat-tests..generalize-pattern)) 4. #161754 ([Compare](https://github.com/pacak/rust/compare/generalize-pattern..push-qvvokrsvvqww)) 5. #161755 ([Compare](https://github.com/pacak/rust/compare/push-qvvokrsvvqww..push-wlxvvvxzrsly)) 6. #161756 ([Compare](https://github.com/pacak/rust/compare/push-wlxvvvxzrsly..push-lkvnuzsvuqzt)) 7. #161757 ([Compare](https://github.com/pacak/rust/compare/push-lkvnuzsvuqzt..push-wuxtmysppkst)) 8. #161758 ([Compare](https://github.com/pacak/rust/compare/push-wuxtmysppkst..push-tnkxmkmykopl)) 9. #161759 ([Compare](https://github.com/pacak/rust/compare/push-tnkxmkmykopl..push-umzmopsxmwry)) 10. #161760 ([Compare](https://github.com/pacak/rust/compare/push-umzmopsxmwry..push-ptqpwloxpyru)) 11. #161761 ([Compare](https://github.com/pacak/rust/compare/push-ptqpwloxpyru..push-rksmzopsvzlk)) 12. #161762 ([Compare](https://github.com/pacak/rust/compare/push-rksmzopsvzlk..push-mkxpywoqusmn)) 13. #161608 ([Compare](https://github.com/pacak/rust/compare/push-mkxpywoqusmn..flavor-pattern)) 14. #161763 ([Compare](https://github.com/pacak/rust/compare/flavor-pattern..push-xoslomrztmly)) 15. #161764 ([Compare](https://github.com/pacak/rust/compare/push-xoslomrztmly..push-pqpumynltyml)) 16. #161765 ([Compare](https://github.com/pacak/rust/compare/push-pqpumynltyml..push-svrvnxkpuqul)) 17. #161610 ([Compare](https://github.com/pacak/rust/compare/push-svrvnxkpuqul..osstr-pattern-tip)) <!-- end jj-vine stack -->
This comment has been minimized.
This comment has been minimized.
Add a Haystack trait describing something that can be searched in and make core::str::Pattern (and related types) generic on that trait. This will allow Pattern to be used for types other than str (most notably OsStr). This somewhat follows the Pattern API 2.0 design. While that design is apparently abandoned (?), it is somewhat helpful when going for patterns on OsStr, so I’m going with it unless someone tells me otherwise. ;) For now leave Pattern, Haystack et al in core::str::pattern. Since they are no longer str-specific, I’ll move them to core::pattern in future commit. This one leaves them in place to make the diff smaller. @pacak: Original code had pattern parametrized by a reference (`Pattern<&str>`), I changed it to be `Pattern<str>`. Original code had indices in Haystack abstracted as an associated type Cursor. Replaced with usize - Cursor adds noise with not much value. Changed wording in 2-3 places - for example Searcher is generic over a few types so it makes more sense to talk about split points in general with utf8 split points as an example for `&str`.
Pattern is no longer str-specific, so move it from core::str::pattern module to a new core::pattern module. This introduces no changes in behaviour or implementation. Just moves stuff around and adjusts documentation.
e2acde3 to
5ad3f42
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
View all comments
OsStrspecifically)core::str::Patterntocore::pattern::Patternto reflect thatNot very useful on its own, but required to extend pattern splitting API to
OsStr.Contains changes from #161604, I'll rebase one that test lands.
Part of the #160971 cinematic universe.
This PR is part of a stack containing 15 PRs:
main