Skip to content

Commit

Permalink
Report more syntax errors
Browse files Browse the repository at this point in the history
Summary: There are some checks for syntax errors in `rust_parser_errors.rs` that were only being run when there were no errors building the full-fidelity parse tree, even though the lowerer was always being run and also producing syntax errors. Now always run both checks.

Reviewed By: CatherineGasnier

Differential Revision: D64111712

fbshipit-source-id: d59320ff0e28182b343c4c6310260f08be7b09fc
  • Loading branch information
Scott Owens authored and facebook-github-bot committed Oct 24, 2024
1 parent e848c96 commit 0e55fbf
Show file tree
Hide file tree
Showing 24 changed files with 124 additions and 20 deletions.
46 changes: 26 additions & 20 deletions hphp/hack/src/parser/aast_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,30 @@ impl<'src> AastParser {
aast: Option<&mut Program<(), ()>>,
default_experimental_features: HashSet<experimental_features::FeatureName>,
) -> Vec<SyntaxError> {
let find_errors = |hhi_mode: bool| -> Vec<SyntaxError> {
let mut errors = tree.errors().into_iter().cloned().collect::<Vec<_>>();
let mut hhi_mode = false;
let (should_check_for_extra_errors, mut errors) = match env.mode {
NamespaceMode::ForCodegen => {
(true, tree.errors().into_iter().cloned().collect::<Vec<_>>())
}
NamespaceMode::ForTypecheck => {
let first_error = tree
.errors()
.into_iter()
.take(1)
.cloned()
.collect::<Vec<_>>();
if env.quick_mode {
(false, first_error)
} else {
hhi_mode = indexed_source_text
.source_text()
.file_path()
.has_extension("hhi");
(true, first_error)
}
}
};
if should_check_for_extra_errors {
let (parse_errors, uses_readonly) = parse_errors_with_text(
tree,
indexed_source_text.clone(),
Expand All @@ -255,7 +277,6 @@ impl<'src> AastParser {
);
errors.extend(parse_errors);
errors.sort_by(SyntaxError::compare_offset);

let mut empty_program = Program(vec![]);
let aast = aast.unwrap_or(&mut empty_program);
if uses_readonly && !env.parser_options.no_parser_readonly_check {
Expand All @@ -269,23 +290,8 @@ impl<'src> AastParser {
errors.extend(expression_tree_check::check_splices(aast));
errors.extend(coeffects_check::check_program(aast, env.mode));
errors
};
match env.mode {
NamespaceMode::ForCodegen => find_errors(false /* hhi_mode */),
NamespaceMode::ForTypecheck => {
let first_error = tree.errors().into_iter().next();
match first_error {
None if !env.quick_mode => {
let is_hhi = indexed_source_text
.source_text()
.file_path()
.has_extension("hhi");
find_errors(is_hhi)
}
None => vec![],
Some(e) => vec![e.clone()],
}
}
} else {
errors
}
}

Expand Down
2 changes: 2 additions & 0 deletions hphp/hack/test/reserved/module_newtype.php.hh.exp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ ERROR: File "module_newtype.php--Static.php", line 3, characters 16-16:
A name is expected here. (Parsing[1002])
ERROR: File "module_newtype.php--Static.php", line 3, characters 16-21:
Encountered unexpected text `Static`, was expecting a type hint. (Parsing[1002])
ERROR: File "module_newtype.php--Static.php", line 3, characters 26-28:
You cannot use this syntax on the left-hand side of an assignment. (Parsing[1002])
ERROR: File "module_newtype.php--Static.php", line 3, characters 23-24:
Hack does not support top level statements. Use the `__EntryPoint` attribute on a function instead (Naming[2128])
ERROR: File "module_newtype.php--Static.php", line 3, characters 26-35:
Expand Down
20 changes: 20 additions & 0 deletions hphp/hack/test/reserved/newtype.php.hh.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ ERROR: File "newtype.php--Arraykey.php", line 2, characters 9-16:
Cannot use `Arraykey` as a type name as it is reserved (Parsing[1002])
ERROR: File "newtype.php--Bool.php", line 2, characters 9-9:
A semicolon `;` is expected here. (Parsing[1002])
ERROR: File "newtype.php--Bool.php", line 2, characters 9-12:
You cannot use this syntax on the left-hand side of an assignment. (Parsing[1002])
ERROR: File "newtype.php--Bool.php", line 2, characters 1-7:
Hack does not support top level statements. Use the `__EntryPoint` attribute on a function instead (Naming[2128])
ERROR: File "newtype.php--Bool.php", line 2, characters 9-19:
Expand All @@ -14,6 +16,8 @@ ERROR: File "newtype.php--Dynamic.php", line 2, characters 9-15:
Cannot use `Dynamic` as a type name as it is reserved (Parsing[1002])
ERROR: File "newtype.php--False.php", line 2, characters 9-9:
A semicolon `;` is expected here. (Parsing[1002])
ERROR: File "newtype.php--False.php", line 2, characters 9-13:
You cannot use this syntax on the left-hand side of an assignment. (Parsing[1002])
ERROR: File "newtype.php--False.php", line 2, characters 1-7:
Hack does not support top level statements. Use the `__EntryPoint` attribute on a function instead (Naming[2128])
ERROR: File "newtype.php--False.php", line 2, characters 9-20:
Expand All @@ -22,6 +26,8 @@ ERROR: File "newtype.php--Float.php", line 2, characters 9-13:
Cannot use `Float` as a type name as it is reserved (Parsing[1002])
ERROR: File "newtype.php--Int.php", line 2, characters 9-9:
A semicolon `;` is expected here. (Parsing[1002])
ERROR: File "newtype.php--Int.php", line 2, characters 9-11:
You cannot use this syntax on the left-hand side of an assignment. (Parsing[1002])
ERROR: File "newtype.php--Int.php", line 2, characters 1-7:
Hack does not support top level statements. Use the `__EntryPoint` attribute on a function instead (Naming[2128])
ERROR: File "newtype.php--Int.php", line 2, characters 9-18:
Expand All @@ -36,6 +42,8 @@ ERROR: File "newtype.php--Nothing.php", line 2, characters 9-15:
Cannot use `Nothing` as a type name as it is reserved (Parsing[1002])
ERROR: File "newtype.php--Null.php", line 2, characters 9-9:
A semicolon `;` is expected here. (Parsing[1002])
ERROR: File "newtype.php--Null.php", line 2, characters 9-12:
You cannot use this syntax on the left-hand side of an assignment. (Parsing[1002])
ERROR: File "newtype.php--Null.php", line 2, characters 1-7:
Hack does not support top level statements. Use the `__EntryPoint` attribute on a function instead (Naming[2128])
ERROR: File "newtype.php--Null.php", line 2, characters 9-19:
Expand All @@ -44,6 +52,8 @@ ERROR: File "newtype.php--Num.php", line 2, characters 9-11:
Cannot use `Num` as a type name as it is reserved (Parsing[1002])
ERROR: File "newtype.php--Parent.php", line 2, characters 9-9:
A semicolon `;` is expected here. (Parsing[1002])
ERROR: File "newtype.php--Parent.php", line 2, characters 9-14:
You cannot use this syntax on the left-hand side of an assignment. (Parsing[1002])
ERROR: File "newtype.php--Parent.php", line 2, characters 1-7:
Hack does not support top level statements. Use the `__EntryPoint` attribute on a function instead (Naming[2128])
ERROR: File "newtype.php--Parent.php", line 2, characters 9-21:
Expand All @@ -52,18 +62,24 @@ ERROR: File "newtype.php--Resource.php", line 2, characters 9-16:
Cannot use `Resource` as a type name as it is reserved (Parsing[1002])
ERROR: File "newtype.php--Self.php", line 2, characters 9-9:
A semicolon `;` is expected here. (Parsing[1002])
ERROR: File "newtype.php--Self.php", line 2, characters 9-12:
You cannot use this syntax on the left-hand side of an assignment. (Parsing[1002])
ERROR: File "newtype.php--Self.php", line 2, characters 1-7:
Hack does not support top level statements. Use the `__EntryPoint` attribute on a function instead (Naming[2128])
ERROR: File "newtype.php--Self.php", line 2, characters 9-19:
Hack does not support top level statements. Use the `__EntryPoint` attribute on a function instead (Naming[2128])
ERROR: File "newtype.php--Static.php", line 2, characters 9-9:
A semicolon `;` is expected here. (Parsing[1002])
ERROR: File "newtype.php--Static.php", line 2, characters 9-14:
You cannot use this syntax on the left-hand side of an assignment. (Parsing[1002])
ERROR: File "newtype.php--Static.php", line 2, characters 1-7:
Hack does not support top level statements. Use the `__EntryPoint` attribute on a function instead (Naming[2128])
ERROR: File "newtype.php--Static.php", line 2, characters 9-21:
Hack does not support top level statements. Use the `__EntryPoint` attribute on a function instead (Naming[2128])
ERROR: File "newtype.php--String.php", line 2, characters 9-9:
A semicolon `;` is expected here. (Parsing[1002])
ERROR: File "newtype.php--String.php", line 2, characters 9-14:
You cannot use this syntax on the left-hand side of an assignment. (Parsing[1002])
ERROR: File "newtype.php--String.php", line 2, characters 1-7:
Hack does not support top level statements. Use the `__EntryPoint` attribute on a function instead (Naming[2128])
ERROR: File "newtype.php--String.php", line 2, characters 9-21:
Expand All @@ -72,6 +88,8 @@ ERROR: File "newtype.php--This.php", line 2, characters 9-12:
Cannot use `This` as a type name as it is reserved (Parsing[1002])
ERROR: File "newtype.php--True.php", line 2, characters 9-9:
A semicolon `;` is expected here. (Parsing[1002])
ERROR: File "newtype.php--True.php", line 2, characters 9-12:
You cannot use this syntax on the left-hand side of an assignment. (Parsing[1002])
ERROR: File "newtype.php--True.php", line 2, characters 1-7:
Hack does not support top level statements. Use the `__EntryPoint` attribute on a function instead (Naming[2128])
ERROR: File "newtype.php--True.php", line 2, characters 9-19:
Expand All @@ -80,6 +98,8 @@ ERROR: File "newtype.php--Varray.php", line 2, characters 9-14:
Cannot use `Varray` as a type name as it is reserved (Parsing[1002])
ERROR: File "newtype.php--Void.php", line 2, characters 9-9:
A semicolon `;` is expected here. (Parsing[1002])
ERROR: File "newtype.php--Void.php", line 2, characters 9-12:
You cannot use this syntax on the left-hand side of an assignment. (Parsing[1002])
ERROR: File "newtype.php--Void.php", line 2, characters 1-7:
Hack does not support top level statements. Use the `__EntryPoint` attribute on a function instead (Naming[2128])
ERROR: File "newtype.php--Void.php", line 2, characters 9-19:
Expand Down
20 changes: 20 additions & 0 deletions hphp/hack/test/reserved/type.php.hh.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ ERROR: File "type.php--Arraykey.php", line 2, characters 6-13:
Cannot use `Arraykey` as a type name as it is reserved (Parsing[1002])
ERROR: File "type.php--Bool.php", line 2, characters 6-6:
A semicolon `;` is expected here. (Parsing[1002])
ERROR: File "type.php--Bool.php", line 2, characters 6-9:
You cannot use this syntax on the left-hand side of an assignment. (Parsing[1002])
ERROR: File "type.php--Bool.php", line 2, characters 1-4:
Hack does not support top level statements. Use the `__EntryPoint` attribute on a function instead (Naming[2128])
ERROR: File "type.php--Bool.php", line 2, characters 6-16:
Expand All @@ -14,6 +16,8 @@ ERROR: File "type.php--Dynamic.php", line 2, characters 6-12:
Cannot use `Dynamic` as a type name as it is reserved (Parsing[1002])
ERROR: File "type.php--False.php", line 2, characters 6-6:
A semicolon `;` is expected here. (Parsing[1002])
ERROR: File "type.php--False.php", line 2, characters 6-10:
You cannot use this syntax on the left-hand side of an assignment. (Parsing[1002])
ERROR: File "type.php--False.php", line 2, characters 1-4:
Hack does not support top level statements. Use the `__EntryPoint` attribute on a function instead (Naming[2128])
ERROR: File "type.php--False.php", line 2, characters 6-17:
Expand All @@ -22,6 +26,8 @@ ERROR: File "type.php--Float.php", line 2, characters 6-10:
Cannot use `Float` as a type name as it is reserved (Parsing[1002])
ERROR: File "type.php--Int.php", line 2, characters 6-6:
A semicolon `;` is expected here. (Parsing[1002])
ERROR: File "type.php--Int.php", line 2, characters 6-8:
You cannot use this syntax on the left-hand side of an assignment. (Parsing[1002])
ERROR: File "type.php--Int.php", line 2, characters 1-4:
Hack does not support top level statements. Use the `__EntryPoint` attribute on a function instead (Naming[2128])
ERROR: File "type.php--Int.php", line 2, characters 6-15:
Expand All @@ -36,6 +42,8 @@ ERROR: File "type.php--Nothing.php", line 2, characters 6-12:
Cannot use `Nothing` as a type name as it is reserved (Parsing[1002])
ERROR: File "type.php--Null.php", line 2, characters 6-6:
A semicolon `;` is expected here. (Parsing[1002])
ERROR: File "type.php--Null.php", line 2, characters 6-9:
You cannot use this syntax on the left-hand side of an assignment. (Parsing[1002])
ERROR: File "type.php--Null.php", line 2, characters 1-4:
Hack does not support top level statements. Use the `__EntryPoint` attribute on a function instead (Naming[2128])
ERROR: File "type.php--Null.php", line 2, characters 6-16:
Expand All @@ -44,6 +52,8 @@ ERROR: File "type.php--Num.php", line 2, characters 6-8:
Cannot use `Num` as a type name as it is reserved (Parsing[1002])
ERROR: File "type.php--Parent.php", line 2, characters 6-6:
A semicolon `;` is expected here. (Parsing[1002])
ERROR: File "type.php--Parent.php", line 2, characters 6-11:
You cannot use this syntax on the left-hand side of an assignment. (Parsing[1002])
ERROR: File "type.php--Parent.php", line 2, characters 1-4:
Hack does not support top level statements. Use the `__EntryPoint` attribute on a function instead (Naming[2128])
ERROR: File "type.php--Parent.php", line 2, characters 6-18:
Expand All @@ -52,18 +62,24 @@ ERROR: File "type.php--Resource.php", line 2, characters 6-13:
Cannot use `Resource` as a type name as it is reserved (Parsing[1002])
ERROR: File "type.php--Self.php", line 2, characters 6-6:
A semicolon `;` is expected here. (Parsing[1002])
ERROR: File "type.php--Self.php", line 2, characters 6-9:
You cannot use this syntax on the left-hand side of an assignment. (Parsing[1002])
ERROR: File "type.php--Self.php", line 2, characters 1-4:
Hack does not support top level statements. Use the `__EntryPoint` attribute on a function instead (Naming[2128])
ERROR: File "type.php--Self.php", line 2, characters 6-16:
Hack does not support top level statements. Use the `__EntryPoint` attribute on a function instead (Naming[2128])
ERROR: File "type.php--Static.php", line 2, characters 6-6:
A semicolon `;` is expected here. (Parsing[1002])
ERROR: File "type.php--Static.php", line 2, characters 6-11:
You cannot use this syntax on the left-hand side of an assignment. (Parsing[1002])
ERROR: File "type.php--Static.php", line 2, characters 1-4:
Hack does not support top level statements. Use the `__EntryPoint` attribute on a function instead (Naming[2128])
ERROR: File "type.php--Static.php", line 2, characters 6-18:
Hack does not support top level statements. Use the `__EntryPoint` attribute on a function instead (Naming[2128])
ERROR: File "type.php--String.php", line 2, characters 6-6:
A semicolon `;` is expected here. (Parsing[1002])
ERROR: File "type.php--String.php", line 2, characters 6-11:
You cannot use this syntax on the left-hand side of an assignment. (Parsing[1002])
ERROR: File "type.php--String.php", line 2, characters 1-4:
Hack does not support top level statements. Use the `__EntryPoint` attribute on a function instead (Naming[2128])
ERROR: File "type.php--String.php", line 2, characters 6-18:
Expand All @@ -72,6 +88,8 @@ ERROR: File "type.php--This.php", line 2, characters 6-9:
Cannot use `This` as a type name as it is reserved (Parsing[1002])
ERROR: File "type.php--True.php", line 2, characters 6-6:
A semicolon `;` is expected here. (Parsing[1002])
ERROR: File "type.php--True.php", line 2, characters 6-9:
You cannot use this syntax on the left-hand side of an assignment. (Parsing[1002])
ERROR: File "type.php--True.php", line 2, characters 1-4:
Hack does not support top level statements. Use the `__EntryPoint` attribute on a function instead (Naming[2128])
ERROR: File "type.php--True.php", line 2, characters 6-16:
Expand All @@ -80,6 +98,8 @@ ERROR: File "type.php--Varray.php", line 2, characters 6-11:
Cannot use `Varray` as a type name as it is reserved (Parsing[1002])
ERROR: File "type.php--Void.php", line 2, characters 6-6:
A semicolon `;` is expected here. (Parsing[1002])
ERROR: File "type.php--Void.php", line 2, characters 6-9:
You cannot use this syntax on the left-hand side of an assignment. (Parsing[1002])
ERROR: File "type.php--Void.php", line 2, characters 1-4:
Hack does not support top level statements. Use the `__EntryPoint` attribute on a function instead (Naming[2128])
ERROR: File "type.php--Void.php", line 2, characters 6-16:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
ERROR: File "unpack_call10_unset.php", line 6, characters 9-11:
You cannot use this syntax in an `unset()` expression. (Parsing[1002])
ERROR: File "unpack_call10_unset.php", line 6, characters 12-12:
An expression is expected here. (Parsing[1002])
ERROR: File "unpack_call10_unset.php", line 6, characters 9-11:
Expand Down
2 changes: 2 additions & 0 deletions hphp/hack/test/typecheck/bad_function_hint.php.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ ERROR: File "bad_function_hint.php", line 12, characters 15-22:
A type specifier is expected here. (Parsing[1002])
ERROR: File "bad_function_hint.php", line 12, characters 15-22:
Encountered unexpected text `function`, was expecting a type hint. (Parsing[1002])
ERROR: File "bad_function_hint.php", line 12, characters 25-42:
Cannot use collection initialization for non-collection class. (Parsing[1002])
2 changes: 2 additions & 0 deletions hphp/hack/test/typecheck/class_top_var.php.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ ERROR: File "class_top_var.php", line 7, characters 3-5:
Encountered unexpected text `var`, was expecting a class element. (Parsing[1002])
ERROR: File "class_top_var.php", line 7, characters 7-7:
A class member, method, type, trait usage, trait require, xhp attribute, xhp use, or xhp category is expected here. (Parsing[1002])
ERROR: File "class_top_var.php", line 7, characters 7-11:
Property declarations require a visibility modifier such as `public`, `private` or `protected`. (Parsing[1002])
ERROR: File "class_top_var.php", line 7, characters 7-10:
Please add a type hint `public SomeType $foo` (Naming[2001])
ERROR: File "class_top_var.php", line 3, characters 19-35:
Expand Down
4 changes: 4 additions & 0 deletions hphp/hack/test/typecheck/conditional_function_decl.php.exp
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
ERROR: File "conditional_function_decl.php", line 13, characters 3-20:
Declarations are not supported outside global scope (Parsing[1002])
ERROR: File "conditional_function_decl.php", line 13, characters 3-23:
Encountered unexpected text `function f(): void {}`, was expecting a statement. (Parsing[1002])
ERROR: File "conditional_function_decl.php", line 13, characters 31-31:
Inline function definitions are not supported in Hack (Parsing[1002])
ERROR: File "conditional_function_decl.php", line 15, characters 3-20:
Declarations are not supported outside global scope (Parsing[1002])
ERROR: File "conditional_function_decl.php", line 12, characters 1-84:
Hack does not support top level statements. Use the `__EntryPoint` attribute on a function instead (Naming[2128])
ERROR: File "conditional_function_decl.php", line 19, characters 12-12:
Expand Down
2 changes: 2 additions & 0 deletions hphp/hack/test/typecheck/decl_nested_fun_stmt.php.exp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
ERROR: File "decl_nested_fun_stmt.php", line 13, characters 3-16:
Declarations are not supported outside global scope (Parsing[1002])
ERROR: File "decl_nested_fun_stmt.php", line 13, characters 3-20:
Encountered unexpected text `function g($a) { }`, was expecting a statement. (Parsing[1002])
ERROR: File "decl_nested_fun_stmt.php", line 13, characters 21-21:
Expand Down
2 changes: 2 additions & 0 deletions hphp/hack/test/typecheck/decl_unclosed_xhp.php.exp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
ERROR: File "decl_unclosed_xhp.php", line 13, characters 3-7:
XHP: mismatched tag: `` not the same as `a` (Parsing[1002])
ERROR: File "decl_unclosed_xhp.php", line 15, characters 1-1:
A closing XHP tag is expected here. (Parsing[1002])
ERROR: File "decl_unclosed_xhp.php", line 13, characters 4-4:
Expand Down
2 changes: 2 additions & 0 deletions hphp/hack/test/typecheck/decl_unclosed_xhp_nested.php.exp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
ERROR: File "decl_unclosed_xhp_nested.php", line 14, characters 12-22:
XHP: mismatched tag: `` not the same as `a` (Parsing[1002])
ERROR: File "decl_unclosed_xhp_nested.php", line 17, characters 1-1:
A closing XHP tag is expected here. (Parsing[1002])
ERROR: File "decl_unclosed_xhp_nested.php", line 14, characters 13-13:
Expand Down
2 changes: 2 additions & 0 deletions hphp/hack/test/typecheck/dict_literal_mixed_brace2.php.exp
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
ERROR: File "dict_literal_mixed_brace2.php", line 4, characters 8-30:
Initializers of `vec`, `dict` and `keyset` should use `[...]` instead of `{...}`. (Parsing[1002])
ERROR: File "dict_literal_mixed_brace2.php", line 4, characters 31-31:
A right brace `}` is expected here. (Parsing[1002])
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
ERROR: File "generic_visitor.php", line 7, characters 18-18:
A semicolon `;` is expected here. (Parsing[1002])
ERROR: File "generic_visitor.php", line 7, characters 18-20:
Expression trees must provide the DSL name. (Parsing[1002])
ERROR: File "generic_visitor.php", line 7, characters 3-12:
Unbound name: `ExampleDsl` (a global function) (Naming[2049])
ERROR: File "generic_visitor.php", line 7, characters 18-18:
Expand Down
Loading

0 comments on commit 0e55fbf

Please sign in to comment.