In my textbook, there is a restriction on universal generalization: UG must not be used within the scope of an indented sequence(i.e. conditional proof or indirect proof) if the instantial variable y is free in the first line of that sequence.
Then the book gives a example using conditional proof:
1.∀x(Rx)→∀x(Sx)$\quad Premise$
$\quad$2.Rx $\quad ACP$
$\quad$3.∀x(Rx) $\quad 2.UG(Invalid)$
$\quad$4.∀x(Sx) $\quad 1,3 \; MP$
$\quad$5.Sx $\quad 4.UI$
6.Rx→Sx $\quad 2-5\; CP$
7.∀x(Rx→Sx) $\quad 6\; UG$
This proof is invalid. However, I don't know what is the logic of this restriction. The UG used in line 3 looks perfectly fine to me. Since the "Rx" in the second line, as my book claims, is a statement function, which does not have truth value unless the variable is replaced by a constant, then what does the assertion of "Rx" in line 2 mean? Probably the assertion of "Rx" means that this statement function is always true. But if I take this meaning, then it seems that line 3 just reexpresses the sense of line 2, and then the UG used here just looks valid. And I find it interesting that this restriction applies only to conditional proof and indirect proof, but why only them? I'm just confused by it.
The problem is that :
is not valid in first-order logic.
Basically, steps 2-3 in the (fallacious) proof of the question amounts to this; assume $\varphi$ [i.e. $Rx$] and infer $\forall x(\varphi)$ [i.e. $\forall x (Rx)$].
In order to show that the formula $\varphi \rightarrow \forall x \varphi$ is not in general correct, consider the following example :
and take as the domain of the interpretation the set $\mathbb N$ of natural numbers.
Thus, the formula :
means :
which is not true in $\mathbb N$.
We have to recall the basic semantic definitions; see Herbert Enderton, A Mathematical Introduction to Logic (2nd - 2001), page 83 :
In our example, if we consider $s$ such that $s(x)=1$, we have that :
Thus :
i.e. the above assignment $s$ does not satisfy our formula in $\mathbb N$.
Thus, according to the semantic "convention" regarding formulae with free variables, having found an assignment $s$ such that the formula is not satisfied by $s$ in $\mathbb N$, we say that the formula is not true in $\mathbb N$. [Note. Intuitively, a formula $\alpha(x)$ with free variable $x$ is said to be true in $\mathfrak A$ if every sentence obtained from it by replacing $x$ by an arbitrary element $s(x) \in |\mathfrak A|$ is true.]
Conclusion
Having found an interpretation in which the formula :
is not true, we conclude that it is not valid.
This is the reason for the restriction regarding the application of $UG$ ...
Added
In David's answer you can find a different explanation : the key-point is the invalidity of :
As you probably know, there are different proof systems for first-order logic : Natural Deduction, Hilbert-style, ...
But all must be sound, i.e. they must not be able to prove invalid formulae.
In order to prevent this, there are some restrictions needed.
Regarding our example, we can roughly say that the problem is connected with the interplay between Generalization (your $UG$) and Conditional Proof (or Deduction Theorem).
Thus, different systems adopt different restrictions :
or (see David's answer) :