mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-29 23:04:49 +00:00
record update value
This commit is contained in:
parent
f8b3d5dce7
commit
5b0d9e693b
4 changed files with 72 additions and 19 deletions
|
@ -181,9 +181,10 @@ pub fn constrain_expr(
|
||||||
region,
|
region,
|
||||||
);
|
);
|
||||||
|
|
||||||
cons.push(con);
|
// ensure constraints are solved in this order, gives better errors
|
||||||
cons.push(fields_con);
|
cons.insert(0, fields_con);
|
||||||
cons.push(record_con);
|
cons.insert(1, con);
|
||||||
|
cons.insert(2, record_con);
|
||||||
|
|
||||||
exists(vars, And(cons))
|
exists(vars, And(cons))
|
||||||
}
|
}
|
||||||
|
|
|
@ -302,11 +302,11 @@ where
|
||||||
Url => {
|
Url => {
|
||||||
self.write_str("<")?;
|
self.write_str("<")?;
|
||||||
}
|
}
|
||||||
GlobalTag | PrivateTag | RecordField | Keyword => {
|
GlobalTag | PrivateTag | Keyword => {
|
||||||
self.write_str("`")?;
|
self.write_str("`")?;
|
||||||
}
|
}
|
||||||
CodeBlock | PlainText | LineNumber | Error | GutterBar | TypeVariable | Alias
|
CodeBlock | PlainText | LineNumber | Error | GutterBar | TypeVariable | Alias
|
||||||
| Module | Structure | Symbol | BinOp => {}
|
| RecordField | Module | Structure | Symbol | BinOp => {}
|
||||||
}
|
}
|
||||||
self.style_stack.push(*annotation);
|
self.style_stack.push(*annotation);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -324,11 +324,11 @@ where
|
||||||
Url => {
|
Url => {
|
||||||
self.write_str(">")?;
|
self.write_str(">")?;
|
||||||
}
|
}
|
||||||
GlobalTag | PrivateTag | RecordField | Keyword => {
|
GlobalTag | PrivateTag | Keyword => {
|
||||||
self.write_str("`")?;
|
self.write_str("`")?;
|
||||||
}
|
}
|
||||||
CodeBlock | PlainText | LineNumber | Error | GutterBar | TypeVariable | Alias
|
CodeBlock | PlainText | LineNumber | Error | GutterBar | TypeVariable | Alias
|
||||||
| Module | Structure | Symbol | BinOp => {}
|
| RecordField | Module | Structure | Symbol | BinOp => {}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -121,7 +121,7 @@ fn to_expr_report(
|
||||||
use ReportText::*;
|
use ReportText::*;
|
||||||
|
|
||||||
match expected {
|
match expected {
|
||||||
Expected::NoExpectation(_expected_type) => todo!(),
|
Expected::NoExpectation(expected_type) => todo!("hit no expectation with type {:?}", expected_type),
|
||||||
Expected::FromAnnotation(_name, _arity, _sub_context, _expected_type) => todo!(),
|
Expected::FromAnnotation(_name, _arity, _sub_context, _expected_type) => todo!(),
|
||||||
Expected::ForReason(reason, expected_type, region) => match reason {
|
Expected::ForReason(reason, expected_type, region) => match reason {
|
||||||
Reason::IfCondition => {
|
Reason::IfCondition => {
|
||||||
|
@ -298,6 +298,26 @@ fn to_expr_report(
|
||||||
plain_text("instead. I need all elements of a list to have the same type!"),
|
plain_text("instead. I need all elements of a list to have the same type!"),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Reason::RecordUpdateValue(field) => report_mismatch(
|
||||||
|
filename,
|
||||||
|
&category,
|
||||||
|
found,
|
||||||
|
expected_type,
|
||||||
|
region,
|
||||||
|
Some(expr_region),
|
||||||
|
Concat(vec![
|
||||||
|
plain_text("I cannot update the "),
|
||||||
|
record_field_text(field.as_str()),
|
||||||
|
plain_text(" field like this"),
|
||||||
|
]),
|
||||||
|
Concat(vec![
|
||||||
|
plain_text("You are trying to update "),
|
||||||
|
record_field_text(field.as_str()),
|
||||||
|
plain_text(" to be"),
|
||||||
|
]),
|
||||||
|
plain_text("But it should be"),
|
||||||
|
plain_text("instead. Record update syntax does not allow you to change the type of fields. You can achieve that with record literal syntax."),
|
||||||
|
),
|
||||||
other => {
|
other => {
|
||||||
// AnonymousFnArg { arg_index: u8 },
|
// AnonymousFnArg { arg_index: u8 },
|
||||||
// NamedFnArg(String /* function name */, u8 /* arg index */),
|
// NamedFnArg(String /* function name */, u8 /* arg index */),
|
||||||
|
|
|
@ -928,6 +928,38 @@ mod test_reporting {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn record_update_value() {
|
||||||
|
report_problem_as(
|
||||||
|
indoc!(
|
||||||
|
r#"
|
||||||
|
x : { foo : {} }
|
||||||
|
x = { foo: {} }
|
||||||
|
|
||||||
|
{ x & foo: "bar" }
|
||||||
|
"#
|
||||||
|
),
|
||||||
|
indoc!(
|
||||||
|
r#"
|
||||||
|
I cannot update the .foo field like this
|
||||||
|
|
||||||
|
4 ┆ { x & foo: "bar" }
|
||||||
|
┆ ^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
You are trying to update .foo to be a string of type
|
||||||
|
|
||||||
|
Str
|
||||||
|
|
||||||
|
But it should be
|
||||||
|
|
||||||
|
{}
|
||||||
|
|
||||||
|
instead. Record update syntax does not allow you to change the type of fields. You can achieve that with record literal syntax.
|
||||||
|
"#
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn circular_type() {
|
fn circular_type() {
|
||||||
report_problem_as(
|
report_problem_as(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue