Skip to content

Commit

Permalink
Merge pull request #262 from HigherOrderCO/bug/sc-510/unused-definiti…
Browse files Browse the repository at this point in the history
…on-warning-triggered-on-matched

[sc-510] Unused definition warning triggered on matched constructors when scott encoding is used
  • Loading branch information
imaqtkatt authored Apr 10, 2024
2 parents 56b3a0b + 570ad97 commit d1bbc40
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/term/transform/definition_pruning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ impl Ctx<'_> {
self.book.find_used_definitions(&def.rule().body, Used::Main, &mut used, adt_encoding);
}

if let AdtEncoding::Scott = adt_encoding {
for (def_name, def) in &self.book.defs {
if !def.builtin && self.book.ctrs.get(def_name).is_some() {
used.insert(def_name.clone(), Used::Adt);
}
}
}

// Even if we don't prune all the defs, we need check what built-ins are accessible through user code
if !prune_all {
for (def_name, def) in &self.book.defs {
Expand Down
13 changes: 13 additions & 0 deletions tests/golden_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,3 +451,16 @@ fn examples() -> Result<(), Diagnostics> {

Ok(())
}

#[test]
fn scott_triggers_unused() {
run_golden_test_dir(function_name!(), &|code, path| {
let mut book = do_parse_book(code, path)?;
let mut opts = CompileOpts::default_strict();
opts.adt_encoding = AdtEncoding::Scott;
let diagnostics_cfg =
DiagnosticsConfig { unused_definition: Severity::Error, ..DiagnosticsConfig::default_strict() };
let res = compile_book(&mut book, opts, diagnostics_cfg, None)?;
Ok(format!("{}{}", res.diagnostics, res.core_book))
})
}
13 changes: 13 additions & 0 deletions tests/golden_tests/scott_triggers_unused/test.hvm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// The test below should not trigger this warning:
//
// In definition 'f':
// Definition is unused.
// In definition 't':
// Definition is unused.
//
// This was happening because the prune algorithm was just collecting constructors
// by searching for tags.

data bool = t | f

main = @b match b { t: 0; f: 1}
9 changes: 9 additions & 0 deletions tests/snapshots/scott_triggers_unused__test.hvm.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/scott_triggers_unused/test.hvm
---
@f = (* (a a))

@main = ((#0 (#1 a)) a)

@t = (a (* a))

0 comments on commit d1bbc40

Please sign in to comment.