use declarative macro for #[derive(TryFromU32)]

This commit is contained in:
Deadbeef
2025-08-16 22:08:41 +08:00
parent d2acb427e4
commit 696b6ac14d
10 changed files with 28 additions and 192 deletions

View File

@@ -6,7 +6,6 @@ use rustc_abi::Align;
use rustc_codegen_ssa::traits::{BaseTypeCodegenMethods, ConstCodegenMethods};
use rustc_data_structures::fx::FxIndexMap;
use rustc_index::IndexVec;
use rustc_macros::TryFromU32;
use rustc_middle::ty::TyCtxt;
use rustc_session::RemapFileNameExt;
use rustc_session::config::RemapPathScopeComponents;
@@ -16,7 +15,7 @@ use tracing::debug;
use crate::common::CodegenCx;
use crate::coverageinfo::llvm_cov;
use crate::coverageinfo::mapgen::covfun::prepare_covfun_record;
use crate::llvm;
use crate::{TryFromU32, llvm};
mod covfun;
mod spans;

View File

@@ -14,6 +14,7 @@
#![feature(if_let_guard)]
#![feature(impl_trait_in_assoc_type)]
#![feature(iter_intersperse)]
#![feature(macro_derive)]
#![feature(rustdoc_internals)]
#![feature(slice_as_array)]
#![feature(try_blocks)]
@@ -65,6 +66,7 @@ mod errors;
mod intrinsic;
mod llvm;
mod llvm_util;
mod macros;
mod mono_item;
mod type_;
mod type_of;
@@ -74,6 +76,8 @@ mod value;
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
pub(crate) use macros::TryFromU32;
#[derive(Clone)]
pub struct LlvmCodegenBackend(());

View File

