Skip to content

Commit 0bb8a0e

Browse files
committed
remove #[derive(TryFromU32)]
seems like it could just be replaced with a declarative macro
1 parent 2e2642e commit 0bb8a0e

File tree

9 files changed

+77
-194
lines changed

9 files changed

+77
-194
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

Lines changed: 3 additions & 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;
@@ -27,12 +26,14 @@ mod unused;
2726
/// or at least the subset that we know and care about.
2827
///
2928
/// Note that version `n` is encoded as `(n-1)`.
30-
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, TryFromU32)]
29+
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
3130
enum CovmapVersion {
3231
/// Used by LLVM 18 onwards.
3332
Version7 = 6,
3433
}
3534

35+
crate::impl_try_from_u32!(CovmapVersion { Version7 });
36+
3637
impl CovmapVersion {
3738
fn to_u32(self) -> u32 {
3839
self as u32

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,27 @@ mod value;
7979

8080
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
8181

82+
// FIXME(fee1-dead) use `macro_attr` feature once that is available in bootstrap beta
83+
macro_rules! impl_try_from_u32 {
84+
($Type:ident { $($Variant:ident),*$(,)? }) => {
85+
impl ::core::convert::TryFrom<u32> for $Type {
86+
type Error = u32;
87+
#[allow(deprecated)] // Don't warn about deprecated variants.
88+
fn try_from(value: u32) -> ::core::result::Result<$Type, Self::Error> {
89+
fn _assert_all_variants_provided(x: $Type) {
90+
match x {
91+
$($Type::$Variant => (),)*
92+
}
93+
}
94+
$( if value == const { $Type::$Variant as u32 } { return Ok($Type::$Variant) } )*
95+
Err(value)
96+
}
97+
}
98+
}
99+
}
100+
101+
pub(crate) use impl_try_from_u32;
102+
82103
#[derive(Clone)]
83104
pub struct LlvmCodegenBackend(());
84105

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use std::ptr;
2020

2121
use bitflags::bitflags;
2222
use libc::{c_char, c_int, c_uchar, c_uint, c_ulonglong, c_void, size_t};
23-
use rustc_macros::TryFromU32;
2423
use rustc_target::spec::SymbolVisibility;
2524

2625
use super::RustString;
@@ -110,7 +109,7 @@ pub(crate) enum TailCallKind {
110109
/// LLVM CallingConv::ID. Should we wrap this?
111110
///
112111
/// See <https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/IR/CallingConv.h>
113-
#[derive(Copy, Clone, PartialEq, Debug, TryFromU32)]
112+
#[derive(Copy, Clone, PartialEq, Debug)]
114113
#[repr(C)]
115114
pub(crate) enum CallConv {
116115
CCallConv = 0,
@@ -134,8 +133,32 @@ pub(crate) enum CallConv {
134133
AmdgpuKernel = 91,
135134
}
136135

136+
crate::impl_try_from_u32! {
137+
CallConv {
138+
CCallConv,
139+
FastCallConv,
140+
ColdCallConv,
141+
PreserveMost,
142+
PreserveAll,
143+
Tail,
144+
X86StdcallCallConv,
145+
X86FastcallCallConv,
146+
ArmAapcsCallConv,
147+
Msp430Intr,
148+
X86_ThisCall,
149+
PtxKernel,
150+
X86_64_SysV,
151+
X86_64_Win64,
152+
X86_VectorCall,
153+
X86_Intr,
154+
AvrNonBlockingInterrupt,
155+
AvrInterrupt,
156+
AmdgpuKernel,
157+
}
158+
}
159+
137160
/// Must match the layout of `LLVMLinkage`.
138-
#[derive(Copy, Clone, PartialEq, TryFromU32)]
161+
#[derive(Copy, Clone, PartialEq)]
139162
#[repr(C)]
140163
pub(crate) enum Linkage {
141164
ExternalLinkage = 0,
@@ -161,15 +184,41 @@ pub(crate) enum Linkage {
161184
LinkerPrivateWeakLinkage = 16,
162185
}
163186

187+
crate::impl_try_from_u32! {
188+
Linkage {
189+
ExternalLinkage,
190+
AvailableExternallyLinkage,
191+
LinkOnceAnyLinkage,
192+
LinkOnceODRLinkage,
193+
LinkOnceODRAutoHideLinkage,
194+
WeakAnyLinkage,
195+
WeakODRLinkage,
196+
AppendingLinkage,
197+
InternalLinkage,
198+
PrivateLinkage,
199+
DLLImportLinkage,
200+
DLLExportLinkage,
201+
ExternalWeakLinkage,
202+
GhostLinkage,
203+
CommonLinkage,
204+
LinkerPrivateLinkage,
205+
LinkerPrivateWeakLinkage,
206+
}
207+
}
208+
164209
/// Must match the layout of `LLVMVisibility`.
165210
#[repr(C)]
166-
#[derive(Copy, Clone, PartialEq, TryFromU32)]
211+
#[derive(Copy, Clone, PartialEq)]
167212
pub(crate) enum Visibility {
168213
Default = 0,
169214
Hidden = 1,
170215
Protected = 2,
171216
}
172217

218+
crate::impl_try_from_u32! {
219+
Visibility { Default, Hidden, Protected }
220+
}
221+
173222
impl Visibility {
174223
pub(crate) fn from_generic(visibility: SymbolVisibility) -> Self {
175224
match visibility {

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)