Skip to content

Commit b291b7c

Browse files
committed
Enforce short type string in more errors
``` error[E0529]: expected an array or slice, found `(..., ..., ..., ...)` --> $DIR/long-E0529.rs:10:9 | LL | let [] = x; | ^^ pattern cannot match with input type `(..., ..., ..., ...)` | = note: the full name for the type has been written to '$TEST_BUILD_DIR/$FILE.long-type-hash.txt' = note: consider using `--verbose` to print the full type name to the console ``` ``` error[E0609]: no field `field` on type `(..., ..., ..., ...)` --> $DIR/long-E0609.rs:10:7 | LL | x.field; | ^^^^^ unknown field | = note: the full name for the type has been written to '$TEST_BUILD_DIR/$FILE.long-type-hash.txt' = note: consider using `--verbose` to print the full type name to the console ``` ``` error[E0614]: type `(..., ..., ..., ...)` cannot be dereferenced --> $DIR/long-E0614.rs:10:5 | LL | *x; | ^^ | = note: the full name for the type has been written to '$TEST_BUILD_DIR/$FILE.long-type-hash.txt' = note: consider using `--verbose` to print the full type name to the console ``` ``` error[E0618]: expected function, found `(..., ..., ..., ...)` --> $DIR/long-E0618.rs:10:5 | LL | fn foo(x: D) { | - `x` has type `(..., ..., ..., ...)` LL | x(); | ^-- | | | call expression requires function | = note: the full name for the type has been written to '$TEST_BUILD_DIR/$FILE.long-type-hash.txt' = note: consider using `--verbose` to print the full type name to the console ``` Use multipart suggestion for cast -> into: ``` error[E0604]: only `u8` can be cast as `char`, not `u32` --> $DIR/E0604.rs:2:5 | LL | 1u32 as char; | ^^^^^^^^^^^^ invalid cast | help: try `char::from_u32` instead | LL - 1u32 as char; LL + char::from_u32(1u32); | ```
1 parent 06a24e9 commit b291b7c

File tree

26 files changed

+248
-103
lines changed

26 files changed

+248
-103
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
312312
..
313313
} = use_spans
314314
{
315+
let ty = self.infcx.tcx.short_string(deref_target_ty, err.long_ty_path());
315316
err.note(format!(
316-
"{} occurs due to deref coercion to `{deref_target_ty}`",
317+
"{} occurs due to deref coercion to `{ty}`",
317318
desired_action.as_noun(),
318319
));
319320

@@ -1222,13 +1223,14 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
12221223
),
12231224
};
12241225
let prefix = if !self.implements_clone(ty) {
1225-
let msg = format!("`{ty}` doesn't implement `Copy` or `Clone`");
1226+
let t = self.infcx.tcx.short_string(ty, err.long_ty_path());
1227+
let msg = format!("`{t}` doesn't implement `Copy` or `Clone`");
12261228
if let ty::Adt(def, _) = ty.kind() {
12271229
err.span_note(self.infcx.tcx.def_span(def.did()), msg);
12281230
} else {
12291231
err.note(msg);
12301232
}
1231-
format!("if `{ty}` implemented `Clone`, you could ")
1233+
format!("if `{t}` implemented `Clone`, you could ")
12321234
} else {
12331235
String::new()
12341236
};
@@ -1270,10 +1272,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
12701272
let mut span: MultiSpan = ty_span.into();
12711273
span.push_span_label(ty_span, "consider implementing `Clone` for this type");
12721274
span.push_span_label(expr.span, "you could clone this value");
1273-
err.span_note(
1274-
span,
1275-
format!("if `{ty}` implemented `Clone`, you could clone the value"),
1276-
);
1275+
let t = self.infcx.tcx.short_string(ty, err.long_ty_path());
1276+
err.span_note(span, format!("if `{t}` implemented `Clone`, you could clone the value"));
12771277
} else if let ty::Param(param) = ty.kind()
12781278
&& let Some(_clone_trait_def) = self.infcx.tcx.lang_items().clone_trait()
12791279
&& let generics = self.infcx.tcx.generics_of(self.mir_def_id())
@@ -1302,10 +1302,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
13021302
"consider constraining this type parameter with `Clone`",
13031303
);
13041304
span.push_span_label(expr.span, "you could clone this value");
1305-
err.span_help(
1306-
span,
1307-
format!("if `{ty}` implemented `Clone`, you could clone the value"),
1308-
);
1305+
let t = self.infcx.tcx.short_string(ty, err.long_ty_path());
1306+
err.span_help(span, format!("if `{t}` implemented `Clone`, you could clone the value"));
13091307
}
13101308
}
13111309

