It's because of the way compgen
deals with quote characters and escape characters in the target. In effect, it takes a very literal view of what completion might mean.
Let's consider something like the first case, but where the possible words don't have any special characters:
ab1
ab2
In bash, you could type the first of these words in any of the following ways (amongst others):
ab1
a'b'1
a''b1
a'b1'
So both ab1
and ab2
are possible completions of a'
. And that's what compgen
returns.
Now, let's go back to the actual targets in the question:
a'b1
a'b2
The first of these could be typed in either of these ways (amongst others):
a\'b1
a''\'b1
but it cannot be typed like this:
a'\''b1
So all of the following can be completed to either of the targets:
a
a'
a\
But the following cannot be
a'b