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(minfier): bigint bitwise operation only works with bigint #7937

Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 13 additions & 11 deletions crates/oxc_ecmascript/src/constant_evaluation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,19 @@ pub trait ConstantEvaluation<'a> {
})
}
BinaryOperator::BitwiseAnd | BinaryOperator::BitwiseOR | BinaryOperator::BitwiseXOR => {
if left.is_big_int_literal() && right.is_big_int_literal() {
let left_bigint = self.get_side_free_bigint_value(left);
let right_bigint = self.get_side_free_bigint_value(right);
if let (Some(left_val), Some(right_val)) = (left_bigint, right_bigint) {
let result_val: BigInt = match operator {
BinaryOperator::BitwiseAnd => left_val & right_val,
BinaryOperator::BitwiseOR => left_val | right_val,
BinaryOperator::BitwiseXOR => left_val ^ right_val,
_ => unreachable!(),
};
return Some(ConstantValue::BigInt(result_val));
}
}
let left_num = self.get_side_free_number_value(left);
let right_num = self.get_side_free_number_value(right);
if let (Some(left_val), Some(right_val)) = (left_num, right_num) {
Expand All @@ -332,17 +345,6 @@ pub trait ConstantEvaluation<'a> {
};
return Some(ConstantValue::Number(result_val));
}
let left_bitint = self.get_side_free_bigint_value(left);
let right_bitint = self.get_side_free_bigint_value(right);
if let (Some(left_val), Some(right_val)) = (left_bitint, right_bitint) {
let result_val: BigInt = match operator {
BinaryOperator::BitwiseAnd => left_val & right_val,
BinaryOperator::BitwiseOR => left_val | right_val,
BinaryOperator::BitwiseXOR => left_val ^ right_val,
_ => unreachable!(),
};
return Some(ConstantValue::BigInt(result_val));
}
None
}
_ => None,
Expand Down
5 changes: 5 additions & 0 deletions crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,11 @@ mod test {
test("x = 3n ^ y ^ 1n", "x = y ^ 2n");
test("x = y ^ 3n ^ 3n", "x = y ^ 0n");
test("x = 3n ^ y ^ 3n", "x = y ^ 0n");

// TypeError: Cannot mix BigInt and other types
test_same("1n & 1");
test_same("1n | 1");
test_same("1n ^ 1");
}

#[test]
Expand Down
Loading