Skip to content

Commit 696b6ac

Browse files
committed
use declarative macro for #[derive(TryFromU32)]
1 parent d2acb42 commit 696b6ac

File tree

10 files changed

+28
-192
lines changed

10 files changed

+28
-192
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use rustc_abi::Align;
66
use rustc_codegen_ssa::traits::{BaseTypeCodegenMethods, ConstCodegenMethods};
77
use rustc_data_structures::fx::FxIndexMap;
88
use rustc_index::IndexVec;
9-
use rustc_macros::TryFromU32;
109
use rustc_middle::ty::TyCtxt;
1110
use rustc_session::RemapFileNameExt;
1211
use rustc_session::config::RemapPathScopeComponents;
@@ -16,7 +15,7 @@ use tracing::debug;
1615
use crate::common::CodegenCx;
1716
use crate::coverageinfo::llvm_cov;
1817
use crate::coverageinfo::mapgen::covfun::prepare_covfun_record;
19-
use crate::llvm;
18+
use crate::{TryFromU32, llvm};
2019

2120
mod covfun;
2221
mod spans;

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#![feature(if_let_guard)]
1515
#![feature(impl_trait_in_assoc_type)]
1616
#![feature(iter_intersperse)]
17+
#![feature(macro_derive)]
1718
#![feature(rustdoc_internals)]
1819
#![feature(slice_as_array)]
1920
#![feature(try_blocks)]
@@ -65,6 +66,7 @@ mod errors;
6566
mod intrinsic;
6667
mod llvm;
6768
mod llvm_util;
69+
mod macros;
6870
mod mono_item;
6971
mod type_;
7072
mod type_of;
@@ -74,6 +76,8 @@ mod value;
7476

7577
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
7678

79+
pub(crate) use macros::TryFromU32;
80+
7781
#[derive(Clone)]
7882
pub struct LlvmCodegenBackend(());
7983

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@ use std::ptr;
1919

2020
use bitflags::bitflags;
2121
use libc::{c_char, c_int, c_uchar, c_uint, c_ulonglong, c_void, size_t};
22-
use rustc_macros::TryFromU32;
2322

2423
use super::RustString;
2524
use super::debuginfo::{
2625
DIArray, DIBuilder, DIDerivedType, DIDescriptor, DIEnumerator, DIFile, DIFlags,
2726
DIGlobalVariableExpression, DILocation, DISPFlags, DIScope, DISubprogram,
2827
DITemplateTypeParameter, DIType, DebugEmissionKind, DebugNameTableKind,
2928
};
30-
use crate::llvm;
3129
use crate::llvm::MetadataKindId;
30+
use crate::{TryFromU32, llvm};
3231

3332
/// In the LLVM-C API, boolean values are passed as `typedef int LLVMBool`,
3433
/// which has a different ABI from Rust or C++ `bool`.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
macro_rules! TryFromU32 {
2+
derive() (
3+
$(#[$meta:meta])*
4+
$vis:vis enum $Type:ident {
5+
$(
6+
$(#[$varmeta:meta])*
7+
$Variant:ident $(= $discr:expr)?
8+
),* $(,)?
9+
}
10+
) => {
11+
impl ::core::convert::TryFrom<u32> for $Type {
12+
type Error = u32;
13+
#[allow(deprecated)] // Don't warn about deprecated variants.
14+
fn try_from(value: u32) -> ::core::result::Result<$Type, Self::Error> {
15+
$( if value == const { $Type::$Variant as u32 } { return Ok($Type::$Variant) } )*
16+
Err(value)
17+
}
18+
}
19+
}
20+
}
21+
22+
pub(crate) use TryFromU32;

compiler/rustc_macros/src/lib.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ mod print_attribute;
1818
mod query;
1919
mod serialize;
2020
mod symbols;
21-
mod try_from;
2221
mod type_foldable;
2322
mod type_visitable;
2423
mod visitable;
@@ -176,14 +175,6 @@ decl_derive!(
176175
applicability)] => diagnostics::subdiagnostic_derive
177176
);
178177

179-
decl_derive! {
180-
[TryFromU32] =>
181-
/// Derives `TryFrom<u32>` for the annotated `enum`, which must have no fields.
182-
/// Each variant maps to the value it would produce under an `as u32` cast.
183-
///
184-
/// The error type is `u32`.
185-
try_from::try_from_u32
186-
}
187178
decl_derive! {
188179
[PrintAttribute] =>
189180
/// Derives `PrintAttribute` for `AttributeKind`.

compiler/rustc_macros/src/try_from.rs

Lines changed: 0 additions & 55 deletions
This file was deleted.

tests/ui-fulldeps/try-from-u32/errors.rs

Lines changed: 0 additions & 24 deletions
This file was deleted.

tests/ui-fulldeps/try-from-u32/errors.stderr

Lines changed: 0 additions & 32 deletions
This file was deleted.

tests/ui-fulldeps/try-from-u32/hygiene.rs

Lines changed: 0 additions & 32 deletions
This file was deleted.

tests/ui-fulldeps/try-from-u32/values.rs

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)