about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_typeck/src/check/compare_method.rs66
1 files changed, 66 insertions, 0 deletions
diff --git a/compiler/rustc_typeck/src/check/compare_method.rs b/compiler/rustc_typeck/src/check/compare_method.rs
index 12d0c14a3d5..d3586888155 100644
--- a/compiler/rustc_typeck/src/check/compare_method.rs
+++ b/compiler/rustc_typeck/src/check/compare_method.rs
@@ -66,6 +66,10 @@ crate fn compare_impl_method<'tcx>(
     {
         return;
     }
+
+    if let Err(ErrorReported) = compare_const_param_types(tcx, impl_m, trait_m, trait_item_span) {
+        return;
+    }
 }
 
 fn compare_predicate_entailment<'tcx>(
@@ -929,6 +933,68 @@ fn compare_synthetic_generics<'tcx>(
     if error_found { Err(ErrorReported) } else { Ok(()) }
 }
 
+fn compare_const_param_types<'tcx>(
+    tcx: TyCtxt<'tcx>,
+    impl_m: &ty::AssocItem,
+    trait_m: &ty::AssocItem,
+    trait_item_span: Option<Span>,
+) -> Result<(), ErrorReported> {
+    let const_params_of = |def_id| {
+        tcx.generics_of(def_id).params.iter().filter_map(|param| match param.kind {
+            GenericParamDefKind::Const { .. } => Some(param.def_id),
+            _ => None,
+        })
+    };
+    let const_params_impl = const_params_of(impl_m.def_id);
+    let const_params_trait = const_params_of(trait_m.def_id);
+
+    for (const_param_impl, const_param_trait) in iter::zip(const_params_impl, const_params_trait) {
+        let impl_ty = tcx.type_of(const_param_impl);
+        let trait_ty = tcx.type_of(const_param_trait);
+        if impl_ty != trait_ty {
+            let (impl_span, impl_ident) = match tcx.hir().get_if_local(const_param_impl) {
+                Some(hir::Node::GenericParam(hir::GenericParam { span, name, .. })) => (
+                    span,
+                    match name {
+                        hir::ParamName::Plain(ident) => Some(ident),
+                        _ => None,
+                    },
+                ),
+                other => bug!(
+                    "expected GenericParam, found {:?}",
+                    other.map_or_else(|| "nothing".to_string(), |n| format!("{:?}", n))
+                ),
+            };
+            let trait_span = match tcx.hir().get_if_local(const_param_trait) {
+                Some(hir::Node::GenericParam(hir::GenericParam { span, .. })) => Some(span),
+                _ => None,
+            };
+            let mut err = struct_span_err!(
+                tcx.sess,
+                *impl_span,
+                E0053,
+                "method `{}` has an incompatible const parameter type for trait",
+                trait_m.ident
+            );
+            err.span_note(
+                trait_span.map_or_else(|| trait_item_span.unwrap_or(*impl_span), |span| *span),
+                &format!(
+                    "the const parameter{} has type `{}`, but the declaration \
+                              in trait `{}` has type `{}`",
+                    &impl_ident.map_or_else(|| "".to_string(), |ident| format!(" `{}`", ident)),
+                    impl_ty,
+                    tcx.def_path_str(trait_m.def_id),
+                    trait_ty
+                ),
+            );
+            err.emit();
+            return Err(ErrorReported);
+        }
+    }
+
+    Ok(())
+}
+
 crate fn compare_const_impl<'tcx>(
     tcx: TyCtxt<'tcx>,
     impl_c: &ty::AssocItem,