Skip to content

Commit

Permalink
Make return value of check_generic_arg_count semantically clearer
Browse files Browse the repository at this point in the history
  • Loading branch information
varkor committed Feb 22, 2020
1 parent c9b7b1f commit dff64eb
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/librustc/ty/mod.rs
Expand Up @@ -941,7 +941,7 @@ impl GenericParamDefKind {
}
}

#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
pub struct GenericParamDef {
pub name: Symbol,
pub def_id: DefId,
Expand Down
6 changes: 0 additions & 6 deletions src/librustc/ty/structural_impls.rs
Expand Up @@ -17,12 +17,6 @@ use std::fmt;
use std::rc::Rc;
use std::sync::Arc;

impl fmt::Debug for ty::GenericParamDef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}({}, {:?}, {})", self.kind.descr(), self.name, self.def_id, self.index)
}
}

impl fmt::Debug for ty::TraitDef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
ty::tls::with(|tcx| {
Expand Down
20 changes: 12 additions & 8 deletions src/librustc_typeck/astconv.rs
Expand Up @@ -132,6 +132,10 @@ enum GenericArgPosition {
MethodCall,
}

/// A marker denoting that the generic arguments that were
/// provided did not match the respective generic parameters.
pub struct GenericArgCountMismatch;

impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
pub fn ast_region_to_region(
&self,
Expand Down Expand Up @@ -262,7 +266,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
def: &ty::Generics,
seg: &hir::PathSegment<'_>,
is_method_call: bool,
) -> bool {
) -> Result<(), GenericArgCountMismatch> {
let empty_args = hir::GenericArgs::none();
let suppress_mismatch = Self::check_impl_trait(tcx, seg, &def);
Self::check_generic_arg_count(
Expand All @@ -287,7 +291,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
position: GenericArgPosition,
has_self: bool,
infer_args: bool,
) -> (bool, Vec<Span>) {
) -> (Result<(), GenericArgCountMismatch>, Vec<Span>) {
// At this stage we are guaranteed that the generic arguments are in the correct order, e.g.
// that lifetimes will proceed types. So it suffices to check the number of each generic
// arguments in order to validate them with respect to the generic parameters.
Expand Down Expand Up @@ -443,7 +447,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
);
}

(arg_count_mismatch, unexpected_spans)
(if arg_count_mismatch { Err(GenericArgCountMismatch) } else { Ok(()) }, unexpected_spans)
}

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

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

if !arg_count_mismatch {
if arg_count_correct.is_ok() {
let kind = arg.descr();
assert_eq!(kind, "lifetime");
let provided =
Expand Down Expand Up @@ -706,7 +710,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
assert!(self_ty.is_none() && parent_substs.is_empty());
}

let (arg_count_mismatch, potential_assoc_types) = Self::check_generic_arg_count(
let (arg_count_correct, potential_assoc_types) = Self::check_generic_arg_count(
tcx,
span,
&generic_params,
Expand Down Expand Up @@ -739,7 +743,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
parent_substs,
self_ty.is_some(),
self_ty,
arg_count_mismatch,
arg_count_correct,
// Provide the generic args, and whether types should be inferred.
|did| {
if did == def_id {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/check/method/confirm.rs
Expand Up @@ -299,7 +299,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
// If they were not explicitly supplied, just construct fresh
// variables.
let generics = self.tcx.generics_of(pick.item.def_id);
let arg_count_mismatch = AstConv::check_generic_arg_count_for_call(
let arg_count_correct = AstConv::check_generic_arg_count_for_call(
self.tcx, self.span, &generics, &seg, true, // `is_method_call`
);

Expand All @@ -313,7 +313,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
parent_substs,
false,
None,
arg_count_mismatch,
arg_count_correct,
// Provide the generic args, and whether types should be inferred.
|def_id| {
// The last component of the returned tuple here is unimportant.
Expand Down
7 changes: 4 additions & 3 deletions src/librustc_typeck/check/mod.rs
Expand Up @@ -87,7 +87,7 @@ mod upvar;
mod wfcheck;
pub mod writeback;

use crate::astconv::{AstConv, PathSeg};
use crate::astconv::{AstConv, GenericArgCountMismatch, PathSeg};
use crate::middle::lang_items;
use rustc::hir::map::blocks::FnLikeNode;
use rustc::hir::map::Map;
Expand Down Expand Up @@ -5454,7 +5454,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// checking here.
let suppress_errors = AstConv::check_generic_arg_count_for_call(
tcx, span, &generics, &seg, false, // `is_method_call`
);
)
.is_err();
if suppress_errors {
infer_args_for_err.insert(index);
self.set_tainted_by_errors(); // See issue #53251.
Expand Down Expand Up @@ -5520,7 +5521,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&[][..],
has_self,
self_ty,
!infer_args_for_err.is_empty(),
if infer_args_for_err.is_empty() { Ok(()) } else { Err(GenericArgCountMismatch) },
// Provide the generic args, and whether types should be inferred.
|def_id| {
if let Some(&PathSeg(_, index)) =
Expand Down

0 comments on commit dff64eb

Please sign in to comment.