Skip to content

remove #[derive(TryFromU32)] #145495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,12 +26,14 @@ mod unused;
/// or at least the subset that we know and care about.
///
/// Note that version `n` is encoded as `(n-1)`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, TryFromU32)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
enum CovmapVersion {
/// Used by LLVM 18 onwards.
Version7 = 6,
}

crate::impl_try_from_u32!(CovmapVersion { Version7 });

impl CovmapVersion {
fn to_u32(self) -> u32 {
self as u32
Expand Down
21 changes: 21 additions & 0 deletions compiler/rustc_codegen_llvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,27 @@ mod value;

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

// FIXME(fee1-dead) use `macro_attr` feature once that is available in bootstrap beta
macro_rules! impl_try_from_u32 {
($Type:ident { $($Variant:ident),*$(,)? }) => {
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> {
fn _assert_all_variants_provided(x: $Type) {
match x {
$($Type::$Variant => (),)*
}
}
$( if value == const { $Type::$Variant as u32 } { return Ok($Type::$Variant) } )*
Err(value)
}
}
}
}

pub(crate) use impl_try_from_u32;

#[derive(Clone)]
pub struct LlvmCodegenBackend(());

Expand Down
57 changes: 53 additions & 4 deletions compiler/rustc_codegen_llvm/src/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,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 rustc_target::spec::SymbolVisibility;

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

crate::impl_try_from_u32! {
CallConv {
CCallConv,
FastCallConv,
ColdCallConv,
PreserveMost,
PreserveAll,
Tail,
X86StdcallCallConv,
X86FastcallCallConv,
ArmAapcsCallConv,
Msp430Intr,
X86_ThisCall,
PtxKernel,
X86_64_SysV,
X86_64_Win64,
X86_VectorCall,
X86_Intr,
AvrNonBlockingInterrupt,
AvrInterrupt,
AmdgpuKernel,
}
}

/// Must match the layout of `LLVMLinkage`.
#[derive(Copy, Clone, PartialEq, TryFromU32)]
#[derive(Copy, Clone, PartialEq)]
#[repr(C)]
pub(crate) enum Linkage {
ExternalLinkage = 0,
Expand All @@ -161,15 +184,41 @@ pub(crate) enum Linkage {
LinkerPrivateWeakLinkage = 16,
}

crate::impl_try_from_u32! {
Linkage {
ExternalLinkage,
AvailableExternallyLinkage,
LinkOnceAnyLinkage,
LinkOnceODRLinkage,
LinkOnceODRAutoHideLinkage,
WeakAnyLinkage,
WeakODRLinkage,
AppendingLinkage,
InternalLinkage,
PrivateLinkage,
DLLImportLinkage,
DLLExportLinkage,
ExternalWeakLinkage,
GhostLinkage,
CommonLinkage,
LinkerPrivateLinkage,
LinkerPrivateWeakLinkage,
}
}

/// Must match the layout of `LLVMVisibility`.
#[repr(C)]
#[derive(Copy, Clone, PartialEq, TryFromU32)]
#[derive(Copy, Clone, PartialEq)]
pub(crate) enum Visibility {
Default = 0,
Hidden = 1,
Protected = 2,
}

crate::impl_try_from_u32! {
Visibility { Default, Hidden, Protected }
}

impl Visibility {
pub(crate) fn from_generic(visibility: SymbolVisibility) -> Self {
match visibility {
Expand Down
9 changes: 0 additions & 9 deletions compiler/rustc_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ mod print_attribute;
mod query;
mod serialize;
mod symbols;
mod try_from;
mod type_foldable;
mod type_visitable;
mod visitable;
Expand Down Expand Up @@ -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`.
Expand Down
55 changes: 0 additions & 55 deletions compiler/rustc_macros/src/try_from.rs

This file was deleted.

24 changes: 0 additions & 24 deletions tests/ui-fulldeps/try-from-u32/errors.rs

This file was deleted.

32 changes: 0 additions & 32 deletions tests/ui-fulldeps/try-from-u32/errors.stderr

This file was deleted.

32 changes: 0 additions & 32 deletions tests/ui-fulldeps/try-from-u32/hygiene.rs

This file was deleted.

36 changes: 0 additions & 36 deletions tests/ui-fulldeps/try-from-u32/values.rs

This file was deleted.

Loading