fix: empty records crashing code gen closes #728

This commit is contained in:
rvcas 2023-08-31 17:39:38 -04:00
parent fb967d4c7b
commit dca09811c1
No known key found for this signature in database
GPG Key ID: C09B64E263F7D68C
3 changed files with 103 additions and 9 deletions

View File

@ -54,7 +54,16 @@ pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError
|(((public, opaque), (name, parameters)), constructors), span| {
ast::UntypedDefinition::DataType(ast::DataType {
location: span,
constructors: constructors
constructors: if constructors.is_empty() {
vec![ast::RecordConstructor {
location: span,
arguments: vec![],
doc: None,
name: name.clone(),
sugar: true,
}]
} else {
constructors
.into_iter()
.map(|mut constructor| {
if constructor.sugar {
@ -63,7 +72,8 @@ pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError
constructor
})
.collect(),
.collect()
},
doc: None,
name,
opaque,
@ -119,4 +129,25 @@ mod tests {
"#
);
}
#[test]
fn record_sugar() {
assert_definition!(
r#"
pub type Foo {
wow: Int,
}
"#
);
}
#[test]
fn empty_record_sugar() {
assert_definition!(
r#"
pub type Foo {
}
"#
);
}
}

View File

@ -0,0 +1,24 @@
---
source: crates/aiken-lang/src/parser/definition/data_type.rs
description: "Code:\n\npub type Foo {\n}\n"
---
DataType(
DataType {
constructors: [
RecordConstructor {
location: 0..16,
name: "Foo",
arguments: [],
doc: None,
sugar: true,
},
],
doc: None,
location: 0..16,
name: "Foo",
opaque: false,
parameters: [],
public: true,
typed_parameters: [],
},
)

View File

@ -0,0 +1,39 @@
---
source: crates/aiken-lang/src/parser/definition/data_type.rs
description: "Code:\n\npub type Foo {\n wow: Int,\n}\n"
---
DataType(
DataType {
constructors: [
RecordConstructor {
location: 13..28,
name: "Foo",
arguments: [
RecordConstructorArg {
label: Some(
"wow",
),
annotation: Constructor {
location: 22..25,
module: None,
name: "Int",
arguments: [],
},
location: 17..25,
tipo: (),
doc: None,
},
],
doc: None,
sugar: true,
},
],
doc: None,
location: 0..28,
name: "Foo",
opaque: false,
parameters: [],
public: true,
typed_parameters: [],
},
)