record update value

This commit is contained in:
Folkert 2020-04-04 23:53:25 +02:00
parent f8b3d5dce7
commit 5b0d9e693b
4 changed files with 72 additions and 19 deletions

View file

@ -181,9 +181,10 @@ pub fn constrain_expr(
region,
);
cons.push(con);
cons.push(fields_con);
cons.push(record_con);
// ensure constraints are solved in this order, gives better errors
cons.insert(0, fields_con);
cons.insert(1, con);
cons.insert(2, record_con);
exists(vars, And(cons))
}

View file

@ -302,11 +302,11 @@ where
Url => {
self.write_str("<")?;
}
GlobalTag | PrivateTag | RecordField | Keyword => {
GlobalTag | PrivateTag | Keyword => {
self.write_str("`")?;
}
CodeBlock | PlainText | LineNumber | Error | GutterBar | TypeVariable | Alias
| Module | Structure | Symbol | BinOp => {}
| RecordField | Module | Structure | Symbol | BinOp => {}
}
self.style_stack.push(*annotation);
Ok(())
@ -324,11 +324,11 @@ where
Url => {
self.write_str(">")?;
}
GlobalTag | PrivateTag | RecordField | Keyword => {
GlobalTag | PrivateTag | Keyword => {
self.write_str("`")?;
}
CodeBlock | PlainText | LineNumber | Error | GutterBar | TypeVariable | Alias
| Module | Structure | Symbol | BinOp => {}
| RecordField | Module | Structure | Symbol | BinOp => {}
},
}
Ok(())

View file

@ -121,7 +121,7 @@ fn to_expr_report(
use ReportText::*;
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::ForReason(reason, expected_type, region) => match reason {
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!"),
)
}
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 => {
// AnonymousFnArg { arg_index: u8 },
// NamedFnArg(String /* function name */, u8 /* arg index */),

View file

@ -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]
fn circular_type() {
report_problem_as(