From 568b0ac624553150330f1cc3bbff9b99e5d358a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 28 Nov 2024 19:31:37 +0000 Subject: [PATCH] Add test for lack of suggestion in stable This test will break when `Step` gets stabilized, but punt until then. --- compiler/rustc_middle/src/ty/diagnostics.rs | 4 ++-- .../missing-bound.rs | 4 ++++ .../missing-unstable-trait-bound/rmake.rs | 22 +++++++++++++++++++ .../trait-bounds/unstable-trait-suggestion.rs | 6 ++++- .../unstable-trait-suggestion.stderr | 19 +++++++++++++--- 5 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 tests/run-make/missing-unstable-trait-bound/missing-bound.rs create mode 100644 tests/run-make/missing-unstable-trait-bound/rmake.rs diff --git a/compiler/rustc_middle/src/ty/diagnostics.rs b/compiler/rustc_middle/src/ty/diagnostics.rs index a8500398082..d34fb0945bb 100644 --- a/compiler/rustc_middle/src/ty/diagnostics.rs +++ b/compiler/rustc_middle/src/ty/diagnostics.rs @@ -488,7 +488,7 @@ pub fn suggest_constraining_type_params<'a>( .into_iter() .filter(|(span, _, _, _)| !span.in_derive_expansion()) .collect::>(); - + let suggested = !suggestions.is_empty(); if suggestions.len() == 1 { let (span, constraint, suggestion, msg) = suggestions.pop().unwrap(); let post = format!( @@ -524,7 +524,7 @@ pub fn suggest_constraining_type_params<'a>( ); } - true + suggested } /// Collect al types that have an implicit `'static` obligation that we could suggest `'_` for. diff --git a/tests/run-make/missing-unstable-trait-bound/missing-bound.rs b/tests/run-make/missing-unstable-trait-bound/missing-bound.rs new file mode 100644 index 00000000000..65d0745f494 --- /dev/null +++ b/tests/run-make/missing-unstable-trait-bound/missing-bound.rs @@ -0,0 +1,4 @@ +pub fn baz(t: std::ops::Range) { + for _ in t {} +} +fn main() {} diff --git a/tests/run-make/missing-unstable-trait-bound/rmake.rs b/tests/run-make/missing-unstable-trait-bound/rmake.rs new file mode 100644 index 00000000000..1e0cb1336a4 --- /dev/null +++ b/tests/run-make/missing-unstable-trait-bound/rmake.rs @@ -0,0 +1,22 @@ +//@ only-linux +//@ ignore-wasm32 +//@ ignore-wasm64 +// ignore-tidy-linelength + +// Ensure that on stable we don't suggest restricting with an unsafe trait and we continue +// mentioning the rest of the obligation chain. + +use run_make_support::{rust_lib_name, rustc}; + +fn main() { + rustc() + .env("RUSTC_BOOTSTRAP", "-1") + .input("missing-bound.rs") + .run_fail() + .assert_stderr_not_contains("help: consider restricting type parameter `T`") + .assert_stderr_contains( + r#" + = note: required for `std::ops::Range` to implement `Iterator` + = note: required for `std::ops::Range` to implement `IntoIterator`"#, + ); +} diff --git a/tests/ui/trait-bounds/unstable-trait-suggestion.rs b/tests/ui/trait-bounds/unstable-trait-suggestion.rs index c49e2a34033..ba96b4f3f97 100644 --- a/tests/ui/trait-bounds/unstable-trait-suggestion.rs +++ b/tests/ui/trait-bounds/unstable-trait-suggestion.rs @@ -9,7 +9,11 @@ pub trait Unstable {} fn foo(_: T) {} #[stable(feature = "unit_test", since = "1.0.0")] -pub fn demo(t: T) { //~ HELP consider restricting type parameter `T` with unstable trait `Unstable` +pub fn bar(t: T) { //~ HELP consider restricting type parameter `T` with unstable trait `Unstable` foo(t) //~ ERROR E0277 } +#[stable(feature = "unit_test", since = "1.0.0")] +pub fn baz(t: std::ops::Range) { //~ HELP consider restricting type parameter `T` with unstable trait + for _ in t {} //~ ERROR E0277 +} fn main() {} diff --git a/tests/ui/trait-bounds/unstable-trait-suggestion.stderr b/tests/ui/trait-bounds/unstable-trait-suggestion.stderr index a326965b683..fa8e428aa1f 100644 --- a/tests/ui/trait-bounds/unstable-trait-suggestion.stderr +++ b/tests/ui/trait-bounds/unstable-trait-suggestion.stderr @@ -13,9 +13,22 @@ LL | fn foo(_: T) {} | ^^^^^^^^ required by this bound in `foo` help: consider restricting type parameter `T` with unstable trait `Unstable` | -LL | pub fn demo(t: T) { - | ++++++++++ +LL | pub fn bar(t: T) { + | ++++++++++ -error: aborting due to 1 previous error +error[E0277]: the trait bound `T: Step` is not satisfied + --> $DIR/unstable-trait-suggestion.rs:17:14 + | +LL | for _ in t {} + | ^ the trait `Step` is not implemented for `T` + | + = note: required for `std::ops::Range` to implement `Iterator` + = note: required for `std::ops::Range` to implement `IntoIterator` +help: consider restricting type parameter `T` with unstable trait `std::iter::Step` + | +LL | pub fn baz(t: std::ops::Range) { + | +++++++++++++++++ + +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0277`.