Skip to content

Commit e193b53

Browse files
committed
Use LLVMGetTypeKind
1 parent c64c6d8 commit e193b53

File tree

3 files changed

+11
-61
lines changed

3 files changed

+11
-61
lines changed

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,15 @@ impl RealPredicate {
333333
}
334334
}
335335

336-
/// LLVMTypeKind
337-
#[derive(Copy, Clone, PartialEq, Debug)]
336+
/// Must match the layout of `LLVMTypeKind`.
337+
///
338+
/// Use [`RawEnum<TypeKind>`] for values of `LLVMTypeKind` returned from LLVM,
339+
/// to avoid risk of UB if LLVM adds new enum values.
340+
///
341+
/// All of LLVM's variants should be declared here, even if no Rust-side code refers
342+
/// to them, because unknown variants will cause [`RawEnum::to_rust`] to panic.
343+
#[derive(Copy, Clone, PartialEq, Debug, TryFromU32)]
338344
#[repr(C)]
339-
#[expect(dead_code, reason = "Some variants are unused, but are kept to match LLVM-C")]
340345
pub(crate) enum TypeKind {
341346
Void = 0,
342347
Half = 1,
@@ -1047,6 +1052,8 @@ unsafe extern "C" {
10471052
CanThrow: llvm::Bool,
10481053
) -> &'ll Value;
10491054

1055+
pub(crate) safe fn LLVMGetTypeKind(Ty: &Type) -> RawEnum<TypeKind>;
1056+
10501057
// Operations on integer types
10511058
pub(crate) fn LLVMInt1TypeInContext(C: &Context) -> &Type;
10521059
pub(crate) fn LLVMInt8TypeInContext(C: &Context) -> &Type;
@@ -1842,9 +1849,6 @@ unsafe extern "C" {
18421849
// Create and destroy contexts.
18431850
pub(crate) fn LLVMRustContextCreate(shouldDiscardNames: bool) -> &'static mut Context;
18441851

1845-
/// See llvm::LLVMTypeKind::getTypeID.
1846-
pub(crate) fn LLVMRustGetTypeKind(Ty: &Type) -> TypeKind;
1847-
18481852
// Operations on all values
18491853
pub(crate) fn LLVMRustGlobalAddMetadata<'a>(
18501854
Val: &'a Value,

compiler/rustc_codegen_llvm/src/type_.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl<'ll, CX: Borrow<SCx<'ll>>> BaseTypeCodegenMethods for GenericCx<'ll, CX> {
204204
}
205205

206206
fn type_kind(&self, ty: &'ll Type) -> TypeKind {
207-
unsafe { llvm::LLVMRustGetTypeKind(ty).to_generic() }
207+
llvm::LLVMGetTypeKind(ty).to_rust().to_generic()
208208
}
209209

210210
fn type_ptr(&self) -> &'ll Type {

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,60 +1460,6 @@ LLVMRustGetDiagInfoKind(LLVMDiagnosticInfoRef DI) {
14601460
return toRust((DiagnosticKind)unwrap(DI)->getKind());
14611461
}
14621462

1463-
// This is kept distinct from LLVMGetTypeKind, because when
1464-
// a new type kind is added, the Rust-side enum must be
1465-
// updated or UB will result.
1466-
extern "C" LLVMTypeKind LLVMRustGetTypeKind(LLVMTypeRef Ty) {
1467-
switch (unwrap(Ty)->getTypeID()) {
1468-
case Type::VoidTyID:
1469-
return LLVMVoidTypeKind;
1470-
case Type::HalfTyID:
1471-
return LLVMHalfTypeKind;
1472-
case Type::FloatTyID:
1473-
return LLVMFloatTypeKind;
1474-
case Type::DoubleTyID:
1475-
return LLVMDoubleTypeKind;
1476-
case Type::X86_FP80TyID:
1477-
return LLVMX86_FP80TypeKind;
1478-
case Type::FP128TyID:
1479-
return LLVMFP128TypeKind;
1480-
case Type::PPC_FP128TyID:
1481-
return LLVMPPC_FP128TypeKind;
1482-
case Type::LabelTyID:
1483-
return LLVMLabelTypeKind;
1484-
case Type::MetadataTyID:
1485-
return LLVMMetadataTypeKind;
1486-
case Type::IntegerTyID:
1487-
return LLVMIntegerTypeKind;
1488-
case Type::FunctionTyID:
1489-
return LLVMFunctionTypeKind;
1490-
case Type::StructTyID:
1491-
return LLVMStructTypeKind;
1492-
case Type::ArrayTyID:
1493-
return LLVMArrayTypeKind;
1494-
case Type::PointerTyID:
1495-
return LLVMPointerTypeKind;
1496-
case Type::FixedVectorTyID:
1497-
return LLVMVectorTypeKind;
1498-
case Type::TokenTyID:
1499-
return LLVMTokenTypeKind;
1500-
case Type::ScalableVectorTyID:
1501-
return LLVMScalableVectorTypeKind;
1502-
case Type::BFloatTyID:
1503-
return LLVMBFloatTypeKind;
1504-
case Type::X86_AMXTyID:
1505-
return LLVMX86_AMXTypeKind;
1506-
default: {
1507-
std::string error;
1508-
auto stream = llvm::raw_string_ostream(error);
1509-
stream << "Rust does not support the TypeID: " << unwrap(Ty)->getTypeID()
1510-
<< " for the type: " << *unwrap(Ty);
1511-
stream.flush();
1512-
report_fatal_error(error.c_str());
1513-
}
1514-
}
1515-
}
1516-
15171463
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(SMDiagnostic, LLVMSMDiagnosticRef)
15181464

15191465
extern "C" LLVMSMDiagnosticRef LLVMRustGetSMDiagnostic(LLVMDiagnosticInfoRef DI,

0 commit comments

Comments
 (0)