Skip to content

StashSaveOptions::pathspec(): return a Result, handle invalid values#1295

Open
DanielEScherzer wants to merge 2 commits into
rust-lang:mainfrom
DanielEScherzer:stash-save-opts-pathspec
Open

StashSaveOptions::pathspec(): return a Result, handle invalid values#1295
DanielEScherzer wants to merge 2 commits into
rust-lang:mainfrom
DanielEScherzer:stash-save-opts-pathspec

Conversation

@DanielEScherzer

Copy link
Copy Markdown
Contributor

When the provided path spec cannot be converted to a CString, return an Err variant rather than panicking.

… specs

The test is currently marked as `#[should_panic]` because the implementation
panics when the path given is invalid, a follow-up commit will switch to
returning a `Result`.
When the provided path spec cannot be converted to a `CString`, return an `Err`
variant rather than panicking.
@rustbot rustbot added the S-waiting-on-review Status: Waiting on review label Jul 16, 2026
@DanielEScherzer

Copy link
Copy Markdown
Contributor Author

@rustbot label +semver-breaking-change

@rustbot rustbot added the semver-breaking-change A SemVer breaking change that will require a new SemVer-incompatible version number bump. label Jul 16, 2026
Comment thread src/stash.rs
/// Add to the array of paths patterns to build the stash.
pub fn pathspec<T: IntoCString>(&mut self, pathspec: T) -> &mut Self {
let s = util::cstring_to_repo_path(pathspec).unwrap();
pub fn pathspec<T: IntoCString>(&mut self, pathspec: T) -> Result<&mut Self, Error> {

@weihanglo weihanglo Jul 16, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I know we kinda work towards this though it is still a bit annoying to users. Most of the people won't hit this issue and they need to take care of Result. I am a bit unsure.

Should we just make it an expect and document it would panic?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We could, but I think it would be nicer to return a result - if the library is able to avoid a panic without being left in a problematic state, why not do so?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, and panic should be use mostly as a program invariant. This is kinda like a bad input. However, we can also see this as an invariant of these functions.

I am not sure though. I think technically it is more correct, though sacrifice ergonomic. And it may also be confusing as API caller now don't know how they should handle the error or not unless looking at the function body and realize it only checks bad cstring.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We could also switch it to requiring that the provided value be a CString rather than just something that can be converted to one?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That also loses ergonomic in another way 😞.

@DanielEScherzer DanielEScherzer Jul 17, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

i figured a result was more ergonomic than requiring a CString, because I think panicking is worse than either

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Waiting on review semver-breaking-change A SemVer breaking change that will require a new SemVer-incompatible version number bump.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants