Skip to content

Commit d429ed2

Browse files
committed
use declarative macro for #[derive(TryFromU32)]
1 parent 5c7ae0c commit d429ed2

File tree

9 files changed

+21
-192
lines changed

9 files changed

+21
-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: 19 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)]
@@ -74,6 +75,24 @@ mod value;
7475

7576
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
7677

78+
macro_rules! TryFromU32 {
79+
derive() ($(#[$meta:meta])* $vis:vis enum $Type:ident {$(
80+
$(#[$varmeta:meta])*
81+
$Variant:ident $(= $discr:expr)?
82+
),*$(,)?}) => {
83+
impl ::core::convert::TryFrom<u32> for $Type {
84+
type Error = u32;
85+
#[allow(deprecated)] // Don't warn about deprecated variants.
86+
fn try_from(value: u32) -> ::core::result::Result<$Type, Self::Error> {
87+
$( if value == const { $Type::$Variant as u32 } { return Ok($Type::$Variant) } )*
88+
Err(value)
89+
}
90+
}
91+
}
92+
}
93+
94+
pub(crate) use TryFromU32;
95+
7796
#[derive(Clone)]
7897
pub struct LlvmCodegenBackend(());
7998

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ 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
use rustc_target::spec::SymbolVisibility;
2423

2524
use super::RustString;
@@ -28,8 +27,8 @@ use super::debuginfo::{
2827
DIGlobalVariableExpression, DILocation, DISPFlags, DIScope, DISubprogram,
2928
DITemplateTypeParameter, DIType, DebugEmissionKind, DebugNameTableKind,
3029
};
31-
use crate::llvm;
3230
use crate::llvm::MetadataKindId;
31+
use crate::{TryFromU32, llvm};
3332

3433
/// In the LLVM-C API, boolean values are passed as `typedef int LLVMBool`,
3534
/// which has a different ABI from Rust or C++ `bool`.

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)