1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
14 use rustc::infer::{InferOk};
15 use rustc::traits::ObligationCause;
18 use syntax_pos::{self, Span};
20 use rustc::hir::def::Def;
21 use rustc::ty::{self, AssociatedItem};
22 use errors::DiagnosticBuilder;
24 use super::method::probe;
26 impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
27 // Requires that the two types unify, and prints an error message if
29 pub fn demand_suptype(&self, sp: Span, expected: Ty<'tcx>, actual: Ty<'tcx>) {
30 let cause = self.misc(sp);
31 match self.sub_types(false, &cause, actual, expected) {
32 Ok(InferOk { obligations, value: () }) => {
33 self.register_predicates(obligations);
36 self.report_mismatched_types(&cause, expected, actual, e).emit();
41 pub fn demand_eqtype(&self, sp: Span, expected: Ty<'tcx>, actual: Ty<'tcx>) {
42 if let Some(mut err) = self.demand_eqtype_diag(sp, expected, actual) {
47 pub fn demand_eqtype_diag(&self,
50 actual: Ty<'tcx>) -> Option<DiagnosticBuilder<'tcx>> {
51 self.demand_eqtype_with_origin(&self.misc(sp), expected, actual)
54 pub fn demand_eqtype_with_origin(&self,
55 cause: &ObligationCause<'tcx>,
57 actual: Ty<'tcx>) -> Option<DiagnosticBuilder<'tcx>> {
58 match self.eq_types(false, cause, actual, expected) {
59 Ok(InferOk { obligations, value: () }) => {
60 self.register_predicates(obligations);
64 Some(self.report_mismatched_types(cause, expected, actual, e))
69 // Checks that the type of `expr` can be coerced to `expected`.
71 // NB: This code relies on `self.diverges` to be accurate. In
72 // particular, assignments to `!` will be permitted if the
73 // diverges flag is currently "always".
74 pub fn demand_coerce(&self,
78 let expected = self.resolve_type_vars_with_obligations(expected);
80 if let Err(e) = self.try_coerce(expr, checked_ty, self.diverges.get(), expected) {
81 let cause = self.misc(expr.span);
82 let expr_ty = self.resolve_type_vars_with_obligations(checked_ty);
83 let mode = probe::Mode::MethodCall;
84 let suggestions = self.probe_for_return_type(syntax_pos::DUMMY_SP,
89 let mut err = self.report_mismatched_types(&cause, expected, expr_ty, e);
90 if suggestions.len() > 0 {
91 err.help(&format!("here are some functions which \
92 might fulfill your needs:\n{}",
93 self.get_best_match(&suggestions).join("\n")));
99 fn format_method_suggestion(&self, method: &AssociatedItem) -> String {
102 if self.has_no_input_arg(method) {
109 fn display_suggested_methods(&self, methods: &[AssociatedItem]) -> Vec<String> {
112 .map(|method| self.format_method_suggestion(&*method))
113 .collect::<Vec<String>>()
116 fn get_best_match(&self, methods: &[AssociatedItem]) -> Vec<String> {
117 let no_argument_methods: Vec<_> =
119 .filter(|ref x| self.has_no_input_arg(&*x))
122 if no_argument_methods.len() > 0 {
123 self.display_suggested_methods(&no_argument_methods)
125 self.display_suggested_methods(&methods)
129 // This function checks if the method isn't static and takes other arguments than `self`.
130 fn has_no_input_arg(&self, method: &AssociatedItem) -> bool {
132 Def::Method(def_id) => {
133 match self.tcx.item_type(def_id).sty {
134 ty::TypeVariants::TyFnDef(_, _, sig) => {
135 sig.inputs().skip_binder().len() == 1