Implement return keyword

This commit is contained in:
Sam Mohr 2024-10-20 04:50:12 -07:00
parent 20a539a96d
commit b3e60f9d3a
No known key found for this signature in database
GPG key ID: EA41D161A3C1BC99
39 changed files with 594 additions and 80 deletions

View file

@ -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)

View file

@ -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:"),
),
}
}