Revise error for reporting

This commit is contained in:
Richard Feldman 2020-04-03 00:48:00 -04:00
parent 930f83a283
commit 67b7be9786
2 changed files with 39 additions and 20 deletions

View file

@ -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_can::expected::{Expected, PExpected};
use roc_module::symbol::Symbol; use roc_module::symbol::Symbol;
use roc_solve::solve; use roc_solve::solve;
@ -84,13 +84,13 @@ fn report_bad_type(
expected_type: ErrorType, expected_type: ErrorType,
region: roc_region::all::Region, region: roc_region::all::Region,
_opt_highlight: Option<roc_region::all::Region>, _opt_highlight: Option<roc_region::all::Region>,
problem: &str, problem: ReportText,
this_is: &str, this_is: &str,
further_details: ReportText, further_details: ReportText,
) -> Report { ) -> Report {
use ReportText::*; use ReportText::*;
let lines = vec![ let lines = vec![
plain_text(problem), problem,
Region(region), Region(region),
lone_type( lone_type(
found, found,
@ -120,21 +120,40 @@ fn to_expr_report(
Expected::FromAnnotation(_name, _arity, _sub_context, _expected_type) => todo!(), Expected::FromAnnotation(_name, _arity, _sub_context, _expected_type) => todo!(),
Expected::ForReason(reason, expected_type, region) => { Expected::ForReason(reason, expected_type, region) => {
match reason { match reason {
Reason::IfCondition => report_bad_type( 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(", but it isnt."),
]);
report_bad_type(
filename, filename,
&category, &category,
found, found,
expected_type, expected_type,
region, region,
Some(expr_region), Some(expr_region),
"This `if` condition does not evaluate to a boolean value, True or False.", problem,
"It is", "Instead its",
Concat(vec![ Concat(vec![
plain_text("But I need this `if` condition to be a "), code_text("if"),
ReportText::Type(Content::Alias(Symbol::BOOL_BOOL, vec![], Variable::BOOL)), plain_text(" conditions must evaluate to a "),
plain_text(" value."), 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 } => { Reason::IfBranch { index } => {
let ith = int_to_ordinal(index); let ith = int_to_ordinal(index);
report_mismatch( report_mismatch(

View file

@ -766,16 +766,16 @@ mod test_reporting {
), ),
indoc!( indoc!(
r#" r#"
This `if` condition does not evaluate to a boolean value, True or False. This `if` condition should be a Bool, but it isnt.
1 if "foo" then 2 else 3 1 if "foo" then 2 else 3
^^^^^ ^^^^^
It is a string of type: Instead its a string of type:
Str Str
But I need this `if` condition to be a Bool value. `if` conditions must evaluate to a Booleither `True` or `False`.
"# "#
), ),