diff --git a/compiler/reporting/src/type_error.rs b/compiler/reporting/src/type_error.rs index ae3fb16232..bd843e558b 100644 --- a/compiler/reporting/src/type_error.rs +++ b/compiler/reporting/src/type_error.rs @@ -1,4 +1,4 @@ -use crate::report::{plain_text, with_indent, Report, ReportText}; +use crate::report::{code_text, plain_text, with_indent, Report, ReportText}; use roc_can::expected::{Expected, PExpected}; use roc_module::symbol::Symbol; use roc_solve::solve; @@ -84,13 +84,13 @@ fn report_bad_type( expected_type: ErrorType, region: roc_region::all::Region, _opt_highlight: Option, - problem: &str, + problem: ReportText, this_is: &str, further_details: ReportText, ) -> Report { use ReportText::*; let lines = vec![ - plain_text(problem), + problem, Region(region), lone_type( found, @@ -120,21 +120,40 @@ fn to_expr_report( Expected::FromAnnotation(_name, _arity, _sub_context, _expected_type) => todo!(), Expected::ForReason(reason, expected_type, region) => { match reason { - Reason::IfCondition => report_bad_type( - filename, - &category, - found, - expected_type, - region, - Some(expr_region), - "This `if` condition does not evaluate to a boolean value, True or False.", - "It is", - Concat(vec![ - plain_text("But I need this `if` condition to be a "), + Reason::IfCondition => { + let problem = Concat(vec![ + plain_text("This "), + code_text("if"), + plain_text(" condition should be a "), ReportText::Type(Content::Alias(Symbol::BOOL_BOOL, vec![], Variable::BOOL)), - plain_text(" value."), - ]), - ), + plain_text(", but it isn’t."), + ]); + + report_bad_type( + filename, + &category, + found, + expected_type, + region, + Some(expr_region), + problem, + "Instead it’s", + Concat(vec![ + code_text("if"), + plain_text(" conditions must evaluate to a "), + ReportText::Type(Content::Alias( + Symbol::BOOL_BOOL, + vec![], + Variable::BOOL, + )), + plain_text("—either "), + code_text("True"), + plain_text(" or "), + code_text("False"), + plain_text("."), + ]), + ) + } Reason::IfBranch { index } => { let ith = int_to_ordinal(index); report_mismatch( diff --git a/compiler/reporting/tests/test_reporting.rs b/compiler/reporting/tests/test_reporting.rs index 0f431f569e..54a2a8b5a9 100644 --- a/compiler/reporting/tests/test_reporting.rs +++ b/compiler/reporting/tests/test_reporting.rs @@ -766,16 +766,16 @@ mod test_reporting { ), indoc!( r#" - This `if` condition does not evaluate to a boolean value, True or False. + This `if` condition should be a Bool, but it isn’t. 1 ┆ if "foo" then 2 else 3 ┆ ^^^^^ - It is a string of type: + Instead it’s a string of type: Str - But I need this `if` condition to be a Bool value. + `if` conditions must evaluate to a Bool—either `True` or `False`. "# ),