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(es/minifier): Fix ordering issue of analyzer #6150

Merged
merged 7 commits into from Oct 15, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,9 +1,8 @@
//// [classDeclarationLoop.ts]
import _class_call_check from "@swc/helpers/src/_class_call_check.mjs";
for(var _loop = function(i) {
var C = function C() {
arr.push(function C() {
"use strict";
_class_call_check(this, C), this.prop = i;
};
arr.push(C);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. In this case arr used to be marked as var_kind None.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah and that's why it was not inlined I guess

}, arr = [], i = 0; i < 10; ++i)_loop(i);
42 changes: 41 additions & 1 deletion crates/swc_ecma_minifier/src/analyzer/mod.rs
Expand Up @@ -68,7 +68,7 @@ where
v.data
}

#[derive(Debug, Default, Clone)]
#[derive(Debug, Clone)]
pub(crate) struct VarUsageInfo {
pub inline_prevented: bool,

Expand Down Expand Up @@ -143,6 +143,46 @@ pub(crate) struct VarUsageInfo {
pub used_recursively: bool,
}

impl Default for VarUsageInfo {
fn default() -> Self {
Self {
inline_prevented: Default::default(),
ref_count: Default::default(),
cond_init: Default::default(),
declared: Default::default(),
declared_count: Default::default(),
declared_as_fn_param: Default::default(),
declared_as_fn_decl: Default::default(),
declared_as_fn_expr: Default::default(),
assign_count: Default::default(),
mutation_by_call_count: Default::default(),
usage_count: Default::default(),
reassigned_with_assignment: Default::default(),
reassigned_with_var_decl: Default::default(),
mutated: Default::default(),
has_property_access: Default::default(),
has_property_mutation: Default::default(),
exported: Default::default(),
used_above_decl: Default::default(),
is_fn_local: true,
executed_multiple_time: Default::default(),
used_in_cond: Default::default(),
var_kind: Default::default(),
var_initialized: Default::default(),
declared_as_catch_param: Default::default(),
no_side_effect_for_member_access: Default::default(),
used_as_callee: Default::default(),
used_as_arg: Default::default(),
indexed_with_dynamic_key: Default::default(),
pure_fn: Default::default(),
infects: Default::default(),
used_in_non_child_fn: Default::default(),
accessed_props: Default::default(),
used_recursively: Default::default(),
}
}
}

impl VarUsageInfo {
pub fn is_mutated_only_by_one_call(&self) -> bool {
self.assign_count == 0 && self.mutation_by_call_count == 1
Expand Down
41 changes: 18 additions & 23 deletions crates/swc_ecma_minifier/src/analyzer/storage/normal.rs
Expand Up @@ -153,32 +153,27 @@ impl Storage for ProgramData {
// debug!(has_init = has_init, "declare_decl(`{}`)", i);
// }

let v = self
.vars
.entry(i.to_id())
.and_modify(|v| {
if has_init && (v.declared || v.var_initialized) {
trace_op!("declare_decl(`{}`): Already declared", i);

v.mutated = true;
v.reassigned_with_var_decl = true;
v.assign_count += 1;
}
let v = self.vars.entry(i.to_id()).or_default();

if v.used_in_non_child_fn {
v.is_fn_local = false;
}
if has_init && (v.declared || v.var_initialized) {
trace_op!("declare_decl(`{}`): Already declared", i);

v.var_initialized |= has_init;
})
.or_insert_with(|| VarUsageInfo {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is problematic because this closure is not called if the analyzer called .var_or_default() for the infection analysis

is_fn_local: true,
var_kind: kind,
var_initialized: has_init,
no_side_effect_for_member_access: ctx.in_decl_with_no_side_effect_for_member_access,
v.mutated = true;
v.reassigned_with_var_decl = true;
v.assign_count += 1;
}

..Default::default()
});
// This is not delcared yet, so this is the first declaration.
if !v.declared {
v.var_kind = kind;
v.no_side_effect_for_member_access = ctx.in_decl_with_no_side_effect_for_member_access;
}

if v.used_in_non_child_fn {
v.is_fn_local = false;
}

v.var_initialized |= has_init;

v.declared_count += 1;
v.declared = true;
Expand Down
Expand Up @@ -14843,8 +14843,7 @@
0 === V && (V = 5);
}
function Uj(a) {
var b = eg();
return gg(99, dk.bind(null, a, b)), null;
return gg(99, dk.bind(null, a, eg())), null;
}
function dk(a, b) {
do Oj();
Expand Down
Expand Up @@ -26501,8 +26501,7 @@
return t > (e = e.childExpirationTime) ? t : e;
}
function ie(e) {
var t = to();
return tu(99, it.bind(null, e, t)), null;
return tu(99, it.bind(null, e, to())), null;
}
function it(e, t) {
if (ir(), (48 & nx) != 0) throw Error(d(327));
Expand Down
Expand Up @@ -8072,8 +8072,7 @@
0 === workInProgressRootExitStatus && (workInProgressRootExitStatus = 5);
}
function commitRoot(root) {
var renderPriorityLevel = getCurrentPriorityLevel();
return runWithPriority$1(99, commitRootImpl.bind(null, root, renderPriorityLevel)), null;
return runWithPriority$1(99, commitRootImpl.bind(null, root, getCurrentPriorityLevel())), null;
kdy1 marked this conversation as resolved.
Show resolved Hide resolved
}
function commitRootImpl(root, renderPriorityLevel) {
do flushPassiveEffects();
Expand Down