Skip to content

Commit f487d83

Browse files
Stop doing so much to handle subdiagnostics
1 parent bc1f1ef commit f487d83

File tree

2 files changed

+69
-137
lines changed

2 files changed

+69
-137
lines changed

compiler/rustc_hir_analysis/src/coherence/orphan.rs

+68-94
Original file line numberDiff line numberDiff line change
@@ -283,15 +283,14 @@ fn emit_orphan_check_error<'tcx>(
283283
let self_ty = trait_ref.self_ty();
284284
Err(match err {
285285
traits::OrphanCheckErr::NonLocalInputType(tys) => {
286-
// FIXME: Someone needs to just turn these into `Subdiag`s and attach
287-
// them to the `Diag` after creating the error.
288-
let mut opaque = vec![];
289-
let mut foreign = vec![];
290-
let mut name = vec![];
291-
let mut pointer = vec![];
292-
let mut ty_diag = vec![];
293-
let mut adt = vec![];
294-
let mut sugg = None;
286+
let mut diag = tcx.dcx().create_err(match self_ty.kind() {
287+
ty::Adt(..) => errors::OnlyCurrentTraits::Outside { span: sp, note: () },
288+
_ if self_ty.is_primitive() => {
289+
errors::OnlyCurrentTraits::Primitive { span: sp, note: () }
290+
}
291+
_ => errors::OnlyCurrentTraits::Arbitrary { span: sp, note: () },
292+
});
293+
295294
for &(mut ty, is_target_ty) in &tys {
296295
let span = if matches!(is_target_ty, IsFirstInputType::Yes) {
297296
// Point at `D<A>` in `impl<A, B> for C<B> in D<A>`
@@ -303,110 +302,85 @@ fn emit_orphan_check_error<'tcx>(
303302

304303
ty = tcx.erase_regions(ty);
305304

306-
fn push_to_foreign_or_name<'tcx>(
307-
is_foreign: bool,
308-
foreign: &mut Vec<errors::OnlyCurrentTraitsForeign>,
309-
name: &mut Vec<errors::OnlyCurrentTraitsName<'tcx>>,
310-
span: Span,
311-
sname: &'tcx str,
312-
) {
313-
if is_foreign {
314-
foreign.push(errors::OnlyCurrentTraitsForeign { span })
315-
} else {
316-
name.push(errors::OnlyCurrentTraitsName { span, name: sname });
317-
}
318-
}
319-
320305
let is_foreign =
321306
!trait_ref.def_id.is_local() && matches!(is_target_ty, IsFirstInputType::No);
322307

323308
match *ty.kind() {
324309
ty::Slice(_) => {
325-
push_to_foreign_or_name(
326-
is_foreign,
327-
&mut foreign,
328-
&mut name,
329-
span,
330-
"slices",
331-
);
310+
if is_foreign {
311+
diag.subdiagnostic(
312+
tcx.dcx(),
313+
errors::OnlyCurrentTraitsForeign { span },
314+
);
315+
} else {
316+
diag.subdiagnostic(
317+
tcx.dcx(),
318+
errors::OnlyCurrentTraitsName { span, name: "slices" },
319+
);
320+
}
332321
}
333322
ty::Array(..) => {
334-
push_to_foreign_or_name(
335-
is_foreign,
336-
&mut foreign,
337-
&mut name,
338-
span,
339-
"arrays",
340-
);
323+
if is_foreign {
324+
diag.subdiagnostic(
325+
tcx.dcx(),
326+
errors::OnlyCurrentTraitsForeign { span },
327+
);
328+
} else {
329+
diag.subdiagnostic(
330+
tcx.dcx(),
331+
errors::OnlyCurrentTraitsName { span, name: "arrays" },
332+
);
333+
}
341334
}
342335
ty::Tuple(..) => {
343-
push_to_foreign_or_name(
344-
is_foreign,
345-
&mut foreign,
346-
&mut name,
347-
span,
348-
"tuples",
349-
);
336+
if is_foreign {
337+
diag.subdiagnostic(
338+
tcx.dcx(),
339+
errors::OnlyCurrentTraitsForeign { span },
340+
);
341+
} else {
342+
diag.subdiagnostic(
343+
tcx.dcx(),
344+
errors::OnlyCurrentTraitsName { span, name: "tuples" },
345+
);
346+
}
350347
}
351348
ty::Alias(ty::Opaque, ..) => {
352-
opaque.push(errors::OnlyCurrentTraitsOpaque { span })
349+
diag.subdiagnostic(tcx.dcx(), errors::OnlyCurrentTraitsOpaque { span });
353350
}
354351
ty::RawPtr(ptr_ty, mutbl) => {
355352
if !self_ty.has_param() {
356-
let mut_key = mutbl.prefix_str();
357-
sugg = Some(errors::OnlyCurrentTraitsPointerSugg {
358-
wrapper_span: self_ty_span,
359-
struct_span: full_impl_span.shrink_to_lo(),
360-
mut_key,
361-
ptr_ty,
362-
});
353+
diag.subdiagnostic(
354+
tcx.dcx(),
355+
errors::OnlyCurrentTraitsPointerSugg {
356+
wrapper_span: self_ty_span,
357+
struct_span: full_impl_span.shrink_to_lo(),
358+
mut_key: mutbl.prefix_str(),
359+
ptr_ty,
360+
},
361+
);
363362
}
364-
pointer.push(errors::OnlyCurrentTraitsPointer { span, pointer: ty });
363+
diag.subdiagnostic(
364+
tcx.dcx(),
365+
errors::OnlyCurrentTraitsPointer { span, pointer: ty },
366+
);
367+
}
368+
ty::Adt(adt_def, _) => {
369+
diag.subdiagnostic(
370+
tcx.dcx(),
371+
errors::OnlyCurrentTraitsAdt {
372+
span,
373+
name: tcx.def_path_str(adt_def.did()),
374+
},
375+
);
376+
}
377+
_ => {
378+
diag.subdiagnostic(tcx.dcx(), errors::OnlyCurrentTraitsTy { span, ty });
365379
}
366-
ty::Adt(adt_def, _) => adt.push(errors::OnlyCurrentTraitsAdt {
367-
span,
368-
name: tcx.def_path_str(adt_def.did()),
369-
}),
370-
_ => ty_diag.push(errors::OnlyCurrentTraitsTy { span, ty }),
371380
}
372381
}
373382

374-
let err_struct = match self_ty.kind() {
375-
ty::Adt(..) => errors::OnlyCurrentTraits::Outside {
376-
span: sp,
377-
note: (),
378-
opaque,
379-
foreign,
380-
name,
381-
pointer,
382-
ty: ty_diag,
383-
adt,
384-
sugg,
385-
},
386-
_ if self_ty.is_primitive() => errors::OnlyCurrentTraits::Primitive {
387-
span: sp,
388-
note: (),
389-
opaque,
390-
foreign,
391-
name,
392-
pointer,
393-
ty: ty_diag,
394-
adt,
395-
sugg,
396-
},
397-
_ => errors::OnlyCurrentTraits::Arbitrary {
398-
span: sp,
399-
note: (),
400-
opaque,
401-
foreign,
402-
name,
403-
pointer,
404-
ty: ty_diag,
405-
adt,
406-
sugg,
407-
},
408-
};
409-
tcx.dcx().emit_err(err_struct)
383+
diag.emit()
410384
}
411385
traits::OrphanCheckErr::UncoveredTy(param_ty, local_type) => {
412386
let mut sp = sp;

compiler/rustc_hir_analysis/src/errors.rs

+1-43
Original file line numberDiff line numberDiff line change
@@ -1376,28 +1376,14 @@ pub struct TyParamSome<'a> {
13761376
}
13771377

13781378
#[derive(Diagnostic)]
1379-
pub enum OnlyCurrentTraits<'a> {
1379+
pub enum OnlyCurrentTraits {
13801380
#[diag(hir_analysis_only_current_traits_outside, code = E0117)]
13811381
Outside {
13821382
#[primary_span]
13831383
#[label(hir_analysis_only_current_traits_label)]
13841384
span: Span,
13851385
#[note(hir_analysis_only_current_traits_note)]
13861386
note: (),
1387-
#[subdiagnostic]
1388-
opaque: Vec<OnlyCurrentTraitsOpaque>,
1389-
#[subdiagnostic]
1390-
foreign: Vec<OnlyCurrentTraitsForeign>,
1391-
#[subdiagnostic]
1392-
name: Vec<OnlyCurrentTraitsName<'a>>,
1393-
#[subdiagnostic]
1394-
pointer: Vec<OnlyCurrentTraitsPointer<'a>>,
1395-
#[subdiagnostic]
1396-
ty: Vec<OnlyCurrentTraitsTy<'a>>,
1397-
#[subdiagnostic]
1398-
adt: Vec<OnlyCurrentTraitsAdt>,
1399-
#[subdiagnostic]
1400-
sugg: Option<OnlyCurrentTraitsPointerSugg<'a>>,
14011387
},
14021388
#[diag(hir_analysis_only_current_traits_primitive, code = E0117)]
14031389
Primitive {
@@ -1406,20 +1392,6 @@ pub enum OnlyCurrentTraits<'a> {
14061392
span: Span,
14071393
#[note(hir_analysis_only_current_traits_note)]
14081394
note: (),
1409-
#[subdiagnostic]
1410-
opaque: Vec<OnlyCurrentTraitsOpaque>,
1411-
#[subdiagnostic]
1412-
foreign: Vec<OnlyCurrentTraitsForeign>,
1413-
#[subdiagnostic]
1414-
name: Vec<OnlyCurrentTraitsName<'a>>,
1415-
#[subdiagnostic]
1416-
pointer: Vec<OnlyCurrentTraitsPointer<'a>>,
1417-
#[subdiagnostic]
1418-
ty: Vec<OnlyCurrentTraitsTy<'a>>,
1419-
#[subdiagnostic]
1420-
adt: Vec<OnlyCurrentTraitsAdt>,
1421-
#[subdiagnostic]
1422-
sugg: Option<OnlyCurrentTraitsPointerSugg<'a>>,
14231395
},
14241396
#[diag(hir_analysis_only_current_traits_arbitrary, code = E0117)]
14251397
Arbitrary {
@@ -1428,20 +1400,6 @@ pub enum OnlyCurrentTraits<'a> {
14281400
span: Span,
14291401
#[note(hir_analysis_only_current_traits_note)]
14301402
note: (),
1431-
#[subdiagnostic]
1432-
opaque: Vec<OnlyCurrentTraitsOpaque>,
1433-
#[subdiagnostic]
1434-
foreign: Vec<OnlyCurrentTraitsForeign>,
1435-
#[subdiagnostic]
1436-
name: Vec<OnlyCurrentTraitsName<'a>>,
1437-
#[subdiagnostic]
1438-
pointer: Vec<OnlyCurrentTraitsPointer<'a>>,
1439-
#[subdiagnostic]
1440-
ty: Vec<OnlyCurrentTraitsTy<'a>>,
1441-
#[subdiagnostic]
1442-
adt: Vec<OnlyCurrentTraitsAdt>,
1443-
#[subdiagnostic]
1444-
sugg: Option<OnlyCurrentTraitsPointerSugg<'a>>,
14451403
},
14461404
}
14471405

0 commit comments

Comments
 (0)