Merge pull request #262 from HigherOrderCO/bug/sc-510/unused-definition-warning-triggered-on-matched

[sc-510] Unused definition warning triggered on matched constructors when scott encoding is used
This commit is contained in:
imaqtkatt 2024-04-10 13:25:09 +00:00 committed by GitHub
commit d1bbc40146
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 43 additions and 0 deletions

View File

@ -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 {

View File

@ -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))
})
}

View File

@ -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}

View File

@ -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))