Skip to content

Commit 78f6178

Browse files
committed
save-anlaysis: misc bug fixes
In particular, handling of struct literals where the struct name is a type alias, and tuple indexing. Plus some other stuff.
1 parent dcbd418 commit 78f6178

File tree

3 files changed

+56
-29
lines changed

3 files changed

+56
-29
lines changed

src/librustc_trans/save/mod.rs

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,13 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
814814
sub_span,
815815
def_id,
816816
self.cur_scope),
817-
def::DefStaticMethod(declid, provenence) => {
817+
def::DefTy(def_id, _) => self.fmt.ref_str(recorder::TypeRef,
818+
span,
819+
sub_span,
820+
def_id,
821+
self.cur_scope),
822+
def::DefStaticMethod(declid, provenence) |
823+
def::DefMethod(declid, _, provenence) => {
818824
let sub_span = self.span.sub_span_for_meth_name(span);
819825
let defid = if declid.krate == ast::LOCAL_CRATE {
820826
let ti = ty::impl_or_trait_item(&self.analysis.ty_cx,
@@ -856,13 +862,17 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
856862
Some(declid),
857863
self.cur_scope);
858864
},
859-
def::DefFn(def_id, _) => self.fmt.fn_call_str(span,
860-
sub_span,
861-
def_id,
862-
self.cur_scope),
865+
def::DefFn(def_id, _) => {
866+
self.fmt.fn_call_str(span,
867+
sub_span,
868+
def_id,
869+
self.cur_scope)
870+
}
863871
_ => self.sess.span_bug(span,
864-
&format!("Unexpected def kind while looking up path in '{}'",
865-
self.span.snippet(span))[]),
872+
&format!("Unexpected def kind while looking \
873+
up path in `{}`: `{:?}`",
874+
self.span.snippet(span),
875+
*def)[]),
866876
}
867877
// modules or types in the path prefix
868878
match *def {
@@ -886,21 +896,21 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
886896
return
887897
}
888898

889-
let mut struct_def: Option<DefId> = None;
890-
match self.lookup_type_ref(ex.id) {
891-
Some(id) => {
892-
struct_def = Some(id);
899+
self.write_sub_paths_truncated(path, false);
900+
901+
let ty = &ty::expr_ty_adjusted(&self.analysis.ty_cx, ex).sty;
902+
let struct_def = match *ty {
903+
ty::ty_struct(def_id, _) => {
893904
let sub_span = self.span.span_for_last_ident(path.span);
894905
self.fmt.ref_str(recorder::StructRef,
895906
path.span,
896907
sub_span,
897-
id,
908+
def_id,
898909
self.cur_scope);
899-
},
900-
None => ()
901-
}
902-
903-
self.write_sub_paths_truncated(path, false);
910+
Some(def_id)
911+
}
912+
_ => None
913+
};
904914

