Repository: aptos-labs/aptos-core
File: third_party/move/move-compiler-v2/src/env_pipeline/inlining_optimization.rs (Rust)

Code before:
fn _has_abort_with_non_const_or_conflicting_error_code(
    function: &FunctionEnv,
    caller_module_env: &ModuleEnv,
) -> bool {
    let Some(exp) = function.get_def() else {
        return false;
    };

    // Only check when caller and callee are in the same module.
    if caller_module_env.get_id() != function.module_env.get_id() {
        return false;
    }

    // Helper: does an abort *argument* count as non-const or conflicting?
    let is_nonconst_or_conflicting = |arg_exp: &Exp, f: &FunctionEnv, caller: &ModuleEnv| -> bool {
        match arg_exp.as_ref() {
            // Constant code: only "bad" if it conflicts with caller's codes.
            ExpData::Value(_, v) => caller.has_error_code_with_value(v),

            // Function call as the abort argument.
            ExpData::Call(_, Operation::MoveFunction(mid, fid), call_args) => {
                // Special-case std::error::<func>(code) and std::error::canonical(category, code)
                if f.env().get_module(*mid).is_std_error() {
                    let called_fun = f.env().get_function(mid.qualified(*fid));
                    let chosen_arg = if call_args.len() == 1 {
                        Some(call_args[0].as_ref())
                    } else if called_fun.get_name_str() == "canonical" {
                        Some(call_args[1].as_ref())
                    } else {
                        None
                    };
                    match chosen_arg {
                        // If the chosen arg is a constant, check for conflict; otherwise it's non-const.
                        Some(ExpData::Value(_, v)) => caller.has_error_code_with_value(v),
                        Some(_) | None => true,
                    }
                } else {
                    // Any other function call as an abort code is non-const.
                    true
                }
            },

            // Anything else as an abort code is non-const.
            _ => true,
        }
    };

    let mut found = false;
    exp.visit_pre_order(&mut |e: &ExpData| {
        if let ExpData::Call(_, Operation::Abort, args) = e {
            debug_assert_eq!(args.len(), 1);

Review comment (suggestion):
Something minor:by replacing this line with `matches!(call_args[0].as_ref(), ExpData::Value(_, v) if caller.has_error_code_with_value(v))` here, you can directly retun here and skip the extra `match`.

Code after:
fn has_access_controls(function: &FunctionEnv) -> bool {
    if let Some(access_specifiers) = function.get_access_specifiers() {
        if access_specifiers.is_empty() {
            // empty access specifiers means no access is allowed, the strictest form
            // of access control
            return true;
        }
        // any reads or writes specification is considered an access control
        access_specifiers
            .iter()
            .any(|spec| spec.kind != AccessSpecifierKind::LegacyAcquires)
    } else {
        false
    }
}

/// Does `function` have the `#[module_lock]` attribute?
fn has_module_lock_attribute(function: &FunctionEnv) -> bool {
    let env = function.env();
    function.has_attribute(|attr| {
        env.symbol_pool().string(attr.name()).as_str() == well_known::MODULE_LOCK_ATTRIBUTE
    })
}

/// Rewriter for a caller function to inline the call sites in it.
struct CallerRewriter<'env> {
    env: &'env GlobalEnv,
    // The call sites that have been picked to inline
    call_sites: BTreeSet<NodeId>,
}

impl ExpRewriterFunctions for CallerRewriter<'_> {
    fn rewrite_call(&mut self, call_id: NodeId, op: &Operation, args: &[Exp]) -> Option<Exp> {
        if self.call_sites.contains(&call_id) {
            if let Operation::MoveFunction(mid, fid) = op {
                let callee = self.env.get_function(mid.qualified(*fid));
                let type_args = self.env.get_node_instantiation(call_id);
                let call_site_loc = self.env.get_node_loc(call_id);
                let mut callee_rewriter = CalleeRewriter {
                    function_env: &callee,
                    type_args: &type_args,
                    call_site_loc: &call_site_loc,
                };
                let body = callee.get_def()?;
                // Rewrite the body of the callee to adjust node ids, types, and locations.
                let rewritten_body = callee_rewriter.rewrite_exp(body.clone());
                // Construct a pattern for the parameters of the callee to create a `let` binding.
                let params_pattern = callee_rewriter.params_to_pattern(&callee);
                // Perform inlining transformation at the call site.
                Some(self.construct_inlined_call_expression(
                    &call_site_loc,