about summary refs log tree commit diff
path: root/compiler/rustc_lint/src
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2022-10-05 19:46:40 +0000
committerMichael Goulet <michael@errs.io>2022-10-05 19:47:19 +0000
commit8e7783bd131e7e5a326985765b7f4399c497f7bb (patch)
treeea71e9d92f50561c94fc9afad4325b73d74f9d48 /compiler/rustc_lint/src
parent24ac6a26bcf5be1ac841e7fe969bd992b3461f9d (diff)
downloadrust-8e7783bd131e7e5a326985765b7f4399c497f7bb.tar.gz
rust-8e7783bd131e7e5a326985765b7f4399c497f7bb.zip
Fix opaque_hidden_inferred_bound lint ICE
Diffstat (limited to 'compiler/rustc_lint/src')
-rw-r--r--compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs55
1 files changed, 37 insertions, 18 deletions
diff --git a/compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs b/compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs
index d8ce20db37c..4ff3b2e2c4a 100644
--- a/compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs
+++ b/compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs
@@ -1,7 +1,10 @@
+use rustc_errors::DecorateLint;
 use rustc_hir as hir;
 use rustc_infer::infer::TyCtxtInferExt;
-use rustc_macros::LintDiagnostic;
-use rustc_middle::ty::{self, fold::BottomUpFolder, Ty, TypeFoldable};
+use rustc_macros::{LintDiagnostic, Subdiagnostic};
+use rustc_middle::ty::{
+    self, fold::BottomUpFolder, print::TraitPredPrintModifiersAndPath, Ty, TypeFoldable,
+};
 use rustc_span::Span;
 use rustc_trait_selection::traits;
 use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
@@ -117,23 +120,29 @@ impl<'tcx> LateLintPass<'tcx> for OpaqueHiddenInferredBound {
                     )) {
                         // If it's a trait bound and an opaque that doesn't satisfy it,
                         // then we can emit a suggestion to add the bound.
-                        let (suggestion, suggest_span) =
+                        let sugg =
                             match (proj_term.kind(), assoc_pred.kind().skip_binder()) {
-                                (ty::Opaque(def_id, _), ty::PredicateKind::Trait(trait_pred)) => (
-                                    format!(" + {}", trait_pred.print_modifiers_and_trait_path()),
-                                    Some(cx.tcx.def_span(def_id).shrink_to_hi()),
-                                ),
-                                _ => (String::new(), None),
+                                (ty::Opaque(def_id, _), ty::PredicateKind::Trait(trait_pred)) => Some(AddBound {
+                                    suggest_span: cx.tcx.def_span(*def_id).shrink_to_hi(),
+                                    trait_ref: trait_pred.print_modifiers_and_trait_path(),
+                                }),
+                                _ => None,
                             };
-                        cx.emit_spanned_lint(
+                        let lint = OpaqueHiddenInferredBoundLint {
+                            ty: cx.tcx.mk_opaque(def_id, ty::InternalSubsts::identity_for_item(cx.tcx, def_id)),
+                            proj_ty: proj_term,
+                            assoc_pred_span,
+                        };
+                        cx.struct_span_lint(
                             OPAQUE_HIDDEN_INFERRED_BOUND,
                             pred_span,
-                            OpaqueHiddenInferredBoundLint {
-                                ty: cx.tcx.mk_opaque(def_id, ty::InternalSubsts::identity_for_item(cx.tcx, def_id)),
-                                proj_ty: proj_term,
-                                assoc_pred_span,
-                                suggestion,
-                                suggest_span,
+                            lint.msg(),
+                            |diag| {
+                                lint.decorate_lint(diag);
+                                if let Some(sugg) = sugg {
+                                    diag.subdiagnostic(sugg);
+                                }
+                                diag
                             },
                         );
                     }
@@ -150,7 +159,17 @@ struct OpaqueHiddenInferredBoundLint<'tcx> {
     proj_ty: Ty<'tcx>,
     #[label(lint::specifically)]
     assoc_pred_span: Span,
-    #[suggestion_verbose(applicability = "machine-applicable", code = "{suggestion}")]
-    suggest_span: Option<Span>,
-    suggestion: String,
+}
+
+#[derive(Subdiagnostic)]
+#[suggestion_verbose(
+    lint::opaque_hidden_inferred_bound_sugg,
+    applicability = "machine-applicable",
+    code = " + {trait_ref}"
+)]
+struct AddBound<'tcx> {
+    #[primary_span]
+    suggest_span: Span,
+    #[skip_arg]
+    trait_ref: TraitPredPrintModifiersAndPath<'tcx>,
 }