feat: import defer 阶段标志纳入模块哈希键

This commit is contained in:
2026-10-09 12:11:02 +00:00
parent a4cd722a30
commit c0648b9014
4 changed files with 44 additions and 20 deletions
@@ -98,6 +98,9 @@ pub(super) fn hash_statement(stmt: &Statement, h: &mut rustc_hash::FxHasher, inc
}
Statement::ImportDeclaration(imp) => {
imp.source.value.hash(h);
// import 阶段标志(import defer / source-phase imports):同源声明
// 语义不同,须区分缓存键,防 defer 与非 defer 碰撞复用错误字节码。
imp.phase.map(|p| p as u8).hash(h);
if let Some(specifiers) = &imp.specifiers {
(specifiers.len() as u32).hash(h);
for spec in specifiers {
@@ -86,6 +86,27 @@ fn import_expression_distinguished() {
assert_ne!(compiled("import('./a.js')"), compiled("import('./a.js', { with: { type: 'json' } })"));
}
#[test]
fn import_defer_distinguished() {
// import defer 与普通 namespace import 语义不同(defer 不触发依赖求值):
// 修复前 phase 不入 hash,两者哈希相同 → 缓存碰撞复用错误字节码。
assert_ne!(
compiled("import defer * as ns from './x.js'"),
compiled("import * as ns from './x.js'"),
"import defer vs plain namespace import must differ"
);
assert_ne!(
structural("import defer * as ns from './x.js'"),
structural("import * as ns from './x.js'"),
"defer flag must be part of the structural key too"
);
// 同源等价回归
assert_eq!(
compiled("import defer * as ns from './x.js'"),
compiled("import defer * as ns from './x.js'")
);
}
#[test]
fn structural_hash_ignores_binding_names() {
// 结构哈希忽略绑定名:仅参数/变量改名后哈希相等。