From 75c059bf655b333e95b555e371fafbbacef0bc7e Mon Sep 17 00:00:00 2001 From: KtorZ Date: Fri, 30 Aug 2024 18:24:35 +0200 Subject: [PATCH] Fix module constant usage warnings. --- crates/aiken-lang/src/tests/check.rs | 27 +++++++++++++++++++++++++++ crates/aiken-lang/src/tipo/infer.rs | 8 ++++++++ 2 files changed, 35 insertions(+) diff --git a/crates/aiken-lang/src/tests/check.rs b/crates/aiken-lang/src/tests/check.rs index 62d34708..c41f807e 100644 --- a/crates/aiken-lang/src/tests/check.rs +++ b/crates/aiken-lang/src/tests/check.rs @@ -3237,3 +3237,30 @@ fn extraneous_fallback_on_exhaustive_handlers() { Err((_, Error::UnexpectedValidatorFallback { .. })) )) } + +#[test] +fn constant_usage() { + let source_code = r#" + pub const some_bool_constant: Bool = True + + const some_int_constant: Int = 42 + + const some_string_constant: String = @"Aiken" + + test foo() { + some_int_constant == 42 + } + "#; + + let result = check(parse(source_code)); + assert!(result.is_ok()); + + let (warnings, _) = result.unwrap(); + assert!(matches!( + &warnings[..], + [Warning::UnusedPrivateModuleConstant { + name, + .. + }] if name == "some_string_constant" + )); +} diff --git a/crates/aiken-lang/src/tipo/infer.rs b/crates/aiken-lang/src/tipo/infer.rs index 7b469c6f..5aa2d40f 100644 --- a/crates/aiken-lang/src/tipo/infer.rs +++ b/crates/aiken-lang/src/tipo/infer.rs @@ -632,6 +632,14 @@ fn infer_definition( location, )?; + // NOTE: The assignment above is only a convenient way to create the TypedExpression + // that will be reduced at compile-time. We must increment its usage to not + // automatically trigger a warning since we are virtually creating a block with a + // single assignment that is then left unused. + // + // The usage of the constant is tracked through different means. + environment.increment_usage(&name); + let typed_expr = match typed_assignment { TypedExpr::Assignment { value, .. } => value, _ => unreachable!("infer_assignment inferred something else than an assignment?"),