From b78457f0fbe15c21dc28efaef8146a0ff9d58059 Mon Sep 17 00:00:00 2001 From: dylan_DPC Date: Sat, 2 Jun 2018 17:27:37 +0530 Subject: [PATCH 1/6] Stabilize unit tests with non-`()` return type --- src/libsyntax/feature_gate.rs | 6 +-- src/libsyntax/test.rs | 38 ++++--------------- .../feature-gate-termination_trait_test.rs | 22 ----------- .../termination-trait-in-test-should-panic.rs | 1 - .../termination-trait-in-test.rs | 1 - .../termination-trait-test-wrong-type.rs | 2 - 6 files changed, 10 insertions(+), 60 deletions(-) delete mode 100644 src/test/compile-fail/feature-gate-termination_trait_test.rs diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 9b84713b0f90f..ecda2b077e1f9 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -398,9 +398,6 @@ declare_features! ( // `foo.rs` as an alternative to `foo/mod.rs` (active, non_modrs_mods, "1.24.0", Some(44660), Some(Edition::Edition2018)), - // Termination trait in tests (RFC 1937) - (active, termination_trait_test, "1.24.0", Some(48854), Some(Edition::Edition2018)), - // `extern` in paths (active, extern_in_paths, "1.23.0", Some(44660), None), @@ -475,6 +472,9 @@ declare_features! ( // 'a: { break 'a; } (active, label_break_value, "1.28.0", Some(48594), None), + + // Termination trait in tests (RFC 1937) + (accepted, termination_trait_test, "1.28.0", Some(48854), Some(Edition::Edition2018)), ); declare_features! ( diff --git a/src/libsyntax/test.rs b/src/libsyntax/test.rs index e63a3d47a828f..5506f408cd269 100644 --- a/src/libsyntax/test.rs +++ b/src/libsyntax/test.rs @@ -351,15 +351,15 @@ fn is_test_fn(cx: &TestCtxt, i: &ast::Item) -> bool { return No(BadTestSignature::NoArgumentsAllowed); } - match (has_output, cx.features.termination_trait_test, has_should_panic_attr) { - (true, true, true) => No(BadTestSignature::ShouldPanicOnlyWithNoArgs), - (true, true, false) => if generics.is_parameterized() { + match (has_output, has_should_panic_attr) { + (true, true) => No(BadTestSignature::ShouldPanicOnlyWithNoArgs), + (true, false) => if generics.is_parameterized() { No(BadTestSignature::WrongTypeSignature) } else { Yes }, - (true, false, _) => No(BadTestSignature::WrongTypeSignature), - (false, _, _) => Yes + (true, _) => No(BadTestSignature::WrongTypeSignature), + (false, _) => Yes } } _ => No(BadTestSignature::NotEvenAFunction), @@ -398,28 +398,9 @@ fn is_bench_fn(cx: &TestCtxt, i: &ast::Item) -> bool { fn has_bench_signature(cx: &TestCtxt, i: &ast::Item) -> bool { match i.node { ast::ItemKind::Fn(ref decl, _, _, _, ref generics, _) => { - let input_cnt = decl.inputs.len(); - - // If the termination trait is active, the compiler will check that the output - // type implements the `Termination` trait as `libtest` enforces that. - let output_matches = if cx.features.termination_trait_test { - true - } else { - let no_output = match decl.output { - ast::FunctionRetTy::Default(..) => true, - ast::FunctionRetTy::Ty(ref t) if t.node == ast::TyKind::Tup(vec![]) => true, - _ => false - }; - let tparm_cnt = generics.params.iter() - .filter(|param| param.is_type_param()) - .count(); - - no_output && tparm_cnt == 0 - }; - // NB: inadequate check, but we're running // well before resolve, can't get too deep. - input_cnt == 1 && output_matches + decl.inputs.len() == 1 } _ => false } @@ -430,13 +411,8 @@ fn is_bench_fn(cx: &TestCtxt, i: &ast::Item) -> bool { if has_bench_attr && !has_bench_signature { let diag = cx.span_diagnostic; - if cx.features.termination_trait_test { - diag.span_err(i.span, "functions used as benches must have signature \ + diag.span_err(i.span, "functions used as benches must have signature \ `fn(&mut Bencher) -> impl Termination`"); - } else { - diag.span_err(i.span, "functions used as benches must have signature \ - `fn(&mut Bencher) -> ()`"); - } } has_bench_attr && has_bench_signature diff --git a/src/test/compile-fail/feature-gate-termination_trait_test.rs b/src/test/compile-fail/feature-gate-termination_trait_test.rs deleted file mode 100644 index 4af7e94671627..0000000000000 --- a/src/test/compile-fail/feature-gate-termination_trait_test.rs +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2017 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -// compile-flags: --test - -fn main() {} - -#[cfg(test)] -mod tests { - #[test] - fn it_works() -> Result<(), ()> { - //~^ ERROR functions used as tests must have signature fn() -> () - Ok(()) - } -} diff --git a/src/test/ui/rfc-1937-termination-trait/termination-trait-in-test-should-panic.rs b/src/test/ui/rfc-1937-termination-trait/termination-trait-in-test-should-panic.rs index 73a0150c0bb3f..a0b2784214ae9 100644 --- a/src/test/ui/rfc-1937-termination-trait/termination-trait-in-test-should-panic.rs +++ b/src/test/ui/rfc-1937-termination-trait/termination-trait-in-test-should-panic.rs @@ -10,7 +10,6 @@ // compile-flags: --test -#![feature(termination_trait_test)] #![feature(test)] extern crate test; diff --git a/src/test/ui/rfc-1937-termination-trait/termination-trait-in-test.rs b/src/test/ui/rfc-1937-termination-trait/termination-trait-in-test.rs index 2cb4552a4b29e..0561b12221d1a 100644 --- a/src/test/ui/rfc-1937-termination-trait/termination-trait-in-test.rs +++ b/src/test/ui/rfc-1937-termination-trait/termination-trait-in-test.rs @@ -11,7 +11,6 @@ // compile-flags: --test // run-pass -#![feature(termination_trait_test)] #![feature(test)] extern crate test; diff --git a/src/test/ui/rfc-1937-termination-trait/termination-trait-test-wrong-type.rs b/src/test/ui/rfc-1937-termination-trait/termination-trait-test-wrong-type.rs index 1c00edee770f2..6153d840c8a7d 100644 --- a/src/test/ui/rfc-1937-termination-trait/termination-trait-test-wrong-type.rs +++ b/src/test/ui/rfc-1937-termination-trait/termination-trait-test-wrong-type.rs @@ -10,8 +10,6 @@ // compile-flags: --test -#![feature(termination_trait_test)] - use std::num::ParseIntError; #[test] From bc7416213c20e165ebe723c4328667e8008c0794 Mon Sep 17 00:00:00 2001 From: dylan_DPC Date: Sat, 2 Jun 2018 17:58:06 +0530 Subject: [PATCH 2/6] fixed feature gate to right place --- src/libsyntax/feature_gate.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index ecda2b077e1f9..9adfb61d92d32 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -473,8 +473,6 @@ declare_features! ( // 'a: { break 'a; } (active, label_break_value, "1.28.0", Some(48594), None), - // Termination trait in tests (RFC 1937) - (accepted, termination_trait_test, "1.28.0", Some(48854), Some(Edition::Edition2018)), ); declare_features! ( @@ -609,6 +607,8 @@ declare_features! ( (accepted, fn_must_use, "1.27.0", Some(43302), None), // Allows use of the :lifetime macro fragment specifier (accepted, macro_lifetime_matcher, "1.27.0", Some(34303), None), + // Termination trait in tests (RFC 1937) + (accepted, termination_trait_test, "1.27.0", Some(48854), Some(Edition::Edition2018)), ); // If you change this, please modify src/doc/unstable-book as well. You must From 4cbf400366bb7d635cf4be31568b41992115220a Mon Sep 17 00:00:00 2001 From: dylan_DPC Date: Tue, 5 Jun 2018 23:28:32 +0530 Subject: [PATCH 3/6] flag changed to none --- src/libsyntax/feature_gate.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 17118e4dff2ee..4873062d659d9 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -610,7 +610,7 @@ declare_features! ( // Allows use of the :lifetime macro fragment specifier (accepted, macro_lifetime_matcher, "1.27.0", Some(34303), None), // Termination trait in tests (RFC 1937) - (accepted, termination_trait_test, "1.27.0", Some(48854), Some(Edition::Edition2018)), + (accepted, termination_trait_test, "1.27.0", Some(48854), None), ); // If you change this, please modify src/doc/unstable-book as well. You must From e8fd74a11d66849c0b5b737d3bbe0cf121f8908e Mon Sep 17 00:00:00 2001 From: dylan_DPC Date: Tue, 5 Jun 2018 23:44:42 +0530 Subject: [PATCH 4/6] remove redundant match branch --- src/libsyntax/test.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libsyntax/test.rs b/src/libsyntax/test.rs index 5506f408cd269..2d5606b2edeb9 100644 --- a/src/libsyntax/test.rs +++ b/src/libsyntax/test.rs @@ -358,7 +358,6 @@ fn is_test_fn(cx: &TestCtxt, i: &ast::Item) -> bool { } else { Yes }, - (true, _) => No(BadTestSignature::WrongTypeSignature), (false, _) => Yes } } From 1048ae29a11f5e4ee188dde0d3ba39be2b05d8ed Mon Sep 17 00:00:00 2001 From: dylan_DPC Date: Wed, 6 Jun 2018 12:22:38 +0530 Subject: [PATCH 5/6] append unused variables with _ --- src/libsyntax/test.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libsyntax/test.rs b/src/libsyntax/test.rs index 2d5606b2edeb9..0c7f70a578a26 100644 --- a/src/libsyntax/test.rs +++ b/src/libsyntax/test.rs @@ -335,7 +335,7 @@ enum BadTestSignature { fn is_test_fn(cx: &TestCtxt, i: &ast::Item) -> bool { let has_test_attr = attr::contains_name(&i.attrs, "test"); - fn has_test_signature(cx: &TestCtxt, i: &ast::Item) -> HasTestSignature { + fn has_test_signature(_cx: &TestCtxt, i: &ast::Item) -> HasTestSignature { let has_should_panic_attr = attr::contains_name(&i.attrs, "should_panic"); match i.node { ast::ItemKind::Fn(ref decl, _, _, _, ref generics, _) => { @@ -394,9 +394,9 @@ fn is_test_fn(cx: &TestCtxt, i: &ast::Item) -> bool { fn is_bench_fn(cx: &TestCtxt, i: &ast::Item) -> bool { let has_bench_attr = attr::contains_name(&i.attrs, "bench"); - fn has_bench_signature(cx: &TestCtxt, i: &ast::Item) -> bool { + fn has_bench_signature(_cx: &TestCtxt, i: &ast::Item) -> bool { match i.node { - ast::ItemKind::Fn(ref decl, _, _, _, ref generics, _) => { + ast::ItemKind::Fn(ref decl, _, _, _, _, _) => { // NB: inadequate check, but we're running // well before resolve, can't get too deep. decl.inputs.len() == 1 From 8ecbd351bb80f03a76110ba9f7b9e6a68b3d2798 Mon Sep 17 00:00:00 2001 From: dylan_DPC Date: Wed, 6 Jun 2018 20:51:57 +0530 Subject: [PATCH 6/6] fix stderrs --- .../termination-trait-in-test-should-panic.stderr | 2 +- .../termination-trait-test-wrong-type.stderr | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/ui/rfc-1937-termination-trait/termination-trait-in-test-should-panic.stderr b/src/test/ui/rfc-1937-termination-trait/termination-trait-in-test-should-panic.stderr index e3dab82df41b9..bfdcf01c325f7 100644 --- a/src/test/ui/rfc-1937-termination-trait/termination-trait-in-test-should-panic.stderr +++ b/src/test/ui/rfc-1937-termination-trait/termination-trait-in-test-should-panic.stderr @@ -1,5 +1,5 @@ error: functions using `#[should_panic]` must return `()` - --> $DIR/termination-trait-in-test-should-panic.rs:22:1 + --> $DIR/termination-trait-in-test-should-panic.rs:21:1 | LL | / fn not_a_num() -> Result<(), ParseIntError> { LL | | //~^ ERROR functions using `#[should_panic]` must return `()` diff --git a/src/test/ui/rfc-1937-termination-trait/termination-trait-test-wrong-type.stderr b/src/test/ui/rfc-1937-termination-trait/termination-trait-test-wrong-type.stderr index 8efd8a216f10f..0972a0994fc0d 100644 --- a/src/test/ui/rfc-1937-termination-trait/termination-trait-test-wrong-type.stderr +++ b/src/test/ui/rfc-1937-termination-trait/termination-trait-test-wrong-type.stderr @@ -1,5 +1,5 @@ error[E0277]: `main` has invalid return type `std::result::Result` - --> $DIR/termination-trait-test-wrong-type.rs:18:1 + --> $DIR/termination-trait-test-wrong-type.rs:16:1 | LL | / fn can_parse_zero_as_f32() -> Result { //~ ERROR LL | | "0".parse()