Residency in an EU candidate country

I would appreciate the feedback from the community on the following scenario.

Let’s imagine someone obtains a temp residency in an EU-candidate country.
Then in due course that country joins the EU.
What would happen to the residence? Will it become automatically an EU-residence, and continued towards EU PR without any breaks? If so, what rights a person with an EU PR has? E.g. can they live and work in any EU country? Or still only in the original country?