|
1 | 1 | //! Diagnostics related methods for `Ty`.
|
2 | 2 |
|
3 |
| -use std::borrow::Cow; |
4 | 3 | use std::fmt::Write;
|
5 | 4 | use std::ops::ControlFlow;
|
6 | 5 |
|
@@ -278,8 +277,21 @@ pub fn suggest_constraining_type_params<'a>(
|
278 | 277 | span_to_replace: Option<Span>,
|
279 | 278 | ) -> bool {
|
280 | 279 | let mut grouped = FxHashMap::default();
|
| 280 | + let mut unstable_suggestion = false; |
281 | 281 | param_names_and_constraints.for_each(|(param_name, constraint, def_id)| {
|
282 |
| - grouped.entry(param_name).or_insert(Vec::new()).push((constraint, def_id)) |
| 282 | + let stable = match def_id { |
| 283 | + Some(def_id) => match tcx.lookup_stability(def_id) { |
| 284 | + Some(s) => s.level.is_stable(), |
| 285 | + None => true, |
| 286 | + }, |
| 287 | + None => true, |
| 288 | + }; |
| 289 | + if stable || tcx.sess.is_nightly_build() { |
| 290 | + grouped.entry(param_name).or_insert(Vec::new()).push((constraint, def_id)); |
| 291 | + if !stable { |
| 292 | + unstable_suggestion = true; |
| 293 | + } |
| 294 | + } |
283 | 295 | });
|
284 | 296 |
|
285 | 297 | let mut applicability = Applicability::MachineApplicable;
|
@@ -464,28 +476,32 @@ pub fn suggest_constraining_type_params<'a>(
|
464 | 476 |
|
465 | 477 | if suggestions.len() == 1 {
|
466 | 478 | let (span, suggestion, msg) = suggestions.pop().unwrap();
|
| 479 | + let post = if unstable_suggestion { " but it is an `unstable` trait" } else { "" }; |
467 | 480 | let msg = match msg {
|
468 | 481 | SuggestChangingConstraintsMessage::RestrictBoundFurther => {
|
469 |
| - Cow::from("consider further restricting this bound") |
| 482 | + format!("consider further restricting this bound{post}") |
470 | 483 | }
|
471 | 484 | SuggestChangingConstraintsMessage::RestrictType { ty } => {
|
472 |
| - Cow::from(format!("consider restricting type parameter `{ty}`")) |
| 485 | + format!("consider restricting type parameter `{ty}`{post}") |
473 | 486 | }
|
474 | 487 | SuggestChangingConstraintsMessage::RestrictTypeFurther { ty } => {
|
475 |
| - Cow::from(format!("consider further restricting type parameter `{ty}`")) |
| 488 | + format!("consider further restricting type parameter `{ty}`{post}") |
476 | 489 | }
|
477 | 490 | SuggestChangingConstraintsMessage::RemoveMaybeUnsized => {
|
478 |
| - Cow::from("consider removing the `?Sized` bound to make the type parameter `Sized`") |
| 491 | + format!( |
| 492 | + "consider removing the `?Sized` bound to make the type parameter `Sized`{post}" |
| 493 | + ) |
479 | 494 | }
|
480 | 495 | SuggestChangingConstraintsMessage::ReplaceMaybeUnsizedWithSized => {
|
481 |
| - Cow::from("consider replacing `?Sized` with `Sized`") |
| 496 | + format!("consider replacing `?Sized` with `Sized`{post}") |
482 | 497 | }
|
483 | 498 | };
|
484 | 499 |
|
485 | 500 | err.span_suggestion_verbose(span, msg, suggestion, applicability);
|
486 | 501 | } else if suggestions.len() > 1 {
|
| 502 | + let post = if unstable_suggestion { " but some of them are `unstable` traits" } else { "" }; |
487 | 503 | err.multipart_suggestion_verbose(
|
488 |
| - "consider restricting type parameters", |
| 504 | + format!("consider restricting type parameters{post}"), |
489 | 505 | suggestions.into_iter().map(|(span, suggestion, _)| (span, suggestion)).collect(),
|
490 | 506 | applicability,
|
491 | 507 | );
|
|
0 commit comments