Skip to content

Commit e8f86dc

Browse files
committed
Use short ty string for binop errors
``` error[E0369]: cannot add `((..., ..., ..., ...), ..., ..., ...)` to `((..., ..., ..., ...), ..., ..., ...)` --> $DIR/binop.rs:9:7 | LL | x + x; | - ^ - ((..., ..., ..., ...), ..., ..., ...) | | | ((..., ..., ..., ...), ..., ..., ...) | = note: the full type name has been written to '$TEST_BUILD_DIR/diagnostic-width/binop/binop.long-type-hash.txt' = note: consider using `--verbose` to print the full type name to the console ``` CC #135919.
1 parent a6434ef commit e8f86dc

File tree

3 files changed

+54
-15
lines changed

3 files changed

+54
-15
lines changed

compiler/rustc_hir_typeck/src/op.rs

+28-15
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
306306
lang_item_for_op(self.tcx, Op::Binary(op, is_assign), op.span);
307307
let missing_trait = trait_def_id
308308
.map(|def_id| with_no_trimmed_paths!(self.tcx.def_path_str(def_id)));
309+
let mut path = None;
310+
let lhs_ty_str = self.tcx.short_ty_string(lhs_ty, &mut path);
311+
let rhs_ty_str = self.tcx.short_ty_string(rhs_ty, &mut path);
309312
let (mut err, output_def_id) = match is_assign {
310313
IsAssign::Yes => {
311314
let mut err = struct_span_code_err!(
@@ -314,53 +317,54 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
314317
E0368,
315318
"binary assignment operation `{}=` cannot be applied to type `{}`",
316319
op.node.as_str(),
317-
lhs_ty,
320+
lhs_ty_str,
318321
);
319322
err.span_label(
320323
lhs_expr.span,
321-
format!("cannot use `{}=` on type `{}`", op.node.as_str(), lhs_ty),
324+
format!("cannot use `{}=` on type `{}`", op.node.as_str(), lhs_ty_str),
322325
);
323326
self.note_unmet_impls_on_type(&mut err, errors, false);
324327
(err, None)
325328
}
326329
IsAssign::No => {
327330
let message = match op.node {
328331
hir::BinOpKind::Add => {
329-
format!("cannot add `{rhs_ty}` to `{lhs_ty}`")
332+
format!("cannot add `{rhs_ty_str}` to `{lhs_ty_str}`")
330333
}
331334
hir::BinOpKind::Sub => {
332-
format!("cannot subtract `{rhs_ty}` from `{lhs_ty}`")
335+
format!("cannot subtract `{rhs_ty_str}` from `{lhs_ty_str}`")
333336
}
334337
hir::BinOpKind::Mul => {
335-
format!("cannot multiply `{lhs_ty}` by `{rhs_ty}`")
338+
format!("cannot multiply `{lhs_ty_str}` by `{rhs_ty_str}`")
336339
}
337340
hir::BinOpKind::Div => {
338-
format!("cannot divide `{lhs_ty}` by `{rhs_ty}`")
341+
format!("cannot divide `{lhs_ty_str}` by `{rhs_ty_str}`")
339342
}
340343
hir::BinOpKind::Rem => {
341344
format!(
342-
"cannot calculate the remainder of `{lhs_ty}` divided by `{rhs_ty}`"
345+
"cannot calculate the remainder of `{lhs_ty_str}` divided by \
346+
`{rhs_ty_str}`"
343347
)
344348
}
345349
hir::BinOpKind::BitAnd => {
346-
format!("no implementation for `{lhs_ty} & {rhs_ty}`")
350+
format!("no implementation for `{lhs_ty_str} & {rhs_ty_str}`")
347351
}
348352
hir::BinOpKind::BitXor => {
349-
format!("no implementation for `{lhs_ty} ^ {rhs_ty}`")
353+
format!("no implementation for `{lhs_ty_str} ^ {rhs_ty_str}`")
350354
}
351355
hir::BinOpKind::BitOr => {
352-
format!("no implementation for `{lhs_ty} | {rhs_ty}`")
356+
format!("no implementation for `{lhs_ty_str} | {rhs_ty_str}`")
353357
}
354358
hir::BinOpKind::Shl => {
355-
format!("no implementation for `{lhs_ty} << {rhs_ty}`")
359+
format!("no implementation for `{lhs_ty_str} << {rhs_ty_str}`")
356360
}
357361
hir::BinOpKind::Shr => {
358-
format!("no implementation for `{lhs_ty} >> {rhs_ty}`")
362+
format!("no implementation for `{lhs_ty_str} >> {rhs_ty_str}`")
359363
}
360364
_ => format!(
361365
"binary operation `{}` cannot be applied to type `{}`",
362366
op.node.as_str(),
363-
lhs_ty
367+
lhs_ty_str,
364368
),
365369
};
366370
let output_def_id = trait_def_id.and_then(|def_id| {
@@ -375,14 +379,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
375379
let mut err =
376380
struct_span_code_err!(self.dcx(), op.span, E0369, "{message}");
377381
if !lhs_expr.span.eq(&rhs_expr.span) {
378-
err.span_label(lhs_expr.span, lhs_ty.to_string());
379-
err.span_label(rhs_expr.span, rhs_ty.to_string());
382+
err.span_label(lhs_expr.span, lhs_ty_str);
383+
err.span_label(rhs_expr.span, rhs_ty_str);
380384
}
381385
let suggest_derive = self.can_eq(self.param_env, lhs_ty, rhs_ty);
382386
self.note_unmet_impls_on_type(&mut err, errors, suggest_derive);
383387
(err, output_def_id)
384388
}
385389
};
390+
if let Some(path) = path {
391+
err.note(format!(
392+
"the full type name has been written to '{}'",
393+
path.display()
394+
));
395+
err.note(
396+
"consider using `--verbose` to print the full type name to the console",
397+
);
398+
}
386399

387400
// Try to suggest a semicolon if it's `A \n *B` where `B` is a place expr
388401
let maybe_missing_semi = self.check_for_missing_semi(expr, &mut err);

tests/ui/diagnostic-width/binop.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ compile-flags: --diagnostic-width=60 -Zwrite-long-types-to-disk=yes
2+
//@ normalize-stderr: "long-type-\d+" -> "long-type-hash"
3+
type A = (i32, i32, i32, i32);
4+
type B = (A, A, A, A);
5+
type C = (B, B, B, B);
6+
type D = (C, C, C, C);
7+
8+
fn foo(x: D) {
9+
x + x; //~ ERROR cannot add `((...
10+
}
11+
12+
fn main() {}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0369]: cannot add `((..., ..., ..., ...), ..., ..., ...)` to `((..., ..., ..., ...), ..., ..., ...)`
2+
--> $DIR/binop.rs:9:7
3+
|
4+
LL | x + x;
5+
| - ^ - ((..., ..., ..., ...), ..., ..., ...)
6+
| |
7+
| ((..., ..., ..., ...), ..., ..., ...)
8+
|
9+
= note: the full type name has been written to '$TEST_BUILD_DIR/diagnostic-width/binop/binop.long-type-hash.txt'
10+
= note: consider using `--verbose` to print the full type name to the console
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0369`.

0 commit comments

Comments
 (0)