]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc/hir/print.rs
rustc: always include elidable lifetimes in HIR types.
[rust.git] / src / librustc / hir / print.rs
index 377f2ea26aba995da2e0da3fcf7e4112b4d7b68f..8e866f5717498c188d35b21eb40412f813ee8e63 100644 (file)
@@ -13,6 +13,7 @@
 use syntax::abi::Abi;
 use syntax::ast;
 use syntax::codemap::{CodeMap, Spanned};
+use syntax::parse::ParseSess;
 use syntax::parse::lexer::comments;
 use syntax::print::pp::{self, break_offset, word, space, hardbreak};
 use syntax::print::pp::{Breaks, eof};
 use syntax::ptr::P;
 use syntax::symbol::keywords;
 use syntax_pos::{self, BytePos};
-use errors;
 
 use hir;
-use hir::{PatKind, RegionTyParamBound, TraitTyParamBound, TraitBoundModifier};
+use hir::{PatKind, RegionTyParamBound, TraitTyParamBound, TraitBoundModifier, RangeEnd};
 
+use std::cell::Cell;
 use std::io::{self, Write, Read};
 
 pub enum AnnNode<'a> {
@@ -116,7 +117,7 @@ fn literals(&self) -> &Option<Vec<comments::Literal>> {
 /// it can scan the input text for comments and literals to
 /// copy forward.
 pub fn print_crate<'a>(cm: &'a CodeMap,
-                       span_diagnostic: &errors::Handler,
+                       sess: &ParseSess,
                        krate: &hir::Crate,
                        filename: String,
                        input: &mut Read,
@@ -124,8 +125,7 @@ pub fn print_crate<'a>(cm: &'a CodeMap,
                        ann: &'a PpAnn,
                        is_expanded: bool)
                        -> io::Result<()> {
-    let mut s = State::new_from_input(cm, span_diagnostic, filename, input,
-                                      out, ann, is_expanded);
+    let mut s = State::new_from_input(cm, sess, filename, input, out, ann, is_expanded);
 
     // When printing the AST, we sometimes need to inject `#[no_std]` here.
     // Since you can't compile the HIR, it's not necessary.
@@ -137,16 +137,14 @@ pub fn print_crate<'a>(cm: &'a CodeMap,
 
 impl<'a> State<'a> {
     pub fn new_from_input(cm: &'a CodeMap,
-                          span_diagnostic: &errors::Handler,
+                          sess: &ParseSess,
                           filename: String,
                           input: &mut Read,
                           out: Box<Write + 'a>,
                           ann: &'a PpAnn,
                           is_expanded: bool)
                           -> State<'a> {
-        let (cmnts, lits) = comments::gather_comments_and_literals(span_diagnostic,
-                                                                   filename,
-                                                                   input);
+        let (cmnts, lits) = comments::gather_comments_and_literals(sess, filename, input);
 
         State::new(cm,
                    out,
@@ -362,9 +360,9 @@ pub fn print_foreign_mod(&mut self,
         Ok(())
     }
 
-    pub fn print_opt_lifetime(&mut self, lifetime: &Option<hir::Lifetime>) -> io::Result<()> {
-        if let Some(l) = *lifetime {
-            self.print_lifetime(&l)?;
+    pub fn print_opt_lifetime(&mut self, lifetime: &hir::Lifetime) -> io::Result<()> {
+        if !lifetime.is_elided() {
+            self.print_lifetime(lifetime)?;
             self.nbsp()?;
         }
         Ok(())
@@ -1556,65 +1554,49 @@ fn print_path_parameters(&mut self,
                              parameters: &hir::PathParameters,
                              colons_before_params: bool)
                              -> io::Result<()> {
-        if parameters.is_empty() {
-            let infer_types = match *parameters {
-                hir::AngleBracketedParameters(ref data) => data.infer_types,
-                hir::ParenthesizedParameters(_) => false
-            };
-
-            // FIXME(eddyb) See the comment below about infer_types.
-            if !(infer_types && false) {
-                return Ok(());
-            }
-        }
-
-        if colons_before_params {
-            word(&mut self.s, "::")?
-        }
-
         match *parameters {
             hir::AngleBracketedParameters(ref data) => {
-                word(&mut self.s, "<")?;
+                let start = if colons_before_params { "::<" } else { "<" };
+                let empty = Cell::new(true);
+                let start_or_comma = |this: &mut Self| {
+                    if empty.get() {
+                        empty.set(false);
+                        word(&mut this.s, start)
+                    } else {
+                        this.word_space(",")
+                    }
+                };
 
-                let mut comma = false;
-                for lifetime in &data.lifetimes {
-                    if comma {
-                        self.word_space(",")?
+                if !data.lifetimes.iter().all(|lt| lt.is_elided()) {
+                    for lifetime in &data.lifetimes {
+                        start_or_comma(self)?;
+                        self.print_lifetime(lifetime)?;
                     }
-                    self.print_lifetime(lifetime)?;
-                    comma = true;
                 }
 
                 if !data.types.is_empty() {
-                    if comma {
-                        self.word_space(",")?
-                    }
+                    start_or_comma(self)?;
                     self.commasep(Inconsistent, &data.types, |s, ty| s.print_type(&ty))?;
-                    comma = true;
                 }
 
                 // FIXME(eddyb) This would leak into error messages, e.g.:
                 // "non-exhaustive patterns: `Some::<..>(_)` not covered".
                 if data.infer_types && false {
-                    if comma {
-                        self.word_space(",")?
-                    }
+                    start_or_comma(self)?;
                     word(&mut self.s, "..")?;
-                    comma = true;
                 }
 
                 for binding in data.bindings.iter() {
-                    if comma {
-                        self.word_space(",")?
-                    }
+                    start_or_comma(self)?;
                     self.print_name(binding.name)?;
                     space(&mut self.s)?;
                     self.word_space("=")?;
                     self.print_type(&binding.ty)?;
-                    comma = true;
                 }
 
-                word(&mut self.s, ">")?
+                if !empty.get() {
+                    word(&mut self.s, ">")?
+                }
             }
 
             hir::ParenthesizedParameters(ref data) => {
@@ -1735,10 +1717,13 @@ pub fn print_pat(&mut self, pat: &hir::Pat) -> io::Result<()> {
                 self.print_pat(&inner)?;
             }
             PatKind::Lit(ref e) => self.print_expr(&e)?,
-            PatKind::Range(ref begin, ref end) => {
+            PatKind::Range(ref begin, ref end, ref end_kind) => {
                 self.print_expr(&begin)?;
                 space(&mut self.s)?;
-                word(&mut self.s, "...")?;
+                match *end_kind {
+                    RangeEnd::Included => word(&mut self.s, "...")?,
+                    RangeEnd::Excluded => word(&mut self.s, "..")?,
+                }
                 self.print_expr(&end)?;
             }
             PatKind::Slice(ref before, ref slice, ref after) => {