-
-
Notifications
You must be signed in to change notification settings - Fork 482
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(traverse): speed up tests (#4538)
Closes #4537. `oxc_traverse`'s tests to ensure code which would be undefined behavior will not compile were using `trybuild`. This is a thorough way to run these tests, but requires running a lengthy compilation each time tests run. Implement these tests as `compile_fail` doc tests instead. This is not quite as thorough - now only testing that they don't compile, rather than also *why* they don't compile - but acceptable given the outsized cost of doing it "properly".
- Loading branch information
1 parent
d6974d4
commit e6a8af6
Showing
12 changed files
with
86 additions
and
178 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#![cfg(doctest)] | ||
|
||
//! Tests to ensure lifetimes prevent references to escape visitor functions. | ||
//! If they could, it'd allow aliasing, which would be undefined behavior. | ||
//! | ||
//! These tests were originally implemented using `trybuild` crate, | ||
//! but it disproportionately hurt time taken to run tests. | ||
//! So using `compile_fail` doc tests instead. | ||
//! <https://github.com/oxc-project/oxc/issues/4537> | ||
/** | ||
```compile_fail | ||
use oxc_ast::ast::IdentifierReference; | ||
use oxc_traverse::{Ancestor, Traverse, TraverseCtx}; | ||
struct Trans<'a, 'b> { | ||
ancestor: Option<&'b Ancestor<'a>>, | ||
} | ||
impl<'a, 'b> Traverse<'a> for Trans<'a, 'b> { | ||
fn enter_identifier_reference( | ||
&mut self, | ||
_node: &mut IdentifierReference<'a>, | ||
ctx: &mut TraverseCtx<'a>, | ||
) { | ||
self.ancestor = Some(ctx.parent()); | ||
} | ||
} | ||
``` | ||
*/ | ||
const CANNOT_HOLD_ONTO_ANCESTOR: () = (); | ||
|
||
/** | ||
```compile_fail | ||
use oxc_ast::ast::IdentifierReference; | ||
use oxc_traverse::{ancestor::ProgramWithoutDirectives, Ancestor, Traverse, TraverseCtx}; | ||
struct Trans<'a, 'b> { | ||
program: Option<&'b ProgramWithoutDirectives<'a>>, | ||
} | ||
impl<'a, 'b> Traverse<'a> for Trans<'a, 'b> { | ||
fn enter_identifier_reference( | ||
&mut self, | ||
_node: &mut IdentifierReference<'a>, | ||
ctx: &mut TraverseCtx<'a>, | ||
) { | ||
if let Ancestor::ProgramDirectives(program) = ctx.parent() { | ||
self.program = Some(program); | ||
} | ||
} | ||
} | ||
``` | ||
*/ | ||
const CANNOT_HOLD_ONTO_ANCESTOR_NODE: () = (); | ||
|
||
/** | ||
```compile_fail | ||
use oxc_ast::ast::{IdentifierReference, Statement}; | ||
use oxc_traverse::{Ancestor, Traverse, TraverseCtx}; | ||
struct Trans<'a, 'b> { | ||
stmt: Option<&'b Statement<'a>>, | ||
} | ||
impl<'a, 'b> Traverse<'a> for Trans<'a, 'b> { | ||
fn enter_identifier_reference( | ||
&mut self, | ||
_node: &mut IdentifierReference<'a>, | ||
ctx: &mut TraverseCtx<'a>, | ||
) { | ||
if let Ancestor::ProgramDirectives(program) = ctx.parent() { | ||
let body = program.body(); | ||
let stmt = &body[0]; | ||
self.stmt = Some(stmt); | ||
} | ||
} | ||
} | ||
``` | ||
*/ | ||
const CANNOT_HOLD_ONTO_AST_NODE: () = (); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
18 changes: 0 additions & 18 deletions
18
crates/oxc_traverse/tests/compile_fail/ancestor_lifetime1.rs
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
crates/oxc_traverse/tests/compile_fail/ancestor_lifetime1.stderr
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
crates/oxc_traverse/tests/compile_fail/ancestor_lifetime2.rs
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
crates/oxc_traverse/tests/compile_fail/ancestor_lifetime2.stderr
This file was deleted.
Oops, something went wrong.
21 changes: 0 additions & 21 deletions
21
crates/oxc_traverse/tests/compile_fail/ancestor_lifetime3.rs
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
crates/oxc_traverse/tests/compile_fail/ancestor_lifetime3.stderr
This file was deleted.
Oops, something went wrong.