Skip to content

Commit 144a12a

Browse files
committed
Add a macro that derives TryFrom<u32> for fieldless enums
1 parent 80d0d92 commit 144a12a

6 files changed

Lines changed: 187 additions & 0 deletions

File tree

‎compiler/rustc_macros/src/lib.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mod lift;
2020
mod query;
2121
mod serialize;
2222
mod symbols;
23+
mod try_from;
2324
mod type_foldable;
2425
mod type_visitable;
2526

@@ -165,3 +166,12 @@ decl_derive!(
165166
suggestion_part,
166167
applicability)] => diagnostics::subdiagnostic_derive
167168
);
169+
170+
decl_derive!{
171+
[TryFromU32] =>
172+
/// Derives `TryFrom<u32>` for the annotated `enum`, which must have no fields.
173+
/// Each variant maps to the value it would produce under an `as u32` cast.
174+
///
175+
/// The error type is `u32`.
176+
try_from::try_from_u32
177+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use proc_macro2::TokenStream;
2+
use quote::{quote, quote_spanned};
3+
use syn::Data;
4+
use syn::spanned::Spanned;
5+
use synstructure::Structure;
6+
7+
pub(crate)fntry_from_u32(s:Structure<'_>) -> TokenStream{
8+
let span_error = |span,message:&str| {
9+
quote_spanned!{ span => const _:() = ::core::compile_error!(#message);}
10+
};
11+
12+
// Must be applied to an enum type.
13+
ifletSome(span) = match&s.ast().data{
14+
Data::Enum(_) => None,
15+
Data::Struct(s) => Some(s.struct_token.span()),
16+
Data::Union(u) => Some(u.union_token.span()),
17+
}{
18+
returnspan_error(span,"type is not an enum (TryFromU32)");
19+
}
20+
21+
// The enum's variants must not have fields.
22+
let variant_field_errors = s
23+
.variants()
24+
.iter()
25+
.filter_map(|v| v.ast().fields.iter().map(|f| f.span()).next())
26+
.map(|span| span_error(span,"enum variant cannot have fields (TryFromU32)"))
27+
.collect::<TokenStream>();
28+
if !variant_field_errors.is_empty(){
29+
return variant_field_errors;
30+
}
31+
32+
let ctor = s
33+
.variants()
34+
.iter()
35+
.map(|v| v.construct(|_, _| -> TokenStream{unreachable!()}))
36+
.collect::<Vec<_>>();
37+
s.gen_impl(quote!{
38+
// The surrounding code might have shadowed these identifiers.
39+
use::core::convert::TryFrom;
40+
use::core::primitive::u32;
41+
use::core::result::Result::{self,Ok,Err};
42+
43+
genimplTryFrom<u32> for @Self{
44+
typeError = u32;
45+
46+
#[allow(deprecated)]// Don't warn about deprecated variants.
47+
fn try_from(value:u32) -> Result<Self,Self::Error> {
48+
#(if value == const{ #ctor asu32}{returnOk(#ctor)})*
49+
Err(value)
50+
}
51+
}
52+
})
53+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#![feature(rustc_private)]
2+
//@ edition: 2021
3+
4+
// Checks the error messages produced by `#[derive(TryFromU32)]`.
5+
6+
externcrate rustc_macros;
7+
8+
use rustc_macros::TryFromU32;
9+
10+
#[derive(TryFromU32)]
11+
structMyStruct{}//~ type is not an enum
12+
13+
#[derive(TryFromU32)]
14+
enumNonTrivial{
15+
A,
16+
B(),
17+
C{},
18+
D(bool),//~ enum variant cannot have fields
19+
E(bool,bool),//~ enum variant cannot have fields
20+
F{x:bool},//~ enum variant cannot have fields
21+
G{x:bool,y:bool},//~ enum variant cannot have fields
22+
}
23+
24+
fnmain(){}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error: type is not an enum (TryFromU32)
2+
--> $DIR/errors.rs:11:1
3+
|
4+
LL | struct MyStruct {}
5+
| ^^^^^^
6+
7+
error: enum variant cannot have fields (TryFromU32)
8+
--> $DIR/errors.rs:18:7
9+
|
10+
LL | D(bool),
11+
| ^^^^
12+
13+
error: enum variant cannot have fields (TryFromU32)
14+
--> $DIR/errors.rs:19:7
15+
|
16+
LL | E(bool, bool),
17+
| ^^^^
18+
19+
error: enum variant cannot have fields (TryFromU32)
20+
--> $DIR/errors.rs:20:9
21+
|
22+
LL | F { x: bool },
23+
| ^
24+
25+
error: enum variant cannot have fields (TryFromU32)
26+
--> $DIR/errors.rs:21:9
27+
|
28+
LL | G { x: bool, y: bool },
29+
| ^
30+
31+
error: aborting due to 5 previous errors
32+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#![feature(rustc_private)]
2+
//@ edition: 2021
3+
//@ check-pass
4+
5+
// Checks that the derive macro still works even if the surrounding code has
6+
// shadowed the relevant library types.
7+
8+
externcrate rustc_macros;
9+
10+
mod submod {
11+
use rustc_macros::TryFromU32;
12+
13+
structResult;
14+
traitTryFrom{}
15+
#[allow(non_camel_case_types)]
16+
structu32;
17+
structOk;
18+
structErr;
19+
mod core {}
20+
mod std {}
21+
22+
#[derive(TryFromU32)]
23+
pub(crate)enumMyEnum{
24+
Zero,
25+
One,
26+
}
27+
}
28+
29+
fnmain(){
30+
use submod::MyEnum;
31+
let _:Result<MyEnum,u32> = MyEnum::try_from(1u32);
32+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#![feature(assert_matches)]
2+
#![feature(rustc_private)]
3+
//@ edition: 2021
4+
//@ run-pass
5+
6+
// Checks the values accepted by the `TryFrom<u32>` impl produced by `#[derive(TryFromU32)]`.
7+
8+
externcrate rustc_macros;
9+
10+
use core::assert_matches::assert_matches;
11+
use rustc_macros::TryFromU32;
12+
13+
#[derive(TryFromU32,Debug,PartialEq)]
14+
#[repr(u32)]
15+
enumRepr{
16+
Zero,
17+
One(),
18+
Seven = 7,
19+
}
20+
21+
#[derive(TryFromU32,Debug)]
22+
enumNoRepr{
23+
Zero,
24+
One,
25+
}
26+
27+
fnmain(){
28+
assert_eq!(Repr::try_from(0u32),Ok(Repr::Zero));
29+
assert_eq!(Repr::try_from(1u32),Ok(Repr::One()));
30+
assert_eq!(Repr::try_from(2u32),Err(2));
31+
assert_eq!(Repr::try_from(7u32),Ok(Repr::Seven));
32+
33+
assert_matches!(NoRepr::try_from(0u32),Ok(NoRepr::Zero));
34+
assert_matches!(NoRepr::try_from(1u32),Ok(NoRepr::One));
35+
assert_matches!(NoRepr::try_from(2u32),Err(2));
36+
}

0 commit comments

Comments
 (0)