FOM has an explanation on this with regard to PhD and postdoc.
https://www.bfm.admin.ch/content/dam...ngen-aug-f.pdf
Extraction from section 3.4.3.3 which covers the C permit for EU/EFTA nationals after 5 years residence.
Original version :
Ne sont pas pris en compte dans le calcul du délai de cinq ans les séjours en Suisse à caractère temporaire (formation, études, traitement médical, cure, séjour de courte durée, etc.). En revanche, les séjours effectués à des fins de formation ou de perfectionnement sont comptabilisés lorsque, une fois ceux-ci achevés, l’étranger a été en possession d’une autorisation de séjour durable pendant deux ans sans interruption (cf. art. 34, al. 5, LEtr).
Pour cette raison, les séjours que les ressortissants UE/AELE effectuent en tant que doctorant ou post-doctorant ne sont pas pris en considération lors du calcul du délai de cinq ans vu que leur séjour est considéré comme temporaire. Toutefois, si le doctorant ou le post-doctorant possédait, pendant son activité scientifique, un contrat de travail (activité lucrative supérieure à quinze heures par semaine), le séjour est compté si l’activité scientifique est suivie d’une prise d’emploi dans l’économie privée ou l’administration publique. La règle est valable également lorsque l’intéressé poursuit son activité scientifique au lieu où il a accompli sa formation ou son perfectionnement (doctorat/post-doctorat). Outre le séjour prévu (cinq ou dix ans selon la nationalité du requérant), l’octroi de l’autorisation d’établissement suppose l’existence d’un contrat de travail portant sur un engagement d’une durée supérieure à une année.
Les doctorants et les post-doctorants possédant un contrat de travail (portant sur plus de quinze heures hebdomadaires) sont considérés comme travailleurs au sens du droit communautaire. Si, au terme de leur activité scientifique, il y a prise d’emploi comme mentionné ci-dessus et qu’ils sont par conséquent intégrés sur le plan professionnel, il n’y a pas de raison de les pénaliser par rapport aux autres travailleurs étrangers qui ont travaillé en Suisse pendant le même laps de temps.
Les ressortissants des autres Etats peuvent obtenir, en principe, l’autorisation d’établissement après un séjour régulier et ininterrompu de dix ans.
Google translation (with my hand-correction for major errors):
residence in Switzerland on a temporary basis (training, education, medical treatment, cure, short stay, etc.) is not taken into account in computing the period of five years. In contrast, stays for the purpose of training and development are recognized when, once they have been completed, the alien was in possession of a "long" residence permit for two years without interruption (cf. art. 34 , al. 5 FNA).
For this reason, residence of the EU / EFTA nationals engaged as a PhD student or post-doc are not considered when calculating the five years since their stay is considered temporary. However, if the student or postdoc possessed during his scientific activity, employment contracts (greater than fifteen hours per week paid employment), the stay is counted if the science is followed by taking up employment in the private sector or government. The rule also applies when the person continues his scientific activity at the place where he completed training or education (PhD / postdoc). Besides the planned stay (five to ten years depending on the nationality of the applicant), the granting of permanent residence permit requires the existence of a contract of employment for a commitment for a period exceeding one year.
PhD students and postdocs with an employment contract (covering more than fifteen hours per week) are considered employees within the meaning of Community law. If at the end of their scientific activity, there taking up employment as mentioned above and are therefore built on a professional level, there is no reason to penalize them in relation to other workers foreigners who have worked in Switzerland for the same length of time.
Nationals of other States may obtain, in principle, C permit after a regular and uninterrupted stay of ten years.