Skip to content

Commit cea683c

Browse files
committed
Use .into_diagnostic() less.
This commit replaces this pattern: ``` err.into_diagnostic(dcx) ``` with this pattern: ``` dcx.create_err(err) ``` in a lot of places. It's a little shorter, makes the error level explicit, avoids some `IntoDiagnostic` imports, and is a necessary prerequisite for the next commit which will add a `level` arg to `into_diagnostic`. This requires adding `track_caller` on `create_err` to avoid mucking up the output of `tests/ui/track-diagnostics/track4.rs`. It probably should have been there already.
1 parent cda4736 commit cea683c

File tree

13 files changed

+109
-130
lines changed

13 files changed

+109
-130
lines changed

compiler/rustc_errors/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,7 @@ impl DiagCtxt {
12741274
self.create_err(err).emit()
12751275
}
12761276

1277+
#[track_caller]
12771278
pub fn create_err<'a>(
12781279
&'a self,
12791280
err: impl IntoDiagnostic<'a>,

compiler/rustc_expand/src/base.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,11 +1204,10 @@ pub fn resolve_path(
12041204
.expect("attempting to resolve a file path in an external file"),
12051205
FileName::DocTest(path, _) => path,
12061206
other => {
1207-
return Err(errors::ResolveRelativePath {
1207+
return Err(parse_sess.dcx.create_err(errors::ResolveRelativePath {
12081208
span,
12091209
path: parse_sess.source_map().filename_for_diagnostics(&other).to_string(),
1210-
}
1211-
.into_diagnostic(&parse_sess.dcx));
1210+
}));
12121211
}
12131212
};
12141213
result.pop();

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::errors::{
55
use crate::infer::error_reporting::TypeErrCtxt;
66
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
77
use crate::infer::InferCtxt;
8-
use rustc_errors::IntoDiagnostic;
98
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed, IntoDiagnosticArg};
109
use rustc_hir as hir;
1110
use rustc_hir::def::Res;
@@ -367,36 +366,33 @@ impl<'tcx> InferCtxt<'tcx> {
367366
let multi_suggestions = Vec::new();
368367
let bad_label = Some(arg_data.make_bad_error(span));
369368
match error_code {
370-
TypeAnnotationNeeded::E0282 => AnnotationRequired {
369+
TypeAnnotationNeeded::E0282 => self.tcx.sess.dcx().create_err(AnnotationRequired {
371370
span,
372371
source_kind,
373372
source_name,
374373
failure_span,
375374
infer_subdiags,
376375
multi_suggestions,
377376
bad_label,
378-
}
379-
.into_diagnostic(self.tcx.sess.dcx()),
380-
TypeAnnotationNeeded::E0283 => AmbiguousImpl {
377+
}),
378+
TypeAnnotationNeeded::E0283 => self.tcx.sess.dcx().create_err(AmbiguousImpl {
381379
span,
382380
source_kind,
383381
source_name,
384382
failure_span,
385383
infer_subdiags,
386384
multi_suggestions,
387385
bad_label,
388-
}
389-
.into_diagnostic(self.tcx.sess.dcx()),
390-
TypeAnnotationNeeded::E0284 => AmbiguousReturn {
386+
}),
387+
TypeAnnotationNeeded::E0284 => self.tcx.sess.dcx().create_err(AmbiguousReturn {
391388
span,
392389
source_kind,
393390
source_name,
394391
failure_span,
395392
infer_subdiags,
396393
multi_suggestions,
397394
bad_label,
398-
}
399-
.into_diagnostic(self.tcx.sess.dcx()),
395+
}),
400396
}
401397
}
402398
}
@@ -574,36 +570,33 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
574570
}
575571
}
576572
match error_code {
577-
TypeAnnotationNeeded::E0282 => AnnotationRequired {
573+
TypeAnnotationNeeded::E0282 => self.tcx.sess.dcx().create_err(AnnotationRequired {
578574
span,
579575
source_kind,
580576
source_name: &name,
581577
failure_span,
582578
infer_subdiags,
583579
multi_suggestions,
584580
bad_label: None,
585-
}
586-
.into_diagnostic(self.tcx.sess.dcx()),
587-
TypeAnnotationNeeded::E0283 => AmbiguousImpl {
581+
}),
582+
TypeAnnotationNeeded::E0283 => self.tcx.sess.dcx().create_err(AmbiguousImpl {
588583
span,
589584
source_kind,
590585
source_name: &name,
591586
failure_span,
592587
infer_subdiags,
593588
multi_suggestions,
594589
bad_label: None,
595-
}
596-
.into_diagnostic(self.tcx.sess.dcx()),
597-
TypeAnnotationNeeded::E0284 => AmbiguousReturn {
590+
}),
591+
TypeAnnotationNeeded::E0284 => self.tcx.sess.dcx().create_err(AmbiguousReturn {
598592
span,
599593
source_kind,
600594
source_name: &name,
601595
failure_span,
602596
infer_subdiags,
603597
multi_suggestions,
604598
bad_label: None,
605-
}
606-
.into_diagnostic(self.tcx.sess.dcx()),
599+
}),
607600
}
608601
}
609602
}

