Comment from an Airbus Pilot:
This is called controlled rest. This is done by reclining the seat and there are procedures in place to make sure everything is safe.
Imagine flying a 6 hour flight overnight without sleeping, that is much more dangerous, so this procedure was introduced. Normally pilot take turns napping, and they have a certain period of time after they wake up where they are not allowed to touch the controls.