mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-03 00:24:34 +00:00
Implement return keyword
This commit is contained in:
parent
20a539a96d
commit
b3e60f9d3a
39 changed files with 594 additions and 80 deletions
|
@ -1346,6 +1346,32 @@ pub fn can_problem<'b>(
|
|||
doc = report.doc;
|
||||
title = report.title;
|
||||
}
|
||||
|
||||
Problem::ReturnOutsideOfFunction { region } => {
|
||||
doc = alloc.stack([
|
||||
alloc.concat([
|
||||
alloc.reflow("This "),
|
||||
alloc.keyword("return"),
|
||||
alloc.reflow(" statement doesn't belong to a function:"),
|
||||
]),
|
||||
alloc.region(lines.convert_region(region), severity),
|
||||
]);
|
||||
|
||||
title = "RETURN OUTSIDE OF FUNCTION".to_string();
|
||||
}
|
||||
|
||||
Problem::StatementsAfterReturn { region } => {
|
||||
doc = alloc.stack([
|
||||
alloc.concat([
|
||||
alloc.reflow("This code won't run because it follows a "),
|
||||
alloc.keyword("return"),
|
||||
alloc.reflow(" statement:"),
|
||||
]),
|
||||
alloc.region(lines.convert_region(region), severity),
|
||||
]);
|
||||
|
||||
title = "UNREACHABLE CODE".to_string();
|
||||
}
|
||||
};
|
||||
|
||||
Report {
|
||||
|
@ -2522,6 +2548,18 @@ fn pretty_runtime_error<'b>(
|
|||
|
||||
title = "OPTIONAL FIELD IN RECORD BUILDER";
|
||||
}
|
||||
RuntimeError::ReturnOutsideOfFunction(region) => {
|
||||
doc = alloc.stack([
|
||||
alloc.concat([
|
||||
alloc.reflow("The "),
|
||||
alloc.keyword("return"),
|
||||
alloc.reflow(" keyword can only be used in functions."),
|
||||
]),
|
||||
alloc.region(lines.convert_region(region), severity),
|
||||
]);
|
||||
|
||||
title = "RETURN OUTSIDE OF FUNCTION";
|
||||
}
|
||||
}
|
||||
|
||||
(doc, title)
|
||||
|
|
|
@ -918,7 +918,6 @@ fn to_expr_report<'b>(
|
|||
alloc.reflow("But I need every "),
|
||||
alloc.keyword("expect"),
|
||||
alloc.reflow(" condition to evaluate to a "),
|
||||
alloc.type_str("Bool"),
|
||||
alloc.reflow("—either "),
|
||||
alloc.tag("Bool.true".into()),
|
||||
alloc.reflow(" or "),
|
||||
|
@ -958,7 +957,6 @@ fn to_expr_report<'b>(
|
|||
alloc.reflow("But I need every "),
|
||||
alloc.keyword("if"),
|
||||
alloc.reflow(" condition to evaluate to a "),
|
||||
alloc.type_str("Bool"),
|
||||
alloc.reflow("—either "),
|
||||
alloc.tag("Bool.true".into()),
|
||||
alloc.reflow(" or "),
|
||||
|
@ -997,7 +995,6 @@ fn to_expr_report<'b>(
|
|||
alloc.reflow("But I need every "),
|
||||
alloc.keyword("if"),
|
||||
alloc.reflow(" guard condition to evaluate to a "),
|
||||
alloc.type_str("Bool"),
|
||||
alloc.reflow("—either "),
|
||||
alloc.tag("Bool.true".into()),
|
||||
alloc.reflow(" or "),
|
||||
|
@ -1645,6 +1642,44 @@ fn to_expr_report<'b>(
|
|||
unimplemented!("record default field is not implemented yet")
|
||||
}
|
||||
Reason::ImportParams(_) => unreachable!(),
|
||||
Reason::Return => {
|
||||
let problem = alloc.concat([
|
||||
alloc.text("This "),
|
||||
alloc.keyword("return"),
|
||||
alloc.reflow(
|
||||
" statement doesn't match the return type of its enclosing function:",
|
||||
),
|
||||
]);
|
||||
|
||||
let comparison = type_comparison(
|
||||
alloc,
|
||||
found,
|
||||
expected_type,
|
||||
ExpectationContext::Arbitrary,
|
||||
add_category(alloc, alloc.text("It is"), &category),
|
||||
alloc.concat([
|
||||
alloc.reflow("But I need every "),
|
||||
alloc.keyword("return"),
|
||||
alloc.reflow(" statement in that function to return:"),
|
||||
]),
|
||||
None,
|
||||
);
|
||||
|
||||
Report {
|
||||
title: "TYPE MISMATCH".to_string(),
|
||||
filename,
|
||||
doc: alloc.stack([
|
||||
problem,
|
||||
alloc.region_with_subregion(
|
||||
lines.convert_region(region),
|
||||
lines.convert_region(expr_region),
|
||||
severity,
|
||||
),
|
||||
comparison,
|
||||
]),
|
||||
severity,
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -1983,6 +2018,15 @@ fn format_category<'b>(
|
|||
alloc.concat([this_is, alloc.text(" a dbg statement")]),
|
||||
alloc.text(" of type:"),
|
||||
),
|
||||
Return => (
|
||||
alloc.concat([
|
||||
this_is,
|
||||
alloc.reflow(" a "),
|
||||
alloc.keyword("return"),
|
||||
alloc.reflow(" statement"),
|
||||
]),
|
||||
alloc.text(" of type:"),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue