Skip to content

Commit 879254b

Browse files
Rollup merge of rust-lang#51298 - Dylan-DPC:stabilise/termination-test, r=nikomatsakis
Stabilize unit tests with non-`()` return type References rust-lang#48854
2 parents 0c6094f + 8ecbd35 commit 879254b

8 files changed

+13
-65
lines changed

src/libsyntax/feature_gate.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,6 @@ declare_features! (
398398
// `foo.rs` as an alternative to `foo/mod.rs`
399399
(active, non_modrs_mods, "1.24.0", Some(44660), Some(Edition::Edition2018)),
400400

401-
// Termination trait in tests (RFC 1937)
402-
(active, termination_trait_test, "1.24.0", Some(48854), Some(Edition::Edition2018)),
403-
404401
// `extern` in paths
405402
(active, extern_in_paths, "1.23.0", Some(44660), None),
406403

@@ -616,6 +613,8 @@ declare_features! (
616613
(accepted, fn_must_use, "1.27.0", Some(43302), None),
617614
// Allows use of the :lifetime macro fragment specifier
618615
(accepted, macro_lifetime_matcher, "1.27.0", Some(34303), None),
616+
// Termination trait in tests (RFC 1937)
617+
(accepted, termination_trait_test, "1.27.0", Some(48854), None),
619618
);
620619

621620
// If you change this, please modify src/doc/unstable-book as well. You must

src/libsyntax/test.rs

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ enum BadTestSignature {
335335
fn is_test_fn(cx: &TestCtxt, i: &ast::Item) -> bool {
336336
let has_test_attr = attr::contains_name(&i.attrs, "test");
337337

338-
fn has_test_signature(cx: &TestCtxt, i: &ast::Item) -> HasTestSignature {
338+
fn has_test_signature(_cx: &TestCtxt, i: &ast::Item) -> HasTestSignature {
339339
let has_should_panic_attr = attr::contains_name(&i.attrs, "should_panic");
340340
match i.node {
341341
ast::ItemKind::Fn(ref decl, _, _, _, ref generics, _) => {
@@ -351,15 +351,14 @@ fn is_test_fn(cx: &TestCtxt, i: &ast::Item) -> bool {
351351
return No(BadTestSignature::NoArgumentsAllowed);
352352
}
353353

354-
match (has_output, cx.features.termination_trait_test, has_should_panic_attr) {
355-
(true, true, true) => No(BadTestSignature::ShouldPanicOnlyWithNoArgs),
356-
(true, true, false) => if generics.is_parameterized() {
354+
match (has_output, has_should_panic_attr) {
355+
(true, true) => No(BadTestSignature::ShouldPanicOnlyWithNoArgs),
356+
(true, false) => if generics.is_parameterized() {
357357
No(BadTestSignature::WrongTypeSignature)
358358
} else {
359359
Yes
360360
},
361-
(true, false, _) => No(BadTestSignature::WrongTypeSignature),
362-
(false, _, _) => Yes
361+
(false, _) => Yes
363362
}
364363
}
365364
_ => No(BadTestSignature::NotEvenAFunction),
@@ -395,31 +394,12 @@ fn is_test_fn(cx: &TestCtxt, i: &ast::Item) -> bool {
395394
fn is_bench_fn(cx: &TestCtxt, i: &ast::Item) -> bool {
396395
let has_bench_attr = attr::contains_name(&i.attrs, "bench");
397396

398-
fn has_bench_signature(cx: &TestCtxt, i: &ast::Item) -> bool {
397+
fn has_bench_signature(_cx: &TestCtxt, i: &ast::Item) -> bool {
399398
match i.node {
400-
ast::ItemKind::Fn(ref decl, _, _, _, ref generics, _) => {
401-
let input_cnt = decl.inputs.len();
402-
403-
// If the termination trait is active, the compiler will check that the output
404-
// type implements the `Termination` trait as `libtest` enforces that.
405-
let output_matches = if cx.features.termination_trait_test {
406-
true
407-
} else {
408-
let no_output = match decl.output {
409-
ast::FunctionRetTy::Default(..) => true,
410-
ast::FunctionRetTy::Ty(ref t) if t.node == ast::TyKind::Tup(vec![]) => true,
411-
_ => false
412-
};
413-
let tparm_cnt = generics.params.iter()
414-
.filter(|param| param.is_type_param())
415-
.count();
416-
417-
no_output && tparm_cnt == 0
418-
};
419-
399+
ast::ItemKind::Fn(ref decl, _, _, _, _, _) => {
420400
// NB: inadequate check, but we're running
421401
// well before resolve, can't get too deep.
422-
input_cnt == 1 && output_matches
402+
decl.inputs.len() == 1
423403
}
424404
_ => false
425405
}
@@ -430,13 +410,8 @@ fn is_bench_fn(cx: &TestCtxt, i: &ast::Item) -> bool {
430410
if has_bench_attr && !has_bench_signature {
431411
let diag = cx.span_diagnostic;
432412

433-
if cx.features.termination_trait_test {
434-
diag.span_err(i.span, "functions used as benches must have signature \
413+
diag.span_err(i.span, "functions used as benches must have signature \
435414
`fn(&mut Bencher) -> impl Termination`");
436-
} else {
437-
diag.span_err(i.span, "functions used as benches must have signature \
438-
`fn(&mut Bencher) -> ()`");
439-
}
440415
}
441416

442417
has_bench_attr && has_bench_signature

src/test/compile-fail/feature-gate-termination_trait_test.rs

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/test/ui/rfc-1937-termination-trait/termination-trait-in-test-should-panic.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
// compile-flags: --test
1212

13-
#![feature(termination_trait_test)]
1413
#![feature(test)]
1514

1615
extern crate test;

src/test/ui/rfc-1937-termination-trait/termination-trait-in-test-should-panic.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: functions using `#[should_panic]` must return `()`
2-
--> $DIR/termination-trait-in-test-should-panic.rs:22:1
2+
--> $DIR/termination-trait-in-test-should-panic.rs:21:1
33
|
44
LL | / fn not_a_num() -> Result<(), ParseIntError> {
55
LL | | //~^ ERROR functions using `#[should_panic]` must return `()`

src/test/ui/rfc-1937-termination-trait/termination-trait-in-test.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
// compile-flags: --test
1212
// run-pass
1313

14-
#![feature(termination_trait_test)]
1514
#![feature(test)]
1615

1716
extern crate test;

src/test/ui/rfc-1937-termination-trait/termination-trait-test-wrong-type.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
// compile-flags: --test
1212

13-
#![feature(termination_trait_test)]
14-
1513
use std::num::ParseIntError;
1614

1715
#[test]

src/test/ui/rfc-1937-termination-trait/termination-trait-test-wrong-type.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0277]: `main` has invalid return type `std::result::Result<f32, std::num::ParseIntError>`
2-
--> $DIR/termination-trait-test-wrong-type.rs:18:1
2+
--> $DIR/termination-trait-test-wrong-type.rs:16:1
33
|
44
LL | / fn can_parse_zero_as_f32() -> Result<f32, ParseIntError> { //~ ERROR
55
LL | | "0".parse()

0 commit comments

Comments
 (0)