@@ -2617,40 +2617,53 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
26172617 if let hir::TyKind::TraitObject([poly_trait_ref, ..], _, TraitObjectSyntax::None) =
26182618 self_ty.kind
26192619 {
2620- let (mut sugg, app) = match tcx.sess.source_map().span_to_snippet(self_ty.span) {
2621- Ok(s) if poly_trait_ref.trait_ref.path.is_global() => {
2622- (format!("dyn ({})", s), Applicability::MachineApplicable)
2623- }
2624- Ok(s) => (format!("dyn {}", s), Applicability::MachineApplicable),
2625- Err(_) => ("dyn <type>".to_string(), Applicability::HasPlaceholders),
2626- };
2627- if in_path {
2628- let has_bracket = tcx
2620+ let needs_bracket = in_path
2621+ && !tcx
26292622 .sess
26302623 .source_map()
26312624 .span_to_prev_source(self_ty.span)
26322625 .ok()
26332626 .map_or(false, |s| s.trim_end().ends_with('<'));
2634- if !has_bracket {
2635- sugg = format!("<{}>", sugg);
2636- }
2637- }
2627+
2628+ let is_global = poly_trait_ref.trait_ref.path.is_global();
2629+ let sugg = Vec::from_iter([
2630+ (
2631+ self_ty.span.shrink_to_lo(),
2632+ format!(
2633+ "{}dyn {}",
2634+ if needs_bracket { "<" } else { "" },
2635+ if is_global { "(" } else { "" },
2636+ ),
2637+ ),
2638+ (
2639+ self_ty.span.shrink_to_hi(),
2640+ format!(
2641+ "{}{}",
2642+ if is_global { ")" } else { "" },
2643+ if needs_bracket { ">" } else { "" },
2644+ ),
2645+ ),
2646+ ]);
26382647 if self_ty.span.edition() >= Edition::Edition2021 {
26392648 let msg = "trait objects must include the `dyn` keyword";
26402649 let label = "add `dyn` keyword before this trait";
2641- let mut err =
2642- rustc_errors::struct_span_err!(tcx.sess, self_ty.span, E0782, "{}", msg);
2643- err.span_suggestion_verbose(self_ty.span, label, sugg, app) .emit();
2650+ rustc_errors::struct_span_err!(tcx.sess, self_ty.span, E0782, "{}", msg)
2651+ .multipart_suggestion_verbose(label, sugg, Applicability::MachineApplicable)
2652+ .emit();
26442653 } else {
26452654 let msg = "trait objects without an explicit `dyn` are deprecated";
26462655 tcx.struct_span_lint_hir(
26472656 BARE_TRAIT_OBJECTS,
26482657 self_ty.hir_id,
26492658 self_ty.span,
26502659 |lint| {
2651- let mut db = lint.build(msg);
2652- db.span_suggestion(self_ty.span, "use `dyn`", sugg, app);
2653- db.emit()
2660+ lint.build(msg)
2661+ .multipart_suggestion_verbose(
2662+ "use `dyn`",
2663+ sugg,
2664+ Applicability::MachineApplicable,
2665+ )
2666+ .emit()
26542667 },
26552668 );
26562669 }
0 commit comments