Skip to content

Commit dff64eb

Browse files
committed
Make return value of check_generic_arg_count semantically clearer
1 parent c9b7b1f commit dff64eb

File tree

5 files changed

+19
-20
lines changed

5 files changed

+19
-20
lines changed

src/librustc/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,7 @@ impl GenericParamDefKind {
941941
}
942942
}
943943

944-
#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
944+
#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
945945
pub struct GenericParamDef {
946946
pub name: Symbol,
947947
pub def_id: DefId,

src/librustc/ty/structural_impls.rs

-6
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ use std::fmt;
1717
use std::rc::Rc;
1818
use std::sync::Arc;
1919

20-
impl fmt::Debug for ty::GenericParamDef {
21-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22-
write!(f, "{}({}, {:?}, {})", self.kind.descr(), self.name, self.def_id, self.index)
23-
}
24-
}
25-
2620
impl fmt::Debug for ty::TraitDef {
2721
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2822
ty::tls::with(|tcx| {

src/librustc_typeck/astconv.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ enum GenericArgPosition {
132132
MethodCall,
133133
}
134134

135+
/// A marker denoting that the generic arguments that were
136+
/// provided did not match the respective generic parameters.
137+
pub struct GenericArgCountMismatch;
138+
135139
impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
136140
pub fn ast_region_to_region(
137141
&self,
@@ -262,7 +266,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
262266
def: &ty::Generics,
263267
seg: &hir::PathSegment<'_>,
264268
is_method_call: bool,
265-
) -> bool {
269+
) -> Result<(), GenericArgCountMismatch> {
266270
let empty_args = hir::GenericArgs::none();
267271
let suppress_mismatch = Self::check_impl_trait(tcx, seg, &def);
268272
Self::check_generic_arg_count(
@@ -287,7 +291,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
287291
position: GenericArgPosition,
288292
has_self: bool,
289293
infer_args: bool,
290-
) -> (bool, Vec<Span>) {
294+
) -> (Result<(), GenericArgCountMismatch>, Vec<Span>) {
291295
// At this stage we are guaranteed that the generic arguments are in the correct order, e.g.
292296
// that lifetimes will proceed types. So it suffices to check the number of each generic
293297
// arguments in order to validate them with respect to the generic parameters.
@@ -443,7 +447,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
443447
);
444448
}
445449

446-
(arg_count_mismatch, unexpected_spans)
450+
(if arg_count_mismatch { Err(GenericArgCountMismatch) } else { Ok(()) }, unexpected_spans)
447451
}
448452

449453
/// Report an error that a generic argument did not match the generic parameter that was
@@ -495,7 +499,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
495499
parent_substs: &[subst::GenericArg<'tcx>],
496500
has_self: bool,
497501
self_ty: Option<Ty<'tcx>>,
498-
arg_count_mismatch: bool,
502+
arg_count_correct: Result<(), GenericArgCountMismatch>,
499503
args_for_def_id: impl Fn(DefId) -> (Option<&'b GenericArgs<'b>>, bool),
500504
provided_kind: impl Fn(&GenericParamDef, &GenericArg<'_>) -> subst::GenericArg<'tcx>,
501505
mut inferred_kind: impl FnMut(
@@ -589,7 +593,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
589593
// another. This is an error. However, if we already know that
590594
// the arguments don't match up with the parameters, we won't issue
591595
// an additional error, as the user already knows what's wrong.
592-
if !arg_count_mismatch {
596+
if arg_count_correct.is_ok() {
593597
Self::generic_arg_mismatch_err(tcx.sess, arg, kind.descr());
594598
}
595599

@@ -615,7 +619,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
615619
// 2. We've inferred some lifetimes, which have been provided later (i.e.
616620
// after a type or const). We want to throw an error in this case.
617621

618-
if !arg_count_mismatch {
622+
if arg_count_correct.is_ok() {
619623
let kind = arg.descr();
620624
assert_eq!(kind, "lifetime");
621625
let provided =
@@ -706,7 +710,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
706710
assert!(self_ty.is_none() && parent_substs.is_empty());
707711
}
708712

709-
let (arg_count_mismatch, potential_assoc_types) = Self::check_generic_arg_count(
713+
let (arg_count_correct, potential_assoc_types) = Self::check_generic_arg_count(
710714
tcx,
711715
span,
712716
&generic_params,
@@ -739,7 +743,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
739743
parent_substs,
740744
self_ty.is_some(),
741745
self_ty,
742-
arg_count_mismatch,
746+
arg_count_correct,
743747
// Provide the generic args, and whether types should be inferred.
744748
|did| {
745749
if did == def_id {

src/librustc_typeck/check/method/confirm.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
299299
// If they were not explicitly supplied, just construct fresh
300300
// variables.
301301
let generics = self.tcx.generics_of(pick.item.def_id);
302-
let arg_count_mismatch = AstConv::check_generic_arg_count_for_call(
302+
let arg_count_correct = AstConv::check_generic_arg_count_for_call(
303303
self.tcx, self.span, &generics, &seg, true, // `is_method_call`
304304
);
305305

@@ -313,7 +313,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
313313
parent_substs,
314314
false,
315315
None,
316-
arg_count_mismatch,
316+
arg_count_correct,
317317
// Provide the generic args, and whether types should be inferred.
318318
|def_id| {
319319
// The last component of the returned tuple here is unimportant.

src/librustc_typeck/check/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ mod upvar;
8787
mod wfcheck;
8888
pub mod writeback;
8989

90-
use crate::astconv::{AstConv, PathSeg};
90+
use crate::astconv::{AstConv, GenericArgCountMismatch, PathSeg};
9191
use crate::middle::lang_items;
9292
use rustc::hir::map::blocks::FnLikeNode;
9393
use rustc::hir::map::Map;
@@ -5454,7 +5454,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
54545454
// checking here.
54555455
let suppress_errors = AstConv::check_generic_arg_count_for_call(
54565456
tcx, span, &generics, &seg, false, // `is_method_call`
5457-
);
5457+
)
5458+
.is_err();
54585459
if suppress_errors {
54595460
infer_args_for_err.insert(index);
54605461
self.set_tainted_by_errors(); // See issue #53251.
@@ -5520,7 +5521,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
55205521
&[][..],
55215522
has_self,
55225523
self_ty,
5523-
!infer_args_for_err.is_empty(),
5524+
if infer_args_for_err.is_empty() { Ok(()) } else { Err(GenericArgCountMismatch) },
55245525
// Provide the generic args, and whether types should be inferred.
55255526
|def_id| {
55265527
if let Some(&PathSeg(_, index)) =

0 commit comments

Comments
 (0)