Skip to content

Commit 4dec6bb

Browse files
committed
Avoid an Option that is always Some
1 parent 61c4b7f commit 4dec6bb

File tree

3 files changed

+22
-23
lines changed

3 files changed

+22
-23
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ pub(crate) fn check_generic_arg_count(
610610
explicit_late_bound,
611611
correct: lifetimes_correct
612612
.and(args_correct)
613-
.map_err(|reported| GenericArgCountMismatch { reported: Some(reported), invalid_args }),
613+
.map_err(|reported| GenericArgCountMismatch { reported, invalid_args }),
614614
}
615615
}
616616

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,9 @@ pub(crate) enum GenericArgPosition {
215215

216216
/// A marker denoting that the generic arguments that were
217217
/// provided did not match the respective generic parameters.
218-
#[derive(Clone, Default, Debug)]
218+
#[derive(Clone, Debug)]
219219
pub struct GenericArgCountMismatch {
220-
/// Indicates whether a fatal error was reported (`Some`), or just a lint (`None`).
221-
pub reported: Option<ErrorGuaranteed>,
220+
pub reported: ErrorGuaranteed,
222221
/// A list of spans of arguments provided that were not valid.
223222
pub invalid_args: Vec<Span>,
224223
}
@@ -404,10 +403,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
404403
self_ty.is_some(),
405404
);
406405

407-
if let Err(err) = &arg_count.correct
408-
&& let Some(reported) = err.reported
409-
{
410-
self.set_tainted_by_errors(reported);
406+
if let Err(err) = &arg_count.correct {
407+
self.set_tainted_by_errors(err.reported);
411408
}
412409

413410
// Skip processing if type has no generic parameters.
@@ -584,13 +581,12 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
584581
&& generics.has_self
585582
&& !tcx.has_attr(def_id, sym::const_trait)
586583
{
587-
let e = tcx.dcx().emit_err(crate::errors::ConstBoundForNonConstTrait {
584+
let reported = tcx.dcx().emit_err(crate::errors::ConstBoundForNonConstTrait {
588585
span,
589586
modifier: constness.as_str(),
590587
});
591-
self.set_tainted_by_errors(e);
592-
arg_count.correct =
593-
Err(GenericArgCountMismatch { reported: Some(e), invalid_args: vec![] });
588+
self.set_tainted_by_errors(reported);
589+
arg_count.correct = Err(GenericArgCountMismatch { reported, invalid_args: vec![] });
594590
}
595591
let args = lower_generic_args(
596592
tcx,

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11181118
// to add defaults. If the user provided *too many* types, that's
11191119
// a problem.
11201120

1121-
let mut infer_args_for_err = FxHashSet::default();
1121+
let mut infer_args_for_err = None;
11221122

11231123
let mut explicit_late_bound = ExplicitLateBound::No;
11241124
for &GenericPathSegment(def_id, index) in &generic_segments {
@@ -1136,9 +1136,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11361136
explicit_late_bound = ExplicitLateBound::Yes;
11371137
}
11381138

1139-
if let Err(GenericArgCountMismatch { reported: Some(e), .. }) = arg_count.correct {
1140-
infer_args_for_err.insert(index);
1141-
self.set_tainted_by_errors(e); // See issue #53251.
1139+
if let Err(GenericArgCountMismatch { reported, .. }) = arg_count.correct {
1140+
infer_args_for_err
1141+
.get_or_insert_with(|| (reported, FxHashSet::default()))
1142+
.1
1143+
.insert(index);
1144+
self.set_tainted_by_errors(reported); // See issue #53251.
11421145
}
11431146
}
11441147

@@ -1232,15 +1235,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12321235
};
12331236
let def_id = res.def_id();
12341237

1235-
let arg_count = GenericArgCountResult {
1236-
explicit_late_bound,
1237-
correct: if infer_args_for_err.is_empty() {
1238-
Ok(())
1239-
} else {
1240-
Err(GenericArgCountMismatch::default())
1241-
},
1238+
let (correct, infer_args_for_err) = match infer_args_for_err {
1239+
Some((reported, args)) => {
1240+
(Err(GenericArgCountMismatch { reported, invalid_args: vec![] }), args)
1241+
}
1242+
None => (Ok(()), Default::default()),
12421243
};
12431244

1245+
let arg_count = GenericArgCountResult { explicit_late_bound, correct };
1246+
12441247
struct CtorGenericArgsCtxt<'a, 'tcx> {
12451248
fcx: &'a FnCtxt<'a, 'tcx>,
12461249
span: Span,

0 commit comments

Comments
 (0)