Merge branch 'trunk' of github.com:rtfeldman/roc into shadowing-report

This commit is contained in:
Chad Stearns 2020-04-05 15:57:26 -04:00
commit 6e00e6f4a2
14 changed files with 654 additions and 27 deletions

View file

@ -14,6 +14,7 @@ use ven_pretty::{BoxAllocator, DocAllocator, DocBuilder, Render, RenderAnnotated
/// A textual report.
pub struct Report {
pub title: String,
pub filename: PathBuf,
pub text: ReportText,
}
@ -128,6 +129,7 @@ pub fn can_problem(filename: PathBuf, problem: Problem) -> Report {
};
Report {
title: "SYNTAX PROBLEM".to_string(),
filename,
text: Concat(texts),
}
@ -327,11 +329,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(())
@ -349,11 +351,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

@ -74,6 +74,7 @@ fn report_mismatch(
];
Report {
title: "TYPE MISMATCH".to_string(),
filename,
text: Concat(lines),
}
@ -104,6 +105,7 @@ fn report_bad_type(
];
Report {
title: "TYPE MISMATCH".to_string(),
filename,
text: Concat(lines),
}
@ -119,8 +121,8 @@ fn to_expr_report(
use ReportText::*;
match expected {
Expected::NoExpectation(_expected_type) => todo!(),
Expected::FromAnnotation(_name, _arity, _sub_context, _expected_type) => todo!(),
Expected::NoExpectation(expected_type) => todo!("hit no expectation with type {:?}", expected_type),
Expected::FromAnnotation(_name, _arity, _sub_context, _expected_type) => todo!("hit from annotation {:?} {:?}",_sub_context, _expected_type ),
Expected::ForReason(reason, expected_type, region) => match reason {
Reason::IfCondition => {
let problem = Concat(vec![
@ -159,6 +161,36 @@ fn to_expr_report(
// they don't know. ("Wait, what's truthiness?")
)
}
Reason::WhenGuard => {
let problem = Concat(vec![
plain_text("This "),
keyword_text("if"),
plain_text(" guard condition needs to be a "),
ReportText::Type(Content::Alias(Symbol::BOOL_BOOL, vec![], Variable::BOOL)),
plain_text("."),
]);
report_bad_type(
filename,
&category,
found,
expected_type,
region,
Some(expr_region),
problem,
plain_text("Right now its"),
Concat(vec![
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)),
plain_text("—either "),
global_tag_text("True"),
plain_text(" or "),
global_tag_text("False"),
plain_text("."),
]),
)
}
Reason::IfBranch {
index,
total_branches,
@ -211,11 +243,96 @@ fn to_expr_report(
)),
plain_text(&format!("The {} branch is", ith)),
plain_text("but all the previous branches have the type"),
plain_text("instead."),
Concat(vec![
plain_text("instead. I need all branches in an "),
keyword_text("if"),
plain_text(" to have the same type!"),
]),
)
}
},
_ => todo!(),
Reason::WhenBranch { index } => {
// NOTE: is 0-based
let ith = int_to_ordinal(index + 1);
report_mismatch(
filename,
&category,
found,
expected_type,
region,
Some(expr_region),
Concat(vec![
plain_text(&format!("The {} branch of this ", ith)),
keyword_text("when"),
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"),
Concat(vec![
plain_text("instead. I need all branches of a "),
keyword_text("when"),
plain_text(" to have the same type!"),
]),
)
}
Reason::ElemInList { index } => {
// NOTE: is 0-based
let ith = int_to_ordinal(index + 1);
report_mismatch(
filename,
&category,
found,
expected_type,
region,
Some(expr_region),
plain_text(&format!(
"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!"),
)
}
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 */),
// AnonymousFnCall { arity: u8 },
// NamedFnCall(String /* function name */, u8 /* arity */),
// BinOpArg(BinOp, ArgSide),
// BinOpRet(BinOp),
// FloatLiteral,
// IntLiteral,
// NumLiteral,
// InterpolatedStringVar,
// RecordUpdateValue(Lowercase),
// RecordUpdateKeys(Symbol, SendMap<Lowercase, Type>),
todo!("I don't have a message yet for reason {:?}", other)
}
},
}
}
@ -300,10 +417,10 @@ fn add_category(this_is: ReportText, category: &Category) -> ReportText {
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")]),
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")]),
@ -319,7 +436,7 @@ fn add_category(this_is: ReportText, category: &Category) -> ReportText {
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 "),
@ -378,6 +495,7 @@ fn to_circular_report(
];
Report {
title: "TYPE MISMATCH".to_string(),
filename,
text: Concat(lines),
}