From 854ffdae5e56a483986b9869059f6dc6c33d9a6d Mon Sep 17 00:00:00 2001 From: Folkert Date: Sun, 5 Apr 2020 14:33:02 +0200 Subject: [PATCH] stylistic improvements --- compiler/reporting/src/report.rs | 8 +-- compiler/reporting/src/type_error.rs | 70 +++++++++++----------- compiler/reporting/tests/test_reporting.rs | 67 ++++++++++----------- 3 files changed, 71 insertions(+), 74 deletions(-) diff --git a/compiler/reporting/src/report.rs b/compiler/reporting/src/report.rs index 493f6d3651..68a95ec442 100644 --- a/compiler/reporting/src/report.rs +++ b/compiler/reporting/src/report.rs @@ -329,11 +329,11 @@ where Url => { self.write_str("<")?; } - GlobalTag | PrivateTag | Keyword => { + GlobalTag | PrivateTag | Keyword | RecordField | Symbol => { self.write_str("`")?; } CodeBlock | PlainText | LineNumber | Error | GutterBar | TypeVariable | Alias - | RecordField | Module | Structure | Symbol | BinOp => {} + | Module | Structure | BinOp => {} } self.style_stack.push(*annotation); Ok(()) @@ -351,11 +351,11 @@ where Url => { self.write_str(">")?; } - GlobalTag | PrivateTag | Keyword => { + GlobalTag | PrivateTag | Keyword | RecordField | Symbol => { self.write_str("`")?; } CodeBlock | PlainText | LineNumber | Error | GutterBar | TypeVariable | Alias - | RecordField | Module | Structure | Symbol | BinOp => {} + | Module | Structure | BinOp => {} }, } Ok(()) diff --git a/compiler/reporting/src/type_error.rs b/compiler/reporting/src/type_error.rs index fc1e30fa87..2479add358 100644 --- a/compiler/reporting/src/type_error.rs +++ b/compiler/reporting/src/type_error.rs @@ -130,7 +130,7 @@ fn to_expr_report( keyword_text("if"), plain_text(" condition needs to be a "), ReportText::Type(Content::Alias(Symbol::BOOL_BOOL, vec![], Variable::BOOL)), - plain_text("."), + plain_text(":"), ]); report_bad_type( @@ -143,7 +143,7 @@ fn to_expr_report( problem, plain_text("Right now it’s"), Concat(vec![ - plain_text("but I need every "), + plain_text("But I need every "), keyword_text("if"), plain_text(" condition to evaluate to a "), ReportText::Type(Content::Alias(Symbol::BOOL_BOOL, vec![], Variable::BOOL)), @@ -167,7 +167,7 @@ fn to_expr_report( keyword_text("if"), plain_text(" guard condition needs to be a "), ReportText::Type(Content::Alias(Symbol::BOOL_BOOL, vec![], Variable::BOOL)), - plain_text("."), + plain_text(":"), ]); report_bad_type( filename, @@ -179,7 +179,7 @@ fn to_expr_report( problem, plain_text("Right now it’s"), Concat(vec![ - plain_text("but I need every "), + plain_text("But I need every "), keyword_text("if"), plain_text(" guard condition to evaluate to a "), ReportText::Type(Content::Alias(Symbol::BOOL_BOOL, vec![], Variable::BOOL)), @@ -219,10 +219,10 @@ fn to_expr_report( Concat(vec![ plain_text("but the "), keyword_text("then"), - plain_text(" branch has the type"), + plain_text(" branch has the type:"), ]), Concat(vec![ - plain_text("instead. I need all branches in an "), + plain_text("I need all branches in an "), keyword_text("if"), plain_text(" to have the same type!"), ]), @@ -242,9 +242,9 @@ fn to_expr_report( ith )), plain_text(&format!("The {} branch is", ith)), - plain_text("but all the previous branches have the type"), + plain_text("But all the previous branches have type:"), Concat(vec![ - plain_text("instead. I need all branches in an "), + plain_text("I need all branches in an "), keyword_text("if"), plain_text(" to have the same type!"), ]), @@ -266,12 +266,12 @@ fn to_expr_report( Concat(vec![ plain_text(&format!("The {} branch of this ", ith)), keyword_text("when"), - plain_text(" does not match all the previous branches"), + plain_text(" does not match all the previous branches:"), ]), plain_text(&format!("The {} branch is", ith)), - plain_text("but all the previous branches have type"), + plain_text("But all the previous branches have type:"), Concat(vec![ - plain_text("instead. I need all branches of a "), + plain_text("I need all branches of a "), keyword_text("when"), plain_text(" to have the same type!"), ]), @@ -290,12 +290,12 @@ fn to_expr_report( region, Some(expr_region), plain_text(&format!( - "The {} element of this list does not match all the previous elements", + "The {} element of this list does not match all the previous elements:", ith )), plain_text(&format!("The {} element is", ith)), - plain_text("but all the previous elements in the list have type"), - plain_text("instead. I need all elements of a list to have the same type!"), + plain_text("But all the previous elements in the list have type:"), + plain_text("I need all elements of a list to have the same type!"), ) } Reason::RecordUpdateValue(field) => report_mismatch( @@ -308,15 +308,15 @@ fn to_expr_report( Concat(vec![ plain_text("I cannot update the "), record_field_text(field.as_str()), - plain_text(" field like this"), + 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."), + plain_text("But it should be:"), + plain_text("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 }, @@ -403,64 +403,64 @@ fn add_category(this_is: ReportText, category: &Category) -> ReportText { Lookup(name) => Concat(vec![ plain_text("This "), Value(*name), - plain_text(" value is a"), + plain_text(" value is a:"), ]), If => Concat(vec![ plain_text("This "), keyword_text("if"), - plain_text("expression produces"), + plain_text("expression produces:"), ]), When => Concat(vec![ plain_text("This "), keyword_text("when"), - plain_text("expression produces"), + plain_text("expression produces:"), ]), - List => Concat(vec![this_is, plain_text(" a list of type")]), - Num => Concat(vec![this_is, plain_text(" a number of type")]), - Int => Concat(vec![this_is, plain_text(" an integer of type")]), - Float => Concat(vec![this_is, plain_text(" a float of type")]), - Str => Concat(vec![this_is, plain_text(" a string of type")]), + List => Concat(vec![this_is, plain_text(" a list of type:")]), + Num => Concat(vec![this_is, plain_text(" a number of type:")]), + Int => Concat(vec![this_is, plain_text(" an integer of type:")]), + Float => Concat(vec![this_is, plain_text(" a float of type:")]), + Str => Concat(vec![this_is, plain_text(" a string of type:")]), - Lambda => Concat(vec![this_is, plain_text("an anonymous function of type")]), + Lambda => Concat(vec![this_is, plain_text("an anonymous function of type:")]), TagApply(TagName::Global(name)) => Concat(vec![ plain_text("This "), global_tag_text(name.as_str()), - plain_text(" global tag application produces"), + plain_text(" global tag application produces:"), ]), TagApply(TagName::Private(name)) => Concat(vec![ plain_text("This "), private_tag_text(*name), - plain_text(" private tag application produces"), + plain_text(" private tag application produces:"), ]), - Record => Concat(vec![this_is, plain_text(" a record of type")]), + Record => Concat(vec![this_is, plain_text(" a record of type:")]), Accessor(field) => Concat(vec![ plain_text("This "), record_field_text(field.as_str()), - plain_text(" value is a"), + plain_text(" value is a:"), ]), Access(field) => Concat(vec![ plain_text("The value at "), record_field_text(field.as_str()), - plain_text(" is a"), + plain_text(" is a:"), ]), CallResult(Some(symbol)) => Concat(vec![ plain_text("This "), Value(*symbol), - plain_text(" call produces"), + plain_text(" call produces:"), ]), - CallResult(None) => Concat(vec![this_is]), + CallResult(None) => Concat(vec![this_is, plain_text(":")]), Uniqueness => Concat(vec![ this_is, - plain_text(" an uniqueness attribute of type"), + plain_text(" an uniqueness attribute of type:"), ]), - Storage => Concat(vec![this_is, plain_text(" a value of type")]), + Storage => Concat(vec![this_is, plain_text(" a value of type:")]), } } diff --git a/compiler/reporting/tests/test_reporting.rs b/compiler/reporting/tests/test_reporting.rs index 9c61ef7ea1..741d56bb4c 100644 --- a/compiler/reporting/tests/test_reporting.rs +++ b/compiler/reporting/tests/test_reporting.rs @@ -222,7 +222,7 @@ mod test_reporting { .text .render_ci(&mut buf, &mut subs, home, &src_lines, &interns); - assert_eq!(buf, "x"); + assert_eq!(buf, "`x`"); } #[test] @@ -296,12 +296,12 @@ mod test_reporting { ), indoc!( r#" - y is not used anywhere in your code. + `y` is not used anywhere in your code. 2 ┆ y = 2 ┆ ^ - If you didn't intend on using y then remove it so future readers of your code don't wonder why it is there."# + If you didn't intend on using `y` then remove it so future readers of your code don't wonder why it is there."# ), ) } @@ -860,16 +860,16 @@ mod test_reporting { ), indoc!( r#" - This `if` condition needs to be a Bool. + This `if` condition needs to be a Bool: 1 ┆ if "foo" then 2 else 3 ┆ ^^^^^ - Right now it’s a string of type + Right now it’s a string of type: Str - but I need every `if` condition to evaluate to a Bool—either `True` or `False`. + But I need every `if` condition to evaluate to a Bool—either `True` or `False`. "# ), ) @@ -887,16 +887,16 @@ mod test_reporting { ), indoc!( r#" - This `if` guard condition needs to be a Bool. + This `if` guard condition needs to be a Bool: 2 ┆ 2 if 1 -> 0x0 ┆ ^ - Right now it’s a number of type + Right now it’s a number of type: Num a - but I need every `if` guard condition to evaluate to a Bool—either `True` or `False`. + But I need every `if` guard condition to evaluate to a Bool—either `True` or `False`. "# ), ) @@ -917,15 +917,15 @@ mod test_reporting { 1 ┆ if True then 2 else "foo" ┆ ^^^^^ - The `else` branch is a string of type + The `else` branch is a string of type: Str - but the `then` branch has the type + but the `then` branch has the type: Num a - instead. I need all branches in an `if` to have the same type! + I need all branches in an `if` to have the same type! "# ), ) @@ -950,12 +950,10 @@ mod test_reporting { // Str - // but all the previous branches have the type + // But all the previous branches have the type // Num a - // instead. - // "# // ), // ) @@ -973,20 +971,20 @@ mod test_reporting { ), indoc!( r#" - The 2nd branch of this `when` does not match all the previous branches + The 2nd branch of this `when` does not match all the previous branches: 3 ┆ 3 -> {} ┆ ^^ - The 2nd branch is a record of type + The 2nd branch is a record of type: {} - but all the previous branches have type + But all the previous branches have type: Str - instead. I need all branches of a `when` to have the same type! + I need all branches of a `when` to have the same type! "# ), ) @@ -1002,20 +1000,20 @@ mod test_reporting { ), indoc!( r#" - The 3rd element of this list does not match all the previous elements + The 3rd element of this list does not match all the previous elements: 1 ┆ [ 1, 3, "foo" ] ┆ ^^^^^ - The 3rd element is a string of type + The 3rd element is a string of type: Str - but all the previous elements in the list have type + But all the previous elements in the list have type: Num a - instead. I need all elements of a list to have the same type! + I need all elements of a list to have the same type! "# ), ) @@ -1034,20 +1032,20 @@ mod test_reporting { ), indoc!( r#" - I cannot update the .foo field like this + I cannot update the `.foo` field like this: 4 ┆ { x & foo: "bar" } ┆ ^^^^^^^^^^^^^^^^^^ - You are trying to update .foo to be a string of type + You are trying to update `.foo` to be a string of type: Str - But it should be + 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. + Record update syntax does not allow you to change the type of fields. You can achieve that with record literal syntax. "# ), ) @@ -1067,12 +1065,12 @@ mod test_reporting { // ), // indoc!( // r#" - // The `x` record does not have a `baz` field + // The `x` record does not have a `baz` field: // // 4 ┆ { x & baz: "bar" } // ┆ ^^^ // - // This is usually a typo. Here are the `x` fields that are most similar + // This is usually a typo. Here are the `x` fields that are most similar: // // { foo : {} // } @@ -1096,20 +1094,19 @@ mod test_reporting { // ), // indoc!( // r#" - // Something is off with the body of the `x` definition + // Something is off with the body of the `x` definition: // // 4 ┆ x = 4 // ┆ ^ // - // The body is a number of type + // The body is a number of type: // // Num a // - // But the type annotation on `x` says that it should be + // But the type annotation on `x` says that it should be: // // Str // - // instead. // "# // ), // ) @@ -1127,7 +1124,7 @@ mod test_reporting { ), indoc!( r#" - I'm inferring a weird self-referential type for g: + I'm inferring a weird self-referential type for `g`: 1 ┆ f = \g -> g g ┆ ^ @@ -1152,7 +1149,7 @@ mod test_reporting { ), indoc!( r#" - I'm inferring a weird self-referential type for f: + I'm inferring a weird self-referential type for `f`: 1 ┆ f = \x -> f [x] ┆ ^