Skip to content

Commit 98e2fd3

Browse files
el-evcuviper
authored andcommitted
Fix infinite recursion in Path::eq with String
(cherry picked from commit 5a4e536)
1 parent 913b7e5 commit 98e2fd3

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

library/std/src/path.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,31 +2107,31 @@ impl PartialEq for PathBuf {
21072107
impl cmp::PartialEq<str> for PathBuf {
21082108
#[inline]
21092109
fn eq(&self, other: &str) -> bool {
2110-
Path::eq(self, other)
2110+
self.as_path() == other
21112111
}
21122112
}
21132113

21142114
#[stable(feature = "eq_str_for_path", since = "1.91.0")]
21152115
impl cmp::PartialEq<PathBuf> for str {
21162116
#[inline]
21172117
fn eq(&self, other: &PathBuf) -> bool {
2118-
other == self
2118+
self == other.as_path()
21192119
}
21202120
}
21212121

21222122
#[stable(feature = "eq_str_for_path", since = "1.91.0")]
21232123
impl cmp::PartialEq<String> for PathBuf {
21242124
#[inline]
21252125
fn eq(&self, other: &String) -> bool {
2126-
**self == **other
2126+
self.as_path() == other.as_str()
21272127
}
21282128
}
21292129

21302130
#[stable(feature = "eq_str_for_path", since = "1.91.0")]
21312131
impl cmp::PartialEq<PathBuf> for String {
21322132
#[inline]
21332133
fn eq(&self, other: &PathBuf) -> bool {
2134-
other == self
2134+
self.as_str() == other.as_path()
21352135
}
21362136
}
21372137

@@ -3426,15 +3426,15 @@ impl cmp::PartialEq<Path> for str {
34263426
impl cmp::PartialEq<String> for Path {
34273427
#[inline]
34283428
fn eq(&self, other: &String) -> bool {
3429-
self == &*other
3429+
self == other.as_str()
34303430
}
34313431
}
34323432

34333433
#[stable(feature = "eq_str_for_path", since = "1.91.0")]
34343434
impl cmp::PartialEq<Path> for String {
34353435
#[inline]
34363436
fn eq(&self, other: &Path) -> bool {
3437-
other == self
3437+
self.as_str() == other
34383438
}
34393439
}
34403440

library/std/tests/path.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2528,7 +2528,17 @@ fn normalize_lexically() {
25282528
}
25292529

25302530
#[test]
2531-
/// See issue#146183
2532-
fn compare_path_to_str() {
2533-
assert!(&PathBuf::from("x") == "x");
2531+
/// See issue#146183 and issue#146940
2532+
fn compare_path_like_to_str_like() {
2533+
let path_buf = PathBuf::from("x");
2534+
let path = Path::new("x");
2535+
let s = String::from("x");
2536+
assert!(path == "x");
2537+
assert!("x" == path);
2538+
assert!(path == &s);
2539+
assert!(&s == path);
2540+
assert!(&path_buf == "x");
2541+
assert!("x" == &path_buf);
2542+
assert!(path_buf == s);
2543+
assert!(s == path_buf);
25342544
}

0 commit comments

Comments
 (0)