Skip to content

Commit 917190f

Browse files
committed
Auto merge of #119146 - nnethercote:rm-DiagCtxt-api-duplication, r=<try>
Remove `DiagCtxt` API duplication `DiagCtxt` defines the internal API for creating and emitting diagnostics: methods like `struct_err`, `struct_span_warn`, `note`, `create_fatal`, `emit_bug`. There are over 50 methods. Some of these methods are then duplicated across several other types: `Session`, `ParseSess`, `Parser`, `ExtCtxt`, and `MirBorrowckCtxt`. `Session` duplicates the most, though half the ones it does are unused. Each duplicated method just calls forward to the corresponding method in `DiagCtxt`. So this duplication exists to (in the best case) shorten chains like `ecx.tcx.sess.parse_sess.dcx.emit_err()` to `ecx.emit_err()`. This API duplication is ugly and has been bugging me for a while. And it's inconsistent: there's no real logic about which methods are duplicated, and the use of `#[rustc_lint_diagnostic]` and `#[track_caller]` attributes vary across the duplicates. This PR removes the duplicated API methods and makes all diagnostic creation and emission go through `DiagCtxt`. It also adds `dcx` getter methods to several types to shorten chains. This approach scales *much* better than API duplication; indeed, the PR adds `dcx()` to numerous types that didn't have API duplication: `TyCtxt`, `LoweringCtxt`, `ConstCx`, `FnCtxt`, `TypeErrCtxt`, `InferCtxt`, `CrateLoader`, `CheckAttrVisitor`, and `Resolver`. These result in a lot of changes from `foo.tcx.sess.emit_err()` to `foo.dcx().emit_err()`. (You could do this with more types, but it gets into diminishing returns territory for types that don't emit many diagnostics.) After all these changes, some call sites are more verbose, some are less verbose, and many are the same. The total number of lines is reduced, mostly because of the removed API duplication. And consistency is increased, because calls to `emit_err` and friends are always preceded with `.dcx()` or `.dcx`. r? `@compiler-errors`
2 parents 3095d31 + 8a1a0de commit 917190f

File tree

345 files changed

+2289
-2668
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

345 files changed

+2289
-2668
lines changed

