Fix hint when suggesting to use named fields

Wrongly suggesting args in lexicographical order instead of definition order... The tests were unfortunately only examplifying situations where the fields where also defined in lexicographical order... thus never really showing the issue. It came up in the real world, though. Whoopsie.

Signed-off-by: KtorZ <matthias.benkort@gmail.com>
This commit is contained in:
KtorZ 2025-03-16 14:07:25 +01:00
parent f786e80924
commit 776aea86f5
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
3 changed files with 55 additions and 1 deletions

View File

@ -16,6 +16,7 @@
### Fixed ### Fixed
- **aiken-lang**: Formatter was removing comments from function type annotation args @rvcas - **aiken-lang**: Formatter was removing comments from function type annotation args @rvcas
- **aiken-lang**: Fix hint when suggesting to use named fields, wrongly suggesting args in lexicographical order instead of definition order. @KtorZ
## v1.1.13 - 2025-02-26 ## v1.1.13 - 2025-02-26

View File

@ -3701,3 +3701,56 @@ fn unused_record_fields_4() {
} }
); );
} }
#[test]
fn unused_record_fields_5() {
let source_code = r#"
pub type Foo {
b_is_before_a: Bool,
a_is_after_b: Bool,
}
test confusing() {
let Foo(a, b) = Foo(True, True)
a || b
}
"#;
let result = check_validator(parse(source_code));
assert!(result.is_ok());
let (warnings, _) = result.unwrap();
assert_eq!(
warnings[0],
Warning::UnusedRecordFields {
location: Span::create(137, 9),
suggestion: UntypedPattern::Constructor {
is_record: true,
location: Span::create(137, 9),
name: "Foo".to_string(),
arguments: vec![
CallArg {
label: Some("b_is_before_a".to_string()),
location: Span::create(141, 13),
value: Pattern::Var {
location: Span::create(141, 1),
name: "a".to_string()
}
},
CallArg {
label: Some("a_is_after_b".to_string()),
location: Span::create(144, 12),
value: Pattern::Var {
location: Span::create(144, 1),
name: "b".to_string()
}
}
],
module: None,
constructor: (),
spread_location: None,
tipo: ()
}
}
);
}

View File

@ -467,7 +467,7 @@ impl<'a, 'b> PatternTyper<'a, 'b> {
let arguments = field_map let arguments = field_map
.fields .fields
.iter() .iter()
.sorted_by(|(a, _), (b, _)| a.cmp(b)) .sorted_by(|(_, (a, _)), (_, (b, _))| a.cmp(b))
.zip(pattern_args.iter()) .zip(pattern_args.iter())
.filter_map(|((field, (_, _)), arg)| { .filter_map(|((field, (_, _)), arg)| {
if arg.value.is_discard() { if arg.value.is_discard() {