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.
And we'd want smart to become real smart.
Once smart guns that can't kill people the owner likes become common, it could be extended. First school shooting done with a smart gun, there'll be political pressure to make the gun detect that it was being used in a school to mow down civilians. First attack on a church or mosque, there'll be pressure to detect it's being used in a place of worship. But to make it compatible with self defence use, or even trickier America's "Stand your Ground" laws, the gun will need to be able to detect if your under a threat and allow you to fire in a church / school / etc if you are under legitimate threat.
This is also needed to feed into the "I carry a gun so i can be the good guy with a gun / hero" belief common in American gun culture, even though in the 1% of times they are on hand when needed, the "good guy with a gun" tends to get shot (often by law enforcement).
The "good guy with a gun" can make mistakes in the heat of a mass shooting, and an AI could help reduce or remove these mistakes made by panicking human users. Guns used in self defensive by a panicking human could adjust their aim to be non-lethal if that would eliminate the threat. I may of trained for a double tap in the centre of mass, and will do that when the adrenaline hits when someone starts running at me with a knife, but a well placed shot in the ankle by my smart gun means I can run away - and not have to defend myself against manslaughter charges.
If the NRA is still around when smart guns become common, and they're acting in the best interests of responsible American gun owners, (two big "ifs"), it would be sensible for them to be for this functionality. It allows good American Patriots to use their legal guns to defend themselves and their property, and stops criminals from using them for crime. It basically is everything Charlton Heston ever said in a speech wrapped up in a single technology. (I wouldn't expect them to be for this though, because they'd act in the best interest of the manufacturers who pay the bulk of their financing, rather than their members, and gun manufactures wouldn't want to do the AI research and put computers in their guns).