Skip to content

Commit e18eaeb

Browse files
authored
parser: fix cast validation issue (#598)
This was incorrect and warning about things that are fine. rel: #597
1 parent f4da3e9 commit e18eaeb

File tree

8 files changed

+162
-54
lines changed

8 files changed

+162
-54
lines changed

crates/squawk_syntax/src/ast/generated/nodes.rs

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/squawk_syntax/src/postgresql.ungram

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ CallExpr =
7070
Expr ArgList
7171

7272
CastExpr =
73-
Expr ColonColon Type
73+
Expr (ColonColon? | 'as') Type
7474

7575
ArrayExpr =
7676
'array' '[' (Expr (',' Expr)*) ']'
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
---
2+
source: crates/squawk_syntax/src/test.rs
3+
input_file: crates/squawk_syntax/test_data/validation/alter_table_ok.sql
4+
---
5+
SOURCE_FILE@0..470
6+
COMMENT@0..66 "-- regression test fo ..."
7+
WHITESPACE@66..67 "\n"
8+
ALTER_TABLE@67..243
9+
ALTER_KW@67..72 "alter"
10+
WHITESPACE@72..73 " "
11+
TABLE_KW@73..78 "table"
12+
WHITESPACE@78..79 " "
13+
RELATION_NAME@79..92
14+
PATH@79..92
15+
PATH@79..85
16+
PATH_SEGMENT@79..85
17+
NAME_REF@79..85
18+
IDENT@79..85 "public"
19+
DOT@85..86 "."
20+
PATH_SEGMENT@86..92
21+
NAME_REF@86..92
22+
IDENT@86..92 "widget"
23+
WHITESPACE@92..95 "\n "
24+
ADD_CONSTRAINT@95..243
25+
ADD_KW@95..98 "add"
26+
WHITESPACE@98..99 " "
27+
CHECK_CONSTRAINT@99..233
28+
CONSTRAINT_KW@99..109 "constraint"
29+
WHITESPACE@109..110 " "
30+
NAME@110..136
31+
IDENT@110..136 "widget_config_schema_ ..."
32+
WHITESPACE@136..137 " "
33+
CHECK_KW@137..142 "check"
34+
WHITESPACE@142..143 " "
35+
L_PAREN@143..144 "("
36+
WHITESPACE@144..149 "\n "
37+
CALL_EXPR@149..229
38+
FIELD_EXPR@149..178
39+
NAME_REF@149..155
40+
IDENT@149..155 "checks"
41+
DOT@155..156 "."
42+
NAME_REF@156..178
43+
IDENT@156..178 "is_widget_config_valid"
44+
ARG_LIST@178..229
45+
L_PAREN@178..179 "("
46+
CAST_EXPR@179..220
47+
LITERAL@179..187
48+
STRING@179..187 "'widget'"
49+
COLON_COLON@187..189
50+
COLON@187..188 ":"
51+
COLON@188..189 ":"
52+
FIELD_EXPR@189..220
53+
NAME_REF@189..201
54+
IDENT@189..201 "custom_types"
55+
DOT@201..202 "."
56+
NAME_REF@202..220
57+
IDENT@202..220 "widget_schema_type"
58+
COMMA@220..221 ","
59+
WHITESPACE@221..222 " "
60+
NAME_REF@222..228
61+
IDENT@222..228 "config"
62+
R_PAREN@228..229 ")"
63+
WHITESPACE@229..232 "\n "
64+
R_PAREN@232..233 ")"
65+
WHITESPACE@233..234 " "
66+
NOT_VALID@234..243
67+
NOT_KW@234..237 "not"
68+
WHITESPACE@237..238 " "
69+
VALID_KW@238..243 "valid"
70+
SEMICOLON@243..244 ";"
71+
WHITESPACE@244..246 "\n\n"
72+
ALTER_TABLE@246..469
73+
ALTER_KW@246..251 "alter"
74+
WHITESPACE@251..252 " "
75+
TABLE_KW@252..257 "table"
76+
WHITESPACE@257..258 " "
77+
RELATION_NAME@258..280
78+
PATH@258..280
79+
PATH@258..264
80+
PATH_SEGMENT@258..264
81+
NAME_REF@258..264
82+
IDENT@258..264 "public"
83+
DOT@264..265 "."
84+
PATH_SEGMENT@265..280
85+
NAME_REF@265..280
86+
IDENT@265..280 "widget_instance"
87+
WHITESPACE@280..283 "\n "
88+
ADD_CONSTRAINT@283..469
89+
ADD_KW@283..286 "add"
90+
WHITESPACE@286..287 " "
91+
CHECK_CONSTRAINT@287..459
92+
CONSTRAINT_KW@287..297 "constraint"
93+
WHITESPACE@297..298 " "
94+
NAME@298..343
95+
IDENT@298..343 "widget_instance_confi ..."
96+
WHITESPACE@343..344 " "
97+
CHECK_KW@344..349 "check"
98+
WHITESPACE@349..350 " "
99+
L_PAREN@350..351 "("
100+
WHITESPACE@351..356 "\n "
101+
CALL_EXPR@356..455
102+
FIELD_EXPR@356..385
103+
NAME_REF@356..362
104+
IDENT@356..362 "checks"
105+
DOT@362..363 "."
106+
NAME_REF@363..385
107+
IDENT@363..385 "is_widget_config_valid"
108+
ARG_LIST@385..455
109+
L_PAREN@385..386 "("
110+
CAST_EXPR@386..436
111+
LITERAL@386..403
112+
STRING@386..403 "'widget_instance'"
113+
COLON_COLON@403..405
114+
COLON@403..404 ":"
115+
COLON@404..405 ":"
116+
FIELD_EXPR@405..436
117+
NAME_REF@405..417
118+
IDENT@405..417 "custom_types"
119+
DOT@417..418 "."
120+
NAME_REF@418..436
121+
IDENT@418..436 "widget_schema_type"
122+
COMMA@436..437 ","
123+
WHITESPACE@437..438 " "
124+
NAME_REF@438..454
125+
IDENT@438..454 "config_overrides"
126+
R_PAREN@454..455 ")"
127+
WHITESPACE@455..458 "\n "
128+
R_PAREN@458..459 ")"
129+
WHITESPACE@459..460 " "
130+
NOT_VALID@460..469
131+
NOT_KW@460..463 "not"
132+
WHITESPACE@463..464 " "
133+
VALID_KW@464..469 "valid"
134+
WHITESPACE@469..470 "\n"

crates/squawk_syntax/src/snapshots/squawk_syntax__test__call_expr_validation.snap

Lines changed: 0 additions & 31 deletions
This file was deleted.

crates/squawk_syntax/src/test.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,17 @@ fn syntaxtest(fixture: Fixture<&str>) {
4343
assert_snapshot!(format!("{}_{}", test_name, parent_dir), buffer);
4444
});
4545

