]> git.lizzy.rs Git - rust.git/commitdiff
Stop pretty-printing anonymous lifetimes.
authorCamille GILLOT <gillot.camille@gmail.com>
Thu, 12 May 2022 19:18:26 +0000 (21:18 +0200)
committerCamille GILLOT <gillot.camille@gmail.com>
Sun, 29 May 2022 10:16:09 +0000 (12:16 +0200)
compiler/rustc_middle/src/ty/print/pretty.rs

index 4c0bc2e4337c712ffd96f09cc6d90f744c4abf20..64c63e3d567d039d91b133a01662b1f46e6a4b29 100644 (file)
@@ -2177,61 +2177,47 @@ fn name_by_region_index(index: usize) -> Symbol {
         define_scoped_cx!(self);
 
         let mut region_index = self.region_index;
+        let mut next_name = |this: &Self| loop {
+            let name = name_by_region_index(region_index);
+            region_index += 1;
+            if !this.used_region_names.contains(&name) {
+                break name;
+            }
+        };
+
         // If we want to print verbosely, then print *all* binders, even if they
         // aren't named. Eventually, we might just want this as the default, but
         // this is not *quite* right and changes the ordering of some output
         // anyways.
         let (new_value, map) = if self.tcx().sess.verbose() {
             // anon index + 1 (BrEnv takes 0) -> name
-            let mut region_map: BTreeMap<u32, Symbol> = BTreeMap::default();
+            let mut region_map: FxHashMap<_, _> = Default::default();
             let bound_vars = value.bound_vars();
             for var in bound_vars {
+                let ty::BoundVariableKind::Region(var) = var else { continue };
                 match var {
-                    ty::BoundVariableKind::Region(ty::BrNamed(_, name)) => {
+                    ty::BrAnon(_) | ty::BrEnv => {
                         start_or_continue(&mut self, "for<", ", ");
+                        let name = next_name(&self);
                         do_continue(&mut self, name);
+                        region_map.insert(var, ty::BrNamed(CRATE_DEF_ID.to_def_id(), name));
                     }
-                    ty::BoundVariableKind::Region(ty::BrAnon(i)) => {
+                    ty::BrNamed(def_id, kw::UnderscoreLifetime) => {
                         start_or_continue(&mut self, "for<", ", ");
-                        let name = loop {
-                            let name = name_by_region_index(region_index);
-                            region_index += 1;
-                            if !self.used_region_names.contains(&name) {
-                                break name;
-                            }
-                        };
+                        let name = next_name(&self);
                         do_continue(&mut self, name);
-                        region_map.insert(i + 1, name);
+                        region_map.insert(var, ty::BrNamed(def_id, name));
                     }
-                    ty::BoundVariableKind::Region(ty::BrEnv) => {
+                    ty::BrNamed(_, name) => {
                         start_or_continue(&mut self, "for<", ", ");
-                        let name = loop {
-                            let name = name_by_region_index(region_index);
-                            region_index += 1;
-                            if !self.used_region_names.contains(&name) {
-                                break name;
-                            }
-                        };
                         do_continue(&mut self, name);
-                        region_map.insert(0, name);
                     }
-                    _ => continue,
                 }
             }
             start_or_continue(&mut self, "", "> ");
 
             self.tcx.replace_late_bound_regions(value.clone(), |br| {
-                let kind = match br.kind {
-                    ty::BrNamed(_, _) => br.kind,
-                    ty::BrAnon(i) => {
-                        let name = region_map[&(i + 1)];
-                        ty::BrNamed(CRATE_DEF_ID.to_def_id(), name)
-                    }
-                    ty::BrEnv => {
-                        let name = region_map[&0];
-                        ty::BrNamed(CRATE_DEF_ID.to_def_id(), name)
-                    }
-                };
+                let kind = region_map[&br.kind];
                 self.tcx.mk_region(ty::ReLateBound(
                     ty::INNERMOST,
                     ty::BoundRegion { var: br.var, kind },
@@ -2242,21 +2228,20 @@ fn name_by_region_index(index: usize) -> Symbol {
             let mut name = |br: ty::BoundRegion| {
                 start_or_continue(&mut self, "for<", ", ");
                 let kind = match br.kind {
-                    ty::BrNamed(_, name) => {
-                        do_continue(&mut self, name);
-                        br.kind
-                    }
                     ty::BrAnon(_) | ty::BrEnv => {
-                        let name = loop {
-                            let name = name_by_region_index(region_index);
-                            region_index += 1;
-                            if !self.used_region_names.contains(&name) {
-                                break name;
-                            }
-                        };
+                        let name = next_name(&self);
                         do_continue(&mut self, name);
                         ty::BrNamed(CRATE_DEF_ID.to_def_id(), name)
                     }
+                    ty::BrNamed(def_id, kw::UnderscoreLifetime) => {
+                        let name = next_name(&self);
+                        do_continue(&mut self, name);
+                        ty::BrNamed(def_id, name)
+                    }
+                    ty::BrNamed(_, name) => {
+                        do_continue(&mut self, name);
+                        br.kind
+                    }
                 };
                 tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BoundRegion { var: br.var, kind }))
             };