Repository: bevyengine/bevy
File: crates/bevy_ecs/src/bundle/insert.rs (Rust)

Code before:
                    change_tick,
                    world: world.as_unsafe_world_cell(),
                }
            }
        };

        if is_new_created {
            inserter
                .world
                .into_deferred()
                .trigger(ArchetypeCreated(new_archetype_id));
        }
        inserter
    }

    unsafe fn before_insert<'a>(
        entity: Entity,
        location: EntityLocation,
        insert_mode: InsertMode,
        caller: MaybeLocation,
        relationship_hook_mode: RelationshipHookMode,
        table: &'a mut Table,
        archetype: &'a mut Archetype,
        archetype_after_insert: &ArchetypeAfterBundleInsert,
        world: &'a UnsafeWorldCell<'w>,
        archetype_move_type: &'a mut ArchetypeMoveType,
    ) -> (
        &'a Archetype,
        EntityLocation,
        &'a mut SparseSets,
        &'a mut Table,
        TableRow,
    ) {
        // SAFETY: All components in the bundle are guaranteed to exist in the World
        // as they must be initialized before creating the BundleInfo.
        unsafe {
            // SAFETY: Mutable references do not alias and will be dropped after this block
            let mut deferred_world = world.into_deferred();

            if insert_mode == InsertMode::Replace {
                if archetype.has_replace_observer() {
                    deferred_world.trigger_observers(
                        REPLACE,
                        Some(entity),
                        archetype_after_insert.iter_existing(),
                        caller,
                    );
                }
                deferred_world.trigger_on_replace(
                    archetype,
                    entity,

Review comment (suggestion):
There are several borrows of world data here. The safety contract of `UnsafeWorldCell::world_mut` makes it not sound to call while these are live.

Code after:
            }
        };

        if is_new_created {
            inserter
                .world
                .into_deferred()
                .trigger(ArchetypeCreated(new_archetype_id));
        }
        inserter
    }

    // A non-generic prelude to insert used to minimize duplicated monomorphized code.
    // In combination with after_insert, this can reduce compile time of bevy by 10%.
    // We inline in release to avoid a minor perf loss.
    #[cfg_attr(not(debug_assertions), inline(always))]
    unsafe fn before_insert<'a>(
        entity: Entity,
        location: EntityLocation,
        insert_mode: InsertMode,
        caller: MaybeLocation,
        relationship_hook_mode: RelationshipHookMode,
        mut table: NonNull<Table>,
        mut archetype: NonNull<Archetype>,
        archetype_after_insert: &ArchetypeAfterBundleInsert,
        world: &'a UnsafeWorldCell<'w>,
        archetype_move_type: &'a mut ArchetypeMoveType,
    ) -> (
        &'a Archetype,
        EntityLocation,
        &'a mut SparseSets,
        &'a mut Table,
        TableRow,
    ) {
        // SAFETY: All components in the bundle are guaranteed to exist in the World
        // as they must be initialized before creating the BundleInfo.
        unsafe {
            // SAFETY: Mutable references do not alias and will be dropped after this block
            let mut deferred_world = world.into_deferred();

            if insert_mode == InsertMode::Replace {
                let archetype = archetype.as_ref();
                if archetype.has_replace_observer() {
                    // SAFETY: the REPLACE event_key corresponds to the Replace event's type
                    deferred_world.trigger_raw(
                        REPLACE,
                        &mut Replace { entity },
                        &mut EntityComponentsTrigger {
                            components: archetype_after_insert.existing(),
                        },
                        caller,