Skip to content

Commit 91b7593

Browse files
committed
Use short ty string for move errors
``` error[E0382]: use of moved value: `x` --> bay.rs:14:14 | 12 | fn foo(x: D) { | - move occurs because `x` has type `(((..., ..., ..., ...), ..., ..., ...), ..., ..., ...)`, which does not implement the `Copy` trait 13 | let _a = x; | - value moved here 14 | let _b = x; //~ ERROR use of moved value | ^ value used here after move | = note: the full type name has been written to 'bay.long-type-14349227078439097973.txt' = note: consider using `--verbose` to print the full type name to the console help: consider cloning the value if the performance cost is acceptable | 13 | let _a = x.clone(); | ++++++++ ```
1 parent 99768c8 commit 91b7593

File tree

9 files changed

+93
-8
lines changed

9 files changed

+93
-8
lines changed

Diff for: compiler/rustc_borrowck/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ borrowck_lifetime_constraints_error =
9292
borrowck_limitations_implies_static =
9393
due to current limitations in the borrow checker, this implies a `'static` lifetime
9494
95+
borrowck_long_type_consider_verbose = consider using `--verbose` to print the full type name to the console
96+
borrowck_long_type_full_path = the full type name has been written to '{$path}'
97+
9598
borrowck_move_closure_suggestion =
9699
consider adding 'move' keyword before the nested closure
97100

Diff for: compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
289289
None => "value".to_owned(),
290290
};
291291
if needs_note {
292+
let mut path = None;
293+
let ty = self.infcx.tcx.short_ty_string(ty, &mut path);
292294
if let Some(local) = place.as_local() {
293295
let span = self.body.local_decls[local].source_info.span;
294296
err.subdiagnostic(crate::session_diagnostics::TypeNoCopy::Label {
@@ -304,6 +306,11 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
304306
place: &note_msg,
305307
});
306308
};
309+
if let Some(path) = path {
310+
err.subdiagnostic(crate::session_diagnostics::LongTypePath {
311+
path: path.display().to_string(),
312+
});
313+
}
307314
}
308315

309316
if let UseSpans::FnSelfUse {

Diff for: compiler/rustc_borrowck/src/diagnostics/move_errors.rs

+24-3
Original file line numberDiff line numberDiff line change
@@ -596,12 +596,19 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
596596
self.suggest_cloning(err, place_ty, expr, None);
597597
}
598598

599+
let mut path = None;
600+
let ty = self.infcx.tcx.short_ty_string(place_ty, &mut path);
599601
err.subdiagnostic(crate::session_diagnostics::TypeNoCopy::Label {
600602
is_partial_move: false,
601-
ty: place_ty,
603+
ty,
602604
place: &place_desc,
603605
span,
604606
});
607+
if let Some(path) = path {
608+
err.subdiagnostic(crate::session_diagnostics::LongTypePath {
609+
path: path.display().to_string(),
610+
});
611+
}
605612
} else {
606613
binds_to.sort();
607614
binds_to.dedup();
@@ -628,12 +635,19 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
628635
self.suggest_cloning(err, place_ty, expr, Some(use_spans));
629636
}
630637

638+
let mut path = None;
639+
let ty = self.infcx.tcx.short_ty_string(place_ty, &mut path);
631640
err.subdiagnostic(crate::session_diagnostics::TypeNoCopy::Label {
632641
is_partial_move: false,
633-
ty: place_ty,
642+
ty,
634643
place: &place_desc,
635644
span: use_span,
636645
});
646+
if let Some(path) = path {
647+
err.subdiagnostic(crate::session_diagnostics::LongTypePath {
648+
path: path.display().to_string(),
649+
});
650+
}
637651

638652
use_spans.args_subdiag(err, |args_span| {
639653
crate::session_diagnostics::CaptureArgLabel::MoveOutPlace {
@@ -831,12 +845,19 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
831845
self.suggest_cloning(err, bind_to.ty, expr, None);
832846
}
833847

848+
let mut path = None;
849+
let ty = self.infcx.tcx.short_ty_string(bind_to.ty, &mut path);
834850
err.subdiagnostic(crate::session_diagnostics::TypeNoCopy::Label {
835851
is_partial_move: false,
836-
ty: bind_to.ty,
852+
ty,
837853
place: place_desc,
838854
span: binding_span,
839855
});
856+
if let Some(path) = path {
857+
err.subdiagnostic(crate::session_diagnostics::LongTypePath {
858+
path: path.display().to_string(),
859+
});
860+
}
840861
}
841862
}
842863

Diff for: compiler/rustc_borrowck/src/session_diagnostics.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -459,17 +459,24 @@ pub(crate) enum OnClosureNote<'a> {
459459
}
460460

