diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index e772aacfbf9..fe53ccdbad5 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -70,7 +70,7 @@ use rustc_middle::ty::{ subst::{Subst, SubstsRef}, Region, Ty, TyCtxt, TypeFoldable, }; -use rustc_span::{DesugaringKind, Pos, Span}; +use rustc_span::{BytePos, DesugaringKind, Pos, Span}; use rustc_target::spec::abi; use std::{cmp, fmt}; @@ -731,16 +731,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { return_sp: Span, arm_spans: impl Iterator, ) { - let snippet = self - .tcx - .sess - .source_map() - .span_to_snippet(return_sp) - .unwrap_or_else(|_| "dyn Trait".to_string()); - err.span_suggestion_verbose( - return_sp, + err.multipart_suggestion( "you could change the return type to be a boxed trait object", - format!("Box", &snippet[5..]), + vec![ + (return_sp.with_hi(return_sp.lo() + BytePos(4)), "Box".to_string()), + ], Applicability::MaybeIncorrect, ); let sugg = arm_spans diff --git a/compiler/rustc_typeck/src/check/coercion.rs b/compiler/rustc_typeck/src/check/coercion.rs index c19bc767563..b669476483b 100644 --- a/compiler/rustc_typeck/src/check/coercion.rs +++ b/compiler/rustc_typeck/src/check/coercion.rs @@ -51,7 +51,7 @@ use rustc_middle::ty::subst::SubstsRef; use rustc_middle::ty::{self, Ty, TypeAndMut}; use rustc_session::parse::feature_err; use rustc_span::symbol::sym; -use rustc_span::{self, Span}; +use rustc_span::{self, BytePos, Span}; use rustc_target::spec::abi::Abi; use rustc_trait_selection::traits::error_reporting::InferCtxtExt; use rustc_trait_selection::traits::{self, ObligationCause, ObligationCauseCode}; @@ -1523,10 +1523,12 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { }; if has_impl { if is_object_safe { - err.span_suggestion_verbose( - return_sp, + err.multipart_suggestion( "you could change the return type to be a boxed trait object", - format!("Box", &snippet[5..]), + vec![ + (return_sp.with_hi(return_sp.lo() + BytePos(4)), "Box".to_string()), + ], Applicability::MachineApplicable, ); } else { diff --git a/src/test/ui/impl-trait/object-unsafe-trait-in-return-position-impl-trait.stderr b/src/test/ui/impl-trait/object-unsafe-trait-in-return-position-impl-trait.stderr index a3bf2183255..9abebeff95a 100644 --- a/src/test/ui/impl-trait/object-unsafe-trait-in-return-position-impl-trait.stderr +++ b/src/test/ui/impl-trait/object-unsafe-trait-in-return-position-impl-trait.stderr @@ -35,7 +35,7 @@ LL | B help: you could change the return type to be a boxed trait object | LL | fn cat() -> Box { - | ^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^ ^ error: aborting due to 2 previous errors diff --git a/src/test/ui/point-to-type-err-cause-on-impl-trait-return.stderr b/src/test/ui/point-to-type-err-cause-on-impl-trait-return.stderr index a198203e245..66d9ff307d9 100644 --- a/src/test/ui/point-to-type-err-cause-on-impl-trait-return.stderr +++ b/src/test/ui/point-to-type-err-cause-on-impl-trait-return.stderr @@ -17,7 +17,7 @@ LL | 1u32 help: you could change the return type to be a boxed trait object | LL | fn foo() -> Box { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^ ^ error[E0308]: mismatched types --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:12:16 @@ -38,7 +38,7 @@ LL | return 1u32; help: you could change the return type to be a boxed trait object | LL | fn bar() -> Box { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^ ^ error[E0308]: mismatched types --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:20:9 @@ -59,7 +59,7 @@ LL | 1u32 help: you could change the return type to be a boxed trait object | LL | fn baz() -> Box { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^ ^ error[E0308]: `if` and `else` have incompatible types --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:28:9 @@ -76,7 +76,7 @@ LL | | } help: you could change the return type to be a boxed trait object | LL | fn qux() -> Box { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^ ^ help: if you change the return type to expect trait objects box the returned expressions | LL | Box::new(0i32) @@ -102,7 +102,7 @@ LL | _ => 1u32, help: you could change the return type to be a boxed trait object | LL | fn bat() -> Box { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^ ^ error[E0308]: mismatched types --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:40:5 @@ -124,7 +124,7 @@ LL | | } help: you could change the return type to be a boxed trait object | LL | fn can() -> Box { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^ ^ error[E0308]: mismatched types --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:53:13 @@ -145,7 +145,7 @@ LL | 1u32 help: you could change the return type to be a boxed trait object | LL | fn cat() -> Box { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^ ^ error[E0308]: `match` arms have incompatible types --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:61:14 @@ -162,7 +162,7 @@ LL | | } help: you could change the return type to be a boxed trait object | LL | fn dog() -> Box { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^ ^ help: if you change the return type to expect trait objects box the returned expressions | LL | 0 => Box::new(0i32),