@@ -19,7 +19,6 @@ use std::ptr;
use bitflags::bitflags;
use libc::{c_char, c_int, c_uchar, c_uint, c_ulonglong, c_void, size_t};
use rustc_macros::TryFromU32;
use super::RustString;
use super::debuginfo::{
@@ -27,8 +26,8 @@ use super::debuginfo::{
DIGlobalVariableExpression, DILocation, DISPFlags, DIScope, DISubprogram,
DITemplateTypeParameter, DIType, DebugEmissionKind, DebugNameTableKind,
};
use crate::llvm;
use crate::llvm::MetadataKindId;
use crate::{TryFromU32, llvm};
/// In the LLVM-C API, boolean values are passed as `typedef int LLVMBool`,
/// which has a different ABI from Rust or C++ `bool`.

View File

@@ -0,0 +1,22 @@
macro_rules! TryFromU32 {
derive() (
$(#[$meta:meta])*
$vis:vis enum $Type:ident {
$(
$(#[$varmeta:meta])*
$Variant:ident $(= $discr:expr)?
),* $(,)?
}
) => {
impl ::core::convert::TryFrom<u32> for $Type {
type Error = u32;
#[allow(deprecated)] // Don't warn about deprecated variants.
fn try_from(value: u32) -> ::core::result::Result<$Type, Self::Error> {
$( if value == const { $Type::$Variant as u32 } { return Ok($Type::$Variant) } )*
Err(value)
}
}
}
}
pub(crate) use TryFromU32;

View File

@@ -18,7 +18,6 @@ mod print_attribute;
mod query;
mod serialize;
mod symbols;
mod try_from;
mod type_foldable;
mod type_visitable;
mod visitable;
@@ -176,14 +175,6 @@ decl_derive!(
applicability)] => diagnostics::subdiagnostic_derive
);
decl_derive! {
[TryFromU32] =>
/// Derives `TryFrom<u32>` for the annotated `enum`, which must have no fields.
/// Each variant maps to the value it would produce under an `as u32` cast.
///
/// The error type is `u32`.
try_from::try_from_u32
}
decl_derive! {
[PrintAttribute] =>
/// Derives `PrintAttribute` for `AttributeKind`.

View File

@@ -1,55 +0,0 @@
use proc_macro2::TokenStream;
use quote::{quote, quote_spanned};
use syn::Data;
use syn::spanned::Spanned;
use synstructure::Structure;
pub(crate) fn try_from_u32(s: Structure<'_>) -> TokenStream {
let span_error = |span, message: &str| {
quote_spanned! { span => const _: () = ::core::compile_error!(#message); }
};
// Must be applied to an enum type.
if let Some(span) = match &s.ast().data {
Data::Enum(_) => None,
Data::Struct(s) => Some(s.struct_token.span()),
Data::Union(u) => Some(u.union_token.span()),
} {
return span_error(span, "type is not an enum (TryFromU32)");
}
// The enum's variants must not have fields.
let variant_field_errors = s
.variants()
.iter()
.filter_map(|v| v.ast().fields.iter().map(|f| f.span()).next())
.map(|span| span_error(span, "enum variant cannot have fields (TryFromU32)"))
.collect::<TokenStream>();
if !variant_field_errors.is_empty() {
return variant_field_errors;
}
let ctor = s
.variants()
.iter()
.map(|v| v.construct(|_, _| -> TokenStream { unreachable!() }))
.collect::<Vec<_>>();
// FIXME(edition_2024): Fix the `keyword_idents_2024` lint to not trigger here?
#[allow(keyword_idents_2024)]
s.gen_impl(quote! {
// The surrounding code might have shadowed these identifiers.
use ::core::convert::TryFrom;
use ::core::primitive::u32;
use ::core::result::Result::{self, Ok, Err};
gen impl TryFrom<u32> for @Self {
type Error = u32;
#[allow(deprecated)] // Don't warn about deprecated variants.
fn try_from(value: u32) -> Result<Self, Self::Error> {
#( if value == const { #ctor as u32 } { return Ok(#ctor) } )*
Err(value)
}
}
})
}

View File

@@ -1,24 +0,0 @@
#![feature(rustc_private)]
//@ edition: 2021
// Checks the error messages produced by `#[derive(TryFromU32)]`.
extern crate rustc_macros;
use rustc_macros::TryFromU32;
#[derive(TryFromU32)]
struct MyStruct {} //~ ERROR type is not an enum
#[derive(TryFromU32)]
enum NonTrivial {
A,
B(),
C {},
D(bool), //~ ERROR enum variant cannot have fields
E(bool, bool), //~ ERROR enum variant cannot have fields
F { x: bool }, //~ ERROR enum variant cannot have fields
G { x: bool, y: bool }, //~ ERROR enum variant cannot have fields
}
fn main() {}

View File

@@ -1,32 +0,0 @@
error: type is not an enum (TryFromU32)
--> $DIR/errors.rs:11:1
|
LL | struct MyStruct {}
| ^^^^^^
error: enum variant cannot have fields (TryFromU32)
--> $DIR/errors.rs:18:7
|
LL | D(bool),
| ^^^^
error: enum variant cannot have fields (TryFromU32)
--> $DIR/errors.rs:19:7
|
LL | E(bool, bool),
| ^^^^
error: enum variant cannot have fields (TryFromU32)
--> $DIR/errors.rs:20:9
|
LL | F { x: bool },
| ^
error: enum variant cannot have fields (TryFromU32)
--> $DIR/errors.rs:21:9
|
LL | G { x: bool, y: bool },
| ^
error: aborting due to 5 previous errors

View File

@@ -1,32 +0,0 @@
#![feature(rustc_private)]
//@ edition: 2021
//@ check-pass
// Checks that the derive macro still works even if the surrounding code has
// shadowed the relevant library types.
extern crate rustc_macros;
mod submod {
use rustc_macros::TryFromU32;
struct Result;
trait TryFrom {}
#[allow(non_camel_case_types)]
struct u32;
struct Ok;
struct Err;
mod core {}
mod std {}
#[derive(TryFromU32)]
pub(crate) enum MyEnum {
Zero,
One,
}
}
fn main() {
use submod::MyEnum;
let _: Result<MyEnum, u32> = MyEnum::try_from(1u32);
}

View File

@@ -1,36 +0,0 @@
#![feature(assert_matches)]
#![feature(rustc_private)]
//@ edition: 2021
//@ run-pass
// Checks the values accepted by the `TryFrom<u32>` impl produced by `#[derive(TryFromU32)]`.
extern crate rustc_macros;
use core::assert_matches::assert_matches;
use rustc_macros::TryFromU32;
#[derive(TryFromU32, Debug, PartialEq)]
#[repr(u32)]
enum Repr {
Zero,
One(),
Seven = 7,
}
#[derive(TryFromU32, Debug)]
enum NoRepr {
Zero,
One,
}
fn main() {
assert_eq!(Repr::try_from(0u32), Ok(Repr::Zero));
assert_eq!(Repr::try_from(1u32), Ok(Repr::One()));
assert_eq!(Repr::try_from(2u32), Err(2));
assert_eq!(Repr::try_from(7u32), Ok(Repr::Seven));
assert_matches!(NoRepr::try_from(0u32), Ok(NoRepr::Zero));
assert_matches!(NoRepr::try_from(1u32), Ok(NoRepr::One));
assert_matches!(NoRepr::try_from(2u32), Err(2));
}