compiler/rustc_ast_lowering/src/asm.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
3232
let asm_arch =
3333
if self.tcx.sess.opts.actually_rustdoc { None } else { self.tcx.sess.asm_arch };
3434
if asm_arch.is_none() && !self.tcx.sess.opts.actually_rustdoc {
35-
self.tcx.sess.emit_err(InlineAsmUnsupportedTarget { span: sp });
35+
self.dcx().emit_err(InlineAsmUnsupportedTarget { span: sp });
3636
}
3737
if let Some(asm_arch) = asm_arch {
3838
// Inline assembly is currently only stable for these architectures.
@@ -60,7 +60,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
6060
&& !matches!(asm_arch, Some(asm::InlineAsmArch::X86 | asm::InlineAsmArch::X86_64))
6161
&& !self.tcx.sess.opts.actually_rustdoc
6262
{
63-
self.tcx.sess.emit_err(AttSyntaxOnlyX86 { span: sp });
63+
self.dcx().emit_err(AttSyntaxOnlyX86 { span: sp });
6464
}
6565
if asm.options.contains(InlineAsmOptions::MAY_UNWIND) && !self.tcx.features().asm_unwind {
6666
feature_err(
@@ -87,7 +87,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
8787
!= source_map.span_to_snippet(*abi_span))
8888
.then_some(());
8989

90-
self.tcx.sess.emit_err(AbiSpecifiedMultipleTimes {
90+
self.dcx().emit_err(AbiSpecifiedMultipleTimes {
9191
abi_span: *abi_span,
9292
prev_name: *prev_name,
9393
prev_span: *prev_sp,
@@ -100,14 +100,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
100100
}
101101
}
102102
Err(&[]) => {
103-
self.tcx.sess.emit_err(ClobberAbiNotSupported { abi_span: *abi_span });
103+
self.dcx().emit_err(ClobberAbiNotSupported { abi_span: *abi_span });
104104
}
105105
Err(supported_abis) => {
106106
let mut abis = format!("`{}`", supported_abis[0]);
107107
for m in &supported_abis[1..] {
108108
let _ = write!(abis, ", `{m}`");
109109
}
110-
self.tcx.sess.emit_err(InvalidAbiClobberAbi {
110+
self.dcx().emit_err(InvalidAbiClobberAbi {
111111
abi_span: *abi_span,
112112
supported_abis: abis,
113113
});
@@ -128,7 +128,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
128128
InlineAsmRegOrRegClass::Reg(reg) => {
129129
asm::InlineAsmRegOrRegClass::Reg(if let Some(asm_arch) = asm_arch {
130130
asm::InlineAsmReg::parse(asm_arch, reg).unwrap_or_else(|error| {
131-
sess.emit_err(InvalidRegister { op_span: *op_sp, reg, error });
131+
self.dcx().emit_err(InvalidRegister {
132+
op_span: *op_sp,
133+
reg,
134+
error,
135+
});
132136
asm::InlineAsmReg::Err
133137
})
134138
} else {
@@ -139,7 +143,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
139143
asm::InlineAsmRegOrRegClass::RegClass(if let Some(asm_arch) = asm_arch {
140144
asm::InlineAsmRegClass::parse(asm_arch, reg_class).unwrap_or_else(
141145
|error| {
142-
sess.emit_err(InvalidRegisterClass {
146+
self.dcx().emit_err(InvalidRegisterClass {
143147
op_span: *op_sp,
144148
reg_class,
145149
error,
@@ -276,22 +280,22 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
276280
class_name: class.name(),
277281
}
278282
};
279-
sess.emit_err(InvalidAsmTemplateModifierRegClass {
283+
self.dcx().emit_err(InvalidAsmTemplateModifierRegClass {
280284
placeholder_span,
281285
op_span: op_sp,
282286
sub,
283287
});
284288
}
285289
}
286290
hir::InlineAsmOperand::Const { .. } => {
287-
sess.emit_err(InvalidAsmTemplateModifierConst {
291+
self.dcx().emit_err(InvalidAsmTemplateModifierConst {
288292
placeholder_span,
289293
op_span: op_sp,
290294
});
291295
}
292296
hir::InlineAsmOperand::SymFn { .. }
293297
| hir::InlineAsmOperand::SymStatic { .. } => {
294-
sess.emit_err(InvalidAsmTemplateModifierSym {
298+
self.dcx().emit_err(InvalidAsmTemplateModifierSym {
295299
placeholder_span,
296300
op_span: op_sp,
297301
});
@@ -315,7 +319,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
315319
// require that the operand name an explicit register, not a
316320
// register class.
317321
if reg_class.is_clobber_only(asm_arch.unwrap()) && !op.is_clobber() {
318-
sess.emit_err(RegisterClassOnlyClobber {
322+
self.dcx().emit_err(RegisterClassOnlyClobber {
319323
op_span: op_sp,
320324
reg_class_name: reg_class.name(),
321325
});
@@ -384,7 +388,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
384388
}
385389
};
386390

387-
sess.emit_err(RegisterConflict {
391+
self.dcx().emit_err(RegisterConflict {
388392
op_span1: op_sp,
389393
op_span2: op_sp2,
390394
reg1_name: reg_str(idx),

compiler/rustc_ast_lowering/src/expr.rs

+16-17
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
249249
self.lower_expr_range(e.span, e1.as_deref(), e2.as_deref(), *lims)
250250
}
251251
ExprKind::Underscore => {
252-
let guar = self.tcx.sess.emit_err(UnderscoreExprLhsAssign { span: e.span });
252+
let guar = self.dcx().emit_err(UnderscoreExprLhsAssign { span: e.span });
253253
hir::ExprKind::Err(guar)
254254
}
255255
ExprKind::Path(qself, path) => {
@@ -294,8 +294,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
294294
let rest = match &se.rest {
295295
StructRest::Base(e) => Some(self.lower_expr(e)),
296296
StructRest::Rest(sp) => {
297-
let guar =
298-
self.tcx.sess.emit_err(BaseExpressionDoubleDot { span: *sp });
297+
let guar = self.dcx().emit_err(BaseExpressionDoubleDot { span: *sp });
299298
Some(&*self.arena.alloc(self.expr_err(*sp, guar)))
300299
}
301300
StructRest::None => None,
@@ -332,9 +331,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
332331
|this| this.with_new_scopes(e.span, |this| this.lower_block_expr(block)),
333332
),
334333
ExprKind::Yield(opt_expr) => self.lower_expr_yield(e.span, opt_expr.as_deref()),
335-
ExprKind::Err => hir::ExprKind::Err(
336-
self.tcx.sess.span_delayed_bug(e.span, "lowered ExprKind::Err"),
337-
),
334+
ExprKind::Err => {
335+
hir::ExprKind::Err(self.dcx().span_delayed_bug(e.span, "lowered ExprKind::Err"))
336+
}
338337
ExprKind::Try(sub_expr) => self.lower_expr_try(e.span, sub_expr),
339338

340339
ExprKind::Paren(_) | ExprKind::ForLoop(..) => {
@@ -584,13 +583,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
584583
if self.tcx.features().never_patterns {
585584
// If the feature is off we already emitted the error after parsing.
586585
let suggestion = span.shrink_to_hi();
587-
self.tcx.sess.emit_err(MatchArmWithNoBody { span, suggestion });
586+
self.dcx().emit_err(MatchArmWithNoBody { span, suggestion });
588587
}
589588
} else if let Some(body) = &arm.body {
590-
self.tcx.sess.emit_err(NeverPatternWithBody { span: body.span });
589+
self.dcx().emit_err(NeverPatternWithBody { span: body.span });
591590
guard = None;
592591
} else if let Some(g) = &arm.guard {
593-
self.tcx.sess.emit_err(NeverPatternWithGuard { span: g.span });
592+
self.dcx().emit_err(NeverPatternWithGuard { span: g.span });
594593
guard = None;
595594
}
596595

@@ -880,7 +879,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
880879
Some(hir::CoroutineKind::Async(_)) => false,
881880
Some(hir::CoroutineKind::AsyncGen(_)) => true,
882881
Some(hir::CoroutineKind::Coroutine) | Some(hir::CoroutineKind::Gen(_)) | None => {
883-
return hir::ExprKind::Err(self.tcx.sess.emit_err(AwaitOnlyInAsyncFnAndBlocks {
882+
return hir::ExprKind::Err(self.dcx().emit_err(AwaitOnlyInAsyncFnAndBlocks {
884883
await_kw_span,
885884
item_span: self.current_item,
886885
}));
@@ -1092,7 +1091,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10921091
match coroutine_kind {
10931092
Some(hir::CoroutineKind::Coroutine) => {
10941093
if decl.inputs.len() > 1 {
1095-
self.tcx.sess.emit_err(CoroutineTooManyParameters { fn_decl_span });
1094+
self.dcx().emit_err(CoroutineTooManyParameters { fn_decl_span });
10961095
}
10971096
Some(movability)
10981097
}
@@ -1105,7 +1104,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11051104
}
11061105
None => {
11071106
if movability == Movability::Static {
1108-
self.tcx.sess.emit_err(ClosureCannotBeStatic { fn_decl_span });
1107+
self.dcx().emit_err(ClosureCannotBeStatic { fn_decl_span });
11091108
}
11101109
None
11111110
}
@@ -1144,7 +1143,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11441143
};
11451144

11461145
if let &ClosureBinder::For { span, .. } = binder {
1147-
self.tcx.sess.emit_err(NotSupportedForLifetimeBinderAsyncClosure { span });
1146+
self.dcx().emit_err(NotSupportedForLifetimeBinderAsyncClosure { span });
11481147
}
11491148

11501149
let (binder_clause, generic_params) = self.lower_closure_binder(binder);
@@ -1155,7 +1154,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11551154
let body = self.with_new_scopes(fn_decl_span, |this| {
11561155
// FIXME(cramertj): allow `async` non-`move` closures with arguments.
11571156
if capture_clause == CaptureBy::Ref && !decl.inputs.is_empty() {
1158-
this.tcx.sess.emit_err(AsyncNonMoveClosureNotSupported { fn_decl_span });
1157+
this.dcx().emit_err(AsyncNonMoveClosureNotSupported { fn_decl_span });
11591158
}
11601159

11611160
// Transform `async |x: u8| -> X { ... }` into
@@ -1411,7 +1410,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
14111410
);
14121411
let fields_omitted = match &se.rest {
14131412
StructRest::Base(e) => {
1414-
self.tcx.sess.emit_err(FunctionalRecordUpdateDestructuringAssignment {
1413+
self.dcx().emit_err(FunctionalRecordUpdateDestructuringAssignment {
14151414
span: e.span,
14161415
});
14171416
true
@@ -1507,7 +1506,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
15071506
(None, Some(..), Closed) => hir::LangItem::RangeToInclusive,
15081507
(Some(..), Some(..), Closed) => unreachable!(),
15091508
(start, None, Closed) => {
1510-
self.tcx.sess.emit_err(InclusiveRangeWithNoEnd { span });
1509+
self.dcx().emit_err(InclusiveRangeWithNoEnd { span });
15111510
match start {
15121511
Some(..) => hir::LangItem::RangeFrom,
15131512
None => hir::LangItem::RangeFull,
@@ -1616,7 +1615,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
16161615
Some(hir::CoroutineKind::AsyncGen(_)) => true,
16171616
Some(hir::CoroutineKind::Async(_)) => {
16181617
return hir::ExprKind::Err(
1619-
self.tcx.sess.emit_err(AsyncCoroutinesNotSupported { span }),
1618+
self.dcx().emit_err(AsyncCoroutinesNotSupported { span }),
16201619
);
16211620
}
16221621
Some(hir::CoroutineKind::Coroutine) | None => {

compiler/rustc_ast_lowering/src/format.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ fn make_count<'hir>(
267267
ctx.expr(
268268
sp,
269269
hir::ExprKind::Err(
270-
ctx.tcx.sess.span_delayed_bug(sp, "lowered bad format_args count"),
270+
ctx.dcx().span_delayed_bug(sp, "lowered bad format_args count"),
271271
),
272272
)
273273
}
@@ -306,7 +306,7 @@ fn make_format_spec<'hir>(
306306
}
307307
Err(_) => ctx.expr(
308308
sp,
309-
hir::ExprKind::Err(ctx.tcx.sess.span_delayed_bug(sp, "lowered bad format_args count")),
309+
hir::ExprKind::Err(ctx.dcx().span_delayed_bug(sp, "lowered bad format_args count")),
310310
),
311311
};
312312
let &FormatOptions {

compiler/rustc_ast_lowering/src/item.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
265265
&ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
266266
|this| match ty {
267267
None => {
268-
let guar = this.tcx.sess.span_delayed_bug(
268+
let guar = this.dcx().span_delayed_bug(
269269
span,
270270
"expected to lower type alias type, but it was missing",
271271
);
@@ -878,7 +878,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
878878
&ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
879879
|this| match ty {
880880
None => {
881-
let guar = this.tcx.sess.span_delayed_bug(
881+
let guar = this.dcx().span_delayed_bug(
882882
i.span,
883883
"expected to lower associated type, but it was missing",
884884
);
@@ -1011,7 +1011,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10111011
fn lower_block_expr_opt(&mut self, span: Span, block: Option<&Block>) -> hir::Expr<'hir> {
10121012
match block {
10131013
Some(block) => self.lower_block_expr(block),
1014-
None => self.expr_err(span, self.tcx.sess.span_delayed_bug(span, "no block")),
1014+
None => self.expr_err(span, self.dcx().span_delayed_bug(span, "no block")),
10151015
}
10161016
}
10171017

@@ -1021,7 +1021,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10211021
&[],
10221022
match expr {
10231023
Some(expr) => this.lower_expr_mut(expr),
1024-
None => this.expr_err(span, this.tcx.sess.span_delayed_bug(span, "no block")),
1024+
None => this.expr_err(span, this.dcx().span_delayed_bug(span, "no block")),
10251025
},
10261026
)
10271027
})
@@ -1295,7 +1295,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12951295
.map(|s| Symbol::intern(s))
12961296
.collect::<Vec<_>>();
12971297
let suggested_name = find_best_match_for_name(&abi_names, abi.symbol_unescaped, None);
1298-
self.tcx.sess.emit_err(InvalidAbi {
1298+
self.dcx().emit_err(InvalidAbi {
12991299
abi: abi.symbol_unescaped,
13001300
span: abi.span,
13011301
explain: match err {
@@ -1376,7 +1376,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13761376
}
13771377
let is_param = *is_param.get_or_insert_with(compute_is_param);
13781378
if !is_param {
1379-
self.tcx.sess.emit_err(MisplacedRelaxTraitBound { span: bound.span() });
1379+
self.dcx().emit_err(MisplacedRelaxTraitBound { span: bound.span() });
13801380
}
13811381
}
13821382
}

0 commit comments

Comments
 (0)