about summary refs log tree commit diff
path: root/tests/pretty
diff options
context:
space:
mode:
authorNicholas Nethercote <n.nethercote@gmail.com>2025-03-26 15:51:41 +1100
committerNicholas Nethercote <n.nethercote@gmail.com>2025-03-28 08:25:06 +1100
commit4924e0a067946d7b810dcc777ffd8370c29da80a (patch)
treea06b84ceb26e78f2da09eda2b28a0c9deabac917 /tests/pretty
parent217693a1f02ca6431a434926ff3417bdb6dbac2e (diff)
downloadrust-4924e0a067946d7b810dcc777ffd8370c29da80a.tar.gz
rust-4924e0a067946d7b810dcc777ffd8370c29da80a.zip
Add a HIR pretty-printing test focused on lifetimes.
HIR printing currently gets very little testing. This increases coverage
a bit, with a focus on lifetimes.

There are some FIXME comments for cases that are printed in a dubious
fashion. This PR won't address those; the point of adding this test is
to ensure that the subsequent commits don't hurt pretty-printing.
Diffstat (limited to 'tests/pretty')
-rw-r--r--tests/pretty/hir-lifetimes.pp96
-rw-r--r--tests/pretty/hir-lifetimes.rs91
2 files changed, 187 insertions, 0 deletions
diff --git a/tests/pretty/hir-lifetimes.pp b/tests/pretty/hir-lifetimes.pp
new file mode 100644
index 00000000000..df3aba516b9
--- /dev/null
+++ b/tests/pretty/hir-lifetimes.pp
@@ -0,0 +1,96 @@
+//@ pretty-compare-only
+//@ pretty-mode:hir
+//@ pp-exact:hir-lifetimes.pp
+
+// This tests the pretty-printing of lifetimes in lots of ways.
+
+#![allow(unused)]
+#[prelude_import]
+use ::std::prelude::rust_2015::*;
+#[macro_use]
+extern crate std;
+
+struct Foo<'a> {
+    x: &'a u32,
+}
+
+impl <'a> Foo<'a> {
+    fn f<'b>(x: &'b u32) { }
+}
+
+impl  Foo<'_> {
+    fn a(x: &'_ u32) { }
+
+    fn b(x: &'_ u32) { }
+
+    fn c(x: &'_ u32, y: &'static u32) { }
+
+    // FIXME: `'a` before `self` is omitted
+    fn d<'a>(&self, x: &'a u32) { }
+
+    // FIXME: impl Traits printed as just `/*impl Trait*/`, ugh
+    fn iter1<'a>(&self)
+        -> /*impl Trait*/ { #[lang = "Range"]{ start: 0,  end: 1,} }
+
+    fn iter2(&self)
+        -> /*impl Trait*/ { #[lang = "Range"]{ start: 0,  end: 1,} }
+}
+
+fn a(x: Foo<'_>) { }
+
+fn b<'a>(x: Foo<'a>) { }
+
+struct Bar<'a, 'b, 'c, T> {
+    x: &'a u32,
+    y: &'b &'c u32,
+    z: T,
+}
+
+fn f1<'a, 'b, T>(x: Bar<'a, 'b, '_, T>) { }
+
+fn f2(x: Bar<'_, '_, '_, u32>) { }
+
+trait MyTrait<'a, 'b> {
+    fn f(&self, x: Foo<'a>, y: Foo<'b>);
+}
+
+impl <'a, 'b, 'c, T> MyTrait<'a, 'b> for Bar<'a, 'b, 'c, T> {
+    fn f(&self, x: Foo<'a>, y: Foo<'b>) { }
+}
+
+fn g(x: &'_ dyn for<'a, 'b> MyTrait<'a, 'b>) { }
+
+trait Blah { }
+
+type T<'a> = dyn Blah + 'a;
+
+type Q<'a> = dyn MyTrait<'a, 'a> + 'a;
+
+fn h<'b, F>(f: F, y: Foo<'b>) where F: for<'d> MyTrait<'d, 'b> { }
+
+// FIXME(?): attr printing is weird
+#[attr = Repr([ReprC])]
+struct S<'a>(&'a u32);
+
+extern "C" {
+    unsafe fn g1(s: S<>);
+    unsafe fn g2(s: S<'_>);
+    unsafe fn g3<'a>(s: S<'a>);
+}
+
+struct St<'a> {
+    x: &'a u32,
+}
+
+fn f() { { let _ = St{ x: &0,}; }; { let _ = St{ x: &0,}; }; }
+
+struct Name<'a>(&'a str);
+
+const A: Name<> = Name("a");
+const B: &'_ str = "";
+static C: &'_ str = "";
+static D: &'static str = "";
+
+fn tr(_: Box<dyn Blah>) { }
+
+fn main() { }
diff --git a/tests/pretty/hir-lifetimes.rs b/tests/pretty/hir-lifetimes.rs
new file mode 100644
index 00000000000..1379be34392
--- /dev/null
+++ b/tests/pretty/hir-lifetimes.rs
@@ -0,0 +1,91 @@
+//@ pretty-compare-only
+//@ pretty-mode:hir
+//@ pp-exact:hir-lifetimes.pp
+
+// This tests the pretty-printing of lifetimes in lots of ways.
+
+#![allow(unused)]
+
+struct Foo<'a> {
+    x: &'a u32,
+}
+
+impl<'a> Foo<'a> {
+    fn f<'b>(x: &'b u32) {}
+}
+
+impl Foo<'_> {
+    fn a(x: &u32) {}
+
+    fn b(x: &'_ u32) {}
+
+    fn c(x: &'_ u32, y: &'static u32) {}
+
+    // FIXME: `'a` before `self` is omitted
+    fn d<'a>(&'a self, x: &'a u32) {}
+
+    // FIXME: impl Traits printed as just `/*impl Trait*/`, ugh
+    fn iter1<'a>(&self) -> impl Iterator<Item = u32> + 'a { 0..1 }
+
+    fn iter2(&self) -> impl Iterator<Item = u32> + '_ { 0..1 }
+}
+
+fn a(x: Foo<'_>) {}
+
+fn b<'a>(x: Foo<'a>) {}
+
+struct Bar<'a, 'b, 'c, T> {
+    x: &'a u32,
+    y: &'b &'c u32,
+    z: T,
+}
+
+fn f1<'a, 'b, T>(x: Bar<'a, 'b, '_, T>) {}
+
+fn f2(x: Bar<'_, '_, '_, u32>) {}
+
+trait MyTrait<'a, 'b> {
+    fn f(&self, x: Foo<'a>, y: Foo<'b>);
+}
+
+impl<'a, 'b, 'c, T> MyTrait<'a, 'b> for Bar<'a, 'b, 'c, T> {
+    fn f(&self, x: Foo<'a>, y: Foo<'b>) {}
+}
+
+fn g(x: &dyn for<'a, 'b> MyTrait<'a, 'b>) {}
+
+trait Blah {}
+
+type T<'a> = dyn Blah + 'a;
+
+type Q<'a> = dyn MyTrait<'a, 'a> + 'a;
+
+fn h<'b, F>(f: F, y: Foo<'b>) where F: for<'d> MyTrait<'d, 'b> {}
+
+// FIXME(?): attr printing is weird
+#[repr(C)]
+struct S<'a>(&'a u32);
+
+extern "C" {
+    fn g1(s: S);
+    fn g2(s: S<'_>);
+    fn g3<'a>(s: S<'a>);
+}
+
+struct St<'a> { x: &'a u32 }
+
+fn f() {
+    _ = St { x: &0 };
+    _ = St::<'_> { x: &0 };
+}
+
+struct Name<'a>(&'a str);
+
+const A: Name = Name("a");
+const B: &str = "";
+static C: &'_ str = "";
+static D: &'static str = "";
+
+fn tr(_: Box<dyn Blah>) {}
+
+fn main() {}