repl treats whens as incomplete until blank line

This commit is contained in:
Richard Feldman 2022-10-30 13:38:22 -04:00
parent fccc2ed497
commit eebf973f11
No known key found for this signature in database
GPG key ID: F1F21AA5B1D9E43B
2 changed files with 26 additions and 17 deletions

View file

@ -442,6 +442,11 @@ pub fn is_incomplete(input: &str) -> bool {
//
// So it's Incomplete until you've pressed Enter again (causing the input to end in "\n")
ParseOutcome::ValueDef(ValueDef::Annotation(_, _)) if !input.ends_with('\n') => true,
ParseOutcome::Expr(Expr::When(_, _)) => {
// There might be lots of `when` branches, so don't assume the user is done entering
// them until they enter a blank line!
!input.ends_with('\n')
}
ParseOutcome::Empty
| ParseOutcome::Help
| ParseOutcome::Exit

View file

@ -43,26 +43,29 @@ fn annotated_body() {
#[test]
fn exhaustiveness_problem() {
let mut input = "t : [A, B, C]".to_string();
incomplete(&mut input);
input.push_str("t = A");
incomplete(&mut input);
let mut state = ReplState::new();
complete(&input, &mut state, Ok(("A : [A]*", "t")));
// Enter an annotated tag union to make it exhaustive
{
let mut input = "t : [A, B, C]".to_string();
input.push_str("when t is");
incomplete(&mut input);
incomplete(&mut input);
input.push_str(" A -> 1");
incomplete(&mut input);
input.push_str("t = A");
const EXPECTED_ERROR: &str = indoc!(
r#"
complete(&input, &mut state, Ok(("A : [A, B, C]", "t")));
}
// Run a `when` on it that isn't exhaustive
{
let mut input = "when t is".to_string();
incomplete(&mut input);
input.push_str(" A -> 1");
incomplete(&mut input);
const EXPECTED_ERROR: &str = indoc!(
r#"
UNSAFE PATTERN
This when does not cover all the possibilities:
@ -77,9 +80,10 @@ fn exhaustiveness_problem() {
I would have to crash if I saw one of those! Add branches for them!
"#
);
);
error(&input, &mut state, EXPECTED_ERROR.to_string());
error(&input, &mut state, EXPECTED_ERROR.to_string());
}
}
#[test]