Can UK PhD studentships legally effectively bar EU applicants by stating that "all eligible applicants must also have been resident in the UK"? I am aware that many UK PhD studentships are only open to UK/EU applicants (question 1, question 2).  In my understanding, they cannot (pre-Brexit) legally discriminate between EU and UK applicants, but they can bar non-EU applicants.  But
my eyes fell upon eligibility requirements for this University of Newcastle PhD studentship, emphasis and link mine, which effectively bars EU applicants, too:


  This studentship is available to UK applicants and EU applicants with settled status in the UK, meaning they have no restrictions on how long they can stay. All eligible applicants must also have been ‘ordinarily resident’ in the UK for 3 years prior to the start of the studentship, and not been residing in the UK wholly or mainly for the purpose of full-time education.


Such an additional requirement effectively discriminates between UK and EU applicants.  The vast majority of EU applicants will not have been also resident in the UK for 3 years prior to the start of the studentship, or if they have, have been so for the purposes of education.  Even some UK citizens will be ineligible if they spent any time studying abroad.

Is such discrimination between UK and EU applicants legal under EU law?  It would appear to effectively amount to the same as stating "Europeans need not apply" by different means.
You have answered your own question in the question: states can (under EU law) put conditions on, e.g., funding, based on residence, but not based on citizenship. As UK citizens who are not ordinarily resident would not be eligible for this funding, this does not discriminate on citizenship, so this is legal (*).

Anecdotally, in my experience a low single-digit percentage of "UK" applicants fail the ordinarily resident test, and I have seen at my institution roughly 5 students over the past 10 years refused funding from similar sources for this reason.

(*) I am definitely not a lawyer: the case law around ordinarily resident status shows that this issue is not unique to universities.