Repository: MystenLabs/sui
File: external-crates/move/crates/move-vm-runtime/src/jit/execution/translate.rs (Rust)

Code before:

            match cur_state {
                State::Visited => continue,
                State::Visiting => {
                    // all deps done, now load if needed
                    let key = package_context.interner.intern_ident_str(cur_id.name())?;
                    if !package_context.loaded_modules.contains_key(&key) {
                        let input_module = input_modules.get(&cur_id).ok_or_else(|| {
                            make_invariant_violation!(format!(
                                "Module {} not found in initial modules",
                                cur_id
                            ))
                        })?;
                        let loaded_module =
                            module(package_context, package_context.version_id, input_module)?;
                        assert!(
                            package_context
                                .loaded_modules
                                .insert(key, loaded_module)
                                .is_none()
                        );
                    }
                    state.insert(cur_id, State::Visited);
                }
                State::NotVisited => {
                    state.insert(cur_id.clone(), State::Visiting);
                    stack.push(cur_id.clone());

                    let input_module = input_modules.get(&cur_id).ok_or_else(|| {
                        make_invariant_violation!(format!(
                            "Module {} not found in initial modules",
                            cur_id
                        ))
                    })?;

                    for dep in input_module
                        .compiled_module
                        .immediate_dependencies()
                        .iter()
                        .filter(|dep| pkg_module_ids.contains(dep) && *dep != &cur_id)
                    {
                        match state.get(dep).copied().unwrap_or(State::NotVisited) {
                            State::Visited => { /* nothing */ }
                            State::Visiting => {
                                return Err(make_invariant_violation!(format!(
                                    "Cycle detected when loading module for package: {}",
                                    dep
                                )));
                            }
                            State::NotVisited => {
                                stack.push(dep.clone());

Review comment (bug):
maybe an invariant violation (+ debug assert?) if the `insert` is not `None` here? 

Code after:
                        .iter()
                        .filter(|dep| pkg_module_ids.contains(dep) && *dep != &cur_id)
                    {
                        match state.get(dep).copied().unwrap_or(State::NotVisited) {
                            State::Visited => { /* nothing */ }
                            State::Visiting => {
                                return Err(make_invariant_violation!(format!(
                                    "Cycle detected when loading module for package: {}",
                                    dep
                                )));
                            }
                            State::NotVisited => {
                                stack.push(dep.clone());
                            }
                        }
                    }
                }
            }
        }
    }

    Ok(())
}

// -------------------------------------------------------------------------------------------------
// Module Translation

fn module(
    context: &mut PackageContext<'_>,
    version_id: VersionId,
    module: &input::Module,
) -> PartialVMResult<Module> {
    let self_id = module.compiled_module.self_id();
    dbg_println!("Loading module: {}", self_id);

    let mkey = context.interner.intern_ident_str(self_id.name())?;

    let cmodule = &module.compiled_module;

    // Initialize module data
    let type_refs = initialize_type_refs(context, cmodule)?;

    let (structs, enums, datatype_descriptors) = datatypes(context, &version_id, &mkey, cmodule)?;
    let (instantiation_signatures, _signature_map) = cache_signatures(context, cmodule)?;
    dbg_println!("Module types loaded");

    let sig_pointers = instantiation_signatures
        .iter()
        .map(VMPointer::from_ref)
        .collect::<Vec<_>>();