@@ -1231,20 +1231,59 @@ impl PathBuf {
12311231 let mut need_sep = self . as_mut_vec ( ) . last ( ) . map ( |c| !is_sep_byte ( * c) ) . unwrap_or ( false ) ;
12321232
12331233 // in the special case of `C:` on Windows, do *not* add a separator
1234+ let comps = self . components ( ) ;
1235+
1236+ if comps. prefix_len ( ) > 0
1237+ && comps. prefix_len ( ) == comps. path . len ( )
1238+ && comps. prefix . unwrap ( ) . is_drive ( )
12341239 {
1235- let comps = self . components ( ) ;
1236- if comps. prefix_len ( ) > 0
1237- && comps. prefix_len ( ) == comps. path . len ( )
1238- && comps. prefix . unwrap ( ) . is_drive ( )
1239- {
1240- need_sep = false
1241- }
1240+ need_sep = false
12421241 }
12431242
12441243 // absolute `path` replaces `self`
12451244 if path. is_absolute ( ) || path. prefix ( ) . is_some ( ) {
12461245 self . as_mut_vec ( ) . truncate ( 0 ) ;
12471246
1247+ // verbatim paths need . and .. removed
1248+ } else if comps. prefix_verbatim ( ) {
1249+ let mut buf: Vec < _ > = comps. collect ( ) ;
1250+ for c in path. components ( ) {
1251+ match c {
1252+ Component :: RootDir => {
1253+ buf. truncate ( 1 ) ;
1254+ buf. push ( c) ;
1255+ }
1256+ Component :: CurDir => ( ) ,
1257+ Component :: ParentDir => {
1258+ if let Some ( Component :: Normal ( _) ) = buf. last ( ) {
1259+ buf. pop ( ) ;
1260+ }
1261+ }
1262+ _ => buf. push ( c) ,
1263+ }
1264+ }
1265+
1266+ let mut res = OsString :: new ( ) ;
1267+ let mut need_sep = false ;
1268+
1269+ for c in buf {
1270+ if need_sep && c != Component :: RootDir {
1271+ res. push ( MAIN_SEP_STR ) ;
1272+ }
1273+ res. push ( c. as_os_str ( ) ) ;
1274+
1275+ need_sep = match c {
1276+ Component :: RootDir => false ,
1277+ Component :: Prefix ( prefix) => {
1278+ !prefix. parsed . is_drive ( ) && prefix. parsed . len ( ) > 0
1279+ }
1280+ _ => true ,
1281+ }
1282+ }
1283+
1284+ self . inner = res;
1285+ return ;
1286+
12481287 // `path` has a root but no prefix, e.g., `\windows` (Windows only)
12491288 } else if path. has_root ( ) {
12501289 let prefix_len = self . components ( ) . prefix_remaining ( ) ;
0 commit comments