I currently work for a smaller private company. It doesn't pay me as much as a large corporation in the industry. Were my compensation public, I would forfeit the option to increase it significantly again.
I work here because I live next door, like the people and the work environment. It is worth it to me. Public registers would not reflect these choices. Otherwise I would need to keep up appearances and optimise my compensation. I like to keep these options.
Not relevant in Norway because everyone earns a lot. Sweden seems to be a country with significant social pressure, not really a role model for me.
That said, under no circumstances should it be illegal to talk about your compensation. Any company that forbids you to do so probably has some dark secrets here.