Skip to content

Commit

Permalink
Add test for lack of suggestion in stable
Browse files Browse the repository at this point in the history
This test will break when `Step` gets stabilized, but punt until then.
  • Loading branch information
estebank committed Nov 28, 2024
1 parent e3dfae8 commit 7e38a45
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions tests/run-make/missing-unstable-trait-bound/missing-bound.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub fn baz<T>(t: std::ops::Range<T>) {
for _ in t {}
}
fn main() {}
22 changes: 22 additions & 0 deletions tests/run-make/missing-unstable-trait-bound/rmake.rs
Original file line number Diff line number Diff line change
@@ -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`"#,
);
}
6 changes: 5 additions & 1 deletion tests/ui/trait-bounds/unstable-trait-suggestion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
19 changes: 16 additions & 3 deletions tests/ui/trait-bounds/unstable-trait-suggestion.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

0 comments on commit 7e38a45

Please sign in to comment.