Skip to content

Commit 54bd3a7

Browse files
authoredJul 4, 2024
Rollup merge of rust-lang#127301 - estebank:fix-suggestions, r=Urgau
Tweak some structured suggestions to be more verbose and accurate Addressing some issues I found while working on rust-lang#127282. ``` error: this URL is not a hyperlink --> $DIR/auxiliary/include-str-bare-urls.md:1:11 | LL | HEADS UP! https://example.com MUST SHOW UP IN THE STDERR FILE! | ^^^^^^^^^^^^^^^^^^^ | = note: bare URLs are not automatically turned into clickable links note: the lint level is defined here --> $DIR/include-str-bare-urls.rs:14:9 | LL | #![deny(rustdoc::bare_urls)] | ^^^^^^^^^^^^^^^^^^ help: use an automatic link instead | LL | HEADS UP! <https://example.com> MUST SHOW UP IN THE STDERR FILE! | + + ``` ``` error[E0384]: cannot assign twice to immutable variable `v` --> $DIR/assign-imm-local-twice.rs:7:5 | LL | v = 1; | ----- first assignment to `v` LL | println!("v={}", v); LL | v = 2; | ^^^^^ cannot assign twice to immutable variable | help: consider making this binding mutable | LL | let mut v: isize; | +++ ``` ``` error[E0393]: the type parameter `Rhs` must be explicitly specified --> $DIR/issue-22560.rs:9:23 | LL | trait Sub<Rhs=Self> { | ------------------- type parameter `Rhs` must be specified for this ... LL | type Test = dyn Add + Sub; | ^^^ | = note: because of the default `Self` reference, type parameters must be specified on object types help: set the type parameter to the desired type | LL | type Test = dyn Add + Sub<Rhs>; | +++++ ``` ``` error[E0596]: cannot borrow `v` as mutable, as it is not declared as mutable --> $DIR/issue-33819.rs:4:34 | LL | Some(ref v) => { let a = &mut v; }, | ^^^^^^ cannot borrow as mutable | help: try removing `&mut` here | LL - Some(ref v) => { let a = &mut v; }, LL + Some(ref v) => { let a = v; }, | ``` ``` help: remove the invocation before committing it to a version control system | LL - dbg!(); | ``` ``` error[E0308]: mismatched types --> $DIR/issue-39974.rs:1:21 | LL | const LENGTH: f64 = 2; | ^ expected `f64`, found integer | help: use a float literal | LL | const LENGTH: f64 = 2.0; | ++ ``` ``` error[E0529]: expected an array or slice, found `Vec<i32>` --> $DIR/match-ergonomics.rs:8:9 | LL | [&v] => {}, | ^^^^ pattern cannot match with input type `Vec<i32>` | help: consider slicing here | LL | match x[..] { | ++++ ``` ``` error[E0609]: no field `0` on type `[u32; 1]` --> $DIR/parenthesized-deref-suggestion.rs:10:21 | LL | (x as [u32; 1]).0; | ^ unknown field | help: instead of using tuple indexing, use array indexing | LL | (x as [u32; 1])[0]; | ~ + ```
2 parents d27136f + a5e7da0 commit 54bd3a7

File tree

82 files changed

+808
-468
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+808
-468
lines changed
 

‎compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3757,13 +3757,11 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
37573757
assigned_span: Span,
37583758
err_place: Place<'tcx>,
37593759
) {
3760-
let (from_arg, local_decl, local_name) = match err_place.as_local() {
3761-
Some(local) => (
3762-
self.body.local_kind(local) == LocalKind::Arg,
3763-
Some(&self.body.local_decls[local]),
3764-
self.local_names[local],
3765-
),
3766-
None => (false, None, None),
3760+
let (from_arg, local_decl) = match err_place.as_local() {
3761+
Some(local) => {
3762+
(self.body.local_kind(local) == LocalKind::Arg, Some(&self.body.local_decls[local]))
3763+
}
3764+
None => (false, None),
37673765
};
37683766

37693767
// If root local is initialized immediately (everything apart from let
@@ -3795,13 +3793,12 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
37953793
err.span_label(assigned_span, format!("first assignment to {place_description}"));
37963794
}
37973795
if let Some(decl) = local_decl
3798-
&& let Some(name) = local_name
37993796
&& decl.can_be_made_mutable()
38003797
{
3801-
err.span_suggestion(
3802-
decl.source_info.span,
3798+
err.span_suggestion_verbose(
3799+
decl.source_info.span.shrink_to_lo(),
38033800
"consider making this binding mutable",
3804-
format!("mut {name}"),
3801+
"mut ".to_string(),
38053802
Applicability::MachineApplicable,
38063803
);
38073804
if !from_arg
@@ -3813,10 +3810,10 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
38133810
}))
38143811
)
38153812
{
3816-
err.span_suggestion(
3817-
decl.source_info.span,
3813+
err.span_suggestion_verbose(
3814+
decl.source_info.span.shrink_to_lo(),
38183815
"to modify the original value, take a borrow instead",
3819-
format!("ref mut {name}"),
3816+
"ref mut ".to_string(),
38203817
Applicability::MaybeIncorrect,
38213818
);
38223819
}

‎compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -408,21 +408,21 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
408408
fn_decl.implicit_self,
409409
hir::ImplicitSelfKind::RefImm | hir::ImplicitSelfKind::RefMut
410410
) {
411-
err.span_suggestion(
412-
upvar_ident.span,
411+
err.span_suggestion_verbose(
412+
upvar_ident.span.shrink_to_lo(),
413413
"consider changing this to be mutable",
414-
format!("mut {}", upvar_ident.name),
414+
"mut ",
415415
Applicability::MachineApplicable,
416416
);
417417
break;
418418
}
419419
}
420420
}
421421
} else {
422-
err.span_suggestion(
423-
upvar_ident.span,
422+
err.span_suggestion_verbose(
423+
upvar_ident.span.shrink_to_lo(),
424424
"consider changing this to be mutable",
425-
format!("mut {}", upvar_ident.name),
425+
"mut ",
426426
Applicability::MachineApplicable,
427427
);
428428
}
@@ -449,8 +449,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
449449
.is_ok_and(|snippet| snippet.starts_with("&mut ")) =>
450450
{
451451
err.span_label(span, format!("cannot {act}"));
452-
err.span_suggestion(
453-
span,
452+
err.span_suggestion_verbose(
453+
span.with_hi(span.lo() + BytePos(5)),
454454
"try removing `&mut` here",
455455
"",
456456
Applicability::MaybeIncorrect,
@@ -755,13 +755,16 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
755755
pat: hir::Pat { kind: hir::PatKind::Ref(_, _), .. },
756756
..
757757
}) = node
758-
&& let Ok(name) =
759-
self.infcx.tcx.sess.source_map().span_to_snippet(local_decl.source_info.span)
760758
{
761-
err.span_suggestion(
762-
pat_span,
759+
err.multipart_suggestion(
763760
"consider changing this to be mutable",
764-
format!("&(mut {name})"),
761+
vec![
762+
(pat_span.until(local_decl.source_info.span), "&(mut ".to_string()),
763+
(
764+
local_decl.source_info.span.shrink_to_hi().with_hi(pat_span.hi()),
765+
")".to_string(),
766+
),
767+
],
765768
Applicability::MachineApplicable,
766769
);
767770
return;

0 commit comments

Comments
 (0)