Skip to content

Commit 68afefc

Browse files
authored
Rollup merge of rust-lang#55837 - Centril:spökdata-skall-vara-strukturellt-matchbar, r=eddyb
Make PhantomData #[structural_match] fixes rust-lang#55028 This makes `PhantomData<T>` structurally matchable, irrespective of whether `T` is, per the discussion on this week's language team meeting (the general consensus was that this was a bug-fix). All types containing `PhantomData<T>` and which used `#[derive(PartialEq, Eq)]` and were previously not `#[structural_match]` only because of `PhantomData<T>` will now be `#[structural_match]`. r? @nikomatsakis
2 parents 985a785 + 5e7b7f2 commit 68afefc

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

src/libcore/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
#![feature(const_transmute)]
131131
#![feature(reverse_bits)]
132132
#![feature(non_exhaustive)]
133+
#![feature(structural_match)]
133134

134135
#[prelude_import]
135136
#[allow(unused)]

src/libcore/marker.rs

+1
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ macro_rules! impls{
578578
///
579579
/// [drop check]: ../../nomicon/dropck.html
580580
#[lang = "phantom_data"]
581+
#[structural_match]
581582
#[stable(feature = "rust1", since = "1.0.0")]
582583
pub struct PhantomData<T:?Sized>;
583584

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// run-pass
2+
3+
// This file checks that `PhantomData` is considered structurally matchable.
4+
5+
use std::marker::PhantomData;
6+
7+
fn main() {
8+
let mut count = 0;
9+
10+
// A type which is not structurally matchable:
11+
struct NotSM;
12+
13+
// And one that is:
14+
#[derive(PartialEq, Eq)]
15+
struct SM;
16+
17+
// Check that SM is #[structural_match]:
18+
const CSM: SM = SM;
19+
match SM {
20+
CSM => count += 1,
21+
};
22+
23+
// Check that PhantomData<T> is #[structural_match] even if T is not.
24+
const CPD1: PhantomData<NotSM> = PhantomData;
25+
match PhantomData {
26+
CPD1 => count += 1,
27+
};
28+
29+
// Check that PhantomData<T> is #[structural_match] when T is.
30+
const CPD2: PhantomData<SM> = PhantomData;
31+
match PhantomData {
32+
CPD2 => count += 1,
33+
};
34+
35+
// Check that a type which has a PhantomData is `#[structural_match]`.
36+
#[derive(PartialEq, Eq, Default)]
37+
struct Foo {
38+
alpha: PhantomData<NotSM>,
39+
beta: PhantomData<SM>,
40+
}
41+
42+
const CFOO: Foo = Foo {
43+
alpha: PhantomData,
44+
beta: PhantomData,
45+
};
46+
47+
match Foo::default() {
48+
CFOO => count += 1,
49+
};
50+
51+
// Final count must be 4 now if all
52+
assert_eq!(count, 4);
53+
}

0 commit comments

Comments
 (0)