Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 20 additions & 24 deletions library/std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1344,33 +1344,36 @@ impl PathBuf {
let buf = self.inner.as_encoded_bytes();
let mut need_sep = buf.last().map(|c| !is_sep_byte(*c)).unwrap_or(false);

// in the special case of `C:` on Windows, do *not* add a separator
let comps = self.components();
let self_comps = self.components();
let self_prefix_len = self_comps.prefix_len();

if comps.prefix_len() > 0
&& comps.prefix_len() == comps.path.len()
&& comps.prefix.unwrap().is_drive()
// in the special case of `C:` on Windows, do *not* add a separator
if self_prefix_len > 0
&& self_prefix_len == self_comps.path.len()
&& self_comps.prefix.unwrap().is_drive()
{
need_sep = false
}

let need_clear = if cfg!(target_os = "cygwin") {
// If path is absolute and its prefix is none, it is like `/foo`,
// and will be handled below.
path.prefix().is_some()
let path_comps = path.components();

let need_clear = if cfg!(any(target_os = "windows", target_os = "cygwin")) {
// Windows/Cygwin: If path is absolute and its prefix is none, it is like `\foo`
// (Windows) or `/foo` (Cygwin), and will be handled below.
path_comps.prefix.is_some()
Comment on lines +1360 to +1363

@Mark-Simulacrum Mark-Simulacrum Nov 14, 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.

Why is this change correct?

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.

(rewritten the commit message to refer to the original naming)

path.is_absolute() || path.prefix().is_some()

expands to

(path.has_root() && path.prefix().is_some()) || path.prefix().is_some(),

which simplifies to just the prefix check.

Pushing C:foo should clear, pushing \foo truncates to the existing prefix, just like on cygwin.

} else {
// On Unix: prefix is always None.
path.is_absolute() || path.prefix().is_some()
path.is_absolute() || path_comps.prefix.is_some()
};

// absolute `path` replaces `self`
if need_clear {
self.inner.clear();

// verbatim paths need . and .. removed
} else if comps.prefix_verbatim() && !path.inner.is_empty() {
let mut buf: Vec<_> = comps.collect();
for c in path.components() {
} else if self_comps.prefix_verbatim() && !path.inner.is_empty() {
let mut buf: Vec<_> = self_comps.collect();
for c in path_comps {
match c {
Component::RootDir => {
buf.truncate(1);
Expand All @@ -1394,23 +1397,15 @@ impl PathBuf {
res.push(MAIN_SEPARATOR_STR);
}
res.push(c.as_os_str());

need_sep = match c {
Component::RootDir => false,
Component::Prefix(prefix) => {
!prefix.parsed.is_drive() && prefix.parsed.len() > 0
}
_ => true,
}
need_sep = c != Component::RootDir;

@Mark-Simulacrum Mark-Simulacrum Nov 14, 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.

Why is this a correct change?

View changes since the review

@seritools seritools Nov 14, 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.

oh, for all three of these, i did put the full explanation into the commit message of the commit that introduced each, should I reproduce that somewhere else as well?

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'll just move them here as well i guess ^^

Both prefix checks are always true:

  • !prefix.parsed.is_drive()
    • We are in the branch that already checked comps.prefix_verbatim(). Verbatim prefixes are never Prefix::Disk.
    • The pushed path can't have a prefix, since that would mean need_clear was true, at which point we wouldn't have to deal with merging verbatim paths in the first place :)
  • prefix.parsed.len() > 0: prefixes are nonempty, and we're already in the comps.prefix_verbatim() branch.

}

self.inner = res;
return;

// `path` has a root but no prefix, e.g., `\windows` (Windows only)
} else if path.has_root() {
let prefix_len = self.components().prefix_remaining();
self.inner.truncate(prefix_len);
} else if path_comps.has_root() {
self.inner.truncate(self_prefix_len);

@Mark-Simulacrum Mark-Simulacrum Nov 14, 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.

Similarly, why is this an identical change? Is prefix_remaining() here the same as prefix_len() above?

View changes since the review

@seritools seritools Nov 14, 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.

self.components().prefix_remaining() ran on a fresh/uniterated Components. self_comps also hasn't iterated , so prefix_remaining is always the same as prefix_len.

The entire point is to truncate to just the prefix, which can also only be the first component, so these are all equivalent, and I'd argue this is now ~more correct.
It's doing this transformation: C:\foo\bar + "\baz" -> C: + "\baz" -> C:\baz


// `path` is a pure relative path
} else if need_sep {
Expand Down Expand Up @@ -2575,6 +2570,7 @@ impl Path {
!self.is_absolute()
}

#[cfg(not(target_os = "uefi"))]
pub(crate) fn prefix(&self) -> Option<Prefix<'_>> {
self.components().prefix
}
Expand Down
Loading