added test for ptr_eq on fresh_name-generated idents

This commit is contained in:
John Clements 2013-06-25 16:48:03 -07:00
parent cd4e637ae4
commit 9d33001a90

View File

@ -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);
}
}