@@ -1951,11 +1949,13 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
19511949
&& let None =
19521950
self.infcx.type_implements_trait_shallow(clone, inner, self.infcx.param_env)
19531951
{
1952+
let rcvr_ty = self.infcx.tcx.short_string(rcvr_ty, err.long_ty_path());
1953+
let inner_str = self.infcx.tcx.short_string(inner, err.long_ty_path());
19541954
err.span_label(
19551955
span,
19561956
format!(
1957-
"this call doesn't do anything, the result is still `{rcvr_ty}` \
1958-
because `{inner}` doesn't implement `Clone`",
1957+
"this call doesn't do anything, the result is still `{rcvr_ty}` because \
1958+
`{inner_str}` doesn't implement `Clone`",
19591959
),
19601960
);
19611961
types_to_constrain.insert(inner);
@@ -3067,6 +3067,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
30673067
let borrow_span = borrow_spans.var_or_use();
30683068

30693069
let mut err = self.cannot_borrow_across_destructor(borrow_span);
3070+
let dropped_ty = self.infcx.tcx.short_string(dropped_ty, err.long_ty_path());
30703071

30713072
let what_was_dropped = match self.describe_place(place.as_ref()) {
30723073
Some(name) => format!("`{name}`"),
@@ -3245,6 +3246,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
32453246
&& path.ident.name == sym::iter
32463247
&& let Some(ty) = expr_ty
32473248
{
3249+
let ty = self.infcx.tcx.short_string(ty, err.long_ty_path());
32483250
err.span_suggestion_verbose(
32493251
path.ident.span,
32503252
format!(
@@ -3799,7 +3801,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
37993801
Some(self.infcx.tcx.fn_arg_names(method_did)[0]),
38003802
)
38013803
{
3802-
err.note(format!("borrow occurs due to deref coercion to `{deref_target_ty}`"));
3804+
let ty = self.infcx.tcx.short_string(deref_target_ty, err.long_ty_path());
3805+
err.note(format!("borrow occurs due to deref coercion to `{ty}`"));
38033806
if let Some(deref_target_span) = deref_target_span {
38043807
err.span_note(deref_target_span, "deref defined here");
38053808
}

compiler/rustc_hir_typeck/src/callee.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,13 +675,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
675675
}
676676

677677
let callee_ty = self.resolve_vars_if_possible(callee_ty);
678+
let mut path = None;
678679
let mut err = self.dcx().create_err(errors::InvalidCallee {
679680
span: callee_expr.span,
680681
ty: match &unit_variant {
681682
Some((_, kind, path)) => format!("{kind} `{path}`"),
682-
None => format!("`{callee_ty}`"),
683+
None => format!("`{}`", self.tcx.short_string(callee_ty, &mut path)),
683684
},
684685
});
686+
*err.long_ty_path() = path;
685687
if callee_ty.references_error() {
686688
err.downgrade_to_delayed_bug();
687689
}
@@ -764,6 +766,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
764766
DefIdOrName::DefId(def_id) => self.tcx.def_descr(def_id),
765767
DefIdOrName::Name(name) => name,
766768
};
769+
let output_ty = self.tcx.short_string(output_ty, err.long_ty_path());
767770
err.span_label(
768771
callee_expr.span,
769772
format!("this {descr} returns an unsized value `{output_ty}`, so it cannot be called")
@@ -779,7 +782,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
779782
}
780783

781784
if let Some(span) = self.tcx.hir().res_span(def) {
782-
let callee_ty = callee_ty.to_string();
785+
let callee_ty = self.tcx.short_string(callee_ty, err.long_ty_path());
783786
let label = match (unit_variant, inner_callee_path) {
784787
(Some((_, kind, path)), _) => Some(format!("{kind} `{path}` defined here")),
785788
(_, Some(hir::QPath::Resolved(_, path))) => self

compiler/rustc_hir_typeck/src/cast.rs

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,18 @@ fn make_invalid_casting_error<'a, 'tcx>(
196196
cast_ty: Ty<'tcx>,
197197
fcx: &FnCtxt<'a, 'tcx>,
198198
) -> Diag<'a> {
199-
type_error_struct!(
199+
let mut path = None;
200+
let expr = fcx.tcx.short_string(expr_ty, &mut path);
201+
let cast = fcx.tcx.short_string(cast_ty, &mut path);
202+
let mut err = type_error_struct!(
200203
fcx.dcx(),
201204
span,
202205
expr_ty,
203206
E0606,
204-
"casting `{}` as `{}` is invalid",
205-
fcx.ty_to_string(expr_ty),
206-
fcx.ty_to_string(cast_ty)
207-
)
207+
"casting `{expr}` as `{cast}` is invalid",
208+
);
209+
*err.long_ty_path() = path;
210+
err
208211
}
209212

210213
/// If a cast from `from_ty` to `to_ty` is valid, returns a `Some` containing the kind
@@ -259,6 +262,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
259262
}
260263

261264
fn report_cast_error(&self, fcx: &FnCtxt<'a, 'tcx>, e: CastError<'tcx>) {
265+
let mut path = None;
262266
match e {
263267
CastError::ErrorGuaranteed(_) => {
264268
// an error has already been reported
@@ -369,27 +373,26 @@ impl<'a, 'tcx> CastCheck<'tcx> {
369373
fcx.dcx().emit_err(errors::CannotCastToBool { span: self.span, expr_ty, help });
370374
}
371375
CastError::CastToChar => {
376+
let expr = fcx.tcx.short_string(self.expr_ty, &mut path);
372377
let mut err = type_error_struct!(
373378
fcx.dcx(),
374379
self.span,
375380
self.expr_ty,
376381
E0604,
377-
"only `u8` can be cast as `char`, not `{}`",
378-
self.expr_ty
382+
"only `u8` can be cast as `char`, not `{expr}`",
379383
);
384+
*err.long_ty_path() = path;
380385
err.span_label(self.span, "invalid cast");
381386
if self.expr_ty.is_numeric() {
382387
if self.expr_ty == fcx.tcx.types.u32 {
383-
match fcx.tcx.sess.source_map().span_to_snippet(self.expr.span) {
384-
Ok(snippet) => err.span_suggestion(
385-
self.span,
386-
"try `char::from_u32` instead",
387-
format!("char::from_u32({snippet})"),
388-
Applicability::MachineApplicable,
389-
),
390-
391-
Err(_) => err.span_help(self.span, "try `char::from_u32` instead"),
392-
};
388+
err.multipart_suggestion_verbose(
389+
"try `char::from_u32` instead",
390+
vec![
391+
(self.span.shrink_to_lo(), "char::from_u32(".to_string()),
392+
(self.span.with_lo(self.expr.span.hi()), ")".to_string()),
393+
],
394+
Applicability::MachineApplicable,
395+
);
393396
} else if self.expr_ty == fcx.tcx.types.i8 {
394397
err.span_help(self.span, "try casting from `u8` instead");
395398
} else {
@@ -399,15 +402,16 @@ impl<'a, 'tcx> CastCheck<'tcx> {
399402
err.emit();
400403
}
401404
CastError::NonScalar => {
405+
let expr = fcx.tcx.short_string(self.expr_ty, &mut path);
406+
let cast = fcx.tcx.short_string(self.cast_ty, &mut path);
402407
let mut err = type_error_struct!(
403408
fcx.dcx(),
404409
self.span,
405410
self.expr_ty,
406411
E0605,
407-
"non-primitive cast: `{}` as `{}`",
408-
self.expr_ty,
409-
fcx.ty_to_string(self.cast_ty)
412+
"non-primitive cast: `{expr}` as `{cast}`",
410413
);
414+
*err.long_ty_path() = path;
411415
let mut sugg = None;
412416
let mut sugg_mutref = false;
413417
if let ty::Ref(reg, cast_ty, mutbl) = *self.cast_ty.kind() {
@@ -548,28 +552,35 @@ impl<'a, 'tcx> CastCheck<'tcx> {
548552
err.emit();
549553
}
550554
CastError::SizedUnsizedCast => {
551-
fcx.dcx().emit_err(errors::CastThinPointerToWidePointer {
555+
let expr_ty = fcx.tcx.short_string(self.expr_ty, &mut path);
556+
let cast_ty = fcx.tcx.short_string(self.cast_ty, &mut path);
557+
let mut err = fcx.dcx().create_err(errors::CastThinPointerToWidePointer {
552558
span: self.span,
553-
expr_ty: self.expr_ty,
554-
cast_ty: fcx.ty_to_string(self.cast_ty),
559+
expr_ty,
560+
cast_ty,
555561
teach: fcx.tcx.sess.teach(E0607),
556562
});
563+
*err.long_ty_path() = path;
564+
err.emit();
557565
}
558566
CastError::IntToWideCast(known_metadata) => {
559567
let expr_if_nightly = fcx.tcx.sess.is_nightly_build().then_some(self.expr_span);
560-
let cast_ty = fcx.resolve_vars_if_possible(self.cast_ty);
561-
let expr_ty = fcx.ty_to_string(self.expr_ty);
568+
let expr_ty = fcx.tcx.short_string(self.expr_ty, &mut path);
569+
let cast_ty =
570+
fcx.tcx.short_string(fcx.resolve_vars_if_possible(self.cast_ty), &mut path);
562571
let metadata = known_metadata.unwrap_or("type-specific metadata");
563572
let known_wide = known_metadata.is_some();
564573
let span = self.cast_span;
565-
fcx.dcx().emit_err(errors::IntToWide {
574+
let mut err = fcx.dcx().create_err(errors::IntToWide {
566575
span,
567576
metadata,
568577
expr_ty,
569578
cast_ty,
570579
expr_if_nightly,
571580
known_wide,
572581
});
582+
*err.long_ty_path() = path;
583+
err.emit();
573584
}
574585
CastError::UnknownCastPtrKind | CastError::UnknownExprPtrKind => {
575586
let unknown_cast_to = match e {
@@ -605,16 +616,18 @@ impl<'a, 'tcx> CastCheck<'tcx> {
605616
return err;
606617
}
607618

608-
let tstr = fcx.ty_to_string(self.cast_ty);
619+
let mut path = None;
620+
let tstr = fcx.tcx.short_string(self.cast_ty, &mut path);
621+
let expr_ty = fcx.tcx.short_string(fcx.resolve_vars_if_possible(self.expr_ty), &mut path);
622+
609623
let mut err = type_error_struct!(
610624
fcx.dcx(),
611625
self.span,
612626
self.expr_ty,
613627
E0620,
614-
"cast to unsized type: `{}` as `{}`",
615-
fcx.resolve_vars_if_possible(self.expr_ty),
616-
tstr
628+
"cast to unsized type: `{expr_ty}` as `{tstr}`",
617629
);
630+
*err.long_ty_path() = path;
618631
match self.expr_ty.kind() {
619632
ty::Ref(_, _, mt) => {
620633
let mtstr = mt.prefix_str();
@@ -1164,18 +1177,17 @@ impl<'a, 'tcx> CastCheck<'tcx> {
11641177
if let Some((deref_ty, _)) = derefed {
11651178
// Give a note about what the expr derefs to.
11661179
if deref_ty != self.expr_ty.peel_refs() {
1167-
err.subdiagnostic(errors::DerefImplsIsEmpty {
1168-
span: self.expr_span,
1169-
deref_ty: fcx.ty_to_string(deref_ty),
1170-
});
1180+
let deref_ty = fcx.tcx.short_string(deref_ty, err.long_ty_path());
1181+
err.subdiagnostic(errors::DerefImplsIsEmpty { span: self.expr_span, deref_ty });
11711182
}
11721183

1184+
let expr_ty = fcx.tcx.short_string(self.expr_ty, err.long_ty_path());
11731185
// Create a multipart suggestion: add `!` and `.is_empty()` in
11741186
// place of the cast.
11751187
err.subdiagnostic(errors::UseIsEmpty {
11761188
lo: self.expr_span.shrink_to_lo(),
11771189
hi: self.span.with_lo(self.expr_span.hi()),
1178-
expr_ty: fcx.ty_to_string(self.expr_ty),
1190+
expr_ty,
11791191
});
11801192
}
11811193
}

compiler/rustc_hir_typeck/src/errors.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ pub(crate) struct IntToWide<'tcx> {
470470
pub span: Span,
471471
pub metadata: &'tcx str,
472472
pub expr_ty: String,
473-
pub cast_ty: Ty<'tcx>,
473+
pub cast_ty: String,
474474
#[label(hir_typeck_int_to_fat_label_nightly)]
475475
pub expr_if_nightly: Option<Span>,
476476
pub known_wide: bool,
@@ -822,10 +822,10 @@ pub(crate) struct ReplaceWithName {
822822

823823
#[derive(Diagnostic)]
824824
#[diag(hir_typeck_cast_thin_pointer_to_wide_pointer, code = E0607)]
825-
pub(crate) struct CastThinPointerToWidePointer<'tcx> {
825+
pub(crate) struct CastThinPointerToWidePointer {
826826
#[primary_span]
827827
pub span: Span,
828-
pub expr_ty: Ty<'tcx>,
828+
pub expr_ty: String,
829829
pub cast_ty: String,
830830
#[note(hir_typeck_teach_help)]
831831
pub(crate) teach: bool,

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,13 +606,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
606606
if let Some(ty) = self.lookup_derefing(expr, oprnd, oprnd_t) {
607607
oprnd_t = ty;
608608
} else {
609+
let mut path = None;
610+
let ty = self.tcx.short_string(oprnd_t, &mut path);
609611
let mut err = type_error_struct!(
610612
self.dcx(),
611613
expr.span,
612614
oprnd_t,
613615
E0614,
614-
"type `{oprnd_t}` cannot be dereferenced",
616+
"type `{ty}` cannot be dereferenced",
615617
);
618+
*err.long_ty_path() = path;
616619
let sp = tcx.sess.source_map().start_point(expr.span).with_parent(None);
617620
if let Some(sp) =
618621
tcx.sess.psess.ambiguous_block_expr_parse.borrow().get(&sp)
@@ -3286,13 +3289,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
32863289
let span = field.span;
32873290
debug!("no_such_field_err(span: {:?}, field: {:?}, expr_t: {:?})", span, field, expr_t);
32883291

3292+
let mut path = None;
3293+
let ty = self.tcx.short_string(expr_t, &mut path);
32893294
let mut err = type_error_struct!(
32903295
self.dcx(),
32913296
span,
32923297
expr_t,
32933298
E0609,
3294-
"no field `{field}` on type `{expr_t}`",
3299+
"no field `{field}` on type `{ty}`",
32953300
);
3301+
*err.long_ty_path() = path;
32963302

32973303
// try to add a suggestion in case the field is a nested field of a field of the Adt
32983304
let mod_id = self.tcx.parent_module(id).to_def_id();

compiler/rustc_hir_typeck/src/gather_locals.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl<'a, 'tcx> GatherLocalsVisitor<'a, 'tcx> {
114114
debug!(
115115
"local variable {:?} is assigned type {}",
116116
decl.pat,
117-
self.fcx.ty_to_string(*self.fcx.locals.borrow().get(&decl.hir_id).unwrap())
117+
*self.fcx.locals.borrow().get(&decl.hir_id).unwrap()
118118
);
119119
}
120120
}
@@ -174,7 +174,7 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> {
174174
debug!(
175175
"pattern binding {} is assigned to {} with type {:?}",
176176
ident,
177-
self.fcx.ty_to_string(*self.fcx.locals.borrow().get(&p.hir_id).unwrap()),
177+
*self.fcx.locals.borrow().get(&p.hir_id).unwrap(),
178178
var_ty
179179
);
180180
}

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1555,7 +1555,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
15551555
pick_diag_hints: &mut PickDiagHints<'_, 'tcx>,
15561556
pick_constraints: Option<&PickConstraintsForShadowed>,
15571557
) -> Option<PickResult<'tcx>> {
1558-
debug!("pick_method(self_ty={})", self.ty_to_string(self_ty));
1558+
debug!("pick_method(self_ty={})", self_ty);
15591559

15601560
for (kind, candidates) in
15611561
[("inherent", &self.inherent_candidates), ("extension", &self.extension_candidates)]

0 commit comments

Comments
 (0)