Add test for lack of suggestion in stable

This test will break when `Step` gets stabilized, but punt until then.
This commit is contained in:
Esteban Küber 2024-11-28 19:31:37 +00:00
parent d13c34828e
commit 568b0ac624
5 changed files with 49 additions and 6 deletions

View File

@ -488,7 +488,7 @@ pub fn suggest_constraining_type_params<'a>(
.into_iter()
.filter(|(span, _, _, _)| !span.in_derive_expansion())
.collect::<Vec<_>>();
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.

View File

@ -0,0 +1,4 @@
pub fn baz<T>(t: std::ops::Range<T>) {
for _ in t {}
}
fn main() {}

View File

@ -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<T>` to implement `Iterator`
= note: required for `std::ops::Range<T>` to implement `IntoIterator`"#,
);
}

View File

@ -9,7 +9,11 @@ pub trait Unstable {}
fn foo<T: Unstable>(_: T) {}
#[stable(feature = "unit_test", since = "1.0.0")]
pub fn demo<T>(t: T) { //~ HELP consider restricting type parameter `T` with unstable trait `Unstable`
pub fn bar<T>(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>(t: std::ops::Range<T>) { //~ HELP consider restricting type parameter `T` with unstable trait
for _ in t {} //~ ERROR E0277
}
fn main() {}

View File

@ -13,9 +13,22 @@ LL | fn foo<T: Unstable>(_: T) {}
| ^^^^^^^^ required by this bound in `foo`
help: consider restricting type parameter `T` with unstable trait `Unstable`
|
LL | pub fn demo<T: Unstable>(t: T) {
| ++++++++++
LL | pub fn bar<T: Unstable>(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<T>` to implement `Iterator`
= note: required for `std::ops::Range<T>` to implement `IntoIterator`
help: consider restricting type parameter `T` with unstable trait `std::iter::Step`
|
LL | pub fn baz<T: std::iter::Step>(t: std::ops::Range<T>) {
| +++++++++++++++++
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0277`.