461461
#[derive(Subdiagnostic)]
462-
pub(crate) enum TypeNoCopy<'a, 'tcx> {
462+
#[note(borrowck_long_type_full_path)]
463+
#[note(borrowck_long_type_consider_verbose)]
464+
pub(crate) struct LongTypePath {
465+
pub(crate) path: String,
466+
}
467+
468+
#[derive(Subdiagnostic)]
469+
pub(crate) enum TypeNoCopy<'a> {
463470
#[label(borrowck_ty_no_impl_copy)]
464471
Label {
465472
is_partial_move: bool,
466-
ty: Ty<'tcx>,
473+
ty: String,
467474
place: &'a str,
468475
#[primary_span]
469476
span: Span,
470477
},
471478
#[note(borrowck_ty_no_impl_copy)]
472-
Note { is_partial_move: bool, ty: Ty<'tcx>, place: &'a str },
479+
Note { is_partial_move: bool, ty: String, place: &'a str },
473480
}
474481

475482
#[derive(Diagnostic)]

Diff for: compiler/rustc_mir_build/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ mir_build_borrow_of_moved_value = borrow of moved value
2525
.occurs_because_label = move occurs because `{$name}` has type `{$ty}`, which does not implement the `Copy` trait
2626
.value_borrowed_label = value borrowed here after move
2727
.suggestion = borrow this binding in the pattern to avoid moving the value
28+
.full_type_name = the full type name has been written to '{$path}'
29+
.consider_verbose = consider using `--verbose` to print the full type name to the console
2830
2931
mir_build_call_to_deprecated_safe_fn_requires_unsafe =
3032
call to deprecated safe function `{$function}` is unsafe and requires unsafe block

Diff for: compiler/rustc_mir_build/src/errors.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -790,17 +790,21 @@ pub(crate) struct IrrefutableLetPatternsWhileLet {
790790

791791
#[derive(Diagnostic)]
792792
#[diag(mir_build_borrow_of_moved_value)]
793-
pub(crate) struct BorrowOfMovedValue<'tcx> {
793+
pub(crate) struct BorrowOfMovedValue {
794794
#[primary_span]
795795
#[label]
796796
#[label(mir_build_occurs_because_label)]
797797
pub(crate) binding_span: Span,
798798
#[label(mir_build_value_borrowed_label)]
799799
pub(crate) conflicts_ref: Vec<Span>,
800800
pub(crate) name: Symbol,
801-
pub(crate) ty: Ty<'tcx>,
801+
pub(crate) ty: String,
802802
#[suggestion(code = "ref ", applicability = "machine-applicable")]
803803
pub(crate) suggest_borrowing: Option<Span>,
804+
#[note(mir_build_full_type_name)]
805+
#[note(mir_build_consider_verbose)]
806+
pub(crate) has_path: bool,
807+
pub(crate) path: String,
804808
}
805809

806810
#[derive(Diagnostic)]

Diff for: compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+4
Original file line numberDiff line numberDiff line change
@@ -795,12 +795,16 @@ fn check_borrow_conflicts_in_at_patterns<'tcx>(cx: &MatchVisitor<'_, 'tcx>, pat:
795795
}
796796
});
797797
if !conflicts_ref.is_empty() {
798+
let mut path = None;
799+
let ty = cx.tcx.short_ty_string(ty, &mut path);
798800
sess.dcx().emit_err(BorrowOfMovedValue {
799801
binding_span: pat.span,
800802
conflicts_ref,
801803
name,
802804
ty,
803805
suggest_borrowing: Some(pat.span.shrink_to_lo()),
806+
has_path: path.is_some(),
807+
path: path.map(|p| p.display().to_string()).unwrap_or_default(),
804808
});
805809
}
806810
return;

Diff for: tests/ui/diagnostic-width/non-copy-type-moved.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ compile-flags: --diagnostic-width=60 -Zwrite-long-types-to-disk=yes
2+
//@ normalize-stderr: "long-type-\d+" -> "long-type-hash"
3+
type A = (String, String, String, String);
4+
type B = (A, A, A, A);
5+
type C = (B, B, B, B);
6+
type D = (C, C, C, C);
7+
8+
trait Trait {}
9+
10+
fn require_trait<T: Trait>() {}
11+
12+
fn foo(x: D) {
13+
let _a = x;
14+
let _b = x; //~ ERROR use of moved value
15+
}
16+
17+
fn main() {}

Diff for: tests/ui/diagnostic-width/non-copy-type-moved.stderr

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0382]: use of moved value: `x`
2+
--> $DIR/non-copy-type-moved.rs:14:14
3+
|
4+
LL | fn foo(x: D) {
5+
| - move occurs because `x` has type `((..., ..., ..., ...), ..., ..., ...)`, which does not implement the `Copy` trait
6+
LL | let _a = x;
7+
| - value moved here
8+
LL | let _b = x;
9+
| ^ value used here after move
10+
|
11+
= note: the full type name has been written to '$TEST_BUILD_DIR/diagnostic-width/non-copy-type-moved/non-copy-type-moved.long-type-hash.txt'
12+
= note: consider using `--verbose` to print the full type name to the console
13+
help: consider cloning the value if the performance cost is acceptable
14+
|
15+
LL | let _a = x.clone();
16+
| ++++++++
17+
18+
error: aborting due to 1 previous error
19+
20+
For more information about this error, try `rustc --explain E0382`.

0 commit comments

Comments
 (0)