]> git.lizzy.rs Git - rust.git/commitdiff
save-anlaysis: misc bug fixes
authorNick Cameron <ncameron@mozilla.com>
Tue, 20 Jan 2015 20:15:37 +0000 (09:15 +1300)
committerNick Cameron <ncameron@mozilla.com>
Thu, 29 Jan 2015 22:58:10 +0000 (11:58 +1300)
In particular, handling of struct literals where the struct name is a type alias, and tuple indexing.

Plus some other stuff.

src/librustc_trans/save/mod.rs
src/librustc_trans/save/recorder.rs
src/librustc_trans/save/span_utils.rs

index f97b785f40f573a0b4291fb172ff0e823d5054b0..354a9cb817bdf2f8b628a647728e7f92e3fe2d1d 100644 (file)
@@ -814,7 +814,13 @@ fn process_path(&mut self,
                                                        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,
@@ -856,13 +862,17 @@ fn process_path(&mut self,
                                        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 {
@@ -886,21 +896,21 @@ fn process_struct_lit(&mut self,
             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 {
@@ -1335,8 +1345,8 @@ fn visit_expr(&mut self, ex: &ast::Expr) {
                 }
 
                 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() {
@@ -1350,9 +1360,9 @@ fn visit_expr(&mut self, ex: &ast::Expr) {
                                 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) => {
@@ -1362,12 +1372,13 @@ fn visit_expr(&mut self, ex: &ast::Expr) {
 
                 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,
@@ -1376,9 +1387,11 @@ fn visit_expr(&mut self, ex: &ast::Expr) {
                                 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) => {
@@ -1454,7 +1467,7 @@ fn visit_arm(&mut self, arm: &ast::Arm) {
                                           &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?
index 5b8c1878cf266cd8b611be3fa3f91b5f09467683..1378d40920808570c6ae64f2ba761e85bee149d3 100644 (file)
@@ -62,7 +62,7 @@ macro_rules! svec {
     })
 }
 
-#[derive(Copy,Debug)]
+#[derive(Copy, Debug, Eq, PartialEq)]
 pub enum Row {
     Variable,
     Enum,
index 97b3cda006bf7b36441b53b188ac58aacd105c8b..0e623ab1b7e04c87abf629bb4e160895e1121399 100644 (file)
@@ -37,7 +37,7 @@ pub fn extent_str(&self, span: Span) -> String {
         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(),
@@ -205,6 +205,7 @@ pub fn sub_span_for_type_name(&self, span: Span) -> Option<Span> {
             bracket_count += match prev.tok {
                 token::Lt => 1,
                 token::Gt => -1,
+                token::BinOp(token::Shl) => 2,
                 token::BinOp(token::Shr) => -2,
                 _ => 0
             };
@@ -296,13 +297,25 @@ pub fn sub_span_of_token(&self, span: Span, tok: Token) -> Option<Span> {
     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
@@ -313,6 +326,7 @@ pub fn sub_span_after_keyword(&self,
         }
     }
 
+
     // 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> {