Let $X$ be an irreducible variety and let $U \subset X$ be a proper open set. Question : can there be a morphism $f : X \rightarrow U$ such that the composition $U \hookrightarrow X \stackrel{f}{\rightarrow} U$ is the identity?

My guess is that the answer is "no", but I can't seem to prove it.

One observation is that if $f$ exists, then it would give a procedure for taking a regular function $\phi : U \rightarrow k$ and extending it to all of $X$. But this can definitely be done in some cases; for instance, if $X = \mathbb{A}^2$ and $U = \mathbb{A}^2 \setminus \{(0,0)\}$.