From 6d09fc2cd80231aab527a27e8112e6167a15f042 Mon Sep 17 00:00:00 2001 From: John Clements Date: Thu, 7 Feb 2013 17:58:50 -0800 Subject: [PATCH 1/9] removed reference to crate file keywords --- doc/rust.md | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/doc/rust.md b/doc/rust.md index fc8ce9f9c38f..3013fe0e0eb8 100644 --- a/doc/rust.md +++ b/doc/rust.md @@ -202,13 +202,7 @@ grammar as double-quoted strings. Other tokens have exact rules given. ### Keywords -The keywords in [crate files](#crate-files) are the following strings: - -~~~~~~~~ {.keyword} -mod priv pub use -~~~~~~~~ - -The keywords in [source files](#source-files) are the following strings: +The keywords are the following strings: ~~~~~~~~ {.keyword} as assert From 25c4676dfa805e027564116b9c37fee7aeaf1cc4 Mon Sep 17 00:00:00 2001 From: John Clements Date: Mon, 4 Feb 2013 13:15:17 -0800 Subject: [PATCH 2/9] Commenting, test cases, cleanup --- src/libcore/dvec.rs | 2 +- src/libsyntax/ast.rs | 11 ++++++- src/libsyntax/ext/base.rs | 12 ++++---- src/libsyntax/ext/tt/transcribe.rs | 6 ++-- src/libsyntax/parse/common.rs | 26 +++++++++++++++-- src/libsyntax/parse/mod.rs | 47 +++++++++++++++++++++++++++++- src/libsyntax/parse/parser.rs | 47 +++++++++++++++++++----------- src/libsyntax/parse/token.rs | 1 + 8 files changed, 121 insertions(+), 31 deletions(-) diff --git a/src/libcore/dvec.rs b/src/libcore/dvec.rs index fe36ed159603..c7a0300a9789 100644 --- a/src/libcore/dvec.rs +++ b/src/libcore/dvec.rs @@ -229,7 +229,7 @@ impl DVec { impl DVec { /** - * Append all elements of a vector to the end of the list + * Append all elements of a vector to the end of the list. * * Equivalent to `append_iter()` but potentially more efficient. */ diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index b109333c3b59..b26cab7694c9 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -779,10 +779,19 @@ pub enum expr_ { #[auto_decode] #[doc="For macro invocations; parsing is delegated to the macro"] pub enum token_tree { + // a single token tt_tok(span, ::parse::token::Token), + // a delimited sequence (the delimiters appear as the first + // and last elements of the vector) tt_delim(~[token_tree]), - // These only make sense for right-hand-sides of MBE macros + // These only make sense for right-hand-sides of MBE macros: + + // a kleene-style repetition sequence with a span, a tt_forest, + // an optional separator (?), and a boolean where true indicates + // zero or more (*), and false indicates one or more (+). tt_seq(span, ~[token_tree], Option<::parse::token::Token>, bool), + + // a syntactic variable that will be filled in by macro expansion. tt_nonterminal(span, ident) } diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 8c3db21f4ea4..a85a7990ace9 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -44,8 +44,8 @@ pub struct SyntaxExpanderTT { span: Option } -pub type SyntaxExpanderTTFun = fn@(ext_ctxt, span, ~[ast::token_tree]) - -> MacResult; +pub type SyntaxExpanderTTFun + = fn@(ext_ctxt, span, ~[ast::token_tree]) -> MacResult; pub struct SyntaxExpanderTTItem { expander: SyntaxExpanderTTItemFun, @@ -59,7 +59,7 @@ pub enum MacResult { MRExpr(@ast::expr), MRItem(@ast::item), MRAny(fn@()-> @ast::expr, fn@()-> Option<@ast::item>, fn@()->@ast::stmt), - MRDef(MacroDef) + MRDef(MacroDef), } pub enum SyntaxExtension { @@ -78,9 +78,11 @@ pub enum SyntaxExtension { // A temporary hard-coded map of methods for expanding syntax extension // AST nodes into full ASTs pub fn syntax_expander_table() -> HashMap<~str, SyntaxExtension> { + // utility function to simplify creating NormalTT syntax extensions fn builtin_normal_tt(f: SyntaxExpanderTTFun) -> SyntaxExtension { NormalTT(SyntaxExpanderTT{expander: f, span: None}) } + // utility function to simplify creating ItemTT syntax extensions fn builtin_item_tt(f: SyntaxExpanderTTItemFun) -> SyntaxExtension { ItemTT(SyntaxExpanderTTItem{expander: f, span: None}) } @@ -112,8 +114,8 @@ pub fn syntax_expander_table() -> HashMap<~str, SyntaxExtension> { ext::deriving::expand_deriving_iter_bytes)); // Quasi-quoting expanders - syntax_expanders.insert( - ~"quote_tokens", builtin_normal_tt(ext::quote::expand_quote_tokens)); + syntax_expanders.insert(~"quote_tokens", + builtin_normal_tt(ext::quote::expand_quote_tokens)); syntax_expanders.insert(~"quote_expr", builtin_normal_tt(ext::quote::expand_quote_expr)); syntax_expanders.insert(~"quote_ty", diff --git a/src/libsyntax/ext/tt/transcribe.rs b/src/libsyntax/ext/tt/transcribe.rs index aa9036d295e5..75120e9c0bc8 100644 --- a/src/libsyntax/ext/tt/transcribe.rs +++ b/src/libsyntax/ext/tt/transcribe.rs @@ -121,15 +121,15 @@ pure fn lookup_cur_matched_by_matched(r: @mut TtReader, vec::foldl(start, r.repeat_idx, red) } -fn lookup_cur_matched(r: @mut TtReader, name: ident) -> @named_match { +fn lookup_cur_matched(r: &TtReader, name: ident) -> @named_match { lookup_cur_matched_by_matched(r, r.interpolations.get(&name)) } enum lis { lis_unconstrained, lis_constraint(uint, ident), lis_contradiction(~str) } -fn lockstep_iter_size(t: token_tree, r: @mut TtReader) -> lis { - fn lis_merge(lhs: lis, rhs: lis, r: @mut TtReader) -> lis { +fn lockstep_iter_size(t: token_tree, r: &TtReader) -> lis { + fn lis_merge(lhs: lis, rhs: lis, r: &TtReader) -> lis { match lhs { lis_unconstrained => rhs, lis_contradiction(_) => lhs, diff --git a/src/libsyntax/parse/common.rs b/src/libsyntax/parse/common.rs index e0d53fadfa0b..7e74163b6bf3 100644 --- a/src/libsyntax/parse/common.rs +++ b/src/libsyntax/parse/common.rs @@ -20,6 +20,8 @@ use core::option::{None, Option, Some}; use core::option; use std::oldmap::HashMap; +// seq_sep : a sequence separator (token) +// and whether a trailing separator is allowed. pub type seq_sep = { sep: Option, trailing_sep_allowed: bool @@ -51,6 +53,8 @@ pub impl Parser { + token_to_str(self.reader, self.token) + ~"`"); } + // expect and consume the token t. Signal an error if + // the next token is not t. fn expect(t: token::Token) { if self.token == t { self.bump(); @@ -88,6 +92,8 @@ pub impl Parser { return self.parse_ident(); } + // consume token 'tok' if it exists. Returns true if the given + // token was present, false otherwise. fn eat(tok: token::Token) -> bool { return if self.token == tok { self.bump(); true } else { false }; } @@ -185,6 +191,8 @@ pub impl Parser { } } + // expect and consume a GT. if a >> is seen, replace it + // with a single > and continue. fn expect_gt() { if self.token == token::GT { self.bump(); @@ -202,16 +210,19 @@ pub impl Parser { } } + // parse a sequence bracketed by '<' and '>', stopping + // before the '>'. fn parse_seq_to_before_gt(sep: Option, f: fn(Parser) -> T) -> ~[T] { let mut first = true; let mut v = ~[]; while self.token != token::GT + // wait... isn't this going to eat a whole '>>' ? && self.token != token::BINOP(token::SHR) { match sep { Some(ref t) => { if first { first = false; } - else { self.expect((*t)); } + else { self.expect(*t); } } _ => () } @@ -229,6 +240,7 @@ pub impl Parser { return v; } + // parse a sequence bracketed by '<' and '>' fn parse_seq_lt_gt(sep: Option, f: fn(Parser) -> T) -> spanned<~[T]> { let lo = self.span.lo; @@ -239,6 +251,9 @@ pub impl Parser { return spanned(lo, hi, result); } + // parse a sequence, including the closing delimiter. The function + // f must consume tokens until reaching the next separator or + // closing bracket. fn parse_seq_to_end(ket: token::Token, sep: seq_sep, f: fn(Parser) -> T) -> ~[T] { let val = self.parse_seq_to_before_end(ket, sep, f); @@ -246,7 +261,9 @@ pub impl Parser { return val; } - + // parse a sequence, not including the closing delimiter. The function + // f must consume tokens until reaching the next separator or + // closing bracket. fn parse_seq_to_before_end(ket: token::Token, sep: seq_sep, f: fn(Parser) -> T) -> ~[T] { let mut first: bool = true; @@ -255,7 +272,7 @@ pub impl Parser { match sep.sep { Some(ref t) => { if first { first = false; } - else { self.expect((*t)); } + else { self.expect(*t); } } _ => () } @@ -265,6 +282,9 @@ pub impl Parser { return v; } + // parse a sequence, including the closing delimiter. The function + // f must consume tokens until reaching the next separator or + // closing bracket. fn parse_unspanned_seq(bra: token::Token, ket: token::Token, sep: seq_sep, diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index b8e671b32659..b863e2bd0e49 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -183,7 +183,6 @@ pub fn new_parser_from_file(sess: parse_sess, let srdr = lexer::new_string_reader(sess.span_diagnostic, filemap, sess.interner); - Ok(Parser(sess, cfg, srdr as reader)) } @@ -222,3 +221,49 @@ pub fn new_parser_from_tts(sess: parse_sess, cfg: ast::crate_cfg, return Parser(sess, cfg, trdr as reader) } + +#[cfg(test)] +mod test { + use super::*; + use std::serialize::Encodable; + use std; + use core::dvec; + use core::str; + use util::testing::*; + + #[test] fn to_json_str (val: Encodable) -> ~str { + let bw = @io::BytesWriter {bytes: dvec::DVec(), pos: 0}; + val.encode(~std::json::Encoder(bw as io::Writer)); + str::from_bytes(bw.bytes.data) + } + + #[test] fn alltts () { + let tts = parse_tts_from_source_str( + ~"bogofile", + @~"fn foo (x : int) { x; }", + ~[], + new_parse_sess(None)); + check_equal(to_json_str(tts as Encodable::), + //[["tt_tok",["IDENT","fn"]]] + ~"abc" + ); + let ast1 = new_parser_from_tts(new_parse_sess(None),~[],tts) + .parse_item(~[]); + let ast2 = parse_item_from_source_str( + ~"bogofile", + @~"fn foo (x : int) { x; }", + ~[],~[], + new_parse_sess(None)); + check_equal(ast1,ast2); + } +} + +// +// Local Variables: +// mode: rust +// fill-column: 78; +// indent-tabs-mode: nil +// c-basic-offset: 4 +// buffer-file-coding-system: utf-8-unix +// End: +// diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 6382413b0816..3c1f306ff079 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -182,7 +182,8 @@ pure fn maybe_append(+lhs: ~[attribute], rhs: Option<~[attribute]>) /* ident is handled by common.rs */ -pub fn Parser(sess: parse_sess, +pub fn Parser(sess: parse_sess + , cfg: ast::crate_cfg, +rdr: reader) -> Parser { @@ -1238,6 +1239,8 @@ pub impl Parser { return e; } + // parse an optional separator followed by a kleene-style + // repetition token (+ or *). fn parse_sep_and_zerok() -> (Option, bool) { if self.token == token::BINOP(token::STAR) || self.token == token::BINOP(token::PLUS) { @@ -1258,20 +1261,18 @@ pub impl Parser { } } + // parse a single token tree from the input. fn parse_token_tree() -> token_tree { maybe_whole!(deref self, nt_tt); - fn parse_tt_tok(p: Parser, delim_ok: bool) -> token_tree { + fn parse_non_delim_tt_tok(p: Parser) -> token_tree { maybe_whole!(deref p, nt_tt); match p.token { token::RPAREN | token::RBRACE | token::RBRACKET - if !delim_ok => { + => { p.fatal(~"incorrect close delimiter: `" + token_to_str(p.reader, p.token) + ~"`"); } - token::EOF => { - p.fatal(~"file ended in the middle of a macro invocation"); - } /* we ought to allow different depths of unquotation */ token::DOLLAR if p.quote_depth > 0u => { p.bump(); @@ -1282,32 +1283,43 @@ pub impl Parser { seq_sep_none(), |p| p.parse_token_tree()); let (s, z) = p.parse_sep_and_zerok(); - return tt_seq(mk_sp(sp.lo ,p.span.hi), seq.node, s, z); + tt_seq(mk_sp(sp.lo ,p.span.hi), seq.node, s, z) } else { - return tt_nonterminal(sp, p.parse_ident()); + tt_nonterminal(sp, p.parse_ident()) } } - _ => { /* ok */ } + _ => { + parse_any_tt_tok(p) + } } - let res = tt_tok(p.span, p.token); - p.bump(); - return res; } - return match self.token { + // turn the next token into a tt_tok: + fn parse_any_tt_tok(p: Parser) -> token_tree{ + let res = tt_tok(p.span, p.token); + p.bump(); + res + } + + match self.token { + token::EOF => { + self.fatal(~"file ended in the middle of a macro invocation"); + } token::LPAREN | token::LBRACE | token::LBRACKET => { // tjc: ?????? let ket = token::flip_delimiter(copy self.token); tt_delim(vec::append( - ~[parse_tt_tok(self, true)], + // the open delimiter: + ~[parse_any_tt_tok(self)], vec::append( self.parse_seq_to_before_end( ket, seq_sep_none(), |p| p.parse_token_tree()), - ~[parse_tt_tok(self, true)]))) + // the close delimiter: + ~[parse_any_tt_tok(self)]))) } - _ => parse_tt_tok(self, false) - }; + _ => parse_non_delim_tt_tok(self) + } } fn parse_all_token_trees() -> ~[token_tree] { @@ -3999,6 +4011,7 @@ pub impl Parser { } } + // // Local Variables: // mode: rust diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index b8d756d893ad..1f8b04630e26 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -86,6 +86,7 @@ pub enum Token { LIT_STR(ast::ident), /* Name components */ + // an identifier contains an "is_mod_name" boolean. IDENT(ast::ident, bool), UNDERSCORE, LIFETIME(ast::ident), From 17d3a55362eb6a0b900d1d828e90d583897fa3b1 Mon Sep 17 00:00:00 2001 From: John Clements Date: Sat, 9 Feb 2013 13:25:47 -0800 Subject: [PATCH 3/9] @mut fix --- src/libsyntax/ext/tt/transcribe.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libsyntax/ext/tt/transcribe.rs b/src/libsyntax/ext/tt/transcribe.rs index 75120e9c0bc8..aa9036d295e5 100644 --- a/src/libsyntax/ext/tt/transcribe.rs +++ b/src/libsyntax/ext/tt/transcribe.rs @@ -121,15 +121,15 @@ pure fn lookup_cur_matched_by_matched(r: @mut TtReader, vec::foldl(start, r.repeat_idx, red) } -fn lookup_cur_matched(r: &TtReader, name: ident) -> @named_match { +fn lookup_cur_matched(r: @mut TtReader, name: ident) -> @named_match { lookup_cur_matched_by_matched(r, r.interpolations.get(&name)) } enum lis { lis_unconstrained, lis_constraint(uint, ident), lis_contradiction(~str) } -fn lockstep_iter_size(t: token_tree, r: &TtReader) -> lis { - fn lis_merge(lhs: lis, rhs: lis, r: &TtReader) -> lis { +fn lockstep_iter_size(t: token_tree, r: @mut TtReader) -> lis { + fn lis_merge(lhs: lis, rhs: lis, r: @mut TtReader) -> lis { match lhs { lis_unconstrained => rhs, lis_contradiction(_) => lhs, From 819c6d1c048c909fe6bbdde4937dc832cf485849 Mon Sep 17 00:00:00 2001 From: John Clements Date: Wed, 6 Feb 2013 18:35:01 -0800 Subject: [PATCH 4/9] deriving-eq all over ast --- src/libsyntax/ast.rs | 582 ++++++--------------------------------- src/libsyntax/codemap.rs | 1 + 2 files changed, 81 insertions(+), 502 deletions(-) diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index b26cab7694c9..02f7029fac2f 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -69,6 +69,7 @@ pub type fn_ident = Option; #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct path { span: span, global: bool, @@ -83,23 +84,18 @@ pub type node_id = int; #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct def_id { crate: crate_num, node: node_id, } -pub impl def_id : cmp::Eq { - pure fn eq(&self, other: &def_id) -> bool { - (*self).crate == (*other).crate && (*self).node == (*other).node - } - pure fn ne(&self, other: &def_id) -> bool { !(*self).eq(other) } -} - pub const local_crate: crate_num = 0; pub const crate_node_id: node_id = 0; #[auto_encode] #[auto_decode] +#[deriving_eq] // The AST represents all type param bounds as types. // typeck::collect::compute_bounds matches these against // the "special" built-in traits (see middle::lang_items) and @@ -111,6 +107,7 @@ pub enum ty_param_bound { #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct ty_param { ident: ident, id: node_id, @@ -119,6 +116,7 @@ pub struct ty_param { #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum def { def_fn(def_id, purity), def_static_method(/* method */ def_id, @@ -147,136 +145,6 @@ pub enum def { def_label(node_id) } -pub impl def : cmp::Eq { - pure fn eq(&self, other: &def) -> bool { - match (*self) { - def_fn(e0a, e1a) => { - match (*other) { - def_fn(e0b, e1b) => e0a == e0b && e1a == e1b, - _ => false - } - } - def_static_method(e0a, e1a, e2a) => { - match (*other) { - def_static_method(e0b, e1b, e2b) => - e0a == e0b && e1a == e1b && e2a == e2b, - _ => false - } - } - def_self(e0a, e1a) => { - match (*other) { - def_self(e0b, e1b) => e0a == e0b && e1a == e1b, - _ => false - } - } - def_self_ty(e0a) => { - match (*other) { - def_self_ty(e0b) => e0a == e0b, - _ => false - } - } - def_mod(e0a) => { - match (*other) { - def_mod(e0b) => e0a == e0b, - _ => false - } - } - def_foreign_mod(e0a) => { - match (*other) { - def_foreign_mod(e0b) => e0a == e0b, - _ => false - } - } - def_const(e0a) => { - match (*other) { - def_const(e0b) => e0a == e0b, - _ => false - } - } - def_arg(e0a, e1a, e2a) => { - match (*other) { - def_arg(e0b, e1b, e2b) => - e0a == e0b && e1a == e1b && e2a == e2b, - _ => false - } - } - def_local(e0a, e1a) => { - match (*other) { - def_local(e0b, e1b) => e0a == e0b && e1a == e1b, - _ => false - } - } - def_variant(e0a, e1a) => { - match (*other) { - def_variant(e0b, e1b) => e0a == e0b && e1a == e1b, - _ => false - } - } - def_ty(e0a) => { - match (*other) { - def_ty(e0b) => e0a == e0b, - _ => false - } - } - def_prim_ty(e0a) => { - match (*other) { - def_prim_ty(e0b) => e0a == e0b, - _ => false - } - } - def_ty_param(e0a, e1a) => { - match (*other) { - def_ty_param(e0b, e1b) => e0a == e0b && e1a == e1b, - _ => false - } - } - def_binding(e0a, e1a) => { - match (*other) { - def_binding(e0b, e1b) => e0a == e0b && e1a == e1b, - _ => false - } - } - def_use(e0a) => { - match (*other) { - def_use(e0b) => e0a == e0b, - _ => false - } - } - def_upvar(e0a, e1a, e2a, e3a) => { - match (*other) { - def_upvar(e0b, e1b, e2b, e3b) => - e0a == e0b && e1a == e1b && e2a == e2b && e3a == e3b, - _ => false - } - } - def_struct(e0a) => { - match (*other) { - def_struct(e0b) => e0a == e0b, - _ => false - } - } - def_typaram_binder(e0a) => { - match (*other) { - def_typaram_binder(e1a) => e0a == e1a, - _ => false - } - } - def_region(e0a) => { - match (*other) { - def_region(e0b) => e0a == e0b, - _ => false - } - } - def_label(e0a) => { - match (*other) { - def_label(e0b) => e0a == e0b, - _ => false - } - } - } - } - pure fn ne(&self, other: &def) -> bool { !(*self).eq(other) } -} // The set of meta_items that define the compilation environment of the crate, // used to drive conditional compilation @@ -284,6 +152,7 @@ pub type crate_cfg = ~[@meta_item]; pub type crate = spanned; +#[deriving_eq] pub struct crate_ { module: _mod, attrs: ~[attribute], @@ -294,6 +163,7 @@ pub type meta_item = spanned; #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum meta_item_ { meta_word(~str), meta_list(~str, ~[@meta_item]), @@ -304,6 +174,7 @@ pub type blk = spanned; #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct blk_ { view_items: ~[@view_item], stmts: ~[@stmt], @@ -314,6 +185,7 @@ pub struct blk_ { #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct pat { id: node_id, node: pat_, @@ -322,6 +194,7 @@ pub struct pat { #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct field_pat { ident: ident, pat: @pat, @@ -352,6 +225,7 @@ pub impl binding_mode : to_bytes::IterBytes { #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum pat_ { pat_wild, // A pat_ident may either be a new bound variable, @@ -377,6 +251,7 @@ pub enum pat_ { #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum mutability { m_mutbl, m_imm, m_const, } pub impl mutability : to_bytes::IterBytes { @@ -385,13 +260,6 @@ pub impl mutability : to_bytes::IterBytes { } } -pub impl mutability : cmp::Eq { - pure fn eq(&self, other: &mutability) -> bool { - ((*self) as uint) == ((*other) as uint) - } - pure fn ne(&self, other: &mutability) -> bool { !(*self).eq(other) } -} - #[auto_encode] #[auto_decode] #[deriving_eq] @@ -440,6 +308,7 @@ pub impl Sigil : ToStr { #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum vstore { // FIXME (#3469): Change uint to @expr (actually only constant exprs) vstore_fixed(Option), // [1,2,3,4] @@ -450,6 +319,7 @@ pub enum vstore { #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum expr_vstore { // FIXME (#3469): Change uint to @expr (actually only constant exprs) expr_vstore_fixed(Option), // [1,2,3,4] @@ -462,6 +332,7 @@ pub enum expr_vstore { #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum binop { add, subtract, @@ -483,15 +354,9 @@ pub enum binop { gt, } -pub impl binop : cmp::Eq { - pure fn eq(&self, other: &binop) -> bool { - ((*self) as uint) == ((*other) as uint) - } - pure fn ne(&self, other: &binop) -> bool { !(*self).eq(other) } -} - #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum unop { box(mutability), uniq(mutability), @@ -500,50 +365,11 @@ pub enum unop { neg } -pub impl unop : cmp::Eq { - pure fn eq(&self, other: &unop) -> bool { - match (*self) { - box(e0a) => { - match (*other) { - box(e0b) => e0a == e0b, - _ => false - } - } - uniq(e0a) => { - match (*other) { - uniq(e0b) => e0a == e0b, - _ => false - } - } - deref => { - match (*other) { - deref => true, - _ => false - } - } - not => { - match (*other) { - not => true, - _ => false - } - } - neg => { - match (*other) { - neg => true, - _ => false - } - } - } - } - pure fn ne(&self, other: &unop) -> bool { - !(*self).eq(other) - } -} - // Generally, after typeck you can get the inferred value // using ty::resolved_T(...). #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum inferable { expl(T), infer(node_id) @@ -561,29 +387,10 @@ pub impl inferable : to_bytes::IterBytes { } } -pub impl inferable : cmp::Eq { - pure fn eq(&self, other: &inferable) -> bool { - match (*self) { - expl(ref e0a) => { - match (*other) { - expl(ref e0b) => (*e0a) == (*e0b), - _ => false - } - } - infer(e0a) => { - match (*other) { - infer(e0b) => e0a == e0b, - _ => false - } - } - } - } - pure fn ne(&self, other: &inferable) -> bool { !(*self).eq(other) } -} - // "resolved" mode: the real modes. #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum rmode { by_ref, by_val, by_copy } pub impl rmode : to_bytes::IterBytes { @@ -592,14 +399,6 @@ pub impl rmode : to_bytes::IterBytes { } } - -pub impl rmode : cmp::Eq { - pure fn eq(&self, other: &rmode) -> bool { - ((*self) as uint) == ((*other) as uint) - } - pure fn ne(&self, other: &rmode) -> bool { !(*self).eq(other) } -} - // inferable mode. pub type mode = inferable; @@ -607,6 +406,7 @@ pub type stmt = spanned; #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum stmt_ { stmt_decl(@decl, node_id), @@ -624,6 +424,7 @@ pub enum stmt_ { // a refinement on pat. #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct local_ { is_mutbl: bool, ty: @Ty, @@ -638,10 +439,12 @@ pub type decl = spanned; #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum decl_ { decl_local(~[@local]), decl_item(@item), } #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct arm { pats: ~[@pat], guard: Option<@expr>, @@ -650,6 +453,7 @@ pub struct arm { #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct field_ { mutbl: mutability, ident: ident, @@ -660,22 +464,12 @@ pub type field = spanned; #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum blk_check_mode { default_blk, unsafe_blk, } -pub impl blk_check_mode : cmp::Eq { - pure fn eq(&self, other: &blk_check_mode) -> bool { - match ((*self), (*other)) { - (default_blk, default_blk) => true, - (unsafe_blk, unsafe_blk) => true, - (default_blk, _) => false, - (unsafe_blk, _) => false, - } - } - pure fn ne(&self, other: &blk_check_mode) -> bool { !(*self).eq(other) } -} - #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct expr { id: node_id, // Extra node ID is only used for index, assign_op, unary, binary, method @@ -687,6 +481,7 @@ pub struct expr { #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum log_level { error, debug, log_other } // 0 = error, 1 = debug, 2 = log_other @@ -701,6 +496,7 @@ pub enum CallSugar { #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum expr_ { expr_vstore(@expr, expr_vstore), expr_vec(~[@expr], mutability), @@ -777,6 +573,7 @@ pub enum expr_ { // #[auto_encode] #[auto_decode] +#[deriving_eq] #[doc="For macro invocations; parsing is delegated to the macro"] pub enum token_tree { // a single token @@ -851,6 +648,7 @@ pub type matcher = spanned; #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum matcher_ { // match one token match_tok(::parse::token::Token), @@ -865,6 +663,7 @@ pub type mac = spanned; #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum mac_ { mac_invoc_tt(@path,~[token_tree]), // new macro-invocation } @@ -873,6 +672,7 @@ pub type lit = spanned; #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum lit_ { lit_str(@~str), lit_int(i64, int_ty), @@ -884,40 +684,11 @@ pub enum lit_ { lit_bool(bool), } -pub impl lit_: cmp::Eq { - pure fn eq(&self, other: &lit_) -> bool { - match ((*self), *other) { - (lit_str(a), lit_str(b)) => a == b, - (lit_int(val_a, ty_a), lit_int(val_b, ty_b)) => { - val_a == val_b && ty_a == ty_b - } - (lit_uint(val_a, ty_a), lit_uint(val_b, ty_b)) => { - val_a == val_b && ty_a == ty_b - } - (lit_int_unsuffixed(a), lit_int_unsuffixed(b)) => a == b, - (lit_float(val_a, ty_a), lit_float(val_b, ty_b)) => { - val_a == val_b && ty_a == ty_b - } - (lit_float_unsuffixed(a), lit_float_unsuffixed(b)) => a == b, - (lit_nil, lit_nil) => true, - (lit_bool(a), lit_bool(b)) => a == b, - (lit_str(_), _) => false, - (lit_int(*), _) => false, - (lit_uint(*), _) => false, - (lit_int_unsuffixed(*), _) => false, - (lit_float(*), _) => false, - (lit_float_unsuffixed(*), _) => false, - (lit_nil, _) => false, - (lit_bool(_), _) => false - } - } - pure fn ne(&self, other: &lit_) -> bool { !(*self).eq(other) } -} - // NB: If you change this, you'll probably want to change the corresponding // type structure in middle/ty.rs as well. #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct mt { ty: @Ty, mutbl: mutability, @@ -925,6 +696,7 @@ pub struct mt { #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct ty_field_ { ident: ident, mt: mt, @@ -934,6 +706,7 @@ pub type ty_field = spanned; #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct ty_method { ident: ident, attrs: ~[attribute], @@ -947,6 +720,7 @@ pub struct ty_method { #[auto_encode] #[auto_decode] +#[deriving_eq] // A trait method is either required (meaning it doesn't have an // implementation, just a signature) or provided (meaning it has a default // implementation). @@ -957,6 +731,7 @@ pub enum trait_method { #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum int_ty { ty_i, ty_char, ty_i8, ty_i16, ty_i32, ty_i64, } pub impl int_ty : ToStr { @@ -971,28 +746,9 @@ pub impl int_ty : to_bytes::IterBytes { } } -pub impl int_ty : cmp::Eq { - pure fn eq(&self, other: &int_ty) -> bool { - match ((*self), (*other)) { - (ty_i, ty_i) => true, - (ty_char, ty_char) => true, - (ty_i8, ty_i8) => true, - (ty_i16, ty_i16) => true, - (ty_i32, ty_i32) => true, - (ty_i64, ty_i64) => true, - (ty_i, _) => false, - (ty_char, _) => false, - (ty_i8, _) => false, - (ty_i16, _) => false, - (ty_i32, _) => false, - (ty_i64, _) => false, - } - } - pure fn ne(&self, other: &int_ty) -> bool { !(*self).eq(other) } -} - #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum uint_ty { ty_u, ty_u8, ty_u16, ty_u32, ty_u64, } pub impl uint_ty : ToStr { @@ -1007,26 +763,9 @@ pub impl uint_ty : to_bytes::IterBytes { } } -pub impl uint_ty : cmp::Eq { - pure fn eq(&self, other: &uint_ty) -> bool { - match ((*self), (*other)) { - (ty_u, ty_u) => true, - (ty_u8, ty_u8) => true, - (ty_u16, ty_u16) => true, - (ty_u32, ty_u32) => true, - (ty_u64, ty_u64) => true, - (ty_u, _) => false, - (ty_u8, _) => false, - (ty_u16, _) => false, - (ty_u32, _) => false, - (ty_u64, _) => false - } - } - pure fn ne(&self, other: &uint_ty) -> bool { !(*self).eq(other) } -} - #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum float_ty { ty_f, ty_f32, ty_f64, } pub impl float_ty : ToStr { @@ -1041,16 +780,7 @@ pub impl float_ty : to_bytes::IterBytes { } } -pub impl float_ty : cmp::Eq { - pure fn eq(&self, other: &float_ty) -> bool { - match ((*self), (*other)) { - (ty_f, ty_f) | (ty_f32, ty_f32) | (ty_f64, ty_f64) => true, - (ty_f, _) | (ty_f32, _) | (ty_f64, _) => false - } - } - pure fn ne(&self, other: &float_ty) -> bool { !(*self).eq(other) } -} - +// NB Eq method appears below. #[auto_encode] #[auto_decode] pub struct Ty { @@ -1062,6 +792,7 @@ pub struct Ty { // Not represented directly in the AST, referred to by name through a ty_path. #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum prim_ty { ty_int(int_ty), ty_uint(uint_ty), @@ -1070,46 +801,9 @@ pub enum prim_ty { ty_bool, } -pub impl prim_ty : cmp::Eq { - pure fn eq(&self, other: &prim_ty) -> bool { - match (*self) { - ty_int(e0a) => { - match (*other) { - ty_int(e0b) => e0a == e0b, - _ => false - } - } - ty_uint(e0a) => { - match (*other) { - ty_uint(e0b) => e0a == e0b, - _ => false - } - } - ty_float(e0a) => { - match (*other) { - ty_float(e0b) => e0a == e0b, - _ => false - } - } - ty_str => { - match (*other) { - ty_str => true, - _ => false - } - } - ty_bool => { - match (*other) { - ty_bool => true, - _ => false - } - } - } - } - pure fn ne(&self, other: &prim_ty) -> bool { !(*self).eq(other) } -} - #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct region { id: node_id, node: region_, @@ -1117,6 +811,7 @@ pub struct region { #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum region_ { re_anon, re_static, @@ -1149,6 +844,7 @@ pub impl Onceness : to_bytes::IterBytes { #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct TyClosure { sigil: Sigil, region: Option<@region>, @@ -1167,6 +863,7 @@ pub struct TyBareFn { #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum ty_ { ty_nil, ty_bot, /* bottom type */ @@ -1207,6 +904,7 @@ pub impl Ty : to_bytes::IterBytes { #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct arg { mode: mode, is_mutbl: bool, @@ -1217,6 +915,7 @@ pub struct arg { #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct fn_decl { inputs: ~[arg], output: @Ty, @@ -1225,6 +924,7 @@ pub struct fn_decl { #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum purity { pure_fn, // declared with "pure fn" unsafe_fn, // declared with "unsafe fn" @@ -1249,15 +949,9 @@ pub impl purity : to_bytes::IterBytes { } } -pub impl purity : cmp::Eq { - pure fn eq(&self, other: &purity) -> bool { - ((*self) as uint) == ((*other) as uint) - } - pure fn ne(&self, other: &purity) -> bool { !(*self).eq(other) } -} - #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum ret_style { noreturn, // functions with return type _|_ that always // raise an error or exit (i.e. never return to the caller) @@ -1270,20 +964,9 @@ pub impl ret_style : to_bytes::IterBytes { } } -pub impl ret_style : cmp::Eq { - pure fn eq(&self, other: &ret_style) -> bool { - match ((*self), (*other)) { - (noreturn, noreturn) => true, - (return_val, return_val) => true, - (noreturn, _) => false, - (return_val, _) => false, - } - } - pure fn ne(&self, other: &ret_style) -> bool { !(*self).eq(other) } -} - #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum self_ty_ { sty_static, // no self: static method sty_by_ref, // old by-reference self: `` @@ -1293,54 +976,11 @@ pub enum self_ty_ { sty_uniq(mutability) // by-unique-pointer self: `~self` } -pub impl self_ty_ : cmp::Eq { - pure fn eq(&self, other: &self_ty_) -> bool { - match (*self) { - sty_static => { - match (*other) { - sty_static => true, - _ => false - } - } - sty_by_ref => { - match (*other) { - sty_by_ref => true, - _ => false - } - } - sty_value => { - match (*other) { - sty_value => true, - _ => false - } - } - sty_region(e0a) => { - match (*other) { - sty_region(e0b) => e0a == e0b, - _ => false - } - } - sty_box(e0a) => { - match (*other) { - sty_box(e0b) => e0a == e0b, - _ => false - } - } - sty_uniq(e0a) => { - match (*other) { - sty_uniq(e0b) => e0a == e0b, - _ => false - } - } - } - } - pure fn ne(&self, other: &self_ty_) -> bool { !(*self).eq(other) } -} - pub type self_ty = spanned; #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct method { ident: ident, attrs: ~[attribute], @@ -1357,6 +997,7 @@ pub struct method { #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct _mod { view_items: ~[@view_item], items: ~[@item], @@ -1364,6 +1005,7 @@ pub struct _mod { #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum foreign_abi { foreign_abi_rust_intrinsic, foreign_abi_cdecl, @@ -1373,31 +1015,12 @@ pub enum foreign_abi { // Foreign mods can be named or anonymous #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum foreign_mod_sort { named, anonymous } -pub impl foreign_mod_sort : cmp::Eq { - pure fn eq(&self, other: &foreign_mod_sort) -> bool { - ((*self) as uint) == ((*other) as uint) - } - pure fn ne(&self, other: &foreign_mod_sort) -> bool { !(*self).eq(other) } -} - -pub impl foreign_abi : cmp::Eq { - pure fn eq(&self, other: &foreign_abi) -> bool { - match ((*self), (*other)) { - (foreign_abi_rust_intrinsic, foreign_abi_rust_intrinsic) => true, - (foreign_abi_cdecl, foreign_abi_cdecl) => true, - (foreign_abi_stdcall, foreign_abi_stdcall) => true, - (foreign_abi_rust_intrinsic, _) => false, - (foreign_abi_cdecl, _) => false, - (foreign_abi_stdcall, _) => false, - } - } - pure fn ne(&self, other: &foreign_abi) -> bool { !(*self).eq(other) } -} - #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct foreign_mod { sort: foreign_mod_sort, abi: ident, @@ -1407,6 +1030,7 @@ pub struct foreign_mod { #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct variant_arg { ty: @Ty, id: node_id, @@ -1414,6 +1038,7 @@ pub struct variant_arg { #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum variant_kind { tuple_variant_kind(~[variant_arg]), struct_variant_kind(@struct_def), @@ -1422,6 +1047,7 @@ pub enum variant_kind { #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct enum_def_ { variants: ~[variant], common: Option<@struct_def>, @@ -1429,10 +1055,12 @@ pub struct enum_def_ { #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum enum_def = enum_def_; #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct variant_ { name: ident, attrs: ~[attribute], @@ -1446,6 +1074,7 @@ pub type variant = spanned; #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct path_list_ident_ { name: ident, id: node_id, @@ -1455,19 +1084,14 @@ pub type path_list_ident = spanned; #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum namespace { module_ns, type_value_ns } -pub impl namespace : cmp::Eq { - pure fn eq(&self, other: &namespace) -> bool { - ((*self) as uint) == ((*other) as uint) - } - pure fn ne(&self, other: &namespace) -> bool { !(*self).eq(other) } -} - pub type view_path = spanned; #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum view_path_ { // quux = foo::bar::baz @@ -1486,6 +1110,7 @@ pub enum view_path_ { #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct view_item { node: view_item_, attrs: ~[attribute], @@ -1495,6 +1120,7 @@ pub struct view_item { #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum view_item_ { view_item_use(ident, ~[@meta_item], node_id), view_item_import(~[@view_path]), @@ -1508,18 +1134,13 @@ pub type attribute = spanned; // distinguished for pretty-printing. #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum attr_style { attr_outer, attr_inner, } -pub impl attr_style : cmp::Eq { - pure fn eq(&self, other: &attr_style) -> bool { - ((*self) as uint) == ((*other) as uint) - } - pure fn ne(&self, other: &attr_style) -> bool { !(*self).eq(other) } -} - // doc-comments are promoted to attributes that have is_sugared_doc = true #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct attribute_ { style: attr_style, value: meta_item, @@ -1535,6 +1156,7 @@ pub struct attribute_ { */ #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct trait_ref { path: @path, ref_id: node_id, @@ -1542,24 +1164,12 @@ pub struct trait_ref { #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum visibility { public, private, inherited } -pub impl visibility : cmp::Eq { - pure fn eq(&self, other: &visibility) -> bool { - match ((*self), (*other)) { - (public, public) => true, - (private, private) => true, - (inherited, inherited) => true, - (public, _) => false, - (private, _) => false, - (inherited, _) => false, - } - } - pure fn ne(&self, other: &visibility) -> bool { !(*self).eq(other) } -} - #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct struct_field_ { kind: struct_field_kind, id: node_id, @@ -1570,40 +1180,15 @@ pub type struct_field = spanned; #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum struct_field_kind { named_field(ident, struct_mutability, visibility), unnamed_field // element of a tuple-like struct } -pub impl struct_field_kind : cmp::Eq { - pure fn eq(&self, other: &struct_field_kind) -> bool { - match (*self) { - named_field(ident_a, struct_mutability_a, visibility_a) => { - match *other { - named_field(ident_b, struct_mutability_b, visibility_b) - => { - ident_a == ident_b && - struct_mutability_a == struct_mutability_b && - visibility_a == visibility_b - } - unnamed_field => false - } - } - unnamed_field => { - match *other { - named_field(*) => false, - unnamed_field => true - } - } - } - } - pure fn ne(&self, other: &struct_field_kind) -> bool { - !(*self).eq(other) - } -} - #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct struct_def { fields: ~[@struct_field], /* fields */ /* (not including ctor or dtor) */ @@ -1620,6 +1205,7 @@ pub struct struct_def { */ #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct item { ident: ident, attrs: ~[attribute], @@ -1631,6 +1217,7 @@ pub struct item { #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum item_ { item_const(@Ty, @expr), item_fn(fn_decl, purity, ~[ty_param], blk), @@ -1641,14 +1228,15 @@ pub enum item_ { item_struct(@struct_def, ~[ty_param]), item_trait(~[ty_param], ~[@trait_ref], ~[trait_method]), item_impl(~[ty_param], - Option<@trait_ref>, /* (optional) trait this impl implements */ - @Ty, /* self */ + Option<@trait_ref>, // (optional) trait this impl implements + @Ty, // self ~[@method]), item_mac(mac), } #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum struct_mutability { struct_mutable, struct_immutable } pub impl struct_mutability : to_bytes::IterBytes { @@ -1657,24 +1245,11 @@ pub impl struct_mutability : to_bytes::IterBytes { } } -pub impl struct_mutability : cmp::Eq { - pure fn eq(&self, other: &struct_mutability) -> bool { - match ((*self), (*other)) { - (struct_mutable, struct_mutable) => true, - (struct_immutable, struct_immutable) => true, - (struct_mutable, _) => false, - (struct_immutable, _) => false, - } - } - pure fn ne(&self, other: &struct_mutability) -> bool { - !(*self).eq(other) - } -} - pub type struct_dtor = spanned; #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct struct_dtor_ { id: node_id, attrs: ~[attribute], @@ -1684,6 +1259,7 @@ pub struct struct_dtor_ { #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct foreign_item { ident: ident, attrs: ~[attribute], @@ -1695,6 +1271,7 @@ pub struct foreign_item { #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum foreign_item_ { foreign_item_fn(fn_decl, purity, ~[ty_param]), foreign_item_const(@Ty) @@ -1705,6 +1282,7 @@ pub enum foreign_item_ { // that we trans. #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum inlined_item { ii_item(@item), ii_method(def_id /* impl id */, @method), diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index a509325facec..3fbf732c1437 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -130,6 +130,7 @@ pub struct span { #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct spanned { node: T, span: span } pub impl span : cmp::Eq { From 0419e36b76a3d00dd313dcb3b079604d3440d2ff Mon Sep 17 00:00:00 2001 From: John Clements Date: Thu, 7 Feb 2013 14:39:49 -0800 Subject: [PATCH 5/9] finish deriving_eq in ast --- src/libsyntax/ast.rs | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 02f7029fac2f..19de5cc62f3d 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -783,6 +783,7 @@ pub impl float_ty : to_bytes::IterBytes { // NB Eq method appears below. #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct Ty { id: node_id, node: ty_, @@ -855,6 +856,7 @@ pub struct TyClosure { #[auto_encode] #[auto_decode] +#[deriving_eq] pub struct TyBareFn { purity: purity, abi: Abi, @@ -885,17 +887,6 @@ pub enum ty_ { ty_infer, } -// Equality and byte-iter (hashing) can be quite approximate for AST types. -// since we only care about this for normalizing them to "real" types. -pub impl Ty : cmp::Eq { - pure fn eq(&self, other: &Ty) -> bool { - ptr::addr_of(&(*self)) == ptr::addr_of(&(*other)) - } - pure fn ne(&self, other: &Ty) -> bool { - ptr::addr_of(&(*self)) != ptr::addr_of(&(*other)) - } -} - pub impl Ty : to_bytes::IterBytes { pure fn iter_bytes(&self, +lsb0: bool, f: to_bytes::Cb) { to_bytes::iter_bytes_2(&self.span.lo, &self.span.hi, lsb0, f); From 16da4e15af02f92d4c7e7cfe36344a276096585d Mon Sep 17 00:00:00 2001 From: John Clements Date: Thu, 7 Feb 2013 11:48:12 -0800 Subject: [PATCH 6/9] use node_id for indexing in ast_to_ty_cache --- src/librustc/middle/ty.rs | 2 +- src/librustc/middle/typeck/astconv.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index d38eef0fcf9f..d0e6025c3eff 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -262,7 +262,7 @@ struct ctxt_ { needs_drop_cache: HashMap, needs_unwind_cleanup_cache: HashMap, mut tc_cache: LinearMap, - ast_ty_to_ty_cache: HashMap<@ast::Ty, ast_ty_to_ty_cache_entry>, + ast_ty_to_ty_cache: HashMap, enum_var_cache: HashMap, trait_method_cache: HashMap, ty_param_bounds: HashMap, diff --git a/src/librustc/middle/typeck/astconv.rs b/src/librustc/middle/typeck/astconv.rs index 8cd5be85a1a4..3ee604426c9f 100644 --- a/src/librustc/middle/typeck/astconv.rs +++ b/src/librustc/middle/typeck/astconv.rs @@ -277,7 +277,7 @@ pub fn ast_ty_to_ty( let tcx = self.tcx(); - match tcx.ast_ty_to_ty_cache.find(&ast_ty) { + match tcx.ast_ty_to_ty_cache.find(&ast_ty.id) { Some(ty::atttce_resolved(ty)) => return ty, Some(ty::atttce_unresolved) => { tcx.sess.span_fatal(ast_ty.span, ~"illegal recursive type; \ @@ -287,7 +287,7 @@ pub fn ast_ty_to_ty( None => { /* go on */ } } - tcx.ast_ty_to_ty_cache.insert(ast_ty, ty::atttce_unresolved); + tcx.ast_ty_to_ty_cache.insert(ast_ty.id, ty::atttce_unresolved); let typ = match /*bad*/copy ast_ty.node { ast::ty_nil => ty::mk_nil(tcx), ast::ty_bot => ty::mk_bot(tcx), @@ -409,7 +409,7 @@ pub fn ast_ty_to_ty( } }; - tcx.ast_ty_to_ty_cache.insert(ast_ty, ty::atttce_resolved(typ)); + tcx.ast_ty_to_ty_cache.insert(ast_ty.id, ty::atttce_resolved(typ)); return typ; } From ded95d2c2891d7ffec1277cb4969ed956ff0bd25 Mon Sep 17 00:00:00 2001 From: John Clements Date: Fri, 8 Feb 2013 18:50:12 -0800 Subject: [PATCH 7/9] deriving_eq for tokens and binops Note that the replaced definition of equality on tokens contains a *huge* shortcut on INTERPOLATED tokens (those that contain ASTs), whereby any two INTERPOLATED tokens are considered equal. This seems like a really broken notion of equality, but it appears that the existing test cases and the compiler don't depend on it. Niko noticed this, BTW. Replace long definition of Eq on tokens and binops w --- src/libsyntax/parse/token.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 1f8b04630e26..391e8b04336b 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -25,6 +25,7 @@ use std::oldmap::HashMap; #[auto_encode] #[auto_decode] +#[deriving_eq] pub enum binop { PLUS, MINUS, @@ -518,12 +519,6 @@ pub fn reserved_keyword_table() -> HashMap<~str, ()> { words } -impl binop : cmp::Eq { - pure fn eq(&self, other: &binop) -> bool { - ((*self) as uint) == ((*other) as uint) - } - pure fn ne(&self, other: &binop) -> bool { !(*self).eq(other) } -} impl Token : cmp::Eq { pure fn eq(&self, other: &Token) -> bool { From 9d962d84669a02805571683707cec712415af4c6 Mon Sep 17 00:00:00 2001 From: John Clements Date: Wed, 13 Feb 2013 12:49:45 -0800 Subject: [PATCH 8/9] add test case --- src/libsyntax/ast.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 19de5cc62f3d..36c8c29ded81 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -152,6 +152,8 @@ pub type crate_cfg = ~[@meta_item]; pub type crate = spanned; +#[auto_encode] +#[auto_decode] #[deriving_eq] pub struct crate_ { module: _mod, @@ -1281,7 +1283,34 @@ pub enum inlined_item { ii_dtor(struct_dtor, ident, ~[ty_param], def_id /* parent id */) } +#[cfg(test)] +mod test { + use std; + use codemap::*; + use super::*; + //are asts encodable? + + // it looks like this *will* be a compiler bug, after + // I get deriving_eq for crates into incoming :) + /* + #[test] fn check_asts_encodable() { + let bogus_span = span {lo:BytePos(10), + hi:BytePos(20), + expn_info:None}; + let _e : crate = + spanned{ + node: crate_{ + module: _mod {view_items: ~[], items: ~[]}, + attrs: ~[], + config: ~[] + }, + span: bogus_span}; + // doesn't matter which encoder we use.... + let _f = (_e as std::serialize::Encodable::); + } + */ +} // // Local Variables: // mode: rust From f9d789fa083220cc9d84cbea94868606600c64a9 Mon Sep 17 00:00:00 2001 From: John Clements Date: Wed, 13 Feb 2013 15:38:42 -0800 Subject: [PATCH 9/9] cleanup, fix test case --- src/libcore/dvec.rs | 2 +- src/libsyntax/ext/base.rs | 2 +- src/libsyntax/parse/common.rs | 1 - src/libsyntax/parse/mod.rs | 13 +++++++++++-- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/libcore/dvec.rs b/src/libcore/dvec.rs index c7a0300a9789..fe36ed159603 100644 --- a/src/libcore/dvec.rs +++ b/src/libcore/dvec.rs @@ -229,7 +229,7 @@ impl DVec { impl DVec { /** - * Append all elements of a vector to the end of the list. + * Append all elements of a vector to the end of the list * * Equivalent to `append_iter()` but potentially more efficient. */ diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index a85a7990ace9..c924acd577d3 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -59,7 +59,7 @@ pub enum MacResult { MRExpr(@ast::expr), MRItem(@ast::item), MRAny(fn@()-> @ast::expr, fn@()-> Option<@ast::item>, fn@()->@ast::stmt), - MRDef(MacroDef), + MRDef(MacroDef) } pub enum SyntaxExtension { diff --git a/src/libsyntax/parse/common.rs b/src/libsyntax/parse/common.rs index 7e74163b6bf3..e7b5005d8dbf 100644 --- a/src/libsyntax/parse/common.rs +++ b/src/libsyntax/parse/common.rs @@ -217,7 +217,6 @@ pub impl Parser { let mut first = true; let mut v = ~[]; while self.token != token::GT - // wait... isn't this going to eat a whole '>>' ? && self.token != token::BINOP(token::SHR) { match sep { Some(ref t) => { diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index b863e2bd0e49..12038898a9d1 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -244,8 +244,17 @@ mod test { ~[], new_parse_sess(None)); check_equal(to_json_str(tts as Encodable::), - //[["tt_tok",["IDENT","fn"]]] - ~"abc" + ~"[[\"tt_tok\",[,[\"IDENT\",[\"fn\",false]]]],\ + [\"tt_tok\",[,[\"IDENT\",[\"foo\",false]]]],\ + [\"tt_delim\",[[[\"tt_tok\",[,[\"LPAREN\",[]]]],\ + [\"tt_tok\",[,[\"IDENT\",[\"x\",false]]]],\ + [\"tt_tok\",[,[\"COLON\",[]]]],\ + [\"tt_tok\",[,[\"IDENT\",[\"int\",false]]]],\ + [\"tt_tok\",[,[\"RPAREN\",[]]]]]]],\ + [\"tt_delim\",[[[\"tt_tok\",[,[\"LBRACE\",[]]]],\ + [\"tt_tok\",[,[\"IDENT\",[\"x\",false]]]],\ + [\"tt_tok\",[,[\"SEMI\",[]]]],\ + [\"tt_tok\",[,[\"RBRACE\",[]]]]]]]]" ); let ast1 = new_parser_from_tts(new_parse_sess(None),~[],tts) .parse_item(~[]);