905915
for field in fields.iter() {
906916
match struct_def {
@@ -1335,8 +1345,8 @@ impl<'l, 'tcx, 'v> Visitor<'v> for DxrVisitor<'l, 'tcx> {
13351345
}
13361346

13371347
self.visit_expr(&**sub_ex);
1338-
1339-
match ty::expr_ty_adjusted(&self.analysis.ty_cx, &**sub_ex).sty {
1348+
let ty = &ty::expr_ty_adjusted(&self.analysis.ty_cx, &**sub_ex).sty;
1349+
match *ty {
13401350
ty::ty_struct(def_id, _) => {
13411351
let fields = ty::lookup_struct_fields(&self.analysis.ty_cx, def_id);
13421352
for f in fields.iter() {
@@ -1350,9 +1360,9 @@ impl<'l, 'tcx, 'v> Visitor<'v> for DxrVisitor<'l, 'tcx> {
13501360
break;
13511361
}
13521362
}
1353-
},
1363+
}
13541364
_ => self.sess.span_bug(ex.span,
1355-
"Expected struct type, but not ty_struct"),
1365+
&format!("Expected struct type, found {:?}", ty)[]),
13561366
}
13571367
},
13581368
ast::ExprTupField(ref sub_ex, idx) => {
@@ -1362,12 +1372,13 @@ impl<'l, 'tcx, 'v> Visitor<'v> for DxrVisitor<'l, 'tcx> {
13621372

13631373
self.visit_expr(&**sub_ex);
13641374

1365-
match ty::expr_ty_adjusted(&self.analysis.ty_cx, &**sub_ex).sty {
1375+
let ty = &ty::expr_ty_adjusted(&self.analysis.ty_cx, &**sub_ex).sty;
1376+
match *ty {
13661377
ty::ty_struct(def_id, _) => {
13671378
let fields = ty::lookup_struct_fields(&self.analysis.ty_cx, def_id);
13681379
for (i, f) in fields.iter().enumerate() {
13691380
if i == idx.node {
1370-
let sub_span = self.span.span_for_last_ident(ex.span);
1381+
let sub_span = self.span.sub_span_after_token(ex.span, token::Dot);
13711382
self.fmt.ref_str(recorder::VarRef,
13721383
ex.span,
13731384
sub_span,
@@ -1376,9 +1387,11 @@ impl<'l, 'tcx, 'v> Visitor<'v> for DxrVisitor<'l, 'tcx> {
13761387
break;
13771388
}
13781389
}
1379-
},
1390+
}
1391+
ty::ty_tup(_) => {}
13801392
_ => self.sess.span_bug(ex.span,
1381-
"Expected struct type, but not ty_struct"),
1393+
&format!("Expected struct or tuple \
1394+
type, found {:?}", ty)[]),
13821395
}
13831396
},
13841397
ast::ExprClosure(_, _, ref decl, ref body) => {
@@ -1454,7 +1467,7 @@ impl<'l, 'tcx, 'v> Visitor<'v> for DxrVisitor<'l, 'tcx> {
14541467
&value[],
14551468
"")
14561469
}
1457-
def::DefVariant(..) => {
1470+
def::DefVariant(..) | def::DefTy(..) | def::DefStruct(..) => {
14581471
paths_to_process.push((id, p.clone(), Some(ref_kind)))
14591472
}
14601473
// FIXME(nrc) what are these doing here?

src/librustc_trans/save/recorder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ macro_rules! svec {
6262
})
6363
}
6464

65-
#[derive(Copy,Debug)]
65+
#[derive(Copy, Debug, Eq, PartialEq)]
6666
pub enum Row {
6767
Variable,
6868
Enum,

src/librustc_trans/save/span_utils.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl<'a> SpanUtils<'a> {
3737
let lo_pos_byte = self.sess.codemap().lookup_byte_offset(span.lo).pos;
3838
let hi_pos_byte = self.sess.codemap().lookup_byte_offset(span.hi).pos;
3939

40-
format!("file_name,{},file_line,{},file_col,{},extent_start,{},extent_start_bytes,{},\
40+
format!("file_name,\"{}\",file_line,{},file_col,{},extent_start,{},extent_start_bytes,{},\
4141
file_line_end,{},file_col_end,{},extent_end,{},extent_end_bytes,{}",
4242
lo_loc.file.name,
4343
lo_loc.line, lo_loc.col.to_usize(), lo_pos.to_usize(), lo_pos_byte.to_usize(),
@@ -205,6 +205,7 @@ impl<'a> SpanUtils<'a> {
205205
bracket_count += match prev.tok {
206206
token::Lt => 1,
207207
token::Gt => -1,
208+
token::BinOp(token::Shl) => 2,
208209
token::BinOp(token::Shr) => -2,
209210
_ => 0
210211
};
@@ -296,13 +297,25 @@ impl<'a> SpanUtils<'a> {
296297
pub fn sub_span_after_keyword(&self,
297298
span: Span,
298299
keyword: keywords::Keyword) -> Option<Span> {
300+
self.sub_span_after(span, |t| t.is_keyword(keyword))
301+
}
302+
303+
pub fn sub_span_after_token(&self,
304+
span: Span,
305+
tok: Token) -> Option<Span> {
306+
self.sub_span_after(span, |t| t == tok)
307+
}
308+
309+
fn sub_span_after<F: Fn(Token) -> bool>(&self,
310+
span: Span,
311+
f: F) -> Option<Span> {
299312
let mut toks = self.retokenise_span(span);
300313
loop {
301314
let ts = toks.real_token();
302315
if ts.tok == token::Eof {
303316
return None;
304317
}
305-
if ts.tok.is_keyword(keyword) {
318+
if f(ts.tok) {
306319
let ts = toks.real_token();
307320
if ts.tok == token::Eof {
308321
return None
@@ -313,6 +326,7 @@ impl<'a> SpanUtils<'a> {
313326
}
314327
}
315328

329+
316330
// Returns a list of the spans of idents in a patch.
317331
// E.g., For foo::bar<x,t>::baz, we return [foo, bar, baz] (well, their spans)
318332
pub fn spans_for_path_segments(&self, path: &ast::Path) -> Vec<Span> {

0 commit comments

Comments
 (0)