Skip to content

Commit 5716abe

Browse files
committed
auto merge of #15537 : jbclements/rust/hygiene-for-methods, r=pcwalton
This patch adds hygiene for methods. This one was more difficult than the others, due principally to issues surrounding `self`. Specifically, there were a whole bunch of places in the code that assumed that a `self` identifier could be discarded and then made up again later, causing the discard of contexts and hygiene breakage.
2 parents ec3efa8 + 4c312b6 commit 5716abe

File tree

23 files changed

+540
-405
lines changed

23 files changed

+540
-405
lines changed

src/libcollections/treemap.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,15 @@ impl<K: Ord, V> TreeMap<K, V> {
185185

186186
macro_rules! bound_setup {
187187
// initialiser of the iterator to manipulate
188-
($iter:expr,
188+
($iter:expr, $k:expr,
189189
// whether we are looking for the lower or upper bound.
190190
$is_lower_bound:expr) => {
191191
{
192192
let mut iter = $iter;
193193
loop {
194194
if !iter.node.is_null() {
195195
let node_k = unsafe {&(*iter.node).key};
196-
match k.cmp(node_k) {
196+
match $k.cmp(node_k) {
197197
Less => iter.traverse_left(),
198198
Greater => iter.traverse_right(),
199199
Equal => {
@@ -230,13 +230,13 @@ impl<K: Ord, V> TreeMap<K, V> {
230230
/// Return a lazy iterator to the first key-value pair whose key is not less than `k`
231231
/// If all keys in map are less than `k` an empty iterator is returned.
232232
pub fn lower_bound<'a>(&'a self, k: &K) -> Entries<'a, K, V> {
233-
bound_setup!(self.iter_for_traversal(), true)
233+
bound_setup!(self.iter_for_traversal(), k, true)
234234
}
235235

236236
/// Return a lazy iterator to the first key-value pair whose key is greater than `k`
237237
/// If all keys in map are not greater than `k` an empty iterator is returned.
238238
pub fn upper_bound<'a>(&'a self, k: &K) -> Entries<'a, K, V> {
239-
bound_setup!(self.iter_for_traversal(), false)
239+
bound_setup!(self.iter_for_traversal(), k, false)
240240
}
241241

242242
/// Get a lazy iterator that should be initialized using
@@ -256,7 +256,7 @@ impl<K: Ord, V> TreeMap<K, V> {
256256
/// If all keys in map are less than `k` an empty iterator is
257257
/// returned.
258258
pub fn mut_lower_bound<'a>(&'a mut self, k: &K) -> MutEntries<'a, K, V> {
259-
bound_setup!(self.mut_iter_for_traversal(), true)
259+
bound_setup!(self.mut_iter_for_traversal(), k, true)
260260
}
261261

262262
/// Return a lazy iterator to the first key-value pair (with the
@@ -265,7 +265,7 @@ impl<K: Ord, V> TreeMap<K, V> {
265265
/// If all keys in map are not greater than `k` an empty iterator
266266
/// is returned.
267267
pub fn mut_upper_bound<'a>(&'a mut self, k: &K) -> MutEntries<'a, K, V> {
268-
bound_setup!(self.mut_iter_for_traversal(), false)
268+
bound_setup!(self.mut_iter_for_traversal(), k, false)
269269
}
270270
}
271271

src/libcore/num/mod.rs

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -769,16 +769,16 @@ pub trait ToPrimitive {
769769
}
770770

771771
macro_rules! impl_to_primitive_int_to_int(
772-
($SrcT:ty, $DstT:ty) => (
772+
($SrcT:ty, $DstT:ty, $slf:expr) => (
773773
{
774774
if size_of::<$SrcT>() <= size_of::<$DstT>() {
775-
Some(*self as $DstT)
775+
Some($slf as $DstT)
776776
} else {
777-
let n = *self as i64;
777+
let n = $slf as i64;
778778
let min_value: $DstT = Bounded::min_value();
779779
let max_value: $DstT = Bounded::max_value();
780780
if min_value as i64 <= n && n <= max_value as i64 {
781-
Some(*self as $DstT)
781+
Some($slf as $DstT)
782782
} else {
783783
None
784784
}
@@ -788,12 +788,12 @@ macro_rules! impl_to_primitive_int_to_int(
788788
)
789789

790790
macro_rules! impl_to_primitive_int_to_uint(
791-
($SrcT:ty, $DstT:ty) => (
791+
($SrcT:ty, $DstT:ty, $slf:expr) => (
792792
{
793793
let zero: $SrcT = Zero::zero();
794794
let max_value: $DstT = Bounded::max_value();
795-
if zero <= *self && *self as u64 <= max_value as u64 {
796-
Some(*self as $DstT)
795+
if zero <= $slf && $slf as u64 <= max_value as u64 {
796+
Some($slf as $DstT)
797797
} else {
798798
None
799799
}
@@ -805,26 +805,26 @@ macro_rules! impl_to_primitive_int(
805805
($T:ty) => (
806806
impl ToPrimitive for $T {
807807
#[inline]
808-
fn to_int(&self) -> Option<int> { impl_to_primitive_int_to_int!($T, int) }
808+
fn to_int(&self) -> Option<int> { impl_to_primitive_int_to_int!($T, int, *self) }
809809
#[inline]
810-
fn to_i8(&self) -> Option<i8> { impl_to_primitive_int_to_int!($T, i8) }
810+
fn to_i8(&self) -> Option<i8> { impl_to_primitive_int_to_int!($T, i8, *self) }
811811
#[inline]
812-
fn to_i16(&self) -> Option<i16> { impl_to_primitive_int_to_int!($T, i16) }
812+
fn to_i16(&self) -> Option<i16> { impl_to_primitive_int_to_int!($T, i16, *self) }
813813
#[inline]
814-
fn to_i32(&self) -> Option<i32> { impl_to_primitive_int_to_int!($T, i32) }
814+
fn to_i32(&self) -> Option<i32> { impl_to_primitive_int_to_int!($T, i32, *self) }
815815
#[inline]
816-
fn to_i64(&self) -> Option<i64> { impl_to_primitive_int_to_int!($T, i64) }
816+
fn to_i64(&self) -> Option<i64> { impl_to_primitive_int_to_int!($T, i64, *self) }
817817

818818
#[inline]
819-
fn to_uint(&self) -> Option<uint> { impl_to_primitive_int_to_uint!($T, uint) }
819+
fn to_uint(&self) -> Option<uint> { impl_to_primitive_int_to_uint!($T, uint, *self) }
820820
#[inline]
821-
fn to_u8(&self) -> Option<u8> { impl_to_primitive_int_to_uint!($T, u8) }
821+
fn to_u8(&self) -> Option<u8> { impl_to_primitive_int_to_uint!($T, u8, *self) }
822822
#[inline]
823-
fn to_u16(&self) -> Option<u16> { impl_to_primitive_int_to_uint!($T, u16) }
823+
fn to_u16(&self) -> Option<u16> { impl_to_primitive_int_to_uint!($T, u16, *self) }
824824
#[inline]
825-
fn to_u32(&self) -> Option<u32> { impl_to_primitive_int_to_uint!($T, u32) }
825+
fn to_u32(&self) -> Option<u32> { impl_to_primitive_int_to_uint!($T, u32, *self) }
826826
#[inline]
827-
fn to_u64(&self) -> Option<u64> { impl_to_primitive_int_to_uint!($T, u64) }
827+
fn to_u64(&self) -> Option<u64> { impl_to_primitive_int_to_uint!($T, u64, *self) }
828828

829829
#[inline]
830830
fn to_f32(&self) -> Option<f32> { Some(*self as f32) }
@@ -841,11 +841,11 @@ impl_to_primitive_int!(i32)
841841
impl_to_primitive_int!(i64)
842842

843843
macro_rules! impl_to_primitive_uint_to_int(
844-
($DstT:ty) => (
844+
($DstT:ty, $slf:expr) => (
845845
{
846846
let max_value: $DstT = Bounded::max_value();
847-
if *self as u64 <= max_value as u64 {
848-
Some(*self as $DstT)
847+
if $slf as u64 <= max_value as u64 {
848+
Some($slf as $DstT)
849849
} else {
850850
None
851851
}
@@ -854,15 +854,15 @@ macro_rules! impl_to_primitive_uint_to_int(
854854
)
855855

856856
macro_rules! impl_to_primitive_uint_to_uint(
857-
($SrcT:ty, $DstT:ty) => (
857+
($SrcT:ty, $DstT:ty, $slf:expr) => (
858858
{
859859
if size_of::<$SrcT>() <= size_of::<$DstT>() {
860-
Some(*self as $DstT)
860+
Some($slf as $DstT)
861861
} else {
862862
let zero: $SrcT = Zero::zero();
863863
let max_value: $DstT = Bounded::max_value();
864-
if zero <= *self && *self as u64 <= max_value as u64 {
865-
Some(*self as $DstT)
864+
if zero <= $slf && $slf as u64 <= max_value as u64 {
865+
Some($slf as $DstT)
866866
} else {
867867
None
868868
}
@@ -875,26 +875,26 @@ macro_rules! impl_to_primitive_uint(
875875
($T:ty) => (
876876
impl ToPrimitive for $T {
877877
#[inline]
878-
fn to_int(&self) -> Option<int> { impl_to_primitive_uint_to_int!(int) }
878+
fn to_int(&self) -> Option<int> { impl_to_primitive_uint_to_int!(int, *self) }
879879
#[inline]
880-
fn to_i8(&self) -> Option<i8> { impl_to_primitive_uint_to_int!(i8) }
880+
fn to_i8(&self) -> Option<i8> { impl_to_primitive_uint_to_int!(i8, *self) }
881881
#[inline]
882-
fn to_i16(&self) -> Option<i16> { impl_to_primitive_uint_to_int!(i16) }
882+
fn to_i16(&self) -> Option<i16> { impl_to_primitive_uint_to_int!(i16, *self) }
883883
#[inline]
884-
fn to_i32(&self) -> Option<i32> { impl_to_primitive_uint_to_int!(i32) }
884+
fn to_i32(&self) -> Option<i32> { impl_to_primitive_uint_to_int!(i32, *self) }
885885
#[inline]
886-
fn to_i64(&self) -> Option<i64> { impl_to_primitive_uint_to_int!(i64) }
886+
fn to_i64(&self) -> Option<i64> { impl_to_primitive_uint_to_int!(i64, *self) }
887887

888888
#[inline]
889-
fn to_uint(&self) -> Option<uint> { impl_to_primitive_uint_to_uint!($T, uint) }
889+
fn to_uint(&self) -> Option<uint> { impl_to_primitive_uint_to_uint!($T, uint, *self) }
890890
#[inline]
891-
fn to_u8(&self) -> Option<u8> { impl_to_primitive_uint_to_uint!($T, u8) }
891+
fn to_u8(&self) -> Option<u8> { impl_to_primitive_uint_to_uint!($T, u8, *self) }
892892
#[inline]
893-
fn to_u16(&self) -> Option<u16> { impl_to_primitive_uint_to_uint!($T, u16) }
893+
fn to_u16(&self) -> Option<u16> { impl_to_primitive_uint_to_uint!($T, u16, *self) }
894894
#[inline]
895-
fn to_u32(&self) -> Option<u32> { impl_to_primitive_uint_to_uint!($T, u32) }
895+
fn to_u32(&self) -> Option<u32> { impl_to_primitive_uint_to_uint!($T, u32, *self) }
896896
#[inline]
897-
fn to_u64(&self) -> Option<u64> { impl_to_primitive_uint_to_uint!($T, u64) }
897+
fn to_u64(&self) -> Option<u64> { impl_to_primitive_uint_to_uint!($T, u64, *self) }
898898

899899
#[inline]
900900
fn to_f32(&self) -> Option<f32> { Some(*self as f32) }
@@ -911,14 +911,14 @@ impl_to_primitive_uint!(u32)
911911
impl_to_primitive_uint!(u64)
912912

913913
macro_rules! impl_to_primitive_float_to_float(
914-
($SrcT:ty, $DstT:ty) => (
914+
($SrcT:ty, $DstT:ty, $slf:expr) => (
915915
if size_of::<$SrcT>() <= size_of::<$DstT>() {
916-
Some(*self as $DstT)
916+
Some($slf as $DstT)
917917
} else {
918-
let n = *self as f64;
918+
let n = $slf as f64;
919919
let max_value: $SrcT = Bounded::max_value();
920920
if -max_value as f64 <= n && n <= max_value as f64 {
921-
Some(*self as $DstT)
921+
Some($slf as $DstT)
922922
} else {
923923
None
924924
}
@@ -952,9 +952,9 @@ macro_rules! impl_to_primitive_float(
952952
fn to_u64(&self) -> Option<u64> { Some(*self as u64) }
953953

954954
#[inline]
955-
fn to_f32(&self) -> Option<f32> { impl_to_primitive_float_to_float!($T, f32) }
955+
fn to_f32(&self) -> Option<f32> { impl_to_primitive_float_to_float!($T, f32, *self) }
956956
#[inline]
957-
fn to_f64(&self) -> Option<f64> { impl_to_primitive_float_to_float!($T, f64) }
957+
fn to_f64(&self) -> Option<f64> { impl_to_primitive_float_to_float!($T, f64, *self) }
958958
}
959959
)
960960
)
@@ -1104,38 +1104,38 @@ pub fn from_f64<A: FromPrimitive>(n: f64) -> Option<A> {
11041104
}
11051105

11061106
macro_rules! impl_from_primitive(
1107-
($T:ty, $to_ty:expr) => (
1107+
($T:ty, $to_ty:ident) => (
11081108
impl FromPrimitive for $T {
1109-
#[inline] fn from_int(n: int) -> Option<$T> { $to_ty }
1110-
#[inline] fn from_i8(n: i8) -> Option<$T> { $to_ty }
1111-
#[inline] fn from_i16(n: i16) -> Option<$T> { $to_ty }
1112-
#[inline] fn from_i32(n: i32) -> Option<$T> { $to_ty }
1113-
#[inline] fn from_i64(n: i64) -> Option<$T> { $to_ty }
1114-
1115-
#[inline] fn from_uint(n: uint) -> Option<$T> { $to_ty }
1116-
#[inline] fn from_u8(n: u8) -> Option<$T> { $to_ty }
1117-
#[inline] fn from_u16(n: u16) -> Option<$T> { $to_ty }
1118-
#[inline] fn from_u32(n: u32) -> Option<$T> { $to_ty }
1119-
#[inline] fn from_u64(n: u64) -> Option<$T> { $to_ty }
1120-
1121-
#[inline] fn from_f32(n: f32) -> Option<$T> { $to_ty }
1122-
#[inline] fn from_f64(n: f64) -> Option<$T> { $to_ty }
1109+
#[inline] fn from_int(n: int) -> Option<$T> { n.$to_ty() }
1110+
#[inline] fn from_i8(n: i8) -> Option<$T> { n.$to_ty() }
1111+
#[inline] fn from_i16(n: i16) -> Option<$T> { n.$to_ty() }
1112+
#[inline] fn from_i32(n: i32) -> Option<$T> { n.$to_ty() }
1113+
#[inline] fn from_i64(n: i64) -> Option<$T> { n.$to_ty() }
1114+
1115+
#[inline] fn from_uint(n: uint) -> Option<$T> { n.$to_ty() }
1116+
#[inline] fn from_u8(n: u8) -> Option<$T> { n.$to_ty() }
1117+
#[inline] fn from_u16(n: u16) -> Option<$T> { n.$to_ty() }
1118+
#[inline] fn from_u32(n: u32) -> Option<$T> { n.$to_ty() }
1119+
#[inline] fn from_u64(n: u64) -> Option<$T> { n.$to_ty() }
1120+
1121+
#[inline] fn from_f32(n: f32) -> Option<$T> { n.$to_ty() }
1122+
#[inline] fn from_f64(n: f64) -> Option<$T> { n.$to_ty() }
11231123
}
11241124
)
11251125
)
11261126

1127-
impl_from_primitive!(int, n.to_int())
1128-
impl_from_primitive!(i8, n.to_i8())
1129-
impl_from_primitive!(i16, n.to_i16())
1130-
impl_from_primitive!(i32, n.to_i32())
1131-
impl_from_primitive!(i64, n.to_i64())
1132-
impl_from_primitive!(uint, n.to_uint())
1133-
impl_from_primitive!(u8, n.to_u8())
1134-
impl_from_primitive!(u16, n.to_u16())
1135-
impl_from_primitive!(u32, n.to_u32())
1136-
impl_from_primitive!(u64, n.to_u64())
1137-
impl_from_primitive!(f32, n.to_f32())
1138-
impl_from_primitive!(f64, n.to_f64())
1127+
impl_from_primitive!(int, to_int)
1128+
impl_from_primitive!(i8, to_i8)
1129+
impl_from_primitive!(i16, to_i16)
1130+
impl_from_primitive!(i32, to_i32)
1131+
impl_from_primitive!(i64, to_i64)
1132+
impl_from_primitive!(uint, to_uint)
1133+
impl_from_primitive!(u8, to_u8)
1134+
impl_from_primitive!(u16, to_u16)
1135+
impl_from_primitive!(u32, to_u32)
1136+
impl_from_primitive!(u64, to_u64)
1137+
impl_from_primitive!(f32, to_f32)
1138+
impl_from_primitive!(f64, to_f64)
11391139

11401140
/// Cast from one machine scalar to another.
11411141
///

src/librustc/metadata/decoder.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -739,10 +739,11 @@ fn get_explicit_self(item: ebml::Doc) -> ast::ExplicitSelf_ {
739739
let explicit_self_kind = string.as_bytes()[0];
740740
match explicit_self_kind as char {
741741
's' => ast::SelfStatic,
742-
'v' => ast::SelfValue,
743-
'~' => ast::SelfUniq,
742+
'v' => ast::SelfValue(special_idents::self_),
743+
'~' => ast::SelfUniq(special_idents::self_),
744744
// FIXME(#4846) expl. region
745-
'&' => ast::SelfRegion(None, get_mutability(string.as_bytes()[1])),
745+
'&' => ast::SelfRegion(None, get_mutability(string.as_bytes()[1]),
746+
special_idents::self_),
746747
_ => fail!("unknown self type code: `{}`", explicit_self_kind as char)
747748
}
748749
}

src/librustc/metadata/encoder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -628,10 +628,10 @@ fn encode_explicit_self(ebml_w: &mut Encoder, explicit_self: ast::ExplicitSelf_)
628628

629629
// Encode the base self type.
630630
match explicit_self {
631-
SelfStatic => { ebml_w.writer.write(&[ 's' as u8 ]); }
632-
SelfValue => { ebml_w.writer.write(&[ 'v' as u8 ]); }
633-
SelfUniq => { ebml_w.writer.write(&[ '~' as u8 ]); }
634-
SelfRegion(_, m) => {
631+
SelfStatic => { ebml_w.writer.write(&[ 's' as u8 ]); }
632+
SelfValue(_) => { ebml_w.writer.write(&[ 'v' as u8 ]); }
633+
SelfUniq(_) => { ebml_w.writer.write(&[ '~' as u8 ]); }
634+
SelfRegion(_, m, _) => {
635635
// FIXME(#4846) encode custom lifetime
636636
ebml_w.writer.write(&['&' as u8]);
637637
encode_mutability(ebml_w, m);

src/librustc/middle/resolve.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use syntax::ast;
2525
use syntax::ast_util::{local_def};
2626
use syntax::ast_util::{walk_pat, trait_method_to_ty_method};
2727
use syntax::ext::mtwt;
28+
use syntax::parse::token::special_names;
2829
use syntax::parse::token::special_idents;
2930
use syntax::parse::token;
3031
use syntax::codemap::{Span, DUMMY_SP, Pos};
@@ -830,9 +831,9 @@ struct Resolver<'a> {
830831
current_self_type: Option<Ty>,
831832

832833
// The ident for the keyword "self".
833-
self_ident: Ident,
834+
self_name: Name,
834835
// The ident for the non-keyword "Self".
835-
type_self_ident: Ident,
836+
type_self_name: Name,
836837

837838
// The idents for the primitive types.
838839
primitive_type_table: PrimitiveTypeTable,
@@ -926,8 +927,8 @@ impl<'a> Resolver<'a> {
926927
current_trait_ref: None,
927928
current_self_type: None,
928929

929-
self_ident: special_idents::self_,
930-
type_self_ident: special_idents::type_self,
930+
self_name: special_names::self_,
931+
type_self_name: special_names::type_self,
931932

932933
primitive_type_table: PrimitiveTypeTable::new(),
933934

@@ -3628,8 +3629,8 @@ impl<'a> Resolver<'a> {
36283629
// Create a new rib for the self type.
36293630
let self_type_rib = Rib::new(ItemRibKind);
36303631

3631-
// plain insert (no renaming)
3632-
let name = self.type_self_ident.name;
3632+
// plain insert (no renaming, types are not currently hygienic....)
3633+
let name = self.type_self_name;
36333634
self_type_rib.bindings.borrow_mut()
36343635
.insert(name, DlDef(DefSelfTy(item.id)));
36353636
self.type_ribs.borrow_mut().push(self_type_rib);
@@ -5159,8 +5160,8 @@ impl<'a> Resolver<'a> {
51595160
false // Stop advancing
51605161
});
51615162

5162-
if method_scope && token::get_name(self.self_ident.name).get()
5163-
== wrong_name.as_slice() {
5163+
if method_scope && token::get_name(self.self_name).get()
5164+
== wrong_name.as_slice() {
51645165
self.resolve_error(
51655166
expr.span,
51665167
"`self` is not available \
@@ -5532,6 +5533,7 @@ impl<'a> Resolver<'a> {
55325533
collect_mod(idents, &*module.upgrade().unwrap());
55335534
}
55345535
BlockParentLink(ref module, _) => {
5536+
// danger, shouldn't be ident?
55355537
idents.push(special_idents::opaque);
55365538
collect_mod(idents, &*module.upgrade().unwrap());
55375539
}

0 commit comments

Comments
 (0)