Skip to content

Commit 2271c26

Browse files
committed
Auto merge of #119146 - nnethercote:rm-DiagCtxt-api-duplication, r=compiler-errors
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 e4c626d + 8a9db25 commit 2271c26

File tree

347 files changed

+2334
-2696
lines changed

Some content is hidden

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

347 files changed

+2334
-2696
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

@@ -902,7 +901,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
902901
Some(hir::CoroutineKind::Coroutine)
903902
| Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _))
904903
| None => {
905-
return hir::ExprKind::Err(self.tcx.sess.emit_err(AwaitOnlyInAsyncFnAndBlocks {
904+
return hir::ExprKind::Err(self.dcx().emit_err(AwaitOnlyInAsyncFnAndBlocks {
906905
await_kw_span,
907906
item_span: self.current_item,
908907
}));
@@ -1129,7 +1128,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11291128
match coroutine_kind {
11301129
Some(hir::CoroutineKind::Coroutine) => {
11311130
if decl.inputs.len() > 1 {
1132-
self.tcx.sess.emit_err(CoroutineTooManyParameters { fn_decl_span });
1131+
self.dcx().emit_err(CoroutineTooManyParameters { fn_decl_span });
11331132
}
11341133
Some(movability)
11351134
}
@@ -1142,7 +1141,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11421141
}
11431142
None => {
11441143
if movability == Movability::Static {
1145-
self.tcx.sess.emit_err(ClosureCannotBeStatic { fn_decl_span });
1144+
self.dcx().emit_err(ClosureCannotBeStatic { fn_decl_span });
11461145
}
11471146
None
11481147
}
@@ -1181,7 +1180,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11811180
};
11821181

11831182
if let &ClosureBinder::For { span, .. } = binder {
1184-
self.tcx.sess.emit_err(NotSupportedForLifetimeBinderAsyncClosure { span });
1183+
self.dcx().emit_err(NotSupportedForLifetimeBinderAsyncClosure { span });
11851184
}
11861185

11871186
let (binder_clause, generic_params) = self.lower_closure_binder(binder);
@@ -1192,7 +1191,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11921191
let body = self.with_new_scopes(fn_decl_span, |this| {
11931192
// FIXME(cramertj): allow `async` non-`move` closures with arguments.
11941193
if capture_clause == CaptureBy::Ref && !decl.inputs.is_empty() {
1195-
this.tcx.sess.emit_err(AsyncNonMoveClosureNotSupported { fn_decl_span });
1194+
this.dcx().emit_err(AsyncNonMoveClosureNotSupported { fn_decl_span });
11961195
}
11971196

11981197
// Transform `async |x: u8| -> X { ... }` into
@@ -1448,7 +1447,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
14481447
);
14491448
let fields_omitted = match &se.rest {
14501449
StructRest::Base(e) => {
1451-
self.tcx.sess.emit_err(FunctionalRecordUpdateDestructuringAssignment {
1450+
self.dcx().emit_err(FunctionalRecordUpdateDestructuringAssignment {
14521451
span: e.span,
14531452
});
14541453
true
@@ -1544,7 +1543,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
15441543
(None, Some(..), Closed) => hir::LangItem::RangeToInclusive,
15451544
(Some(..), Some(..), Closed) => unreachable!(),
15461545
(start, None, Closed) => {
1547-
self.tcx.sess.emit_err(InclusiveRangeWithNoEnd { span });
1546+
self.dcx().emit_err(InclusiveRangeWithNoEnd { span });
15481547
match start {
15491548
Some(..) => hir::LangItem::RangeFrom,
15501549
None => hir::LangItem::RangeFull,
@@ -1653,7 +1652,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
16531652
Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, _)) => true,
16541653
Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _)) => {
16551654
return hir::ExprKind::Err(
1656-
self.tcx.sess.emit_err(AsyncCoroutinesNotSupported { span }),
1655+
self.dcx().emit_err(AsyncCoroutinesNotSupported { span }),
16571656
);
16581657
}
16591658
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
);
@@ -879,7 +879,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
879879
&ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
880880
|this| match ty {
881881
None => {
882-
let guar = this.tcx.sess.span_delayed_bug(
882+
let guar = this.dcx().span_delayed_bug(
883883
i.span,
884884
"expected to lower associated type, but it was missing",
885885
);
@@ -1012,7 +1012,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10121012
fn lower_block_expr_opt(&mut self, span: Span, block: Option<&Block>) -> hir::Expr<'hir> {
10131013
match block {
10141014
Some(block) => self.lower_block_expr(block),
1015-
None => self.expr_err(span, self.tcx.sess.span_delayed_bug(span, "no block")),
1015+
None => self.expr_err(span, self.dcx().span_delayed_bug(span, "no block")),
10161016
}
10171017
}
10181018

@@ -1022,7 +1022,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10221022
&[],
10231023
match expr {
10241024
Some(expr) => this.lower_expr_mut(expr),
1025-
None => this.expr_err(span, this.tcx.sess.span_delayed_bug(span, "no block")),
1025+
None => this.expr_err(span, this.dcx().span_delayed_bug(span, "no block")),
10261026
},
10271027
)
10281028
})
@@ -1296,7 +1296,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12961296
.map(|s| Symbol::intern(s))
12971297
.collect::<Vec<_>>();
12981298
let suggested_name = find_best_match_for_name(&abi_names, abi.symbol_unescaped, None);
1299-
self.tcx.sess.emit_err(InvalidAbi {
1299+
self.dcx().emit_err(InvalidAbi {
13001300
abi: abi.symbol_unescaped,
13011301
span: abi.span,
13021302
explain: match err {
@@ -1383,7 +1383,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13831383
}
13841384
let is_param = *is_param.get_or_insert_with(compute_is_param);
13851385
if !is_param {
1386-
self.tcx.sess.emit_err(MisplacedRelaxTraitBound { span: bound.span() });
1386+
self.dcx().emit_err(MisplacedRelaxTraitBound { span: bound.span() });
13871387
}
13881388
}
13891389
}

0 commit comments

Comments
 (0)