Auto merge of #146053 - joboet:split-paths-regression, r=Mark-Simulacrum

std: fix `SplitPaths` regression

Fixes rust-lang/rust#146045 by defining the TAIT more precisely, ensuring that `'a` does not need to be live on drop.
This commit is contained in:
bors 2025-08-31 16:19:09 +00:00
commit f73bcd50a4
2 changed files with 26 additions and 5 deletions

View file

@ -186,14 +186,24 @@ pub fn chdir(p: &path::Path) -> io::Result<()> {
if result == 0 { Ok(()) } else { Err(io::Error::last_os_error()) }
}
pub type SplitPaths<'a> = impl Iterator<Item = PathBuf>;
// This can't just be `impl Iterator` because that requires `'a` to be live on
// drop (see #146045).
pub type SplitPaths<'a> = iter::Map<
slice::Split<'a, u8, impl FnMut(&u8) -> bool + 'static>,
impl FnMut(&[u8]) -> PathBuf + 'static,
>;
#[define_opaque(SplitPaths)]
pub fn split_paths(unparsed: &OsStr) -> SplitPaths<'_> {
unparsed
.as_bytes()
.split(|&b| b == PATH_SEPARATOR)
.map(|part| PathBuf::from(OsStr::from_bytes(part)))
fn is_separator(&b: &u8) -> bool {
b == PATH_SEPARATOR
}
fn into_pathbuf(part: &[u8]) -> PathBuf {
PathBuf::from(OsStr::from_bytes(part))
}
unparsed.as_bytes().split(is_separator).map(into_pathbuf)
}
#[derive(Debug)]

View file

@ -0,0 +1,11 @@
// Regression test for issue #146045 - ensure that the TAIT `SplitPaths` does not
// require the borrowed string to be live.
//@ check-pass
//@ edition:2015
pub fn repro() -> Option<std::path::PathBuf> {
let unparsed = std::ffi::OsString::new();
std::env::split_paths(&unparsed).next()
}
fn main() {}