core: add sanity checks in `BuiltinBuilder::build_builtin_of_prim`

This commit is contained in:
lyken 2024-06-12 12:43:37 +08:00
parent 2e3b00214c
commit b08cd7f8e4
2 changed files with 31 additions and 4 deletions

View File

@ -1,6 +1,6 @@
use std::iter::once;
use helper::debug_assert_prim_is_allowed;
use helper::{debug_assert_prim_is_allowed, PrimDefDetails};
use indexmap::IndexMap;
use inkwell::{
attributes::{Attribute, AttributeLoc},
@ -441,7 +441,7 @@ impl<'a> BuiltinBuilder<'a> {
/// Build the [`TopLevelDef`] associated of a [`PrimDef`].
fn build_builtin_of_prim(&mut self, prim: PrimDef) -> TopLevelDef {
match prim {
let tld = match prim {
PrimDef::Int32
| PrimDef::Int64
| PrimDef::UInt32
@ -544,7 +544,34 @@ impl<'a> BuiltinBuilder<'a> {
| PrimDef::FunNpLdExp
| PrimDef::FunNpHypot
| PrimDef::FunNpNextAfter => self.build_np_2ary_function(prim),
};
if cfg!(debug_assertions) {
// Sanity checks on the constructed [`TopLevelDef`]
match (&tld, prim.details()) {
(
TopLevelDef::Class { name, object_id, .. },
PrimDefDetails::PrimClass { name: exp_name },
) => {
let exp_object_id = prim.id();
assert_eq!(name, &exp_name.into());
assert_eq!(object_id, &exp_object_id);
}
(
TopLevelDef::Function { name, simple_name, .. },
PrimDefDetails::PrimFunction { name: exp_name, simple_name: exp_simple_name },
) => {
assert_eq!(name, exp_name);
assert_eq!(simple_name, &exp_simple_name.into());
}
_ => {
panic!("Class/function variant of the constructed TopLevelDef of PrimDef {prim:?} is different than what is defined by {prim:?}")
}
}
}
tld
}
/// Build "simple" primitive classes.

View File

@ -103,7 +103,7 @@ pub enum PrimDef {
}
/// Associated details of a [`PrimDef`]
enum PrimDefDetails {
pub enum PrimDefDetails {
PrimFunction { name: &'static str, simple_name: &'static str },
PrimClass { name: &'static str },
}
@ -153,7 +153,7 @@ impl PrimDef {
/// Get the associated details of this [`PrimDef`]
#[must_use]
fn details(self) -> PrimDefDetails {
pub fn details(self) -> PrimDefDetails {
fn class(name: &'static str) -> PrimDefDetails {
PrimDefDetails::PrimClass { name }
}