compiler/rustc_infer/src/infer/error_reporting/note.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ use crate::errors::{
55
use crate::fluent_generated as fluent;
66
use crate::infer::error_reporting::{note_and_explain_region, TypeErrCtxt};
77
use crate::infer::{self, SubregionOrigin};
8-
use rustc_errors::{
9-
AddToDiagnostic, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, IntoDiagnostic,
10-
};
8+
use rustc_errors::{AddToDiagnostic, Diagnostic, DiagnosticBuilder, ErrorGuaranteed};
119
use rustc_hir::def_id::{DefId, LocalDefId};
1210
use rustc_middle::traits::ObligationCauseCode;
1311
use rustc_middle::ty::error::TypeError;
@@ -136,11 +134,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
136134
note_and_explain::PrefixKind::ContentValidFor,
137135
note_and_explain::SuffixKind::Empty,
138136
);
139-
OutlivesContent {
137+
self.tcx.sess.dcx().create_err(OutlivesContent {
140138
span,
141139
notes: reference_valid.into_iter().chain(content_valid).collect(),
142-
}
143-
.into_diagnostic(self.tcx.sess.dcx())
140+
})
144141
}
145142
infer::RelateObjectBound(span) => {
146143
let object_valid = note_and_explain::RegionExplanation::new(
@@ -157,11 +154,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
157154
note_and_explain::PrefixKind::SourcePointerValidFor,
158155
note_and_explain::SuffixKind::Empty,
159156
);
160-
OutlivesBound {
157+
self.tcx.sess.dcx().create_err(OutlivesBound {
161158
span,
162159
notes: object_valid.into_iter().chain(pointer_valid).collect(),
163-
}
164-
.into_diagnostic(self.tcx.sess.dcx())
160+
})
165161
}
166162
infer::RelateParamBound(span, ty, opt_span) => {
167163
let prefix = match *sub {
@@ -176,8 +172,11 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
176172
let note = note_and_explain::RegionExplanation::new(
177173
self.tcx, sub, opt_span, prefix, suffix,
178174
);
179-
FulfillReqLifetime { span, ty: self.resolve_vars_if_possible(ty), note }
180-
.into_diagnostic(self.tcx.sess.dcx())
175+
self.tcx.sess.dcx().create_err(FulfillReqLifetime {
176+
span,
177+
ty: self.resolve_vars_if_possible(ty),
178+
note,
179+
})
181180
}
182181
infer::RelateRegionParamBound(span) => {
183182
let param_instantiated = note_and_explain::RegionExplanation::new(
@@ -194,11 +193,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
194193
note_and_explain::PrefixKind::LfParamMustOutlive,
195194
note_and_explain::SuffixKind::Empty,
196195
);
197-
LfBoundNotSatisfied {
196+
self.tcx.sess.dcx().create_err(LfBoundNotSatisfied {
198197
span,
199198
notes: param_instantiated.into_iter().chain(param_must_outlive).collect(),
200-
}
201-
.into_diagnostic(self.tcx.sess.dcx())
199+
})
202200
}
203201
infer::ReferenceOutlivesReferent(ty, span) => {
204202
let pointer_valid = note_and_explain::RegionExplanation::new(
@@ -215,12 +213,11 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
215213
note_and_explain::PrefixKind::DataValidFor,
216214
note_and_explain::SuffixKind::Empty,
217215
);
218-
RefLongerThanData {
216+
self.tcx.sess.dcx().create_err(RefLongerThanData {
219217
span,
220218
ty: self.resolve_vars_if_possible(ty),
221219
notes: pointer_valid.into_iter().chain(data_valid).collect(),
222-
}
223-
.into_diagnostic(self.tcx.sess.dcx())
220+
})
224221
}
225222
infer::CompareImplItemObligation { span, impl_item_def_id, trait_item_def_id } => {
226223
let mut err = self.report_extra_impl_obligation(
@@ -277,11 +274,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
277274
note_and_explain::PrefixKind::LfMustOutlive,
278275
note_and_explain::SuffixKind::Empty,
279276
);
280-
LfBoundNotSatisfied {
277+
self.tcx.sess.dcx().create_err(LfBoundNotSatisfied {
281278
span,
282279
notes: instantiated.into_iter().chain(must_outlive).collect(),
283-
}
284-
.into_diagnostic(self.tcx.sess.dcx())
280+
})
285281
}
286282
};
287283
if sub.is_error() || sup.is_error() {

compiler/rustc_parse/src/parser/attr.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::{AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, PathStyle
55
use rustc_ast as ast;
66
use rustc_ast::attr;
77
use rustc_ast::token::{self, Delimiter, Nonterminal};
8-
use rustc_errors::{error_code, Diagnostic, IntoDiagnostic, PResult};
8+
use rustc_errors::{error_code, Diagnostic, PResult};
99
use rustc_span::{sym, BytePos, Span};
1010
use thin_vec::ThinVec;
1111
use tracing::debug;
@@ -416,8 +416,9 @@ impl<'a> Parser<'a> {
416416
Err(err) => err.cancel(),
417417
}
418418

419-
Err(InvalidMetaItem { span: self.token.span, token: self.token.clone() }
420-
.into_diagnostic(self.dcx()))
419+
Err(self
420+
.dcx()
421+
.create_err(InvalidMetaItem { span: self.token.span, token: self.token.clone() }))
421422
}
422423
}
423424

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use rustc_ast_pretty::pprust;
3535
use rustc_data_structures::fx::FxHashSet;
3636
use rustc_errors::{
3737
pluralize, AddToDiagnostic, Applicability, DiagCtxt, Diagnostic, DiagnosticBuilder,
38-
DiagnosticMessage, ErrorGuaranteed, FatalError, IntoDiagnostic, MultiSpan, PResult,
38+
DiagnosticMessage, ErrorGuaranteed, FatalError, MultiSpan, PResult,
3939
};
4040
use rustc_session::errors::ExprParenthesesNeeded;
4141
use rustc_span::source_map::Spanned;
@@ -280,11 +280,10 @@ impl<'a> Parser<'a> {
280280
recover: bool,
281281
) -> PResult<'a, (Ident, /* is_raw */ bool)> {
282282
if let TokenKind::DocComment(..) = self.prev_token.kind {
283-
return Err(DocCommentDoesNotDocumentAnything {
283+
return Err(self.dcx().create_err(DocCommentDoesNotDocumentAnything {
284284
span: self.prev_token.span,
285285
missing_comma: None,
286-
}
287-
.into_diagnostic(self.dcx()));
286+
}));
288287
}
289288