46-
assert_ne!(
47-
errors.len(),
48-
0,
49-
"tests defined in the `syntax/test_data` must have errors."
50-
);
46+
if test_name.ends_with("_ok") {
47+
assert_eq!(
48+
errors.len(),
49+
0,
50+
"tests defined in the `syntax/test_data` ending with `_ok` can't have errors."
51+
);
52+
} else {
53+
assert_ne!(
54+
errors.len(),
55+
0,
56+
"tests defined in the `syntax/test_data` must have errors."
57+
);
58+
}
5159
}

crates/squawk_syntax/src/validation.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ pub(crate) fn validate(root: &SyntaxNode, errors: &mut Vec<SyntaxError>) {
1818
ast::PrefixExpr(it) => validate_prefix_expr(it, errors),
1919
ast::ArrayExpr(it) => validate_array_expr(it, errors),
2020
ast::DropAggregate(it) => validate_drop_aggregate(it, errors),
21-
ast::CallExpr(it) => validate_call_expr(it, errors),
2221
ast::JoinExpr(it) => validate_join_expr(it, errors),
2322
ast::Literal(it) => validate_literal(it, errors),
2423
ast::NonStandardParam(it) => validate_non_standard_param(it, errors),
@@ -28,20 +27,6 @@ pub(crate) fn validate(root: &SyntaxNode, errors: &mut Vec<SyntaxError>) {
2827
}
2928
}
3029

31-
fn validate_call_expr(it: ast::CallExpr, acc: &mut Vec<SyntaxError>) {
32-
let Some(arg_list) = it.arg_list() else {
33-
return;
34-
};
35-
for arg in arg_list.args() {
36-
if let ast::Expr::CastExpr(cast_expr) = &arg {
37-
acc.push(SyntaxError::new(
38-
"Invalid cast. Use `cast`, `treat`, or `::` instead.",
39-
cast_expr.syntax().text_range(),
40-
));
41-
}
42-
}
43-
}
44-
4530
fn validate_create_table(it: ast::CreateTable, acc: &mut Vec<SyntaxError>) {
4631
let Some(arg_list) = it.table_arg_list() else {
4732
return;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- regression test for https://github.com/sbdchd/squawk/issues/597
2+
alter table public.widget
3+
add constraint widget_config_schema_check check (
4+
checks.is_widget_config_valid('widget'::custom_types.widget_schema_type, config)
5+
) not valid;
6+
7+
alter table public.widget_instance
8+
add constraint widget_instance_config_overrides_schema_check check (
9+
checks.is_widget_config_valid('widget_instance'::custom_types.widget_schema_type, config_overrides)
10+
) not valid

crates/squawk_syntax/test_data/validation/call_expr.sql

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)