Skip to content

Commit

Permalink
add test for bad provides
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Jun 25, 2023
1 parent addd513 commit 1b1ab03
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 5 deletions.
2 changes: 1 addition & 1 deletion crates/compiler/parse/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ fn provides_to_package<'a>() -> impl Parser<'a, To<'a>, EProvides<'a>> {
|_, pos| EProvides::Identifier(pos),
map!(lowercase_ident(), To::ExistingPackage)
),
specialize(EProvides::Package, map!(package_name(), To::NewPackage))
specialize(EProvides::PackageName, map!(package_name(), To::NewPackage))
]
}

Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/parse/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub enum EProvides<'a> {
ListStart(Position),
ListEnd(Position),
Identifier(Position),
Package(EPackageName<'a>, Position),
PackageName(EPackageName<'a>, Position),
Space(BadInputError, Position),
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Header(Imports(ListEnd(@89), @67))
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
app "provides-bare-symbol"
packages { pf: "platform/main.roc" }
imports [pf.Task Base64]
provides main to pf
4 changes: 3 additions & 1 deletion crates/compiler/test_syntax/tests/test_snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ mod test_snapshots {
let kind = parts.next().unwrap();
println!("{:?} {:?} {:?}", pass_or_fail, test_name, kind);
assert!(parts.next().is_none());
assert!(kind == "expr" || kind == "moduledefs" || kind == "full");
assert!(["expr", "moduledefs", "full", "header"].contains(&kind));
(pass_or_fail, test_name, kind)
};
let line = format!(" {}/{}.{},", pass_or_fail, test_name, kind);
Expand Down Expand Up @@ -217,6 +217,7 @@ mod test_snapshots {
fail/pattern_in_parens_end_comma.expr,
fail/pattern_in_parens_indent_open.expr,
fail/pattern_in_parens_open.expr,
fail/provides_bare_symbol.header,
fail/record_type_end.expr,
fail/record_type_keyword_field_name.expr,
fail/record_type_missing_comma.expr,
Expand Down Expand Up @@ -341,6 +342,7 @@ mod test_snapshots {
pass/multi_backpassing_in_def.moduledefs,
pass/multi_backpassing_with_apply.expr,
pass/multi_char_string.expr,
pass/multi_param_backpassing.moduledefs,
pass/multiline_string.expr,
pass/multiline_string_in_apply.expr,
pass/multiline_tuple_with_comments.expr,
Expand Down
48 changes: 46 additions & 2 deletions crates/reporting/src/error/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3444,8 +3444,7 @@ fn to_provides_report<'a>(
use roc_parse::parser::EProvides;

match *parse_problem {
EProvides::ListEnd(pos) | // TODO: give this its own error message
EProvides::Identifier(pos) => {
EProvides::ListEnd(pos) | EProvides::Open(pos) | EProvides::Identifier(pos) => {
let surroundings = Region::new(start, pos);
let region = LineColumnRegion::from_pos(lines.convert_pos(pos));

Expand Down Expand Up @@ -3496,6 +3495,51 @@ fn to_provides_report<'a>(

EProvides::Space(error, pos) => to_space_report(alloc, lines, filename, &error, pos),

EProvides::ListStart(pos) => {
let surroundings = Region::new(start, pos);
let region = LineColumnRegion::from_pos(lines.convert_pos(pos));

let doc = alloc.stack([
alloc.reflow(r"I am partway through parsing a header, but I got stuck here:"),
alloc.region_with_subregion(lines.convert_region(surroundings), region),
alloc.concat([
alloc.reflow("I am expecting the "),
alloc.keyword("provides"),
alloc.reflow(" keyword next, like"),
]),
alloc.parser_suggestion("provides [main] to pf").indent(4),
]);

Report {
filename,
doc,
title: "BAD PROVIDES".to_string(),
severity: Severity::RuntimeError,
}
}

EProvides::PackageName(_, pos) => {
let surroundings = Region::new(start, pos);
let region = LineColumnRegion::from_pos(lines.convert_pos(pos));

let doc = alloc.stack([
alloc.reflow(r"I am partway through parsing a package header, but got stuck here:"),
alloc.region_with_subregion(lines.convert_region(surroundings), region),
alloc.concat([
alloc.reflow("I am expecting a package name next, like "),
alloc.parser_suggestion("\"roc/core\""),
alloc.reflow(". Package names must be quoted."),
]),
]);

Report {
filename,
doc,
title: "INVALID PACKAGE NAME".to_string(),
severity: Severity::RuntimeError,
}
}

_ => todo!("unhandled parse error {:?}", parse_problem),
}
}
Expand Down

0 comments on commit 1b1ab03

Please sign in to comment.