Skip to content

Commit 568b0ac

Browse files
committed
Add test for lack of suggestion in stable
This test will break when `Step` gets stabilized, but punt until then.
1 parent d13c348 commit 568b0ac

File tree

5 files changed

+49
-6
lines changed

5 files changed

+49
-6
lines changed

compiler/rustc_middle/src/ty/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ pub fn suggest_constraining_type_params<'a>(
488488
.into_iter()
489489
.filter(|(span, _, _, _)| !span.in_derive_expansion())
490490
.collect::<Vec<_>>();
491-
491+
let suggested = !suggestions.is_empty();
492492
if suggestions.len() == 1 {
493493
let (span, constraint, suggestion, msg) = suggestions.pop().unwrap();
494494
let post = format!(
@@ -524,7 +524,7 @@ pub fn suggest_constraining_type_params<'a>(
524524
);
525525
}
526526

527-
true
527+
suggested
528528
}
529529

530530
/// Collect al types that have an implicit `'static` obligation that we could suggest `'_` for.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub fn baz<T>(t: std::ops::Range<T>) {
2+
for _ in t {}
3+
}
4+
fn main() {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//@ only-linux
2+
//@ ignore-wasm32
3+
//@ ignore-wasm64
4+
// ignore-tidy-linelength
5+
6+
// Ensure that on stable we don't suggest restricting with an unsafe trait and we continue
7+
// mentioning the rest of the obligation chain.
8+
9+
use run_make_support::{rust_lib_name, rustc};
10+
11+
fn main() {
12+
rustc()
13+
.env("RUSTC_BOOTSTRAP", "-1")
14+
.input("missing-bound.rs")
15+
.run_fail()
16+
.assert_stderr_not_contains("help: consider restricting type parameter `T`")
17+
.assert_stderr_contains(
18+
r#"
19+
= note: required for `std::ops::Range<T>` to implement `Iterator`
20+
= note: required for `std::ops::Range<T>` to implement `IntoIterator`"#,
21+
);
22+
}

tests/ui/trait-bounds/unstable-trait-suggestion.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ pub trait Unstable {}
99
fn foo<T: Unstable>(_: T) {}
1010

1111
#[stable(feature = "unit_test", since = "1.0.0")]
12-
pub fn demo<T>(t: T) { //~ HELP consider restricting type parameter `T` with unstable trait `Unstable`
12+
pub fn bar<T>(t: T) { //~ HELP consider restricting type parameter `T` with unstable trait `Unstable`
1313
foo(t) //~ ERROR E0277
1414
}
15+
#[stable(feature = "unit_test", since = "1.0.0")]
16+
pub fn baz<T>(t: std::ops::Range<T>) { //~ HELP consider restricting type parameter `T` with unstable trait
17+
for _ in t {} //~ ERROR E0277
18+
}
1519
fn main() {}

tests/ui/trait-bounds/unstable-trait-suggestion.stderr

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,22 @@ LL | fn foo<T: Unstable>(_: T) {}
1313
| ^^^^^^^^ required by this bound in `foo`
1414
help: consider restricting type parameter `T` with unstable trait `Unstable`
1515
|
16-
LL | pub fn demo<T: Unstable>(t: T) {
17-
| ++++++++++
16+
LL | pub fn bar<T: Unstable>(t: T) {
17+
| ++++++++++
1818

19-
error: aborting due to 1 previous error
19+
error[E0277]: the trait bound `T: Step` is not satisfied
20+
--> $DIR/unstable-trait-suggestion.rs:17:14
21+
|
22+
LL | for _ in t {}
23+
| ^ the trait `Step` is not implemented for `T`
24+
|
25+
= note: required for `std::ops::Range<T>` to implement `Iterator`
26+
= note: required for `std::ops::Range<T>` to implement `IntoIterator`
27+
help: consider restricting type parameter `T` with unstable trait `std::iter::Step`
28+
|
29+
LL | pub fn baz<T: std::iter::Step>(t: std::ops::Range<T>) {
30+
| +++++++++++++++++
31+
32+
error: aborting due to 2 previous errors
2033

2134
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)