feat(validators): unused param warning

Params being unused were being incorrectly reported.
This was because params need to be initialized
at a scope above both the validator functions. This
manifested when using a multi-validator where one of
the params was not used in both validators.

The easy fix was to add a field called
`is_validator_param` to `ArgName`. Then
when infering a function we don't initialize args
that are validator params. We now handle this
in a scope that is created before in the match branch for
validator in the `infer_definition` function. In there
we call `.in_new_scope` and initialize params for usage
detection.
This commit is contained in:
rvcas
2023-03-30 16:34:22 -04:00
committed by Lucas
parent 8fad5b77c6
commit d8cbcde61d
7 changed files with 208 additions and 118 deletions

View File

@@ -113,6 +113,47 @@ fn validator_in_lib_warning() {
))
}
#[test]
fn multi_validator() {
let source_code = r#"
validator(foo: ByteArray, bar: Int) {
fn spend(_d, _r, _c) {
foo == #"aabb"
}
fn mint(_r, _c) {
bar == 0
}
}
"#;
let (warnings, _) = check_validator(parse(source_code)).unwrap();
assert_eq!(warnings.len(), 0)
}
#[test]
fn multi_validator_warning() {
let source_code = r#"
validator(foo: ByteArray, bar: Int) {
fn spend(_d, _r, _c) {
foo == #"aabb"
}
fn mint(_r, _c) {
True
}
}
"#;
let (warnings, _) = check_validator(parse(source_code)).unwrap();
assert!(matches!(
warnings[0],
Warning::UnusedVariable { ref name, .. } if name == "bar"
))
}
#[test]
fn if_scoping() {
let source_code = r#"

View File

@@ -60,6 +60,7 @@ fn validator() {
name: "datum".to_string(),
label: "datum".to_string(),
location: Span::new((), 21..26),
is_validator_param: false,
},
location: Span::new((), 21..26),
annotation: None,
@@ -70,6 +71,7 @@ fn validator() {
name: "rdmr".to_string(),
label: "rdmr".to_string(),
location: Span::new((), 28..32),
is_validator_param: false,
},
location: Span::new((), 28..32),
annotation: None,
@@ -80,6 +82,7 @@ fn validator() {
name: "ctx".to_string(),
label: "ctx".to_string(),
location: Span::new((), 34..37),
is_validator_param: false,
},
location: Span::new((), 34..37),
annotation: None,
@@ -131,6 +134,7 @@ fn double_validator() {
name: "datum".to_string(),
label: "datum".to_string(),
location: Span::new((), 21..26),
is_validator_param: false,
},
location: Span::new((), 21..26),
annotation: None,
@@ -141,6 +145,7 @@ fn double_validator() {
name: "rdmr".to_string(),
label: "rdmr".to_string(),
location: Span::new((), 28..32),
is_validator_param: false,
},
location: Span::new((), 28..32),
annotation: None,
@@ -151,6 +156,7 @@ fn double_validator() {
name: "ctx".to_string(),
label: "ctx".to_string(),
location: Span::new((), 34..37),
is_validator_param: false,
},
location: Span::new((), 34..37),
annotation: None,
@@ -176,6 +182,7 @@ fn double_validator() {
name: "rdmr".to_string(),
label: "rdmr".to_string(),
location: Span::new((), 64..68),
is_validator_param: false,
},
location: Span::new((), 64..68),
annotation: None,
@@ -186,6 +193,7 @@ fn double_validator() {
name: "ctx".to_string(),
label: "ctx".to_string(),
location: Span::new((), 70..73),
is_validator_param: false,
},
location: Span::new((), 70..73),
annotation: None,
@@ -589,6 +597,7 @@ fn plus_binop() {
label: "a".to_string(),
name: "a".to_string(),
location: Span::new((), 15..16),
is_validator_param: false,
},
location: Span::new((), 15..16),
annotation: None,
@@ -640,6 +649,7 @@ fn pipeline() {
name: "a".to_string(),
label: "thing".to_string(),
location: Span::new((), 13..20),
is_validator_param: false,
},
location: Span::new((), 13..25),
annotation: Some(ast::Annotation::Constructor {
@@ -808,6 +818,7 @@ fn let_bindings() {
label: "a".to_string(),
name: "a".to_string(),
location: Span::new((), 11..12),
is_validator_param: false,
},
location: Span::new((), 11..17),
annotation: Some(ast::Annotation::Constructor {
@@ -934,6 +945,7 @@ fn block() {
label: "a".to_string(),
name: "a".to_string(),
location: Span::new((), 12..13),
is_validator_param: false,
},
location: Span::new((), 12..18),
annotation: Some(ast::Annotation::Constructor {
@@ -1027,6 +1039,7 @@ fn when() {
label: "a".to_string(),
name: "a".to_string(),
location: Span::new((), 12..13),
is_validator_param: false,
},
location: Span::new((), 12..18),
annotation: Some(ast::Annotation::Constructor {
@@ -1160,6 +1173,7 @@ fn anonymous_function() {
label: "a".to_string(),
name: "a".to_string(),
location: Span::new((), 43..44),
is_validator_param: false,
},
location: Span::new((), 43..49),
annotation: Some(ast::Annotation::Constructor {
@@ -1243,6 +1257,7 @@ fn field_access() {
label: "user".to_string(),
name: "user".to_string(),
location: Span::new((), 8..12),
is_validator_param: false,
},
location: Span::new((), 8..18),
annotation: Some(ast::Annotation::Constructor {
@@ -1325,6 +1340,7 @@ fn call() {
label: "_capture__0".to_string(),
name: "_capture__0".to_string(),
location: Span::new((), 0..0),
is_validator_param: false,
},
location: Span::new((), 0..0),
annotation: None,
@@ -1351,6 +1367,7 @@ fn call() {
label: "y".to_string(),
name: "y".to_string(),
location: Span::new((), 69..70),
is_validator_param: false,
},
location: Span::new((), 69..70),
annotation: None,
@@ -1450,6 +1467,7 @@ fn record_update() {
label: "user".to_string(),
name: "user".to_string(),
location: Span::new((), 15..19),
is_validator_param: false,
},
location: Span::new((), 15..25),
annotation: Some(ast::Annotation::Constructor {
@@ -1465,6 +1483,7 @@ fn record_update() {
label: "name".to_string(),
name: "name".to_string(),
location: Span::new((), 27..31),
is_validator_param: false,
},
location: Span::new((), 27..42),
annotation: Some(ast::Annotation::Constructor {