]> git.lizzy.rs Git - rust.git/commitdiff
added test for ptr_eq on fresh_name-generated idents
authorJohn Clements <clements@racket-lang.org>
Tue, 25 Jun 2013 23:48:03 +0000 (16:48 -0700)
committerJohn Clements <clements@racket-lang.org>
Fri, 6 Sep 2013 20:35:09 +0000 (13:35 -0700)
src/libsyntax/parse/token.rs

index 132ad95e639bcf1ea2620a3f450affbf9e287585..29c460c5c3d08ac8cd0fe89ebfdf3d63717108cf 100644 (file)
@@ -20,6 +20,7 @@
 use std::local_data;
 use std::rand;
 use std::rand::RngUtil;
+use std::ptr::to_unsafe_ptr;
 
 #[deriving(Clone, Encodable, Decodable, Eq, IterBytes)]
 pub enum binop {
@@ -544,15 +545,31 @@ pub fn gensym_ident(str : &str) -> ast::Ident {
 }
 
 // create a fresh name that maps to the same string as the old one.
+// note that this guarantees that ptr_eq(ident_to_str(src),interner_get(fresh_name(src)));
+// that is, that the new name and the old one are connected to ptr_eq strings.
 pub fn fresh_name(src : &ast::Ident) -> Name {
     gensym(ident_to_str(src))
     // following: debug version. Could work in final except that it's incompatible with
     // good error messages and uses of struct names in ambiguous could-be-binding
-    // locations.
+    // locations. Also definitely destroys the guarantee given above about ptr_eq.
     /*let num = rand::rng().gen_uint_range(0,0xffff);
     gensym(fmt!("%s_%u",ident_to_str(src),num))*/
 }
 
+// it looks like there oughta be a str_ptr_eq fn, but no one bothered to implement it?
+pub fn str_ptr_eq<T>(a: @str, b: @str) -> bool {
+    // doesn't compile! ...because of rebase mangling. this should be fixed
+    // in the commit that follows this.
+    let (a_ptr, b_ptr): (*uint, *uint) = (to_unsafe_ptr(a), to_unsafe_ptr(b));
+    a_ptr == b_ptr
+}
+
+
+
+// return true when two identifiers refer (through the intern table) to the same ptr_eq
+// string. This is used to compare identifiers in places where hygienic comparison is
+// not wanted (i.e. not lexical vars).
+
 // create a fresh mark.
 pub fn fresh_mark() -> Mrk {
     gensym("mark")
@@ -698,5 +715,19 @@ pub fn is_reserved_keyword(tok: &Token) -> bool {
 #[cfg(test)]
 mod test {
     use super::*;
+    use std::io;
+    use std::managed;
+    use ast;
+    use ast_util;
+
+
+    #[test] fn t1() {
+        let ghi = str_to_ident("ghi");
+        assert_eq!(ident_to_str(&ghi),@"ghi");
+        let fresh = ast::Ident::new(fresh_name(&ghi));
+        assert_eq!(ident_to_str(&fresh),@"ghi");
+        assert!(str_ptr_eq(ident_to_str(&ghi),ident_to_str(&fresh)));
+        assert_eq!(3,4);
+    }
 
 }