Skip to content

Commit 692bc5b

Browse files
nnethercotecuviper
authored andcommitted
Fix TyKind::is_simple_path.
PR rust-lang#98758 introduced code to avoid redundant assertions in derived code like this: ``` let _: ::core::clone::AssertParamIsClone<u32>; let _: ::core::clone::AssertParamIsClone<u32>; ``` But the predicate `is_simple_path` introduced as part of this failed to account for generic arguments. Therefore the deriving code erroneously considers types like `Option<bool>` and `Option<f32>` to be the same. This commit fixes `is_simple_path`. Fixes rust-lang#103157. (cherry picked from commit 9a23f60)
1 parent 8f1050e commit 692bc5b

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

compiler/rustc_ast/src/ast.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -2060,8 +2060,11 @@ impl TyKind {
20602060
}
20612061

20622062
pub fn is_simple_path(&self) -> Option<Symbol> {
2063-
if let TyKind::Path(None, Path { segments, .. }) = &self && segments.len() == 1 {
2064-
Some(segments[0].ident.name)
2063+
if let TyKind::Path(None, Path { segments, .. }) = &self
2064+
&& let [segment] = &segments[..]
2065+
&& segment.args.is_none()
2066+
{
2067+
Some(segment.ident.name)
20652068
} else {
20662069
None
20672070
}

src/test/ui/deriving/deriving-all-codegen.stdout

+2
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,7 @@ impl ::core::clone::Clone for Mixed {
799799
fn clone(&self) -> Mixed {
800800
let _: ::core::clone::AssertParamIsClone<u32>;
801801
let _: ::core::clone::AssertParamIsClone<Option<u32>>;
802+
let _: ::core::clone::AssertParamIsClone<Option<i32>>;
802803
*self
803804
}
804805
}
@@ -866,6 +867,7 @@ impl ::core::cmp::Eq for Mixed {
866867
fn assert_receiver_is_total_eq(&self) -> () {
867868
let _: ::core::cmp::AssertParamIsEq<u32>;
868869
let _: ::core::cmp::AssertParamIsEq<Option<u32>>;
870+
let _: ::core::cmp::AssertParamIsEq<Option<i32>>;
869871
}
870872
}
871873
#[automatically_derived]

src/test/ui/deriving/issue-103157.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// check-fail
2+
3+
#[derive(PartialEq, Eq)]
4+
pub enum Value {
5+
Boolean(Option<bool>),
6+
Float(Option<f64>), //~ ERROR the trait bound `f64: Eq` is not satisfied
7+
}
8+
9+
fn main() {
10+
let a = Value::Float(Some(f64::NAN));
11+
assert!(a == a);
12+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
error[E0277]: the trait bound `f64: Eq` is not satisfied
2+
--> $DIR/issue-103157.rs:6:11
3+
|
4+
LL | #[derive(PartialEq, Eq)]
5+
| -- in this derive macro expansion
6+
...
7+
LL | Float(Option<f64>),
8+
| ^^^^^^^^^^^ the trait `Eq` is not implemented for `f64`
9+
|
10+
= help: the following other types implement trait `Eq`:
11+
i128
12+
i16
13+
i32
14+
i64
15+
i8
16+
isize
17+
u128
18+
u16
19+
and 4 others
20+
= note: required for `Option<f64>` to implement `Eq`
21+
note: required by a bound in `AssertParamIsEq`
22+
--> $SRC_DIR/core/src/cmp.rs:LL:COL
23+
|
24+
LL | pub struct AssertParamIsEq<T: Eq + ?Sized> {
25+
| ^^ required by this bound in `AssertParamIsEq`
26+
= note: this error originates in the derive macro `Eq` (in Nightly builds, run with -Z macro-backtrace for more info)
27+
28+
error: aborting due to previous error
29+
30+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)