Skip to content

Commit

Permalink
save-anlaysis: misc bug fixes
Browse files Browse the repository at this point in the history
In particular, handling of struct literals where the struct name is a type alias, and tuple indexing.

Plus some other stuff.
  • Loading branch information
nrc committed Jan 29, 2015
1 parent dcbd418 commit 78f6178
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 29 deletions.
65 changes: 39 additions & 26 deletions src/librustc_trans/save/mod.rs
Expand Up @@ -814,7 +814,13 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
sub_span,
def_id,
self.cur_scope),
def::DefStaticMethod(declid, provenence) => {
def::DefTy(def_id, _) => self.fmt.ref_str(recorder::TypeRef,
span,
sub_span,
def_id,
self.cur_scope),
def::DefStaticMethod(declid, provenence) |
def::DefMethod(declid, _, provenence) => {
let sub_span = self.span.sub_span_for_meth_name(span);
let defid = if declid.krate == ast::LOCAL_CRATE {
let ti = ty::impl_or_trait_item(&self.analysis.ty_cx,
Expand Down Expand Up @@ -856,13 +862,17 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
Some(declid),
self.cur_scope);
},
def::DefFn(def_id, _) => self.fmt.fn_call_str(span,
sub_span,
def_id,
self.cur_scope),
def::DefFn(def_id, _) => {
self.fmt.fn_call_str(span,
sub_span,
def_id,
self.cur_scope)
}
_ => self.sess.span_bug(span,
&format!("Unexpected def kind while looking up path in '{}'",
self.span.snippet(span))[]),
&format!("Unexpected def kind while looking \
up path in `{}`: `{:?}`",
self.span.snippet(span),
*def)[]),
}
// modules or types in the path prefix
match *def {
Expand All @@ -886,21 +896,21 @@ impl <'l, 'tcx> DxrVisitor<'l, 'tcx> {
return
}

let mut struct_def: Option<DefId> = None;
match self.lookup_type_ref(ex.id) {
Some(id) => {
struct_def = Some(id);
self.write_sub_paths_truncated(path, false);

let ty = &ty::expr_ty_adjusted(&self.analysis.ty_cx, ex).sty;
let struct_def = match *ty {
ty::ty_struct(def_id, _) => {
let sub_span = self.span.span_for_last_ident(path.span);
self.fmt.ref_str(recorder::StructRef,
path.span,
sub_span,
id,
def_id,
self.cur_scope);
},
None => ()
}

self.write_sub_paths_truncated(path, false);
Some(def_id)
}
_ => None
};

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

self.visit_expr(&**sub_ex);

match ty::expr_ty_adjusted(&self.analysis.ty_cx, &**sub_ex).sty {
let ty = &ty::expr_ty_adjusted(&self.analysis.ty_cx, &**sub_ex).sty;
match *ty {
ty::ty_struct(def_id, _) => {
let fields = ty::lookup_struct_fields(&self.analysis.ty_cx, def_id);
for f in fields.iter() {
Expand All @@ -1350,9 +1360,9 @@ impl<'l, 'tcx, 'v> Visitor<'v> for DxrVisitor<'l, 'tcx> {
break;
}
}
},
}
_ => self.sess.span_bug(ex.span,
"Expected struct type, but not ty_struct"),
&format!("Expected struct type, found {:?}", ty)[]),
}
},
ast::ExprTupField(ref sub_ex, idx) => {
Expand All @@ -1362,12 +1372,13 @@ impl<'l, 'tcx, 'v> Visitor<'v> for DxrVisitor<'l, 'tcx> {

self.visit_expr(&**sub_ex);

match ty::expr_ty_adjusted(&self.analysis.ty_cx, &**sub_ex).sty {
let ty = &ty::expr_ty_adjusted(&self.analysis.ty_cx, &**sub_ex).sty;
match *ty {
ty::ty_struct(def_id, _) => {
let fields = ty::lookup_struct_fields(&self.analysis.ty_cx, def_id);
for (i, f) in fields.iter().enumerate() {
if i == idx.node {
let sub_span = self.span.span_for_last_ident(ex.span);
let sub_span = self.span.sub_span_after_token(ex.span, token::Dot);
self.fmt.ref_str(recorder::VarRef,
ex.span,
sub_span,
Expand All @@ -1376,9 +1387,11 @@ impl<'l, 'tcx, 'v> Visitor<'v> for DxrVisitor<'l, 'tcx> {
break;
}
}
},
}
ty::ty_tup(_) => {}
_ => self.sess.span_bug(ex.span,
"Expected struct type, but not ty_struct"),
&format!("Expected struct or tuple \
type, found {:?}", ty)[]),
}
},
ast::ExprClosure(_, _, ref decl, ref body) => {
Expand Down Expand Up @@ -1454,7 +1467,7 @@ impl<'l, 'tcx, 'v> Visitor<'v> for DxrVisitor<'l, 'tcx> {
&value[],
"")
}
def::DefVariant(..) => {
def::DefVariant(..) | def::DefTy(..) | def::DefStruct(..) => {
paths_to_process.push((id, p.clone(), Some(ref_kind)))
}
// FIXME(nrc) what are these doing here?
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/save/recorder.rs
Expand Up @@ -62,7 +62,7 @@ macro_rules! svec {
})
}

#[derive(Copy,Debug)]
#[derive(Copy, Debug, Eq, PartialEq)]
pub enum Row {
Variable,
Enum,
Expand Down
18 changes: 16 additions & 2 deletions src/librustc_trans/save/span_utils.rs
Expand Up @@ -37,7 +37,7 @@ impl<'a> SpanUtils<'a> {
let lo_pos_byte = self.sess.codemap().lookup_byte_offset(span.lo).pos;
let hi_pos_byte = self.sess.codemap().lookup_byte_offset(span.hi).pos;

format!("file_name,{},file_line,{},file_col,{},extent_start,{},extent_start_bytes,{},\
format!("file_name,\"{}\",file_line,{},file_col,{},extent_start,{},extent_start_bytes,{},\
file_line_end,{},file_col_end,{},extent_end,{},extent_end_bytes,{}",
lo_loc.file.name,
lo_loc.line, lo_loc.col.to_usize(), lo_pos.to_usize(), lo_pos_byte.to_usize(),
Expand Down Expand Up @@ -205,6 +205,7 @@ impl<'a> SpanUtils<'a> {
bracket_count += match prev.tok {
token::Lt => 1,
token::Gt => -1,
token::BinOp(token::Shl) => 2,
token::BinOp(token::Shr) => -2,
_ => 0
};
Expand Down Expand Up @@ -296,13 +297,25 @@ impl<'a> SpanUtils<'a> {
pub fn sub_span_after_keyword(&self,
span: Span,
keyword: keywords::Keyword) -> Option<Span> {
self.sub_span_after(span, |t| t.is_keyword(keyword))
}

pub fn sub_span_after_token(&self,
span: Span,
tok: Token) -> Option<Span> {
self.sub_span_after(span, |t| t == tok)
}

fn sub_span_after<F: Fn(Token) -> bool>(&self,
span: Span,
f: F) -> Option<Span> {
let mut toks = self.retokenise_span(span);
loop {
let ts = toks.real_token();
if ts.tok == token::Eof {
return None;
}
if ts.tok.is_keyword(keyword) {
if f(ts.tok) {
let ts = toks.real_token();
if ts.tok == token::Eof {
return None
Expand All @@ -313,6 +326,7 @@ impl<'a> SpanUtils<'a> {
}
}


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

0 comments on commit 78f6178

Please sign in to comment.