This could happen accidentally.
Imagine a targeting computer that learns from each shot fired. As it's used, it learns how the wind works on that planet, how a new target behaves in particular situations, etc. An AI that learns could be invaluable, especially if it's designed to share with others in your unit what's its learnt. The guns communicate after the missions "I missed because I didnt consider heat changing the airflow. I didnt know those anthill thing were hot", " ooh that's good experience, I'll consider that next shot I fire that passes near those things".
In a space opera setting, where your exploring new crazy worlds, theres no realistic way every firing scenario could be thought of in advance, so rather than putting software engineers in the units, they just put an ic filled with simulated neurons into the guns.
If that society is wiped out, a lot of ai guns could get lonely and develop personalities that make great sidekick characters.
I would want a smart weapon now. So do lawmakers.
There is a huge desire for (semi-) smart weapons on earth now. 10% of cops who die on duty are shot with their own gun, we have prototype smart weapons that read finger prints, or match against an RFID chip in a watch. We had them as prototypes last century too. In a world where I've forgotten my phone pin number because the facial recognition is instantaneous and perfect, a gun that can't be used to kill me or my family seems like a no brainer upgrade if I wanted a gun In the house.
It's only because of an obscure New Jersey law (once a smart gun is sold in the USA, all guns sold in NJ must be smart) that has basically prevented this from coming to market, and the USA is such a large gun market no manufacturer will want to jeopardise this.