Skip to content

Commit ff297fa

Browse files
committed
Make suggestion have a more targetted underline
1 parent fd9133b commit ff297fa

File tree

4 files changed

+21
-23
lines changed

4 files changed

+21
-23
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ use rustc_middle::ty::{
7070
subst::{Subst, SubstsRef},
7171
Region, Ty, TyCtxt, TypeFoldable,
7272
};
73-
use rustc_span::{DesugaringKind, Pos, Span};
73+
use rustc_span::{BytePos, DesugaringKind, Pos, Span};
7474
use rustc_target::spec::abi;
7575
use std::{cmp, fmt};
7676

@@ -731,16 +731,12 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
731731
return_sp: Span,
732732
arm_spans: impl Iterator<Item = Span>,
733733
) {
734-
let snippet = self
735-
.tcx
736-
.sess
737-
.source_map()
738-
.span_to_snippet(return_sp)
739-
.unwrap_or_else(|_| "dyn Trait".to_string());
740-
err.span_suggestion_verbose(
741-
return_sp,
734+
err.multipart_suggestion(
742735
"you could change the return type to be a boxed trait object",
743-
format!("Box<dyn {}>", &snippet[5..]),
736+
vec![
737+
(return_sp.with_hi(return_sp.lo() + BytePos(4)), "Box<dyn".to_string()),
738+
(return_sp.shrink_to_hi(), ">".to_string()),
739+
],
744740
Applicability::MaybeIncorrect,
745741
);
746742
let sugg = arm_spans

compiler/rustc_typeck/src/check/coercion.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use rustc_middle::ty::subst::SubstsRef;
5151
use rustc_middle::ty::{self, Ty, TypeAndMut};
5252
use rustc_session::parse::feature_err;
5353
use rustc_span::symbol::sym;
54-
use rustc_span::{self, Span};
54+
use rustc_span::{self, BytePos, Span};
5555
use rustc_target::spec::abi::Abi;
5656
use rustc_trait_selection::traits::error_reporting::InferCtxtExt;
5757
use rustc_trait_selection::traits::{self, ObligationCause, ObligationCauseCode};
@@ -1523,10 +1523,12 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
15231523
};
15241524
if has_impl {
15251525
if is_object_safe {
1526-
err.span_suggestion_verbose(
1527-
return_sp,
1526+
err.multipart_suggestion(
15281527
"you could change the return type to be a boxed trait object",
1529-
format!("Box<dyn {}>", &snippet[5..]),
1528+
vec![
1529+
(return_sp.with_hi(return_sp.lo() + BytePos(4)), "Box<dyn".to_string()),
1530+
(return_sp.shrink_to_hi(), ">".to_string()),
1531+
],
15301532
Applicability::MachineApplicable,
15311533
);
15321534
} else {

src/test/ui/impl-trait/object-unsafe-trait-in-return-position-impl-trait.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ LL | B
3535
help: you could change the return type to be a boxed trait object
3636
|
3737
LL | fn cat() -> Box<dyn ObjectSafe> {
38-
| ^^^^^^^^^^^^^^^^^^^
38+
| ^^^^^^^ ^
3939

4040
error: aborting due to 2 previous errors
4141

src/test/ui/point-to-type-err-cause-on-impl-trait-return.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ LL | 1u32
1717
help: you could change the return type to be a boxed trait object
1818
|
1919
LL | fn foo() -> Box<dyn std::fmt::Display> {
20-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
| ^^^^^^^ ^
2121

2222
error[E0308]: mismatched types
2323
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:12:16
@@ -38,7 +38,7 @@ LL | return 1u32;
3838
help: you could change the return type to be a boxed trait object
3939
|
4040
LL | fn bar() -> Box<dyn std::fmt::Display> {
41-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
41+
| ^^^^^^^ ^
4242

4343
error[E0308]: mismatched types
4444
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:20:9
@@ -59,7 +59,7 @@ LL | 1u32
5959
help: you could change the return type to be a boxed trait object
6060
|
6161
LL | fn baz() -> Box<dyn std::fmt::Display> {
62-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
62+
| ^^^^^^^ ^
6363

6464
error[E0308]: `if` and `else` have incompatible types
6565
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:28:9
@@ -76,7 +76,7 @@ LL | | }
7676
help: you could change the return type to be a boxed trait object
7777
|
7878
LL | fn qux() -> Box<dyn std::fmt::Display> {
79-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
79+
| ^^^^^^^ ^
8080
help: if you change the return type to expect trait objects box the returned expressions
8181
|
8282
LL | Box::new(0i32)
@@ -102,7 +102,7 @@ LL | _ => 1u32,
102102
help: you could change the return type to be a boxed trait object
103103
|
104104
LL | fn bat() -> Box<dyn std::fmt::Display> {
105-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
105+
| ^^^^^^^ ^
106106

107107
error[E0308]: mismatched types
108108
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:40:5
@@ -124,7 +124,7 @@ LL | | }
124124
help: you could change the return type to be a boxed trait object
125125
|
126126
LL | fn can() -> Box<dyn std::fmt::Display> {
127-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
127+
| ^^^^^^^ ^
128128

129129
error[E0308]: mismatched types
130130
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:53:13
@@ -145,7 +145,7 @@ LL | 1u32
145145
help: you could change the return type to be a boxed trait object
146146
|
147147
LL | fn cat() -> Box<dyn std::fmt::Display> {
148-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
148+
| ^^^^^^^ ^
149149

150150
error[E0308]: `match` arms have incompatible types
151151
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:61:14
@@ -162,7 +162,7 @@ LL | | }
162162
help: you could change the return type to be a boxed trait object
163163
|
164164
LL | fn dog() -> Box<dyn std::fmt::Display> {
165-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
165+
| ^^^^^^^ ^
166166
help: if you change the return type to expect trait objects box the returned expressions
167167
|
168168
LL | 0 => Box::new(0i32),

0 commit comments

Comments
 (0)