]> git.lizzy.rs Git - rust.git/commitdiff
Pretty print lifetimes captured by RPIT
authorMichael Goulet <michael@errs.io>
Sat, 22 Oct 2022 20:18:16 +0000 (20:18 +0000)
committerMichael Goulet <michael@errs.io>
Sat, 22 Oct 2022 20:29:40 +0000 (20:29 +0000)
compiler/rustc_middle/src/ty/print/pretty.rs
src/test/ui/impl-trait/hidden-lifetimes.stderr

index 0b06ccc19ecc8fa539559bd2ef1068fb2102bc5e..2a229e0474382b975237feba851d48d06d590cd9 100644 (file)
@@ -16,6 +16,7 @@
 use rustc_span::symbol::{kw, Ident, Symbol};
 use rustc_target::abi::Size;
 use rustc_target::spec::abi::Abi;
+use smallvec::SmallVec;
 
 use std::cell::Cell;
 use std::char;
@@ -794,6 +795,7 @@ fn pretty_print_opaque_impl_type(
         let mut traits = FxIndexMap::default();
         let mut fn_traits = FxIndexMap::default();
         let mut is_sized = false;
+        let mut lifetimes = SmallVec::<[ty::Region<'tcx>; 1]>::new();
 
         for predicate in bounds.transpose_iter().map(|e| e.map_bound(|(p, _)| *p)) {
             let predicate = predicate.subst(tcx, substs);
@@ -825,6 +827,9 @@ fn pretty_print_opaque_impl_type(
                         &mut fn_traits,
                     );
                 }
+                ty::PredicateKind::TypeOutlives(outlives) => {
+                    lifetimes.push(outlives.1);
+                }
                 _ => {}
             }
         }
@@ -978,6 +983,17 @@ fn pretty_print_opaque_impl_type(
             write!(self, "Sized")?;
         }
 
+        if let [re] = lifetimes.as_slice()
+            && re.is_static()
+        {
+            // Don't print a single static lifetime
+        } else {
+            for re in lifetimes {
+                write!(self, " + ")?;
+                self = self.print_region(re)?;
+            }
+        }
+
         Ok(self)
     }
 
index efc228de58be5d482c231114cc102698ea53366b..de06ded7acdb6f7666bd4e81ef40a89f22612d0c 100644 (file)
@@ -1,4 +1,4 @@
-error[E0700]: hidden type for `impl Swap` captures lifetime that does not appear in bounds
+error[E0700]: hidden type for `impl Swap + 'a` captures lifetime that does not appear in bounds
   --> $DIR/hidden-lifetimes.rs:29:5
    |
 LL | fn hide_ref<'a, 'b, T: 'static>(x: &'a mut &'b T) -> impl Swap + 'a {
@@ -11,7 +11,7 @@ help: to declare that the `impl Trait` captures `'b`, you can add an explicit `'
 LL | fn hide_ref<'a, 'b, T: 'static>(x: &'a mut &'b T) -> impl Swap + 'a + 'b {
    |                                                                     ++++
 
-error[E0700]: hidden type for `impl Swap` captures lifetime that does not appear in bounds
+error[E0700]: hidden type for `impl Swap + 'a` captures lifetime that does not appear in bounds
   --> $DIR/hidden-lifetimes.rs:46:5
    |
 LL | fn hide_rc_refcell<'a, 'b: 'a, T: 'static>(x: Rc<RefCell<&'b T>>) -> impl Swap + 'a {