From 9835e2ae736235810b4ea1c162ca5e65c547e770 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 18 May 2024 04:49:50 +0200 Subject: Merging upstream version 1.71.1+dfsg1. Signed-off-by: Daniel Baumann --- tests/ui/fn/fn-item-lifetime-bounds.rs | 37 ++++++++++++++++++++++ .../fn/implied-bounds-impl-header-projections.rs | 31 ++++++++++++++++++ tests/ui/fn/issue-3099.rs | 11 +++++++ tests/ui/fn/issue-3099.stderr | 14 ++++++++ 4 files changed, 93 insertions(+) create mode 100644 tests/ui/fn/fn-item-lifetime-bounds.rs create mode 100644 tests/ui/fn/implied-bounds-impl-header-projections.rs create mode 100644 tests/ui/fn/issue-3099.rs create mode 100644 tests/ui/fn/issue-3099.stderr (limited to 'tests/ui/fn') diff --git a/tests/ui/fn/fn-item-lifetime-bounds.rs b/tests/ui/fn/fn-item-lifetime-bounds.rs new file mode 100644 index 000000000..68a1d0ce9 --- /dev/null +++ b/tests/ui/fn/fn-item-lifetime-bounds.rs @@ -0,0 +1,37 @@ +// check-pass +// known-bug: #84533 + +// Should fail. Lifetimes are checked correctly when `foo` is called, but NOT +// when only the lifetime parameters are instantiated. + +use std::marker::PhantomData; + +#[allow(dead_code)] +fn foo<'b, 'a>() -> PhantomData<&'b &'a ()> { + PhantomData +} + +#[allow(dead_code)] +#[allow(path_statements)] +fn caller<'b, 'a>() { + foo::<'b, 'a>; +} + +// In contrast to above, below code correctly does NOT compile. +// fn caller<'b, 'a>() { +// foo::<'b, 'a>(); +// } + +// error: lifetime may not live long enough +// --> src/main.rs:22:5 +// | +// 21 | fn caller<'b, 'a>() { +// | -- -- lifetime `'a` defined here +// | | +// | lifetime `'b` defined here +// 22 | foo::<'b, 'a>(); +// | ^^^^^^^^^^^^^^^ requires that `'a` must outlive `'b` +// | +// = help: consider adding the following bound: `'a: 'b` + +fn main() {} diff --git a/tests/ui/fn/implied-bounds-impl-header-projections.rs b/tests/ui/fn/implied-bounds-impl-header-projections.rs new file mode 100644 index 000000000..28cec8050 --- /dev/null +++ b/tests/ui/fn/implied-bounds-impl-header-projections.rs @@ -0,0 +1,31 @@ +// check-pass +// known-bug: #100051 + +// Should fail. Implied bounds from projections in impl headers can create +// improper lifetimes. Variant of issue #98543 which was fixed by #99217. + +trait Trait { + type Type; +} + +impl Trait for T { + type Type = (); +} + +trait Extend<'a, 'b> { + fn extend(self, s: &'a str) -> &'b str; +} + +impl<'a, 'b> Extend<'a, 'b> for <&'b &'a () as Trait>::Type +where + for<'what, 'ever> &'what &'ever (): Trait, +{ + fn extend(self, s: &'a str) -> &'b str { + s + } +} + +fn main() { + let y = <() as Extend<'_, '_>>::extend((), &String::from("Hello World")); + println!("{}", y); +} diff --git a/tests/ui/fn/issue-3099.rs b/tests/ui/fn/issue-3099.rs new file mode 100644 index 000000000..ee75b3593 --- /dev/null +++ b/tests/ui/fn/issue-3099.rs @@ -0,0 +1,11 @@ +fn a(x: String) -> String { + format!("First function with {}", x) +} + +fn a(x: String, y: String) -> String { //~ ERROR the name `a` is defined multiple times + format!("Second function with {} and {}", x, y) +} + +fn main() { + println!("Result: "); +} diff --git a/tests/ui/fn/issue-3099.stderr b/tests/ui/fn/issue-3099.stderr new file mode 100644 index 000000000..32ee2e1d2 --- /dev/null +++ b/tests/ui/fn/issue-3099.stderr @@ -0,0 +1,14 @@ +error[E0428]: the name `a` is defined multiple times + --> $DIR/issue-3099.rs:5:1 + | +LL | fn a(x: String) -> String { + | ------------------------- previous definition of the value `a` here +... +LL | fn a(x: String, y: String) -> String { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `a` redefined here + | + = note: `a` must be defined only once in the value namespace of this module + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0428`. -- cgit v1.2.3