The Mishna 41b speaks about one who was chased by goyim out of his techum and ends up in an enclosed area, Rabban Gamliel and R. Elazar Ben Azarya consider the entire enclosure to be like 4 amos and permit him to walk anywhere within. Although R. Yehoshua and R. Akiva disagree and limit him to 4 amos, the gemara 42b paskens in accordance with Rabban Gamliel. In truth, that itself is a dispute between Rav and Shmuel whether we pasken like Rabban Gamliel only in regard to a boat that sets sail on Shabbos since the person began Shabbos in those mechitzos, but not when he was placed in an enclosed area on Shabbos, Tosafos 43a writes that we follow the opinion of Rav who rules like Rabban Gamliel even by דיר וסוהר. Based on this, Tosafos rejects the opinion of the Rashbam who writes that if one entered a boat on Shabbos that then set sail he would be bound to his 4 amos. That would be true if we differentiate between the case of the boat and דיר וסוהר (and only permit it by a boat when he began shabbos in that enclosure), but being that even by דיר וסוהר we pasken like Rabban Gamliel that one can walk throughout the entire enclosure, the same would be true for one who enters a boat on shabbos (Rabbeinu Chananel paskens like Shmuel, but the Rif also paskens like Rav).
It is not clear what the halacha is if one were to intentionally leave their techum and find himself in an enclosure, are we stricter in that case to limit him to his 4 amos? The Shulchan Aruch (405:6) writes that we are only lenient to consider the enclosure to be 4 amos if he were transported there by goyim, or some other level of accident, but not if he went there on his own. - אבל אם יצא חוץ לתחומו לדעת, אע"פ שהוא בתוך אחד מאלו אין לו אלא ד' אמות. Apparently, when one intentionally violates the issur of techum, we impose a penalty on him and don't allow him to consider an enclosed area to be like within his 4 amos. The Biur Halacha writes that this is the opinion of the Rambam and many other Rishonim. However, Tosafos 29a (last answer) and the Rashba and Ritva all hold that even if one leaves his techum intentionally and ends up in an enclosed area, he can consider the entire enclosure to be within his 4 amos.
It seems to me that there is a contradiction between Rashi on 42a and 42b regarding this point. Rashi in both places is trying to justify the statement of Rav Nachman 42a that if one begins shabbos in an empty field and on Shabbos there is a wall built around him, he is still bound to his original 2000 Amah techum and cannot walk throughout the enclosure. Why is this different than the case of Rabban Gamliel where one is airlifted and dropped in a דיר וסוהר where we allow him to walk throughout the enclosure? Rashi 42a answers that in the דיר וסוהר case where otherwise we would only allow him to have 4 amos, we are lenient on him to consider the entire enclosure like 4 amos. But in the case where one begins shabbos in an empty field, he is not stuck, he has a techum of 2000 amos, so we have no reason to be especially lenient and give the area a status of 4 amos. Rashi makes no mention of the nature of how he ended up in that situation, implying that it doesn't matter. However, Rashi 42b d.h. lo gazrinan, answer the question differently, by saying that when goyim transport him against his will or some other type of o'nes, we are easy on him to consider the entire area like 4 amos, but if nothing is done against his will, we don't. According to Rashi on 42a we should apply the leniency even if he willfully leaves his techum, but according to Rashi on 42b we only are lenient to consider the entire enclosure to be 4 amos if he ends up there due to some sort of o'nes.
As an aside, we also learn from Rashi 42b that the restriction of not considering the enclosure to be like his 4 amos, is not a penalty for violating techum, rather it is a leniency that we only provide in a situation of o'nes. Therefore, if he wasn't o'nes, even though he wasn't מזיד, we also don't allow him to walk within the entire enclosure.