feat: support punning in record updates

This commit is contained in:
rvcas 2022-12-04 18:58:49 -05:00 committed by Lucas
parent 9ebc836b89
commit 85f839abe4
2 changed files with 43 additions and 21 deletions

View File

@ -573,18 +573,30 @@ pub fn expr_parser(
.then(
just(Token::Comma)
.ignore_then(
select! { Token::Name {name} => name }
.then_ignore(just(Token::Colon))
.then(r.clone())
.map_with_span(|(label, value), span| {
ast::UntypedRecordUpdateArg {
label,
value,
choice((
select! { Token::Name {name} => name }
.then_ignore(just(Token::Colon))
.then(r.clone())
.map_with_span(|(label, value), span| {
ast::UntypedRecordUpdateArg {
label,
value,
location: span,
}
}),
select! {Token::Name {name} => name}.map_with_span(
|name, span| ast::UntypedRecordUpdateArg {
location: span,
}
})
.separated_by(just(Token::Comma))
.allow_trailing(),
value: expr::UntypedExpr::Var {
name: name.clone(),
location: span,
},
label: name,
},
),
))
.separated_by(just(Token::Comma))
.allow_trailing(),
)
.or_not(),
)

View File

@ -1177,7 +1177,7 @@ fn call() {
fn record_update() {
let code = indoc! {r#"
fn update_name(user: User, name: String) -> User {
User { ..user, name: "Aiken", }
User { ..user, name: "Aiken", age }
}
"#};
@ -1215,7 +1215,7 @@ fn record_update() {
},
],
body: expr::UntypedExpr::RecordUpdate {
location: Span::new((), 53..84),
location: Span::new((), 53..88),
constructor: Box::new(expr::UntypedExpr::Var {
location: Span::new((), 53..57),
name: "User".to_string(),
@ -1227,14 +1227,24 @@ fn record_update() {
}),
location: Span::new((), 60..66),
},
arguments: vec![ast::UntypedRecordUpdateArg {
label: "name".to_string(),
location: Span::new((), 68..81),
value: expr::UntypedExpr::String {
location: Span::new((), 74..81),
value: "Aiken".to_string(),
arguments: vec![
ast::UntypedRecordUpdateArg {
label: "name".to_string(),
location: Span::new((), 68..81),
value: expr::UntypedExpr::String {
location: Span::new((), 74..81),
value: "Aiken".to_string(),
},
},
}],
ast::UntypedRecordUpdateArg {
label: "age".to_string(),
location: Span::new((), 83..86),
value: expr::UntypedExpr::Var {
location: Span::new((), 83..86),
name: "age".to_string(),
},
},
],
},
doc: None,
location: Span::new((), 0..48),
@ -1247,7 +1257,7 @@ fn record_update() {
arguments: vec![],
}),
return_type: (),
end_position: 85,
end_position: 89,
}),
)
}