Skip to content

Commit 7088c45

Browse files
committed
auto merge of #18141 : phildawes/rust/master, r=brson
Hello! I noticed spans are wrong for the PatIdents of self args. (I use spans a lot in racer)
2 parents c29a752 + 9c7865f commit 7088c45

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/libsyntax/parse/mod.rs

+41-1
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ pub fn integer_lit(s: &str, sd: &SpanHandler, sp: Span) -> ast::Lit_ {
721721
mod test {
722722
use super::*;
723723
use serialize::json;
724-
use codemap::{Span, BytePos, Spanned, NO_EXPANSION};
724+
use codemap::{Span, BytePos, Pos, Spanned, NO_EXPANSION};
725725
use owned_slice::OwnedSlice;
726726
use ast;
727727
use abi;
@@ -1121,6 +1121,46 @@ mod test {
11211121
span: sp(0,21)})));
11221122
}
11231123

1124+
fn get_spans_of_pat_idents(src: &str) -> Vec<Span> {
1125+
let item = string_to_item(src.to_string()).unwrap();
1126+
1127+
struct PatIdentVisitor {
1128+
spans: Vec<Span>
1129+
}
1130+
impl<'v> ::visit::Visitor<'v> for PatIdentVisitor {
1131+
fn visit_pat(&mut self, p: &'v ast::Pat) {
1132+
match p.node {
1133+
ast::PatIdent(_ , ref spannedident, _) => {
1134+
self.spans.push(spannedident.span.clone());
1135+
}
1136+
_ => {
1137+
::visit::walk_pat(self, p);
1138+
}
1139+
}
1140+
}
1141+
}
1142+
let mut v = PatIdentVisitor { spans: Vec::new() };
1143+
::visit::walk_item(&mut v, &*item);
1144+
return v.spans;
1145+
}
1146+
1147+
#[test] fn span_of_self_arg_pat_idents_are_correct() {
1148+
1149+
let srcs = ["impl z { fn a (&self, &myarg: int) {} }",
1150+
"impl z { fn a (&mut self, &myarg: int) {} }",
1151+
"impl z { fn a (&'a self, &myarg: int) {} }",
1152+
"impl z { fn a (self, &myarg: int) {} }",
1153+
"impl z { fn a (self: Foo, &myarg: int) {} }",
1154+
];
1155+
1156+
for &src in srcs.iter() {
1157+
let spans = get_spans_of_pat_idents(src);
1158+
let Span{lo:lo,hi:hi,..} = spans[0];
1159+
assert!("self" == src.slice(lo.to_uint(), hi.to_uint()),
1160+
"\"{}\" != \"self\". src=\"{}\"",
1161+
src.slice(lo.to_uint(), hi.to_uint()), src)
1162+
}
1163+
}
11241164

11251165
#[test] fn parse_exprs () {
11261166
// just make sure that they parse....

src/libsyntax/parse/parser.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -4165,10 +4165,16 @@ impl<'a> Parser<'a> {
41654165
// A bit of complexity and lookahead is needed here in order to be
41664166
// backwards compatible.
41674167
let lo = self.span.lo;
4168+
let mut self_ident_lo = self.span.lo;
4169+
let mut self_ident_hi = self.span.hi;
4170+
41684171
let mut mutbl_self = MutImmutable;
41694172
let explicit_self = match self.token {
41704173
token::BINOP(token::AND) => {
4171-
maybe_parse_borrowed_explicit_self(self)
4174+
let eself = maybe_parse_borrowed_explicit_self(self);
4175+
self_ident_lo = self.last_span.lo;
4176+
self_ident_hi = self.last_span.hi;
4177+
eself
41724178
}
41734179
token::TILDE => {
41744180
// We need to make sure it isn't a type
@@ -4240,7 +4246,7 @@ impl<'a> Parser<'a> {
42404246
_ => SelfStatic,
42414247
};
42424248

4243-
let explicit_self_sp = mk_sp(lo, self.span.hi);
4249+
let explicit_self_sp = mk_sp(self_ident_lo, self_ident_hi);
42444250

42454251
// shared fall-through for the three cases below. borrowing prevents simply
42464252
// writing this as a closure

0 commit comments

Comments
 (0)