Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(semantic)!: ensure program outlives semantic #8455

Merged
merged 7 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/oxc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ let SemanticBuilderReturn {
.with_check_syntax_error(true) // Enable extra syntax error checking
.with_build_jsdoc(true) // Enable JSDoc parsing
.with_cfg(true) // Build a Control Flow Graph
.build(&program); // Produce the `Semantic`
.build(program); // Produce the `Semantic`

errors.extend(semantic_errors);
if errors.is_empty() {
Expand Down
43 changes: 16 additions & 27 deletions crates/oxc/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,7 @@ pub trait CompilerInterface {
ControlFlow::Continue(())
}

fn after_semantic(
&mut self,
_program: &mut Program<'_>,
_semantic_return: &mut SemanticBuilderReturn,
) -> ControlFlow<()> {
fn after_semantic(&mut self, _semantic_return: &mut SemanticBuilderReturn) -> ControlFlow<()> {
ControlFlow::Continue(())
}

Expand Down Expand Up @@ -134,21 +130,21 @@ pub trait CompilerInterface {
self.handle_errors(parser_return.errors);
}

let mut program = parser_return.program;
let program = parser_return.program;

/* Isolated Declarations */
if let Some(options) = self.isolated_declaration_options() {
self.isolated_declaration(options, &allocator, &program, source_path);
self.isolated_declaration(options, &allocator, program, source_path);
}

/* Semantic */

let mut semantic_return = self.semantic(&program);
let mut semantic_return = self.semantic(program);
if !semantic_return.errors.is_empty() {
self.handle_errors(semantic_return.errors);
return;
}
if self.after_semantic(&mut program, &mut semantic_return).is_break() {
if self.after_semantic(&mut semantic_return).is_break() {
return;
}

Expand All @@ -159,14 +155,14 @@ pub trait CompilerInterface {

if let Some(options) = self.transform_options() {
let mut transformer_return =
self.transform(options, &allocator, &mut program, source_path, symbols, scopes);
self.transform(options, &allocator, program, source_path, symbols, scopes);

if !transformer_return.errors.is_empty() {
self.handle_errors(transformer_return.errors);
return;
}

if self.after_transform(&mut program, &mut transformer_return).is_break() {
if self.after_transform(program, &mut transformer_return).is_break() {
return;
}

Expand All @@ -180,48 +176,41 @@ pub trait CompilerInterface {
if inject_options.is_some() || define_options.is_some() {
(symbols, scopes) = SemanticBuilder::new()
.with_stats(stats)
.build(&program)
.build(program)
.semantic
.into_symbol_table_and_scope_tree();
}

if let Some(options) = inject_options {
let ret = InjectGlobalVariables::new(&allocator, options).build(
symbols,
scopes,
&mut program,
);
let ret =
InjectGlobalVariables::new(&allocator, options).build(symbols, scopes, program);
symbols = ret.symbols;
scopes = ret.scopes;
}

if let Some(options) = define_options {
let ret =
ReplaceGlobalDefines::new(&allocator, options).build(symbols, scopes, &mut program);
ReplaceGlobalDefines::new(&allocator, options).build(symbols, scopes, program);
Compressor::new(&allocator, CompressOptions::default())
.dead_code_elimination_with_symbols_and_scopes(
ret.symbols,
ret.scopes,
&mut program,
);
.dead_code_elimination_with_symbols_and_scopes(ret.symbols, ret.scopes, program);
// symbols = ret.symbols;
// scopes = ret.scopes;
}

/* Compress */

if let Some(options) = self.compress_options() {
self.compress(&allocator, &mut program, options);
self.compress(&allocator, program, options);
}

/* Mangler */

let mangler = self.mangle_options().map(|options| self.mangle(&mut program, options));
let mangler = self.mangle_options().map(|options| self.mangle(program, options));

/* Codegen */

if let Some(options) = self.codegen_options() {
let ret = self.codegen(&program, source_path, mangler, options);
let ret = self.codegen(program, source_path, mangler, options);
self.after_codegen(ret);
}
}
Expand All @@ -235,7 +224,7 @@ pub trait CompilerInterface {
Parser::new(allocator, source_text, source_type).with_options(self.parse_options()).parse()
}

fn semantic<'a>(&self, program: &Program<'a>) -> SemanticBuilderReturn<'a> {
fn semantic<'a>(&self, program: &'a Program<'a>) -> SemanticBuilderReturn<'a> {
let mut builder = SemanticBuilder::new();

if self.transform_options().is_some() {
Expand Down
24 changes: 23 additions & 1 deletion crates/oxc_allocator/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::Allocator;
/// being called to guarantee soundness.
pub struct Box<'alloc, T: ?Sized>(NonNull<T>, PhantomData<(&'alloc (), T)>);

impl<T> Box<'_, T> {
impl<'a, T> Box<'a, T> {
/// Take ownership of the value stored in this [`Box`], consuming the box in
/// the process.
///
Expand All @@ -54,6 +54,28 @@ impl<T> Box<'_, T> {
// one just allocated from a Bump.
unsafe { ptr::read(self.0.as_ptr()) }
}

/// Consumes and leaks the `Box`, returning a mutable reference,
/// `&'a mut T`.
///
/// ## Example
/// ```
/// use oxc_allocator::{Allocator, Box};
///
/// let arena = Allocator::default();
///
/// // Put `5` into the arena and on the heap.
/// let boxed: Box<i32> = Box::new_in(5, &arena);
/// // Get a reference to the underlying value within the `arena`. `boxed` has been consumed.
/// let i = boxed.unbox();
///
/// assert_eq!(i, 5);
/// ```
#[inline]
pub fn leak(mut b: Self) -> &'a mut T {
// SAFETY: reference obtained from value stored in the box will always be valid
unsafe { b.0.as_mut() }
}
}

impl<T> Box<'_, T> {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_codegen/examples/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ fn parse<'a>(
fn codegen(ret: &ParserReturn<'_>, minify: bool) -> String {
CodeGenerator::new()
.with_options(CodegenOptions { minify, ..CodegenOptions::default() })
.build(&ret.program)
.build(ret.program)
.code
}
2 changes: 1 addition & 1 deletion crates/oxc_codegen/examples/sourcemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn main() -> std::io::Result<()> {
source_map_path: Some(path.to_path_buf()),
..CodegenOptions::default()
})
.build(&ret.program);
.build(ret.program);

if let Some(source_map) = map {
let result = source_map.to_json_string();
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl<'a> Codegen<'a> {
///
/// A source map will be generated if [`CodegenOptions::source_map_path`] is set.
#[must_use]
pub fn build(mut self, program: &Program<'a>) -> CodegenReturn {
pub fn build(mut self, program: &'a Program<'a>) -> CodegenReturn {
self.quote = if self.options.single_quote { b'\'' } else { b'"' };
self.source_text = program.source_text;
self.code.reserve(program.source_text.len());
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_codegen/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn codegen_options(source_text: &str, options: &CodegenOptions) -> CodegenRe
let ret = Parser::new(&allocator, source_text, source_type).parse();
let mut options = options.clone();
options.single_quote = true;
CodeGenerator::new().with_options(options).build(&ret.program)
CodeGenerator::new().with_options(options).build(ret.program)
}

pub fn snapshot(name: &str, cases: &[&str]) {
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_codegen/tests/integration/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn test_options_with_source_type(
) {
let allocator = Allocator::default();
let ret = Parser::new(&allocator, source_text, source_type).parse();
let result = CodeGenerator::new().with_options(options).build(&ret.program).code;
let result = CodeGenerator::new().with_options(options).build(ret.program).code;
assert_eq!(result, expected, "\nfor source: {source_text:?}");
}

Expand All @@ -38,7 +38,7 @@ pub fn test_minify(source_text: &str, expected: &str) {
let ret = Parser::new(&allocator, source_text, source_type).parse();
let result = CodeGenerator::new()
.with_options(CodegenOptions { minify: true, ..CodegenOptions::default() })
.build(&ret.program)
.build(ret.program)
.code;
assert_eq!(result, expected, "\nfor minify source: {source_text}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn main() {

let id_ret =
IsolatedDeclarations::new(&allocator, IsolatedDeclarationsOptions { strip_internal: true })
.build(&ret.program);
.build(ret.program);
let printed = CodeGenerator::new().build(&id_ret.program).code;

println!("Dts Emit:\n");
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_isolated_declarations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl<'a> IsolatedDeclarations<'a> {
/// # Errors
///
/// Returns `Vec<Error>` if any errors were collected during the transformation.
pub fn build(mut self, program: &Program<'a>) -> IsolatedDeclarationsReturn<'a> {
pub fn build(mut self, program: &'a Program<'a>) -> IsolatedDeclarationsReturn<'a> {
self.internal_annotations = self
.strip_internal
.then(|| Self::build_internal_annotations(program))
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_isolated_declarations/tests/deno/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ mod tests {
&allocator,
IsolatedDeclarationsOptions { strip_internal: true },
)
.build(&ret.program);
.build(ret.program);
let actual = CodeGenerator::new().build(&ret.program).code;
let expected_program = Parser::new(&allocator, expected, source_type).parse().program;
let expected = CodeGenerator::new().build(&expected_program).code;
let expected = CodeGenerator::new().build(expected_program).code;
assert_eq!(actual.trim(), expected.trim());
}

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_isolated_declarations/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn transform(path: &Path, source_text: &str) -> String {

let id_ret =
IsolatedDeclarations::new(&allocator, IsolatedDeclarationsOptions { strip_internal: true })
.build(&parser_ret.program);
.build(parser_ret.program);
let code = CodeGenerator::new().build(&id_ret.program).code;

let mut snapshot =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl IsolatedLintHandler {
.with_cfg(true)
.with_scope_tree_child_ids(true)
.with_check_syntax_error(true)
.build(&ret.program);
.build(ret.program);

if !semantic_ret.errors.is_empty() {
let reports = semantic_ret
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/examples/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn main() -> std::io::Result<()> {
return Ok(());
}

let semantic_ret = SemanticBuilder::new().build(&ret.program);
let semantic_ret = SemanticBuilder::new().build(ret.program);

let mut errors: Vec<OxcDiagnostic> = vec![];

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/service/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ impl Runtime {
.with_scope_tree_child_ids(true)
.with_build_jsdoc(true)
.with_check_syntax_error(check_syntax_errors)
.build(&ret.program);
.build(ret.program);

if !semantic_ret.errors.is_empty() {
return semantic_ret.errors.into_iter().map(|err| Message::new(err, None)).collect();
Expand Down
3 changes: 1 addition & 2 deletions crates/oxc_linter/src/utils/jest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,7 @@ mod test {
let allocator = Allocator::default();
let source_type = SourceType::default();
let parser_ret = Parser::new(&allocator, "", source_type).parse();
let semantic_ret =
SemanticBuilder::new().with_cfg(true).build(&parser_ret.program).semantic;
let semantic_ret = SemanticBuilder::new().with_cfg(true).build(parser_ret.program).semantic;
let semantic_ret = Rc::new(semantic_ret);

let build_ctx = |path: &'static str| {
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_minifier/examples/dce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ fn main() -> std::io::Result<()> {

fn dce(allocator: &Allocator, source_text: &str, source_type: SourceType, nospace: bool) -> String {
let ret = Parser::new(allocator, source_text, source_type).parse();
let mut program = ret.program;
Compressor::new(allocator, CompressOptions::default()).dead_code_elimination(&mut program);
let program = ret.program;
Compressor::new(allocator, CompressOptions::default()).dead_code_elimination(program);
CodeGenerator::new()
.with_options(CodegenOptions { minify: nospace, ..CodegenOptions::default() })
.build(&program)
.build(program)
.code
}
4 changes: 2 additions & 2 deletions crates/oxc_minifier/examples/mangler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ fn mangler(source_text: &str, source_type: SourceType, debug: bool) -> String {
let ret = Parser::new(&allocator, source_text, source_type).parse();
let mangler = Mangler::new()
.with_options(MangleOptions { debug, top_level: source_type.is_module() })
.build(&ret.program);
CodeGenerator::new().with_mangler(Some(mangler)).build(&ret.program).code
.build(ret.program);
CodeGenerator::new().with_mangler(Some(mangler)).build(ret.program).code
}
6 changes: 3 additions & 3 deletions crates/oxc_minifier/examples/minifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ fn minify(
nospace: bool,
) -> String {
let ret = Parser::new(allocator, source_text, source_type).parse();
let mut program = ret.program;
let program = ret.program;
let options = MinifierOptions {
mangle: mangle.then(MangleOptions::default),
compress: CompressOptions::default(),
};
let ret = Minifier::new(options).build(allocator, &mut program);
let ret = Minifier::new(options).build(allocator, program);
CodeGenerator::new()
.with_options(CodegenOptions { minify: nospace, ..CodegenOptions::default() })
.with_mangler(ret.mangler)
.build(&program)
.build(program)
.code
}
12 changes: 6 additions & 6 deletions crates/oxc_minifier/src/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ fn run<'a, P: CompressorPass<'a>>(
remove_whitespace: bool,
) -> String {
let source_type = SourceType::mjs();
let mut program = Parser::new(allocator, source_text, source_type).parse().program;
let program = Parser::new(allocator, source_text, source_type).parse().program;

if let Some(pass) = pass {
let (symbols, scopes) =
SemanticBuilder::new().build(&program).semantic.into_symbol_table_and_scope_tree();
SemanticBuilder::new().build(program).semantic.into_symbol_table_and_scope_tree();
let mut ctx = ReusableTraverseCtx::new(scopes, symbols, allocator);
RemoveSyntax::new(CompressOptions::all_false()).build(&mut program, &mut ctx);
Normalize::new().build(&mut program, &mut ctx);
pass.build(&mut program, &mut ctx);
RemoveSyntax::new(CompressOptions::all_false()).build(program, &mut ctx);
Normalize::new().build(program, &mut ctx);
pass.build(program, &mut ctx);
}

CodeGenerator::new()
Expand All @@ -55,6 +55,6 @@ fn run<'a, P: CompressorPass<'a>>(
minify: remove_whitespace,
..CodegenOptions::default()
})
.build(&program)
.build(program)
.code
}
4 changes: 2 additions & 2 deletions crates/oxc_minifier/tests/ast_passes/dead_code_elimination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use oxc_span::SourceType;

fn run(source_text: &str, source_type: SourceType) -> String {
let allocator = Allocator::default();
let mut ret = Parser::new(&allocator, source_text, source_type).parse();
let program = &mut ret.program;
let ret = Parser::new(&allocator, source_text, source_type).parse();
let program = ret.program;
Compressor::new(&allocator, CompressOptions::default()).dead_code_elimination(program);
Codegen::new().build(program).code
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_minifier/tests/ecmascript/prop_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ fn test_prop_name() {
assert!(ret.errors.is_empty());

let mut visitor = TestVisitor;
visitor.visit_program(&ret.program);
visitor.visit_program(ret.program);
}
4 changes: 2 additions & 2 deletions crates/oxc_minifier/tests/mangler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ fn mangle(source_text: &str, top_level: bool) -> String {
let ret = Parser::new(&allocator, source_text, source_type).parse();
let program = ret.program;
let mangler =
Mangler::new().with_options(MangleOptions { debug: false, top_level }).build(&program);
CodeGenerator::new().with_mangler(Some(mangler)).build(&program).code
Mangler::new().with_options(MangleOptions { debug: false, top_level }).build(program);
CodeGenerator::new().with_mangler(Some(mangler)).build(program).code
}

#[test]
Expand Down
Loading
Loading