about summary refs log tree commit diff
path: root/compiler/rustc_middle/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-06-15 22:54:43 +0000
committerbors <bors@rust-lang.org>2023-06-15 22:54:43 +0000
commit0252b4093ff19a8aaae2a1965b76b9ba4528e5c0 (patch)
treebbd28a869da0e03dd01081a0ea0b87e3e0af9ed7 /compiler/rustc_middle/src
parent114fb86ca08cfa6a99087e0f0bc264d03590dc37 (diff)
parent05d5449522a4318b740c8acae2f19cc064ba48a8 (diff)
downloadrust-0252b4093ff19a8aaae2a1965b76b9ba4528e5c0.tar.gz
rust-0252b4093ff19a8aaae2a1965b76b9ba4528e5c0.zip
Auto merge of #112681 - GuillaumeGomez:rollup-rwn4086, r=GuillaumeGomez
Rollup of 8 pull requests

Successful merges:

 - #112403 (Prevent `.eh_frame` from being emitted for `-C panic=abort`)
 - #112517 (`suspicious_double_ref_op`: don't lint on `.borrow()`)
 - #112529 (Extend `unused_must_use` to cover block exprs)
 - #112614 (tweak suggestion for argument-position `impl ?Sized`)
 - #112654 (normalize closure output in equate_inputs_and_outputs)
 - #112660 (Migrate GUI colors test to original CSS color format)
 - #112664 (Add support for test tmpdir to fuchsia test runner)
 - #112669 (Fix comment for ptr alignment checks in codegen)

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'compiler/rustc_middle/src')
-rw-r--r--compiler/rustc_middle/src/ty/diagnostics.rs48
1 files changed, 31 insertions, 17 deletions
diff --git a/compiler/rustc_middle/src/ty/diagnostics.rs b/compiler/rustc_middle/src/ty/diagnostics.rs
index 9c948dba1e4..d89baa9c88d 100644
--- a/compiler/rustc_middle/src/ty/diagnostics.rs
+++ b/compiler/rustc_middle/src/ty/diagnostics.rs
@@ -14,8 +14,8 @@ use rustc_errors::{Applicability, Diagnostic, DiagnosticArgValue, IntoDiagnostic
 use rustc_hir as hir;
 use rustc_hir::def::DefKind;
 use rustc_hir::def_id::DefId;
-use rustc_hir::WherePredicate;
-use rustc_span::Span;
+use rustc_hir::{PredicateOrigin, WherePredicate};
+use rustc_span::{BytePos, Span};
 use rustc_type_ir::sty::TyKind::*;
 
 impl<'tcx> IntoDiagnosticArg for Ty<'tcx> {
@@ -156,10 +156,11 @@ enum SuggestChangingConstraintsMessage<'a> {
     RestrictBoundFurther,
     RestrictType { ty: &'a str },
     RestrictTypeFurther { ty: &'a str },
-    RemovingQSized,
+    RemoveMaybeUnsized,
+    ReplaceMaybeUnsizedWithSized,
 }
 
-fn suggest_removing_unsized_bound(
+fn suggest_changing_unsized_bound(
     generics: &hir::Generics<'_>,
     suggestions: &mut Vec<(Span, String, SuggestChangingConstraintsMessage<'_>)>,
     param: &hir::GenericParam<'_>,
@@ -183,12 +184,25 @@ fn suggest_removing_unsized_bound(
             if poly.trait_ref.trait_def_id() != def_id {
                 continue;
             }
-            let sp = generics.span_for_bound_removal(where_pos, pos);
-            suggestions.push((
-                sp,
-                String::new(),
-                SuggestChangingConstraintsMessage::RemovingQSized,
-            ));
+            if predicate.origin == PredicateOrigin::ImplTrait && predicate.bounds.len() == 1 {
+                // For `impl ?Sized` with no other bounds, suggest `impl Sized` instead.
+                let bound_span = bound.span();
+                if bound_span.can_be_used_for_suggestions() {
+                    let question_span = bound_span.with_hi(bound_span.lo() + BytePos(1));
+                    suggestions.push((
+                        question_span,
+                        String::new(),
+                        SuggestChangingConstraintsMessage::ReplaceMaybeUnsizedWithSized,
+                    ));
+                }
+            } else {
+                let sp = generics.span_for_bound_removal(where_pos, pos);
+                suggestions.push((
+                    sp,
+                    String::new(),
+                    SuggestChangingConstraintsMessage::RemoveMaybeUnsized,
+                ));
+            }
         }
     }
 }
@@ -238,14 +252,11 @@ pub fn suggest_constraining_type_params<'a>(
         {
             let mut sized_constraints =
                 constraints.extract_if(|(_, def_id)| *def_id == tcx.lang_items().sized_trait());
-            if let Some((constraint, def_id)) = sized_constraints.next() {
+            if let Some((_, def_id)) = sized_constraints.next() {
                 applicability = Applicability::MaybeIncorrect;
 
-                err.span_label(
-                    param.span,
-                    format!("this type parameter needs to be `{}`", constraint),
-                );
-                suggest_removing_unsized_bound(generics, &mut suggestions, param, def_id);
+                err.span_label(param.span, "this type parameter needs to be `Sized`");
+                suggest_changing_unsized_bound(generics, &mut suggestions, param, def_id);
             }
         }
 
@@ -395,9 +406,12 @@ pub fn suggest_constraining_type_params<'a>(
             SuggestChangingConstraintsMessage::RestrictTypeFurther { ty } => {
                 Cow::from(format!("consider further restricting type parameter `{}`", ty))
             }
-            SuggestChangingConstraintsMessage::RemovingQSized => {
+            SuggestChangingConstraintsMessage::RemoveMaybeUnsized => {
                 Cow::from("consider removing the `?Sized` bound to make the type parameter `Sized`")
             }
+            SuggestChangingConstraintsMessage::ReplaceMaybeUnsizedWithSized => {
+                Cow::from("consider replacing `?Sized` with `Sized`")
+            }
         };
 
         err.span_suggestion_verbose(span, msg, suggestion, applicability);