Halachically it is prohibited to think about Torah in the bathroom. However, sometimes I'll get up to go to the bathroom while I'm in the middle of a tough sugya and I'll find myself still thinking about my learning. Sometimes I have by accident come up with a solution to a difficult Rashi or Tosfos. What is the status of such Torah? Is it assur behana'a? Was it a mitzvah habaah b'aveirah? From a Kabbalistic standpoint, is the Torah unholy as it was thought in a unclean place?
Shulchan Aruch HaRav writes (Hilchos Talmud Torah 3:8):
אסור לתלמיד חכם לעמוד במקום הטינופת לפי שאי אפשר לו בלא הרהורי תורה ומכל מקום מותר לו ליכנס לבית הכסא או למרחץ אף מתוך פלפול והלכה שאינה פסוקה ואין חוששין שיהרהר שם בה כמו שחוששין לכך בתפלה וגם אם יבא לו הרהור בעל כרחו שלא ברצונו אנוס הוא ואפילו אם מדבר בה לאונסו מפני רוב רגילותו לדבר בה כמעשה דרבי אלעזר ברבי שמעון
It is forbidden for a Torah Scholar to stand in a dirty place because he will be unable to go without thinking about Torah. Nevertheless he is allowed to go to the toilet or bathroom even in the middle of Pilpul and an undecided Halacha, and we are not concerned that he will think there (whereas we are concerned about this for Tefilah) and even if he will think about it by force majeure against his will this is an Oneis. And even if he speaks it out loud in his Oneis because of his constant habit to speak about it like the story of Rabbi Elazar B'Rabbi Shimon. (Emphasis added).
That last reference is to Zevachim 102b:
אמר רבא האי דינא מרבי אלעזר ברבי שמעון גמירנא דאמר בבית הכסא
Rava said this law we learned from Rabbi Elazar B'Rabbi Shimon who said in the toilet ...
The Talmud concludes the point:
והיכי עביד הכי ... לאונסו שאני
How could he do this ... in his Oneis is different.
So the Talmud has no problem preserving a Halacha which was sourced from a saying in the bathroom.
Edit: I should add that it is a standard recommendation (as quoted in the footnotes there) to have something secular available in the bathroom to take your mind off the Torah. Math or a dictionary are two I've heard of.
The Rebbe first brings the Talmud in Zevachim discussed in the other answer and then quotes the Yerushalmi (Brachos 3:4, 26b) which frames the discussion differently and says that Rav Zeira and Rabbi Elazar B'Rabbi Shimon said they had great insights and resolved difficult problems in the bathroom.
(A side point - A simple reading of the Yerushalmi might imply [unlike the Bavli] that they pasken like Chizkia who says one is allowed to think Torah in the toilet, and that they are saying they got their best ideas there).
The Munkatcher Rebbe quotes a teaching that he received that this should not be understood simply, rather the point being, in the way of the teaching of the Ba'al Shem Tov, that there are sparks of holiness hidden even in such lowly places, and if R. Zeira got that idea in the bathroom, he did not push it away and would use it if he saw it was a proper idea.
He then harmonizes the Bavli and the Yerushalmi with regard to Rabbi Elazar B'Rabbi Shimon, in that it is saying even if he got the idea against his will, he would think about it again in a holy place in order to elevate the idea into holiness.
The crux of the explanation is:
אם כן הוא מן השמים להוציא הנצוץ הקדוש הזה שהוא בתוך העומק בור תחתיות על כן נפל שם זה בדעתו והוא סברא עמוקה באמתת קדושתה בעצם שהוא שכל הישר בדברי תורה. ע״כ לא יניח הניצוץ הלז לדחותו
If so [that the idea was a proper idea] it is from Heaven in order to take out this holy spark which is in the depths of the deep pit. Therefore this idea came into his mind and it is a deep idea in its true essential holiness as it is a proper idea in words of Torah. Therefore he didn't leave this spark behind to push it away.
He concludes that this is the way of the Tanaim and Amoraim, to not leave anything in the world, because it serves the purpose of elevating the sparks of holiness that are found in the "prison pit" as is known from the teachings of the Ba'al Shem Tov, as transmitted by his students.