Skip to content

Commit

Permalink
fix: Don't panic on invalid take (PRQL#1338)
Browse files Browse the repository at this point in the history
  • Loading branch information
max-sixty committed Dec 27, 2022
1 parent 896fe21 commit 3a1adf0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
9 changes: 8 additions & 1 deletion prql-compiler/src/semantic/transforms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ pub fn cast_transform(resolver: &mut Resolver, closure: Closure) -> Result<Resul
let range = match expr.kind {
ExprKind::Literal(Literal::Integer(n)) => Range::from_ints(None, Some(n)),
ExprKind::Range(range) => range,
_ => unimplemented!("`take` range: {expr}"),
_ => bail!(Error::new(Reason::Expected {
who: Some("`take`".to_string()),
expected: "int or range".to_string(),
found: expr.to_string(),
})
// Possibly this should refer to the item after the `take` where
// one exists?
.with_span(expr.span)),
};

(TransformKind::Take { range }, tbl)
Expand Down
53 changes: 34 additions & 19 deletions prql-compiler/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1797,25 +1797,6 @@ fn test_casting() {
);
}

#[test]
/// Start testing some error messages. This can hopefully be expanded significantly.
fn test_errors() {
assert_display_snapshot!(compile(r###"
from x
select a
select b
"###).unwrap_err(),
@r###"
Error:
╭─[:4:12]
4 │ select b
· ┬
· ╰── Unknown name b
───╯
"###);
}

#[test]
fn test_toposort() {
// #1183
Expand Down Expand Up @@ -2351,3 +2332,37 @@ fn test_closures_and_pipelines() {
"###
);
}

#[test]
/// Start testing some error messages. This can hopefully be expanded significantly.
// It's also fine to put errors by the things that they're testing.
fn test_errors() {
assert_display_snapshot!(compile(r###"
from x
select a
select b
"###).unwrap_err(),
@r###"
Error:
╭─[:4:12]
4 │ select b
· ┬
· ╰── Unknown name b
───╯
"###);

assert_display_snapshot!(compile(r###"
from employees
take 1.8
"###).unwrap_err(),
@r###"
Error:
╭─[:3:10]
3 │ take 1.8
· ─┬─
· ╰─── `take` expected int or range, but found 1.8
───╯
"###);
}

0 comments on commit 3a1adf0

Please sign in to comment.