Skip to content

Source Generator causes warning on each query method: Warning CS8602 : Dereference of a possibly null reference. #107

@hhyyrylainen

Description

@hhyyrylainen

I get a bunch of warnings when building my project once I started adding Arch. All the warnings I'm talking about here are coming from generated code for each of the query methods I have in my systems.

This is the generated code for one such system:

    partial class ToxinCollisionSystem
    {
        private QueryDescription Update_QueryDescription = new QueryDescription(all: new Signature(typeof(global::Components.ToxinDamageSource), typeof(global::Components.CollisionManagement), typeof(global::Components.Physics), typeof(global::Components.TimedLife)), any: Signature.Null, none: Signature.Null, exclusive: Signature.Null);
        private World? _Update_Initialized;
        private Query? _Update_Query;
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public void UpdateQuery(World world)
        {
            if (!ReferenceEquals(_Update_Initialized, world))
            {
                _Update_Query = world.Query(in Update_QueryDescription);
                _Update_Initialized = world;
            }

            foreach (ref var chunk in _Update_Query)
            {
                ref var entityFirstElement = ref chunk.Entity(0);
                ref var @toxindamagesourceFirstElement = ref chunk.GetFirst<global::Components.ToxinDamageSource>();
                ref var @collisionmanagementFirstElement = ref chunk.GetFirst<global::Components.CollisionManagement>();
                foreach (var entityIndex in chunk)
                {
                    ref readonly var entity = ref Unsafe.Add(ref entityFirstElement, entityIndex);
                    ref var @damagesource = ref Unsafe.Add(ref toxindamagesourceFirstElement, entityIndex);
                    ref var @collisions = ref Unsafe.Add(ref collisionmanagementFirstElement, entityIndex);
                    Update(ref @damagesource, ref @collisions, in @entity);
                }
            }
        }
    }

The error comes from this part: foreach (ref var chunk in _Update_Query)

It looks like a fix would be either changing one of the lines to:

if (!ReferenceEquals(_Update_Initialized, world) || _Update_Query == null)

Or:

foreach (ref var chunk in _Update_Query!)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions