Skip to content

don't gratuitously error on tests returning Result with lifetime #80934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,12 @@ pub struct GenericParam {
pub kind: GenericParamKind,
}

impl GenericParam {
pub fn is_lifetime(&self) -> bool {
matches!(self.kind, GenericParamKind::Lifetime)
}
}

/// Represents lifetime, type and const parameters attached to a declaration of
/// a function, enum, trait, etc.
#[derive(Clone, Encodable, Decodable, Debug)]
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_builtin_macros/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,8 @@ fn has_test_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool {
false
}
(true, false) => {
if !generics.params.is_empty() {
sd.span_err(i.span, "functions used as tests must have signature fn() -> ()");
if !generics.params.iter().all(|p| p.is_lifetime()) {
sd.span_err(i.span, "return value of functions used as tests must either be `()` or implement `Termination`, and cannot use generics");
false
} else {
true
Expand Down
47 changes: 47 additions & 0 deletions src/test/ui/test-fn-with-lifetime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// compile-flags: --test

// Issue #55228

#![feature(termination_trait_lib)]

use std::process::Termination;

#[test]
fn unit_1() -> Result<(), &'static str> { // this is OK
Ok(())
}

#[test]
fn unit_2<'a>() -> Result<(), &'a str> { // also OK
Ok(())
}

#[test]
fn unit_3<T: Termination>() -> Result<(), T> { // nope (how would this get monomorphized?)
//~^ ERROR return value of functions used as tests must either be `()` or implement
Ok(())
}


struct Quux;

trait Bar {
type Baz;

fn rah(&self) -> Self::Baz;
}

impl Bar for Quux {
type Baz = String;

fn rah(&self) -> Self::Baz {
"rah".to_owned()
}
}

#[test]
fn unit_4<T: Bar<Baz = String>>() -> <T as Bar>::Baz { // also nope
//~^ ERROR return value of functions used as tests must either be `()` or implement
let q = Quux{};
<Quux as Bar>::rah(&q)
}
21 changes: 21 additions & 0 deletions src/test/ui/test-fn-with-lifetime.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error: return value of functions used as tests must either be `()` or implement `Termination`, and cannot use generics
--> $DIR/test-fn-with-lifetime.rs:20:1
|
LL | / fn unit_3<T: Termination>() -> Result<(), T> { // nope (how would this get monomorphized?)
LL | |
LL | | Ok(())
LL | | }
| |_^

error: return value of functions used as tests must either be `()` or implement `Termination`, and cannot use generics
--> $DIR/test-fn-with-lifetime.rs:43:1
|
LL | / fn unit_4<T: Bar<Baz = String>>() -> <T as Bar>::Baz { // also nope
LL | |
LL | | let q = Quux{};
LL | | <Quux as Bar>::rah(&q)
LL | | }
| |_^

error: aborting due to 2 previous errors