Skip to content

Commit c0cda2b

Browse files
Pretty print lifetimes captured by RPIT
1 parent 289b2b8 commit c0cda2b

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

compiler/rustc_middle/src/ty/print/pretty.rs

+16
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_session::cstore::{ExternCrate, ExternCrateSource};
1616
use rustc_span::symbol::{kw, Ident, Symbol};
1717
use rustc_target::abi::Size;
1818
use rustc_target::spec::abi::Abi;
19+
use smallvec::SmallVec;
1920

2021
use std::cell::Cell;
2122
use std::char;
@@ -794,6 +795,7 @@ pub trait PrettyPrinter<'tcx>:
794795
let mut traits = FxIndexMap::default();
795796
let mut fn_traits = FxIndexMap::default();
796797
let mut is_sized = false;
798+
let mut lifetimes = SmallVec::<[ty::Region<'tcx>; 1]>::new();
797799

798800
for predicate in bounds.transpose_iter().map(|e| e.map_bound(|(p, _)| *p)) {
799801
let predicate = predicate.subst(tcx, substs);
@@ -825,6 +827,9 @@ pub trait PrettyPrinter<'tcx>:
825827
&mut fn_traits,
826828
);
827829
}
830+
ty::PredicateKind::TypeOutlives(outlives) => {
831+
lifetimes.push(outlives.1);
832+
}
828833
_ => {}
829834
}
830835
}
@@ -978,6 +983,17 @@ pub trait PrettyPrinter<'tcx>:
978983
write!(self, "Sized")?;
979984
}
980985

986+
if let [re] = lifetimes.as_slice()
987+
&& re.is_static()
988+
{
989+
// Don't print a single static lifetime
990+
} else {
991+
for re in lifetimes {
992+
write!(self, " + ")?;
993+
self = self.print_region(re)?;
994+
}
995+
}
996+
981997
Ok(self)
982998
}
983999

src/test/ui/impl-trait/hidden-lifetimes.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0700]: hidden type for `impl Swap` captures lifetime that does not appear in bounds
1+
error[E0700]: hidden type for `impl Swap + 'a` captures lifetime that does not appear in bounds
22
--> $DIR/hidden-lifetimes.rs:29:5
33
|
44
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 `'
1111
LL | fn hide_ref<'a, 'b, T: 'static>(x: &'a mut &'b T) -> impl Swap + 'a + 'b {
1212
| ++++
1313

14-
error[E0700]: hidden type for `impl Swap` captures lifetime that does not appear in bounds
14+
error[E0700]: hidden type for `impl Swap + 'a` captures lifetime that does not appear in bounds
1515
--> $DIR/hidden-lifetimes.rs:46:5
1616
|
1717
LL | fn hide_rc_refcell<'a, 'b: 'a, T: 'static>(x: Rc<RefCell<&'b T>>) -> impl Swap + 'a {

0 commit comments

Comments
 (0)