Skip to content

Commit 4ce6329

Browse files
fix(semantic)!: ensure program outlives semantic (#8455)
fixes: #8437 In semantic builder make sure `Program` reference has a lifetime of the Arena. --------- Co-authored-by: overlookmotel <theoverlookmotel@gmail.com>
1 parent 250bbd1 commit 4ce6329

File tree

9 files changed

+18
-20
lines changed

9 files changed

+18
-20
lines changed

crates/oxc/src/compiler.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,7 @@ pub trait CompilerInterface {
101101
ControlFlow::Continue(())
102102
}
103103

104-
fn after_semantic(
105-
&mut self,
106-
_program: &mut Program<'_>,
107-
_semantic_return: &mut SemanticBuilderReturn,
108-
) -> ControlFlow<()> {
104+
fn after_semantic(&mut self, _semantic_return: &mut SemanticBuilderReturn) -> ControlFlow<()> {
109105
ControlFlow::Continue(())
110106
}
111107

@@ -148,7 +144,7 @@ pub trait CompilerInterface {
148144
self.handle_errors(semantic_return.errors);
149145
return;
150146
}
151-
if self.after_semantic(&mut program, &mut semantic_return).is_break() {
147+
if self.after_semantic(&mut semantic_return).is_break() {
152148
return;
153149
}
154150

@@ -235,7 +231,7 @@ pub trait CompilerInterface {
235231
Parser::new(allocator, source_text, source_type).with_options(self.parse_options()).parse()
236232
}
237233

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

241237
if self.transform_options().is_some() {

crates/oxc_linter/src/service/runtime.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ impl Runtime {
223223
.with_scope_tree_child_ids(true)
224224
.with_build_jsdoc(true)
225225
.with_check_syntax_error(check_syntax_errors)
226-
.build(&ret.program);
226+
.build(allocator.alloc(ret.program));
227227

228228
if !semantic_ret.errors.is_empty() {
229229
return semantic_ret.errors.into_iter().map(|err| Message::new(err, None)).collect();

crates/oxc_semantic/src/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ impl<'a> SemanticBuilder<'a> {
223223
/// Finalize the builder.
224224
///
225225
/// # Panics
226-
pub fn build(mut self, program: &Program<'a>) -> SemanticBuilderReturn<'a> {
226+
pub fn build(mut self, program: &'a Program<'a>) -> SemanticBuilderReturn<'a> {
227227
self.source_text = program.source_text;
228228
self.source_type = program.source_type;
229229
if self.build_jsdoc {

crates/oxc_semantic/src/jsdoc/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ mod test {
207207
) -> Semantic<'a> {
208208
let source_type = source_type.unwrap_or_default();
209209
let ret = Parser::new(allocator, source_text, source_type).parse();
210-
SemanticBuilder::new().with_build_jsdoc(true).build(&ret.program).semantic
210+
SemanticBuilder::new().with_build_jsdoc(true).build(allocator.alloc(ret.program)).semantic
211211
}
212212

213213
fn get_jsdocs<'a>(

crates/oxc_semantic/src/jsdoc/parser/jsdoc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ mod test {
4545
fn build_semantic<'a>(allocator: &'a Allocator, source_text: &'a str) -> Semantic<'a> {
4646
let source_type = SourceType::default();
4747
let ret = Parser::new(allocator, source_text, source_type).parse();
48-
SemanticBuilder::new().with_build_jsdoc(true).build(&ret.program).semantic
48+
SemanticBuilder::new().with_build_jsdoc(true).build(allocator.alloc(ret.program)).semantic
4949
}
5050

5151
#[test]

crates/oxc_semantic/src/jsdoc/parser/jsdoc_tag.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ mod test {
194194
fn build_semantic<'a>(allocator: &'a Allocator, source_text: &'a str) -> Semantic<'a> {
195195
let source_type = SourceType::default();
196196
let ret = Parser::new(allocator, source_text, source_type).parse();
197-
SemanticBuilder::new().with_build_jsdoc(true).build(&ret.program).semantic
197+
SemanticBuilder::new().with_build_jsdoc(true).build(allocator.alloc(ret.program)).semantic
198198
}
199199

200200
#[test]

crates/oxc_semantic/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ mod tests {
247247
) -> Semantic<'s> {
248248
let parse = oxc_parser::Parser::new(allocator, source, source_type).parse();
249249
assert!(parse.errors.is_empty());
250-
let semantic = SemanticBuilder::new().build(&parse.program);
250+
let semantic = SemanticBuilder::new().build(allocator.alloc(parse.program));
251251
assert!(semantic.errors.is_empty(), "Parse error: {}", semantic.errors[0]);
252252
semantic.semantic
253253
}

crates/oxc_semantic/tests/integration/util/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl<'a> SemanticTester<'a> {
171171
.with_check_syntax_error(true)
172172
.with_cfg(self.cfg)
173173
.with_scope_tree_child_ids(self.scope_tree_child_ids)
174-
.build(&parse.program)
174+
.build(self.allocator.alloc(parse.program))
175175
}
176176

177177
pub fn basic_blocks_count(&self) -> usize {

tasks/coverage/src/driver.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_hash::FxHashSet;
44

55
use oxc::{
66
allocator::Allocator,
7-
ast::{ast::Program, Comment},
7+
ast::{ast::Program, AstKind, Comment},
88
codegen::{CodegenOptions, CodegenReturn},
99
diagnostics::OxcDiagnostic,
1010
minifier::CompressOptions,
@@ -80,12 +80,14 @@ impl CompilerInterface for Driver {
8080
ControlFlow::Continue(())
8181
}
8282

83-
fn after_semantic(
84-
&mut self,
85-
program: &mut Program<'_>,
86-
ret: &mut SemanticBuilderReturn,
87-
) -> ControlFlow<()> {
83+
fn after_semantic(&mut self, ret: &mut SemanticBuilderReturn) -> ControlFlow<()> {
8884
if self.check_semantic {
85+
let Some(root_node) = ret.semantic.nodes().root_node() else {
86+
return ControlFlow::Break(());
87+
};
88+
let AstKind::Program(program) = root_node.kind() else {
89+
return ControlFlow::Break(());
90+
};
8991
if let Some(errors) = check_semantic_ids(program) {
9092
self.errors.extend(errors);
9193
return ControlFlow::Break(());

0 commit comments

Comments
 (0)