better error message for type start

This commit is contained in:
Folkert 2021-03-21 15:50:32 +01:00
parent 22f77ed966
commit 8d80dc97c8
4 changed files with 85 additions and 71 deletions

View file

@ -221,10 +221,7 @@ fn loc_possibly_negative_or_negated_term<'a>(
] ]
} }
fn fail_expr_start_e<'a, T>() -> impl Parser<'a, T, EExpr<'a>> fn fail_expr_start_e<'a, T: 'a>() -> impl Parser<'a, T, EExpr<'a>> {
where
T: 'a,
{
|_arena, state: State<'a>| Err((NoProgress, EExpr::Start(state.line, state.column), state)) |_arena, state: State<'a>| Err((NoProgress, EExpr::Start(state.line, state.column), state))
} }
@ -1019,7 +1016,7 @@ fn parse_expr_operator<'a>(
match expr_to_pattern_help(arena, &call.value) { match expr_to_pattern_help(arena, &call.value) {
Ok(good) => { Ok(good) => {
let (_, mut ann_type, state) = specialize( let parser = specialize(
EExpr::Type, EExpr::Type,
space0_before_e( space0_before_e(
type_annotation::located_help(indented_more), type_annotation::located_help(indented_more),
@ -1027,22 +1024,29 @@ fn parse_expr_operator<'a>(
Type::TSpace, Type::TSpace,
Type::TIndentStart, Type::TIndentStart,
), ),
) );
.parse(arena, state)?;
match parser.parse(arena, state) {
Err((_, fail, state)) => return Err((MadeProgress, fail, state)),
Ok((_, mut ann_type, state)) => {
// put the spaces from after the operator in front of the call // put the spaces from after the operator in front of the call
if !spaces_after_operator.is_empty() { if !spaces_after_operator.is_empty() {
ann_type = arena ann_type = arena.alloc(ann_type.value).with_spaces_before(
.alloc(ann_type.value) spaces_after_operator,
.with_spaces_before(spaces_after_operator, ann_type.region); ann_type.region,
);
} }
let alias_region = Region::span_across(&call.region, &ann_type.region); let alias_region =
Region::span_across(&call.region, &ann_type.region);
let alias = Def::Annotation(Located::at(expr_region, good), ann_type); let alias =
Def::Annotation(Located::at(expr_region, good), ann_type);
(&*arena.alloc(Located::at(alias_region, alias)), state) (&*arena.alloc(Located::at(alias_region, alias)), state)
} }
}
}
Err(_) => { Err(_) => {
// this `:` likely occured inline; treat it as an invalid operator // this `:` likely occured inline; treat it as an invalid operator
let fail = EExpr::BadOperator( let fail = EExpr::BadOperator(

View file

@ -45,6 +45,10 @@ fn tag_union_type<'a>(min_indent: u16) -> impl Parser<'a, TypeAnnotation<'a>, TT
} }
} }
fn fail_type_start<'a, T: 'a>() -> impl Parser<'a, T, Type<'a>> {
|_arena, state: State<'a>| Err((NoProgress, Type::TStart(state.line, state.column), state))
}
fn term<'a>(min_indent: u16) -> impl Parser<'a, Located<TypeAnnotation<'a>>, Type<'a>> { fn term<'a>(min_indent: u16) -> impl Parser<'a, Located<TypeAnnotation<'a>>, Type<'a>> {
map_with_arena!( map_with_arena!(
and!( and!(
@ -54,7 +58,8 @@ fn term<'a>(min_indent: u16) -> impl Parser<'a, Located<TypeAnnotation<'a>>, Typ
loc!(specialize(Type::TRecord, record_type(min_indent))), loc!(specialize(Type::TRecord, record_type(min_indent))),
loc!(specialize(Type::TTagUnion, tag_union_type(min_indent))), loc!(specialize(Type::TTagUnion, tag_union_type(min_indent))),
loc!(applied_type(min_indent)), loc!(applied_type(min_indent)),
loc!(parse_type_variable) loc!(parse_type_variable),
fail_type_start(),
), ),
// Inline alias notation, e.g. [ Nil, Cons a (List a) ] as List a // Inline alias notation, e.g. [ Nil, Cons a (List a) ] as List a
one_of![ one_of![

View file

@ -1781,7 +1781,13 @@ fn to_type_report<'a>(
let doc = alloc.stack(vec![ let doc = alloc.stack(vec![
alloc.reflow(r"I just started parsing a type, but I got stuck here:"), alloc.reflow(r"I just started parsing a type, but I got stuck here:"),
alloc.region_with_subregion(surroundings, region), alloc.region_with_subregion(surroundings, region),
alloc.note("I may be confused by indentation"), alloc.concat(vec![
alloc.reflow(r"I am expecting a type next, like "),
alloc.parser_suggestion("Bool"),
alloc.reflow(r" or "),
alloc.parser_suggestion("List a"),
alloc.reflow("."),
]),
]); ]);
Report { Report {

View file

@ -4474,16 +4474,16 @@ mod test_reporting {
report_problem_as( report_problem_as(
"# comment with a \t\n4", "# comment with a \t\n4",
indoc!( indoc!(
r#" "
TAB CHARACTER TAB CHARACTER
I encountered a tab character I encountered a tab character
1 # comment with a 1 # comment with a \t
^ ^
Tab characters are not allowed. Tab characters are not allowed.
"# "
), ),
) )
} }
@ -4499,12 +4499,14 @@ mod test_reporting {
), ),
indoc!( indoc!(
r#" r#"
BAD TYPE VARIABLE UNFINISHED TYPE
I am expecting a type variable, but I got stuck here: I just started parsing a type, but I got stuck here:
1 f : ( 1 f : (
^ ^
I am expecting a type next, like Bool or List a.
"# "#
), ),
) )
@ -4583,7 +4585,6 @@ mod test_reporting {
} }
#[test] #[test]
#[ignore]
fn type_apply_stray_dot() { fn type_apply_stray_dot() {
// TODO good message // TODO good message
report_problem_as( report_problem_as(
@ -4594,16 +4595,14 @@ mod test_reporting {
), ),
indoc!( indoc!(
r#" r#"
UNFINISHED PARENTHESES UNFINISHED TYPE
I am partway through parsing a type in parentheses, but I got stuck I just started parsing a type, but I got stuck here:
here:
1 f : ( I64 1 f : .
^ ^
I was expecting to see a closing parenthesis before this, so try I am expecting a type next, like Bool or List a.
adding a ) and see if that helps?
"# "#
), ),
) )