Skip to content

Commit 93a9683

Browse files
Ariel Ben-Yehudaarielb1
Ariel Ben-Yehuda
authored andcommitted
use diagnostic-mutating style for note_type_err too
that is much cleaner than the `type_err!` style I used earlier.
1 parent 1e4f6d5 commit 93a9683

File tree

4 files changed

+53
-55
lines changed

4 files changed

+53
-55
lines changed

src/librustc/infer/error_reporting.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -521,25 +521,25 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
521521
}
522522
}
523523

524-
pub fn report_and_explain_type_error_with_code(&self,
525-
origin: TypeOrigin,
526-
values: Option<ValuePairs<'tcx>>,
527-
terr: &TypeError<'tcx>,
528-
message: &str,
529-
code: &str)
530-
-> DiagnosticBuilder<'tcx>
524+
pub fn note_type_err(&self,
525+
diag: &mut DiagnosticBuilder<'tcx>,
526+
origin: TypeOrigin,
527+
values: Option<ValuePairs<'tcx>>,
528+
terr: &TypeError<'tcx>)
531529
{
532530
let expected_found = match values {
533531
None => None,
534532
Some(values) => match self.values_str(&values) {
535533
Some((expected, found)) => Some((expected, found)),
536-
None => return self.tcx.sess.diagnostic().struct_dummy() /* derived error */
534+
None => {
535+
// Derived error. Cancel the emitter.
536+
self.tcx.sess.diagnostic().cancel(diag);
537+
return
538+
}
537539
}
538540
};
539541

540542
let span = origin.span();
541-
let mut err = self.tcx.sess.struct_span_err_with_code(
542-
span, message, code);
543543

544544
let mut is_simple_error = false;
545545

@@ -551,21 +551,19 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
551551
};
552552

553553
if !is_simple_error || check_old_school() {
554-
err.note_expected_found(&"type", &expected, &found);
554+
diag.note_expected_found(&"type", &expected, &found);
555555
}
556556
}
557557

558558
if !is_simple_error && check_old_school() {
559-
err.span_note(span, &format!("{}", terr));
559+
diag.span_note(span, &format!("{}", terr));
560560
} else {
561-
err.span_label(span, &terr);
561+
diag.span_label(span, &terr);
562562
}
563563

564-
self.note_error_origin(&mut err, &origin);
565-
self.check_and_note_conflicting_crates(&mut err, terr, span);
566-
self.tcx.note_and_explain_type_err(&mut err, terr, span);
567-
568-
err
564+
self.note_error_origin(diag, &origin);
565+
self.check_and_note_conflicting_crates(diag, terr, span);
566+
self.tcx.note_and_explain_type_err(diag, terr, span);
569567
}
570568

571569
pub fn report_and_explain_type_error(&self,
@@ -574,8 +572,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
574572
-> DiagnosticBuilder<'tcx>
575573
{
576574
// FIXME: do we want to use a different error code for each origin?
577-
let failure_str = trace.origin.as_failure_str();
578-
type_err!(self, trace.origin, Some(trace.values), terr, E0308, "{}", failure_str)
575+
let mut diag = struct_span_err!(
576+
self.tcx.sess, trace.origin.span(), E0308,
577+
"{}", trace.origin.as_failure_str()
578+
);
579+
self.note_type_err(&mut diag, trace.origin, Some(trace.values), terr);
580+
diag
579581
}
580582

581583
/// Returns a string of the form "expected `{}`, found `{}`".

src/librustc/macros.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,3 @@ macro_rules! span_bug {
5959
$crate::session::span_bug_fmt(file!(), line!(), $span, format_args!($($message)*))
6060
})
6161
}
62-
63-
#[macro_export]
64-
macro_rules! type_err {
65-
($infcx:expr, $origin: expr, $values: expr, $terr: expr, $code:ident, $($message:tt)*) => ({
66-
__diagnostic_used!($code);
67-
$infcx.report_and_explain_type_error_with_code(
68-
$origin,
69-
$values,
70-
&$terr,
71-
&format!($($message)*),
72-
stringify!($code))
73-
})
74-
}

src/librustc/traits/error_reporting.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,11 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
157157
}
158158
}
159159

160-
let mut diag = type_err!(
161-
self,
162-
origin,
163-
values,
164-
err,
165-
E0271,
166-
"type mismatch resolving `{}`",
167-
predicate);
160+
let mut diag = struct_span_err!(
161+
self.tcx.sess, origin.span(), E0271,
162+
"type mismatch resolving `{}`", predicate
163+
);
164+
self.note_type_err(&mut diag, origin, values, err);
168165
self.note_obligation_cause(&mut diag, obligation);
169166
diag.emit();
170167
});

src/librustc_typeck/check/compare_method.rs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,19 @@ pub fn compare_impl_method<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
325325
debug!("sub_types failed: impl ty {:?}, trait ty {:?}",
326326
impl_fty,
327327
trait_fty);
328-
let values = Some(infer::ValuePairs::Types(ExpectedFound {
329-
expected: trait_fty,
330-
found: impl_fty
331-
}));
332-
type_err!(infcx, origin, values, terr, E0053,
333-
"method `{}` has an incompatible type for trait",
334-
trait_m.name).emit();
328+
329+
let mut diag = struct_span_err!(
330+
tcx.sess, origin.span(), E0053,
331+
"method `{}` has an incompatible type for trait", trait_m.name
332+
);
333+
infcx.note_type_err(
334+
&mut diag, origin,
335+
Some(infer::ValuePairs::Types(ExpectedFound {
336+
expected: trait_fty,
337+
found: impl_fty
338+
})), &terr
339+
);
340+
diag.emit();
335341
return
336342
}
337343

@@ -476,13 +482,19 @@ pub fn compare_const_impl<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
476482
debug!("checking associated const for compatibility: impl ty {:?}, trait ty {:?}",
477483
impl_ty,
478484
trait_ty);
479-
let values = Some(infer::ValuePairs::Types(ExpectedFound {
480-
expected: trait_ty,
481-
found: impl_ty
482-
}));
483-
type_err!(infcx, origin, values, terr, E0326,
484-
"implemented const `{}` has an incompatible type for \
485-
trait", trait_c.name).emit();
485+
let mut diag = struct_span_err!(
486+
tcx.sess, origin.span(), E0326,
487+
"implemented const `{}` has an incompatible type for trait",
488+
trait_c.name
489+
);
490+
infcx.note_type_err(
491+
&mut diag, origin,
492+
Some(infer::ValuePairs::Types(ExpectedFound {
493+
expected: trait_ty,
494+
found: impl_ty
495+
})), &terr
496+
);
497+
diag.emit();
486498
}
487499
});
488500
}

0 commit comments

Comments
 (0)