290289
let valid_follow = &[
@@ -347,7 +346,7 @@ impl<'a> Parser<'a> {
347346
suggest_remove_comma,
348347
help_cannot_start_number,
349348
};
350-
let mut err = err.into_diagnostic(self.dcx());
349+
let mut err = self.dcx().create_err(err);
351350

352351
// if the token we have is a `<`
353352
// it *might* be a misplaced generic
@@ -1410,7 +1409,7 @@ impl<'a> Parser<'a> {
14101409
// Not entirely sure now, but we bubble the error up with the
14111410
// suggestion.
14121411
self.restore_snapshot(snapshot);
1413-
Err(err.into_diagnostic(self.dcx()))
1412+
Err(self.dcx().create_err(err))
14141413
}
14151414
}
14161415
} else if token::OpenDelim(Delimiter::Parenthesis) == self.token.kind {
@@ -1425,7 +1424,7 @@ impl<'a> Parser<'a> {
14251424
}
14261425
// Consume the fn call arguments.
14271426
match self.consume_fn_args() {
1428-
Err(()) => Err(err.into_diagnostic(self.dcx())),
1427+
Err(()) => Err(self.dcx().create_err(err)),
14291428
Ok(()) => {
14301429
self.sess.emit_err(err);
14311430
// FIXME: actually check that the two expressions in the binop are
@@ -1451,7 +1450,7 @@ impl<'a> Parser<'a> {
14511450
mk_err_expr(self, inner_op.span.to(self.prev_token.span))
14521451
} else {
14531452
// These cases cause too many knock-down errors, bail out (#61329).
1454-
Err(err.into_diagnostic(self.dcx()))
1453+
Err(self.dcx().create_err(err))
14551454
}
14561455
};
14571456
}
@@ -2539,7 +2538,7 @@ impl<'a> Parser<'a> {
25392538
Ok(Some(GenericArg::Const(self.parse_const_arg()?)))
25402539
} else {
25412540
let after_kw_const = self.token.span;
2542-
self.recover_const_arg(after_kw_const, err.into_diagnostic(self.dcx())).map(Some)
2541+
self.recover_const_arg(after_kw_const, self.dcx().create_err(err)).map(Some)
25432542
}
25442543
}
25452544

@@ -2893,11 +2892,10 @@ impl<'a> Parser<'a> {
28932892
let (a_span, b_span) = (a.span(), b.span());
28942893
let between_span = a_span.shrink_to_hi().to(b_span.shrink_to_lo());
28952894
if self.span_to_snippet(between_span).as_deref() == Ok(":: ") {
2896-
return Err(DoubleColonInBound {
2895+
return Err(self.dcx().create_err(DoubleColonInBound {
28972896
span: path.span.shrink_to_hi(),
28982897
between: between_span,
2899-
}
2900-
.into_diagnostic(self.dcx()));
2898+
}));
29012899
}
29022900
}
29032901
}

0 commit comments

Comments
 (0)