Fix assignment of generated field on flattened rule definitions

This commit is contained in:
LunaAmora 2024-01-17 15:22:28 -03:00
parent a3252fba15
commit 293eaf4a28
3 changed files with 10 additions and 4 deletions

View File

@ -38,6 +38,7 @@ fn make_non_pattern_matching_def(book: &mut Book, def_id: DefId) {
fn make_pattern_matching_def(book: &mut Book, def_id: DefId, def_type: &[Type]) {
let def_name = book.def_names.name(&def_id).unwrap().clone();
let def = book.defs.get_mut(&def_id).unwrap();
let is_generated = def.generated;
let crnt_rules = (0 .. def.rules.len()).collect();
// First create a definition for each rule body
@ -49,7 +50,6 @@ fn make_pattern_matching_def(book: &mut Book, def_id: DefId, def_type: &[Type])
}
for (rule_idx, body) in rule_bodies.into_iter().enumerate() {
let rule_name = make_rule_name(&def_name, rule_idx);
let is_generated = false;
book.insert_def(rule_name, is_generated, vec![Rule { pats: vec![], body }]);
}

View File

@ -20,11 +20,17 @@ fn flatten_def(def: &Definition, def_names: &mut DefNames) -> Vec<Definition> {
// For each group, split its internal rules
let rules = def.rules.iter().map(|r| (def_name.clone(), r.clone())).collect_vec();
let new_rules = split_group(&rules, def_names);
let len = new_rules.len();
new_rules
.into_iter()
.map(|(name, rules)| {
let def_id = def_names.def_id(&name).unwrap();
Definition { def_id, generated: def.generated, rules }
// If the rule was splitted into multiple, the rule taking place of the original is a generated one
let generated = if name == def_name && len > 1 { true } else { def.generated };
Definition { def_id, generated, rules }
})
.collect()
}

View File

@ -11,7 +11,7 @@ input_file: tests/golden_tests/compile_file/list_merge_sort.hvm
@Pure = (a {4 {8 a {6 @Nil b}} {4 * b}})
@Q = {8 a {6 {4 @i {4 @j (b (a c))}} (b c)}}
@R = (* @Nil)
@Unpack = (a ({4 @Q {4 @R (a b)}} b))
@S = (a ({4 @Q {4 @R (a b)}} b))
@V = {8 a {6 {4 @o {4 @p (b (a c))}} (b c)}}
@W = (* @Nil)
@b = {8 {15 a {17 b c}} {6 {19 {4 @b {4 @c (d (e (f g)))}} h} ({5 d {7 i (j (c {2 @F {2 @G ({4 {8 k {6 l m}} {4 * m}} ({4 {8 a {6 g n}} {4 * n}} o))}}))}} ({9 e {11 k j}} ({13 f {4 @d {4 @e (i ({4 {8 b {6 h p}} {4 * p}} l))}}} o)))}}
@ -21,7 +21,7 @@ input_file: tests/golden_tests/compile_file/list_merge_sort.hvm
@i = {8 a {6 {4 @V {4 @W (b {4 @i {4 @j (c (d e))}})}} ({21 {23 b f} c} ({4 @d {4 @e (f (a d))}} e))}}
@j = (* (a a))
@main = (a (b c))
& @Unpack ~ (a (d c))
& @S ~ (a (d c))
& @Map ~ (b (@Pure d))
@o = {8 a {6 {4 @V {4 @W (b c)}} ({23 b d} ({4 @d {4 @e (d (a e))}} {4 {8 e {6 c f}} {4 * f}}))}}
@p = (* @u)