]> git.lizzy.rs Git - rust.git/commitdiff
Make return value of `check_generic_arg_count` semantically clearer
authorvarkor <github@varkor.com>
Thu, 23 Jan 2020 00:41:33 +0000 (00:41 +0000)
committervarkor <github@varkor.com>
Sat, 22 Feb 2020 00:28:49 +0000 (00:28 +0000)
src/librustc/ty/mod.rs
src/librustc/ty/structural_impls.rs
src/librustc_typeck/astconv.rs
src/librustc_typeck/check/method/confirm.rs
src/librustc_typeck/check/mod.rs

index 54c0a267fe7b2cbdaf688ddb34b262f21237b9f5..7df1383f86addad33a3759287668c30167c6d6dc 100644 (file)
@@ -941,7 +941,7 @@ pub fn descr(&self) -> &'static str {
     }
 }
 
-#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
+#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
 pub struct GenericParamDef {
     pub name: Symbol,
     pub def_id: DefId,
index 388afc15c855973e71dc1af03750b8c0f3488635..03ff1b8a3171f6b5af9a9ddc154f1bda741bbcc7 100644 (file)
 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| {
index a590a942e4274e46f7e623ba8ad5441a2427c930..b3131c159a68d0cbb2707fa73eccfea6645c5a59 100644 (file)
@@ -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,
@@ -262,7 +266,7 @@ pub fn check_generic_arg_count_for_call(
         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(
@@ -287,7 +291,7 @@ fn check_generic_arg_count(
         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.
@@ -443,7 +447,7 @@ fn check_generic_arg_count(
             );
         }
 
-        (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
@@ -495,7 +499,7 @@ pub fn create_substs_for_generic_args<'b>(
         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(
@@ -589,7 +593,7 @@ pub fn create_substs_for_generic_args<'b>(
                                 // 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());
                                 }
 
@@ -615,7 +619,7 @@ pub fn create_substs_for_generic_args<'b>(
                         //  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 =
@@ -706,7 +710,7 @@ fn create_substs_for_ast_path<'a>(
             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,
@@ -739,7 +743,7 @@ fn create_substs_for_ast_path<'a>(
             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 {
index e3fde9159cc38257993f2941dd70faa7cdcbc012..38773abeef28c5380ee04c31389c088f7ec9c473 100644 (file)
@@ -299,7 +299,7 @@ fn instantiate_method_substs(
         // 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`
         );
 
@@ -313,7 +313,7 @@ fn instantiate_method_substs(
             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.
index 989f98682f93e31d16205d43217afce3686054ef..426ba0ddaaa4f04e5d3dc903876ebf767f8d8ab2 100644 (file)
@@ -87,7 +87,7 @@
 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;
@@ -5454,7 +5454,8 @@ pub fn instantiate_value_path(
             // 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.
@@ -5520,7 +5521,7 @@ pub fn instantiate_value_path(
                 &[][..],
                 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)) =