Skip to content

Commit 774f782

Browse files
author
Lukas Markeffsky
committed
Deny non-lifetime generics for test functions.
Previously, these were allowed if the function returned `()`, but always led to an ambiguity error.
1 parent 4bf0061 commit 774f782

File tree

1 file changed

+15
-18
lines changed
  • compiler/rustc_builtin_macros/src

1 file changed

+15
-18
lines changed

compiler/rustc_builtin_macros/src/test.rs

+15-18
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::errors;
33
/// Ideally, this code would be in libtest but for efficiency and error messages it lives here.
44
use crate::util::{check_builtin_macro_attribute, warn_on_duplicate_attribute};
55
use rustc_ast::ptr::P;
6-
use rustc_ast::{self as ast, attr};
6+
use rustc_ast::{self as ast, attr, GenericParamKind};
77
use rustc_ast_pretty::pprust;
88
use rustc_errors::Applicability;
99
use rustc_expand::base::*;
@@ -550,24 +550,21 @@ fn has_test_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool {
550550
return false;
551551
}
552552

553-
match (has_output, has_should_panic_attr) {
554-
(true, true) => {
555-
sd.span_err(i.span, "functions using `#[should_panic]` must return `()`");
556-
false
557-
}
558-
(true, false) => {
559-
if !generics.params.is_empty() {
560-
sd.span_err(
561-
i.span,
562-
"functions used as tests must have signature fn() -> ()",
563-
);
564-
false
565-
} else {
566-
true
567-
}
568-
}
569-
(false, _) => true,
553+
if has_should_panic_attr && has_output {
554+
sd.span_err(i.span, "functions using `#[should_panic]` must return `()`");
555+
return false;
570556
}
557+
558+
if generics.params.iter().any(|param| !matches!(param.kind, GenericParamKind::Lifetime))
559+
{
560+
sd.span_err(
561+
i.span,
562+
"functions used as tests can not have any non-lifetime generic parameters",
563+
);
564+
return false;
565+
}
566+
567+
true
571568
}
572569
_ => {
573570
// should be unreachable because `is_test_fn_item` should catch all non-fn items

0 commit comments

Comments
 (0)