diff options
| author | John Clements <clements@racket-lang.org> | 2013-06-25 16:48:03 -0700 |
|---|---|---|
| committer | John Clements <clements@racket-lang.org> | 2013-09-06 13:35:09 -0700 |
| commit | 9d33001a90319fc242dcf43ec3c7e1fa1c11d847 (patch) | |
| tree | 626d19086e8a52d22c09e88551ef32f5bf699568 /src/libsyntax/parse | |
| parent | cd4e637ae4113d4c49a2e5c533b5bf48fbf7cc6e (diff) | |
| download | rust-9d33001a90319fc242dcf43ec3c7e1fa1c11d847.tar.gz rust-9d33001a90319fc242dcf43ec3c7e1fa1c11d847.zip | |
added test for ptr_eq on fresh_name-generated idents
Diffstat (limited to 'src/libsyntax/parse')
| -rw-r--r-- | src/libsyntax/parse/token.rs | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 132ad95e639..29c460c5c3d 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -20,6 +20,7 @@ use std::cmp::Equiv; 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); + } } |
