Parser: Fix spans of explicit self arg idents
This commit is contained in:
parent
c7c342d10c
commit
9c7865fa6f
2 changed files with 49 additions and 3 deletions
|
@ -721,7 +721,7 @@ pub fn integer_lit(s: &str, sd: &SpanHandler, sp: Span) -> ast::Lit_ {
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
use serialize::json;
|
use serialize::json;
|
||||||
use codemap::{Span, BytePos, Spanned, NO_EXPANSION};
|
use codemap::{Span, BytePos, Pos, Spanned, NO_EXPANSION};
|
||||||
use owned_slice::OwnedSlice;
|
use owned_slice::OwnedSlice;
|
||||||
use ast;
|
use ast;
|
||||||
use abi;
|
use abi;
|
||||||
|
@ -1121,6 +1121,46 @@ mod test {
|
||||||
span: sp(0,21)})));
|
span: sp(0,21)})));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_spans_of_pat_idents(src: &str) -> Vec<Span> {
|
||||||
|
let item = string_to_item(src.to_string()).unwrap();
|
||||||
|
|
||||||
|
struct PatIdentVisitor {
|
||||||
|
spans: Vec<Span>
|
||||||
|
}
|
||||||
|
impl<'v> ::visit::Visitor<'v> for PatIdentVisitor {
|
||||||
|
fn visit_pat(&mut self, p: &'v ast::Pat) {
|
||||||
|
match p.node {
|
||||||
|
ast::PatIdent(_ , ref spannedident, _) => {
|
||||||
|
self.spans.push(spannedident.span.clone());
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
::visit::walk_pat(self, p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let mut v = PatIdentVisitor { spans: Vec::new() };
|
||||||
|
::visit::walk_item(&mut v, &*item);
|
||||||
|
return v.spans;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test] fn span_of_self_arg_pat_idents_are_correct() {
|
||||||
|
|
||||||
|
let srcs = ["impl z { fn a (&self, &myarg: int) {} }",
|
||||||
|
"impl z { fn a (&mut self, &myarg: int) {} }",
|
||||||
|
"impl z { fn a (&'a self, &myarg: int) {} }",
|
||||||
|
"impl z { fn a (self, &myarg: int) {} }",
|
||||||
|
"impl z { fn a (self: Foo, &myarg: int) {} }",
|
||||||
|
];
|
||||||
|
|
||||||
|
for &src in srcs.iter() {
|
||||||
|
let spans = get_spans_of_pat_idents(src);
|
||||||
|
let Span{lo:lo,hi:hi,..} = spans[0];
|
||||||
|
assert!("self" == src.slice(lo.to_uint(), hi.to_uint()),
|
||||||
|
"\"{}\" != \"self\". src=\"{}\"",
|
||||||
|
src.slice(lo.to_uint(), hi.to_uint()), src)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test] fn parse_exprs () {
|
#[test] fn parse_exprs () {
|
||||||
// just make sure that they parse....
|
// just make sure that they parse....
|
||||||
|
|
|
@ -4164,10 +4164,16 @@ impl<'a> Parser<'a> {
|
||||||
// A bit of complexity and lookahead is needed here in order to be
|
// A bit of complexity and lookahead is needed here in order to be
|
||||||
// backwards compatible.
|
// backwards compatible.
|
||||||
let lo = self.span.lo;
|
let lo = self.span.lo;
|
||||||
|
let mut self_ident_lo = self.span.lo;
|
||||||
|
let mut self_ident_hi = self.span.hi;
|
||||||
|
|
||||||
let mut mutbl_self = MutImmutable;
|
let mut mutbl_self = MutImmutable;
|
||||||
let explicit_self = match self.token {
|
let explicit_self = match self.token {
|
||||||
token::BINOP(token::AND) => {
|
token::BINOP(token::AND) => {
|
||||||
maybe_parse_borrowed_explicit_self(self)
|
let eself = maybe_parse_borrowed_explicit_self(self);
|
||||||
|
self_ident_lo = self.last_span.lo;
|
||||||
|
self_ident_hi = self.last_span.hi;
|
||||||
|
eself
|
||||||
}
|
}
|
||||||
token::TILDE => {
|
token::TILDE => {
|
||||||
// We need to make sure it isn't a type
|
// We need to make sure it isn't a type
|
||||||
|
@ -4239,7 +4245,7 @@ impl<'a> Parser<'a> {
|
||||||
_ => SelfStatic,
|
_ => SelfStatic,
|
||||||
};
|
};
|
||||||
|
|
||||||
let explicit_self_sp = mk_sp(lo, self.span.hi);
|
let explicit_self_sp = mk_sp(self_ident_lo, self_ident_hi);
|
||||||
|
|
||||||
// shared fall-through for the three cases below. borrowing prevents simply
|
// shared fall-through for the three cases below. borrowing prevents simply
|
||||||
// writing this as a closure
|
// writing this as a closure
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue