From bee08f7284d5badb32ab8e5bc8554bc0aefc159a Mon Sep 17 00:00:00 2001 From: r0nsha Date: Mon, 25 Mar 2024 17:06:14 +0200 Subject: [PATCH] fix: polymorphic drop hook --- compiler_core/src/ty.rs | 2 +- compiler_mir/src/lib.rs | 10 ++++++---- compiler_mir/src/lower.rs | 10 ++-------- compiler_mir/src/monomorphize.rs | 2 +- compiler_mir/src/ownck.rs | 9 ++++++--- tests/drop-hook.jin | 14 ++++++++++++++ tests/hooks.jin | 9 --------- 7 files changed, 30 insertions(+), 26 deletions(-) create mode 100644 tests/drop-hook.jin delete mode 100644 tests/hooks.jin diff --git a/compiler_core/src/ty.rs b/compiler_core/src/ty.rs index 9e6db290..04da7611 100644 --- a/compiler_core/src/ty.rs +++ b/compiler_core/src/ty.rs @@ -915,7 +915,7 @@ impl TyFolder for ParamFolder<'_> { // NOTE: It currently makes sense to not instantiate params that are part of // the currently typechecked function. panic!( - "type param `{:?}` not part of instantation: {:?}", + "type param `{:?}` not part of instantiation: {:?}", p, self.instantiation ) } diff --git a/compiler_mir/src/lib.rs b/compiler_mir/src/lib.rs index 24370c96..dd899fbb 100644 --- a/compiler_mir/src/lib.rs +++ b/compiler_mir/src/lib.rs @@ -111,7 +111,7 @@ pub struct Body { blocks: Blocks, values: Values, - // A mapping for values to their instantation, used for specialization + // A mapping for values to their instantiation, used for specialization instantations: FxHashMap, // A mapping of values to their drop flags @@ -228,7 +228,7 @@ impl Body { self.create_value(ty, ValueKind::Register(None)) } - pub fn instantation(&self, value: ValueId) -> Option<&Instantiation> { + pub fn instantiation(&self, value: ValueId) -> Option<&Instantiation> { self.instantations.get(&value) } @@ -241,8 +241,10 @@ impl Body { &mut self.instantations } - pub fn create_instantation(&mut self, value: ValueId, instantation: Instantiation) { - self.instantations.insert(value, instantation); + pub fn create_instantiation(&mut self, value: ValueId, instantiation: Instantiation) { + if !instantiation.is_empty() { + self.instantations.insert(value, instantiation); + } } pub fn ins(&mut self, block: BlockId) -> InstBuilder { diff --git a/compiler_mir/src/lower.rs b/compiler_mir/src/lower.rs index be36a4d6..bb74681a 100644 --- a/compiler_mir/src/lower.rs +++ b/compiler_mir/src/lower.rs @@ -707,11 +707,7 @@ impl<'cx, 'db> LowerBody<'cx, 'db> { hir::ExprKind::Variant(variant) => { let id = self.cx.get_or_create_variant_ctor(variant.id); let value = self.create_value(self.cx.mir.fn_sigs[id].ty, ValueKind::Fn(id)); - - if !variant.instantiation.is_empty() { - self.body.create_instantation(value, variant.instantiation.clone()); - } - + self.body.create_instantiation(value, variant.instantiation.clone()); value } hir::ExprKind::SliceLit(lit) => { @@ -1327,9 +1323,7 @@ impl<'cx, 'db> LowerBody<'cx, 'db> { DefKind::TyAlias | DefKind::BuiltinTy(_) => unreachable!("{:?}", &self.cx.db[id]), }; - if !instantiation.is_empty() { - self.body.create_instantation(value, instantiation.clone()); - } + self.body.create_instantiation(value, instantiation.clone()); value } diff --git a/compiler_mir/src/monomorphize.rs b/compiler_mir/src/monomorphize.rs index c7a551fc..0bba9a75 100644 --- a/compiler_mir/src/monomorphize.rs +++ b/compiler_mir/src/monomorphize.rs @@ -173,7 +173,7 @@ impl<'db, 'cx> MonomorphizeBody<'db, 'cx> { let mut body_subst = BodySubst::new(); for value in body.values() { - if let Some(instantiation) = body.instantation(value.id) { + if let Some(instantiation) = body.instantiation(value.id) { // This is a polymorphic value which requires specialization if let Some(new_kind) = self.monomorphize_value(mir, &value.kind, instantiation) { body_subst.insert_value(value.id, new_kind); diff --git a/compiler_mir/src/ownck.rs b/compiler_mir/src/ownck.rs index 13cd9d97..8c891194 100644 --- a/compiler_mir/src/ownck.rs +++ b/compiler_mir/src/ownck.rs @@ -458,9 +458,14 @@ impl<'cx, 'db> LowerBody<'cx, 'db> { let sig_id = self.cx.def_to_fn_sig[&hook_id]; let sig_ty = self.cx.mir.fn_sigs[sig_id].ty; let tparams = sig_ty.collect_params(); - let instantiation = Instantiation::from((tparams.as_slice(), targs.as_slice())); + let instantiation = if tparams.is_empty() { + Instantiation::default() + } else { + Instantiation::from((tparams.as_slice(), targs.as_slice())) + }; let callee = self.create_untracked_value(instantiation.fold(sig_ty), ValueKind::Fn(sig_id)); + self.body.create_instantiation(callee, instantiation); let arg = { let arg = value; @@ -476,8 +481,6 @@ impl<'cx, 'db> LowerBody<'cx, 'db> { args: vec![arg], span, }); - - self.body.create_instantation(value, instantiation); } fn set_drop_flag(&mut self, value: ValueId, span: Span) { diff --git a/tests/drop-hook.jin b/tests/drop-hook.jin new file mode 100644 index 00000000..3f99996a --- /dev/null +++ b/tests/drop-hook.jin @@ -0,0 +1,14 @@ +fn main() unit = { + let wow = Wow("bye bye!") + println("hello") + let gen = Gen(1) +} + +type Wow(msg: &str) +type Gen[T](value: T) + +fn `=drop`(w: &mut Wow) unit = + println("dropping wow: {w.msg}") + +fn `=drop`[T](g: &mut Gen[T]) unit = + println("dropping gen") diff --git a/tests/hooks.jin b/tests/hooks.jin deleted file mode 100644 index 74d6bc7d..00000000 --- a/tests/hooks.jin +++ /dev/null @@ -1,9 +0,0 @@ -fn main() unit = { - let mut wow = Wow("bye bye!") - println("hello") -} - -type Wow*(msg: &str) - -fn `=drop`(w: &mut Wow) unit = - println(w.msg) -- 2.51.2