]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #43968 - petrochenkov:span2, r=michaelwoerister
authorbors <bors@rust-lang.org>
Wed, 30 Aug 2017 11:08:26 +0000 (11:08 +0000)
committerbors <bors@rust-lang.org>
Wed, 30 Aug 2017 11:08:26 +0000 (11:08 +0000)
Make fields of `Span` private

I actually tried to intern spans and benchmark the result<sup>*</sup>, and this was a prerequisite.
This kind of encapsulation will be a prerequisite for any other attempt to compress span's representation, so I decided to submit this change alone.

The issue https://github.com/rust-lang/rust/issues/43088 seems relevant, but it looks like `SpanId` won't be able to reuse this interface, unless the tables are global (like interner that I tried) and are not a part of HIR.
r? @michaelwoerister anyway

<sup>*</sup> Interning means 2-3 times more space is required for a single span, but duplicates are free. In practice it turned out that duplicates are not *that* common, so more memory was wasted by interning rather than saved.

34 files changed:
src/bootstrap/dist.rs
src/bootstrap/doc.rs
src/bootstrap/flags.rs
src/bootstrap/lib.rs
src/bootstrap/tool.rs
src/libcore/fmt/num.rs
src/libcore/intrinsics.rs
src/libcore/option.rs
src/libcore/str/mod.rs
src/librustc/infer/freshen.rs
src/librustc/traits/select.rs
src/librustc_errors/lib.rs
src/librustc_trans/intrinsic.rs
src/librustc_typeck/check/intrinsic.rs
src/librustc_typeck/check/method/confirm.rs
src/librustc_typeck/check/method/mod.rs
src/librustc_typeck/check/method/probe.rs
src/librustc_typeck/check/method/suggest.rs
src/librustc_typeck/check/mod.rs
src/librustdoc/html/format.rs
src/librustdoc/html/highlight.rs
src/librustdoc/html/render.rs
src/librustdoc/html/static/rustdoc.css
src/libstd/net/addr.rs
src/test/compile-fail/unboxed-closures-infer-explicit-call-too-early.rs [deleted file]
src/test/run-pass/method-argument-inference-associated-type.rs [new file with mode: 0644]
src/test/run-pass/unboxed-closures-infer-explicit-call-early.rs [new file with mode: 0644]
src/test/rustdoc/issue-41783.rs
src/test/ui/impl-trait/no-method-suggested-traits.stderr
src/test/ui/mismatched_types/issue-36053-2.stderr
src/test/ui/suggestions/issue-43420-no-over-suggest.rs [new file with mode: 0644]
src/test/ui/suggestions/issue-43420-no-over-suggest.stderr [new file with mode: 0644]
src/tools/build-manifest/src/main.rs
src/tools/compiletest/src/common.rs

index 153ffe509f3086d531739161e1918fc871e8a6d3..746f85a9d59d67291137c0ce5347a26759fa2cfa 100644 (file)
@@ -724,6 +724,9 @@ fn run(self, builder: &Builder) -> PathBuf {
         let dst_src = dst.join("rust");
         t!(fs::create_dir_all(&dst_src));
 
+        let src_files = [
+            "src/Cargo.lock",
+        ];
         // This is the reduced set of paths which will become the rust-src component
         // (essentially libstd and all of its path dependencies)
         let std_src_dirs = [
@@ -759,6 +762,9 @@ fn run(self, builder: &Builder) -> PathBuf {
         ];
 
         copy_src_dirs(build, &std_src_dirs[..], &std_src_dirs_exclude[..], &dst_src);
+        for file in src_files.iter() {
+            copy(&build.src.join(file), &dst_src.join(file));
+        }
 
         // Create source tarball in rust-installer format
         let mut cmd = rust_installer(builder);
index 86f5346bea1fb1558afd3a07306d563462b5c0a1..b9a52a66793dfc604092e1fcc9ba40aa1c94451e 100644 (file)
@@ -669,11 +669,6 @@ fn run(self, builder: &Builder) {
         let build = builder.build;
         let target = self.target;
 
-        builder.ensure(compile::Rustc {
-            compiler: builder.compiler(0, build.build),
-            target,
-        });
-
         println!("Documenting error index ({})", target);
         let out = build.doc_out(target);
         t!(fs::create_dir_all(&out));
index a84d43d3deedef29b70483cfcfc7e29d36e9d702..7546d7fd4f07af7405c217aa4f718082566d4643 100644 (file)
@@ -136,7 +136,7 @@ pub fn parse(args: &[String]) -> Flags {
             None => {
                 // No subcommand -- show the general usage and subcommand help
                 println!("{}\n", subcommand_help);
-                process::exit(0);
+                process::exit(1);
             }
         };
 
index 55358f2ffcb7334deda2c7e4ecde1180cdd9c7d6..f21b382619d0a7c5144040277273fceb4032ec50 100644 (file)
 extern crate serde_derive;
 #[macro_use]
 extern crate lazy_static;
-extern crate serde;
 extern crate serde_json;
 extern crate cmake;
 extern crate filetime;
@@ -718,7 +717,7 @@ fn python(&self) -> &Path {
     fn force_use_stage1(&self, compiler: Compiler, target: Interned<String>) -> bool {
         !self.config.full_bootstrap &&
             compiler.stage >= 2 &&
-            self.hosts.iter().any(|h| *h == target)
+            (self.hosts.iter().any(|h| *h == target) || target == self.build)
     }
 
     /// Returns the directory that OpenSSL artifacts are compiled into if
index d798e8de3dffae66fff02079891f75b21586fe9b..e759f1a3e6f85c073f532e5955e32ab51cb5eede 100644 (file)
@@ -198,7 +198,7 @@ fn run(self, builder: &Builder) -> PathBuf {
     Linkchecker, "src/tools/linkchecker", "linkchecker", Mode::Libstd;
     CargoTest, "src/tools/cargotest", "cargotest", Mode::Libstd;
     Compiletest, "src/tools/compiletest", "compiletest", Mode::Libtest;
-    BuildManifest, "src/tools/build-manifest", "build-manifest", Mode::Librustc;
+    BuildManifest, "src/tools/build-manifest", "build-manifest", Mode::Libstd;
     RemoteTestClient, "src/tools/remote-test-client", "remote-test-client", Mode::Libstd;
     RustInstaller, "src/tools/rust-installer", "rust-installer", Mode::Libstd;
 );
index 4ca303dee43f2ad600237d82680c0a6ddfa6d0f7..8ea388fddf8848c8a8c4f3c31e75be7cb25bac40 100644 (file)
@@ -242,7 +242,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                 // decode last 1 or 2 chars
                 if n < 10 {
                     curr -= 1;
-                    *buf_ptr.offset(curr) = (n as u8) + 48;
+                    *buf_ptr.offset(curr) = (n as u8) + b'0';
                 } else {
                     let d1 = n << 1;
                     curr -= 2;
index ad776c8605ac83cef3dec2ce8194603fa6f1a5f8..607f6f3701799c3337f0bad75fe5878f17c34fd9 100644 (file)
@@ -1343,4 +1343,81 @@ pub fn volatile_copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T,
     /// on MSVC it's `*mut [usize; 2]`. For more information see the compiler's
     /// source as well as std's catch implementation.
     pub fn try(f: fn(*mut u8), data: *mut u8, local_ptr: *mut u8) -> i32;
+
+    /// Computes the byte offset that needs to be applied to `ptr` in order to
+    /// make it aligned to `align`.
+    /// If it is not possible to align `ptr`, the implementation returns
+    /// `usize::max_value()`.
+    ///
+    /// There are no guarantees whatsover that offsetting the pointer will not
+    /// overflow or go beyond the allocation that `ptr` points into.
+    /// It is up to the caller to ensure that the returned offset is correct
+    /// in all terms other than alignment.
+    ///
+    /// # Examples
+    ///
+    /// Accessing adjacent `u8` as `u16`
+    ///
+    /// ```
+    /// # #![feature(core_intrinsics)]
+    /// # fn foo(n: usize) {
+    /// # use std::intrinsics::align_offset;
+    /// # use std::mem::align_of;
+    /// # unsafe {
+    /// let x = [5u8, 6u8, 7u8, 8u8, 9u8];
+    /// let ptr = &x[n] as *const u8;
+    /// let offset = align_offset(ptr as *const (), align_of::<u16>());
+    /// if offset < x.len() - n - 1 {
+    ///     let u16_ptr = ptr.offset(offset as isize) as *const u16;
+    ///     assert_ne!(*u16_ptr, 500);
+    /// } else {
+    ///     // while the pointer can be aligned via `offset`, it would point
+    ///     // outside the allocation
+    /// }
+    /// # } }
+    /// ```
+    #[cfg(not(stage0))]
+    pub fn align_offset(ptr: *const (), align: usize) -> usize;
+}
+
+#[cfg(stage0)]
+/// Computes the byte offset that needs to be applied to `ptr` in order to
+/// make it aligned to `align`.
+/// If it is not possible to align `ptr`, the implementation returns
+/// `usize::max_value()`.
+///
+/// There are no guarantees whatsover that offsetting the pointer will not
+/// overflow or go beyond the allocation that `ptr` points into.
+/// It is up to the caller to ensure that the returned offset is correct
+/// in all terms other than alignment.
+///
+/// # Examples
+///
+/// Accessing adjacent `u8` as `u16`
+///
+/// ```
+/// # #![feature(core_intrinsics)]
+/// # fn foo(n: usize) {
+/// # use std::intrinsics::align_offset;
+/// # use std::mem::align_of;
+/// # unsafe {
+/// let x = [5u8, 6u8, 7u8, 8u8, 9u8];
+/// let ptr = &x[n] as *const u8;
+/// let offset = align_offset(ptr as *const (), align_of::<u16>());
+/// if offset < x.len() - n - 1 {
+///     let u16_ptr = ptr.offset(offset as isize) as *const u16;
+///     assert_ne!(*u16_ptr, 500);
+/// } else {
+///     // while the pointer can be aligned via `offset`, it would point
+///     // outside the allocation
+/// }
+/// # } }
+/// ```
+pub unsafe fn align_offset(ptr: *const (), align: usize) -> usize {
+    let offset = ptr as usize % align;
+    if offset == 0 {
+        0
+    } else {
+        align - offset
+    }
 }
index aecf2ee9325eee9dde0a13cef3d65e293610c147..138e04c7737e06caf0e876894f2f3b014f9a164b 100644 (file)
@@ -774,6 +774,26 @@ pub fn cloned(self) -> Option<T> {
     }
 }
 
+impl<'a, T: Clone> Option<&'a mut T> {
+    /// Maps an `Option<&mut T>` to an `Option<T>` by cloning the contents of the
+    /// option.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(option_ref_mut_cloned)]
+    /// let mut x = 12;
+    /// let opt_x = Some(&mut x);
+    /// assert_eq!(opt_x, Some(&mut 12));
+    /// let cloned = opt_x.cloned();
+    /// assert_eq!(cloned, Some(12));
+    /// ```
+    #[unstable(feature = "option_ref_mut_cloned", issue = "43738")]
+    pub fn cloned(self) -> Option<T> {
+        self.map(|t| t.clone())
+    }
+}
+
 impl<T: Default> Option<T> {
     /// Returns the contained value or a default
     ///
index a5f6e49a53b4f7cff5238d4b2a85b8edce0c589c..62e84c9ebd0175f5278e644411e5950493fcfd72 100644 (file)
@@ -23,6 +23,7 @@
 use iter::{Map, Cloned, FusedIterator};
 use slice::{self, SliceIndex};
 use mem;
+use intrinsics::align_offset;
 
 pub mod pattern;
 
@@ -1468,7 +1469,10 @@ macro_rules! next { () => {{
             // When the pointer is aligned, read 2 words of data per iteration
             // until we find a word containing a non-ascii byte.
             let ptr = v.as_ptr();
-            let align = (ptr as usize + index) & (usize_bytes - 1);
+            let align = unsafe {
+                // the offset is safe, because `index` is guaranteed inbounds
+                align_offset(ptr.offset(index as isize) as *const (), usize_bytes)
+            };
             if align == 0 {
                 while index < blocks_end {
                     unsafe {
index b8b5a55f57806eaf9cee92cb70e7be71175fc771..c274f8bda9fb054d4bce1434af121dc48cbadabf 100644 (file)
 //! fact an unbound type variable, we want the match to be regarded as ambiguous, because depending
 //! on what type that type variable is ultimately assigned, the match may or may not succeed.
 //!
+//! To handle closures, freshened types also have to contain the signature and kind of any
+//! closure in the local inference context, as otherwise the cache key might be invalidated.
+//! The way this is done is somewhat hacky - the closure signature is appended to the substs,
+//! as well as the closure kind "encoded" as a type. Also, special handling is needed when
+//! the closure signature contains a reference to the original closure.
+//!
 //! Note that you should be careful not to allow the output of freshening to leak to the user in
 //! error messages or in any other form. Freshening is only really useful as an internal detail.
 //!
-//! __An important detail concerning regions.__ The freshener also replaces *all* regions with
+//! Because of the manipulation required to handle closures, doing arbitrary operations on
+//! freshened types is not recommended. However, in addition to doing equality/hash
+//! comparisons (for caching), it is possible to do a `ty::_match` operation between
+//! 2 freshened types - this works even with the closure encoding.
+//!
+//! __An important detail concerning regions.__ The freshener also replaces *all* free regions with
 //! 'erased. The reason behind this is that, in general, we do not take region relationships into
 //! account when making type-overloaded decisions. This is important because of the design of the
 //! region inferencer, which is not based on unification but rather on accumulating and then
 
 use ty::{self, Ty, TyCtxt, TypeFoldable};
 use ty::fold::TypeFolder;
+use ty::subst::Substs;
 use util::nodemap::FxHashMap;
+use hir::def_id::DefId;
+
 use std::collections::hash_map::Entry;
 
 use super::InferCtxt;
@@ -42,6 +56,7 @@ pub struct TypeFreshener<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
     infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
     freshen_count: u32,
     freshen_map: FxHashMap<ty::InferTy, Ty<'tcx>>,
+    closure_set: Vec<DefId>,
 }
 
 impl<'a, 'gcx, 'tcx> TypeFreshener<'a, 'gcx, 'tcx> {
@@ -51,6 +66,7 @@ pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>)
             infcx,
             freshen_count: 0,
             freshen_map: FxHashMap(),
+            closure_set: vec![],
         }
     }
 
@@ -76,6 +92,88 @@ fn freshen<F>(&mut self,
             }
         }
     }
+
+    fn next_fresh<F>(&mut self,
+                     freshener: F)
+                     -> Ty<'tcx>
+        where F: FnOnce(u32) -> ty::InferTy,
+    {
+        let index = self.freshen_count;
+        self.freshen_count += 1;
+        self.infcx.tcx.mk_infer(freshener(index))
+    }
+
+    fn freshen_closure_like<M, C>(&mut self,
+                                  def_id: DefId,
+                                  substs: ty::ClosureSubsts<'tcx>,
+                                  t: Ty<'tcx>,
+                                  markers: M,
+                                  combine: C)
+                                  -> Ty<'tcx>
+        where M: FnOnce(&mut Self) -> (Ty<'tcx>, Ty<'tcx>),
+              C: FnOnce(&'tcx Substs<'tcx>) -> Ty<'tcx>
+    {
+        let tcx = self.infcx.tcx;
+
+        let closure_in_progress = self.infcx.in_progress_tables.map_or(false, |tables| {
+            tcx.hir.as_local_node_id(def_id).map_or(false, |closure_id| {
+                tables.borrow().local_id_root ==
+                    Some(DefId::local(tcx.hir.node_to_hir_id(closure_id).owner))
+            })
+        });
+
+        if !closure_in_progress {
+            // If this closure belongs to another infcx, its kind etc. were
+            // fully inferred and its signature/kind are exactly what's listed
+            // in its infcx. So we don't need to add the markers for them.
+            return t.super_fold_with(self);
+        }
+
+        // We are encoding a closure in progress. Because we want our freshening
+        // key to contain all inference information needed to make sense of our
+        // value, we need to encode the closure signature and kind. The way
+        // we do that is to add them as 2 variables to the closure substs,
+        // basically because it's there (and nobody cares about adding extra stuff
+        // to substs).
+        //
+        // This means the "freshened" closure substs ends up looking like
+        //     fresh_substs = [PARENT_SUBSTS* ; UPVARS* ; SIG_MARKER ; KIND_MARKER]
+        let (marker_1, marker_2) = if self.closure_set.contains(&def_id) {
+            // We found the closure def-id within its own signature. Just
+            // leave a new freshened type - any matching operations would
+            // have found and compared the exterior closure already to
+            // get here.
+            //
+            // In that case, we already know what the signature would
+            // be - the parent closure on the stack already contains a
+            // "copy" of the signature, so there is no reason to encode
+            // it again for injectivity. Just use a fresh type variable
+            // to make everything comparable.
+            //
+            // For example (closure kinds omitted for clarity)
+            //     t=[closure FOO sig=[closure BAR sig=[closure FOO ..]]]
+            // Would get encoded to
+            //     t=[closure FOO sig=[closure BAR sig=[closure FOO sig=$0]]]
+            //
+            // and we can decode by having
+            //     $0=[closure BAR {sig doesn't exist in decode}]
+            // and get
+            //     t=[closure FOO]
+            //     sig[FOO] = [closure BAR]
+            //     sig[BAR] = [closure FOO]
+            (self.next_fresh(ty::FreshTy), self.next_fresh(ty::FreshTy))
+        } else {
+            self.closure_set.push(def_id);
+            let markers = markers(self);
+            self.closure_set.pop();
+            markers
+        };
+
+        combine(tcx.mk_substs(
+            substs.substs.iter().map(|k| k.fold_with(self)).chain(
+                [marker_1, marker_2].iter().cloned().map(From::from)
+                    )))
+    }
 }
 
 impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for TypeFreshener<'a, 'gcx, 'tcx> {
@@ -105,7 +203,8 @@ fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
     }
 
     fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
-        if !t.needs_infer() && !t.has_erasable_regions() {
+        if !t.needs_infer() && !t.has_erasable_regions() &&
+            !(t.has_closure_types() && self.infcx.in_progress_tables.is_some()) {
             return t;
         }
 
@@ -150,6 +249,51 @@ fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
                 t
             }
 
+            ty::TyClosure(def_id, substs) => {
+                self.freshen_closure_like(
+                    def_id, substs, t,
+                    |this| {
+                        // HACK: use a "random" integer type to mark the kind. Because
+                        // different closure kinds shouldn't get unified during
+                        // selection, the "subtyping" relationship (where any kind is
+                        // better than no kind) shouldn't  matter here, just that the
+                        // types are different.
+                        let closure_kind = this.infcx.closure_kind(def_id);
+                        let closure_kind_marker = match closure_kind {
+                            None => tcx.types.i8,
+                            Some(ty::ClosureKind::Fn) => tcx.types.i16,
+                            Some(ty::ClosureKind::FnMut) => tcx.types.i32,
+                            Some(ty::ClosureKind::FnOnce) => tcx.types.i64,
+                        };
+
+                        let closure_sig = this.infcx.fn_sig(def_id);
+                        (tcx.mk_fn_ptr(closure_sig.fold_with(this)),
+                         closure_kind_marker)
+                    },
+                    |substs| tcx.mk_closure(def_id, substs)
+                )
+            }
+
+            ty::TyGenerator(def_id, substs, interior) => {
+                self.freshen_closure_like(
+                    def_id, substs, t,
+                    |this| {
+                        let gen_sig = this.infcx.generator_sig(def_id).unwrap();
+                        // FIXME: want to revise this strategy when generator
+                        // signatures can actually contain LBRs.
+                        let sig = this.tcx().no_late_bound_regions(&gen_sig)
+                            .unwrap_or_else(|| {
+                                bug!("late-bound regions in signature of {:?}",
+                                     def_id)
+                            });
+                        (sig.yield_ty, sig.return_ty).fold_with(this)
+                    },
+                    |substs| {
+                        tcx.mk_generator(def_id, ty::ClosureSubsts { substs }, interior)
+                    }
+                )
+            }
+
             ty::TyBool |
             ty::TyChar |
             ty::TyInt(..) |
@@ -165,8 +309,6 @@ fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
             ty::TyFnDef(..) |
             ty::TyFnPtr(_) |
             ty::TyDynamic(..) |
-            ty::TyClosure(..) |
-            ty::TyGenerator(..) |
             ty::TyNever |
             ty::TyTuple(..) |
             ty::TyProjection(..) |
index 44b8af3c1df980800a6ac51f3ca76b8aacc480ae..4e9398e5058066ee3f19709ffe556590be839b15 100644 (file)
@@ -194,13 +194,12 @@ enum SelectionCandidate<'tcx> {
     ProjectionCandidate,
 
     /// Implementation of a `Fn`-family trait by one of the anonymous types
-    /// generated for a `||` expression. The ty::ClosureKind informs the
-    /// confirmation step what ClosureKind obligation to emit.
-    ClosureCandidate(/* closure */ DefId, ty::ClosureSubsts<'tcx>, ty::ClosureKind),
+    /// generated for a `||` expression.
+    ClosureCandidate,
 
     /// Implementation of a `Generator` trait by one of the anonymous types
     /// generated for a generator.
-    GeneratorCandidate(/* function / closure */ DefId, ty::ClosureSubsts<'tcx>),
+    GeneratorCandidate,
 
     /// Implementation of a `Fn`-family trait by one of the anonymous
     /// types generated for a fn pointer type (e.g., `fn(int)->int`)
@@ -229,20 +228,12 @@ fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lif
             ObjectCandidate => ObjectCandidate,
             BuiltinObjectCandidate => BuiltinObjectCandidate,
             BuiltinUnsizeCandidate => BuiltinUnsizeCandidate,
+            ClosureCandidate => ClosureCandidate,
+            GeneratorCandidate => GeneratorCandidate,
 
             ParamCandidate(ref trait_ref) => {
                 return tcx.lift(trait_ref).map(ParamCandidate);
             }
-            GeneratorCandidate(def_id, ref substs) => {
-                return tcx.lift(substs).map(|substs| {
-                    GeneratorCandidate(def_id, substs)
-                });
-            }
-            ClosureCandidate(def_id, ref substs, kind) => {
-                return tcx.lift(substs).map(|substs| {
-                    ClosureCandidate(def_id, substs, kind)
-                });
-            }
         })
     }
 }
@@ -904,14 +895,9 @@ fn insert_evaluation_cache(&mut self,
                                dep_node: DepNodeIndex,
                                result: EvaluationResult)
     {
-        // Avoid caching results that depend on more than just the trait-ref:
-        // The stack can create recursion, and closure signatures
-        // being yet uninferred can create "spurious" EvaluatedToAmbig
-        // and EvaluatedToOk.
-        if result.is_stack_dependent() ||
-            ((result == EvaluatedToAmbig || result == EvaluatedToOk)
-             && trait_ref.has_closure_types())
-        {
+        // Avoid caching results that depend on more than just the trait-ref
+        // - the stack can create recursion.
+        if result.is_stack_dependent() {
             return;
         }
 
@@ -971,15 +957,12 @@ fn candidate_from_obligation<'o>(&mut self,
             this.candidate_from_obligation_no_cache(stack)
         });
 
-        if self.should_update_candidate_cache(&cache_fresh_trait_pred, &candidate) {
-            debug!("CACHE MISS: SELECT({:?})={:?}",
-                   cache_fresh_trait_pred, candidate);
-            self.insert_candidate_cache(stack.obligation.param_env,
-                                        cache_fresh_trait_pred,
-                                        dep_node,
-                                        candidate.clone());
-        }
-
+        debug!("CACHE MISS: SELECT({:?})={:?}",
+               cache_fresh_trait_pred, candidate);
+        self.insert_candidate_cache(stack.obligation.param_env,
+                                    cache_fresh_trait_pred,
+                                    dep_node,
+                                    candidate.clone());
         candidate
     }
 
@@ -1219,45 +1202,6 @@ fn insert_candidate_cache(&mut self,
                                   .insert(trait_ref, WithDepNode::new(dep_node, candidate));
     }
 
-    fn should_update_candidate_cache(&mut self,
-                                     cache_fresh_trait_pred: &ty::PolyTraitPredicate<'tcx>,
-                                     candidate: &SelectionResult<'tcx, SelectionCandidate<'tcx>>)
-                                     -> bool
-    {
-        // In general, it's a good idea to cache results, even
-        // ambiguous ones, to save us some trouble later. But we have
-        // to be careful not to cache results that could be
-        // invalidated later by advances in inference. Normally, this
-        // is not an issue, because any inference variables whose
-        // types are not yet bound are "freshened" in the cache key,
-        // which means that if we later get the same request once that
-        // type variable IS bound, we'll have a different cache key.
-        // For example, if we have `Vec<_#0t> : Foo`, and `_#0t` is
-        // not yet known, we may cache the result as `None`. But if
-        // later `_#0t` is bound to `Bar`, then when we freshen we'll
-        // have `Vec<Bar> : Foo` as the cache key.
-        //
-        // HOWEVER, it CAN happen that we get an ambiguity result in
-        // one particular case around closures where the cache key
-        // would not change. That is when the precise types of the
-        // upvars that a closure references have not yet been figured
-        // out (i.e., because it is not yet known if they are captured
-        // by ref, and if by ref, what kind of ref). In these cases,
-        // when matching a builtin bound, we will yield back an
-        // ambiguous result. But the *cache key* is just the closure type,
-        // it doesn't capture the state of the upvar computation.
-        //
-        // To avoid this trap, just don't cache ambiguous results if
-        // the self-type contains no inference byproducts (that really
-        // shouldn't happen in other circumstances anyway, given
-        // coherence).
-
-        match *candidate {
-            Ok(Some(_)) | Err(_) => true,
-            Ok(None) => cache_fresh_trait_pred.has_infer_types()
-        }
-    }
-
     fn assemble_candidates<'o>(&mut self,
                                stack: &TraitObligationStack<'o, 'tcx>)
                                -> Result<SelectionCandidateSet<'tcx>, SelectionError<'tcx>>
@@ -1518,23 +1462,22 @@ fn assemble_generator_candidates(&mut self,
         // touch bound regions, they just capture the in-scope
         // type/region parameters
         let self_ty = *obligation.self_ty().skip_binder();
-        let (closure_def_id, substs) = match self_ty.sty {
-            ty::TyGenerator(id, substs, _) => (id, substs),
+        match self_ty.sty {
+            ty::TyGenerator(..) => {
+                debug!("assemble_generator_candidates: self_ty={:?} obligation={:?}",
+                       self_ty,
+                       obligation);
+
+                candidates.vec.push(GeneratorCandidate);
+                Ok(())
+            }
             ty::TyInfer(ty::TyVar(_)) => {
                 debug!("assemble_generator_candidates: ambiguous self-type");
                 candidates.ambiguous = true;
                 return Ok(());
             }
             _ => { return Ok(()); }
-        };
-
-        debug!("assemble_generator_candidates: self_ty={:?} obligation={:?}",
-               self_ty,
-               obligation);
-
-        candidates.vec.push(GeneratorCandidate(closure_def_id, substs));
-
-        Ok(())
+        }
     }
 
     /// Check for the artificial impl that the compiler will create for an obligation like `X :
@@ -1556,36 +1499,31 @@ fn assemble_closure_candidates(&mut self,
         // ok to skip binder because the substs on closure types never
         // touch bound regions, they just capture the in-scope
         // type/region parameters
-        let self_ty = *obligation.self_ty().skip_binder();
-        let (closure_def_id, substs) = match self_ty.sty {
-            ty::TyClosure(id, substs) => (id, substs),
+        match obligation.self_ty().skip_binder().sty {
+            ty::TyClosure(closure_def_id, _) => {
+                debug!("assemble_unboxed_candidates: kind={:?} obligation={:?}",
+                       kind, obligation);
+                match self.infcx.closure_kind(closure_def_id) {
+                    Some(closure_kind) => {
+                        debug!("assemble_unboxed_candidates: closure_kind = {:?}", closure_kind);
+                        if closure_kind.extends(kind) {
+                            candidates.vec.push(ClosureCandidate);
+                        }
+                    }
+                    None => {
+                        debug!("assemble_unboxed_candidates: closure_kind not yet known");
+                        candidates.vec.push(ClosureCandidate);
+                    }
+                };
+                Ok(())
+            }
             ty::TyInfer(ty::TyVar(_)) => {
                 debug!("assemble_unboxed_closure_candidates: ambiguous self-type");
                 candidates.ambiguous = true;
                 return Ok(());
             }
             _ => { return Ok(()); }
-        };
-
-        debug!("assemble_unboxed_candidates: self_ty={:?} kind={:?} obligation={:?}",
-               self_ty,
-               kind,
-               obligation);
-
-        match self.infcx.closure_kind(closure_def_id) {
-            Some(closure_kind) => {
-                debug!("assemble_unboxed_candidates: closure_kind = {:?}", closure_kind);
-                if closure_kind.extends(kind) {
-                    candidates.vec.push(ClosureCandidate(closure_def_id, substs, kind));
-                }
-            }
-            None => {
-                debug!("assemble_unboxed_candidates: closure_kind not yet known");
-                candidates.vec.push(ClosureCandidate(closure_def_id, substs, kind));
-            }
         }
-
-        Ok(())
     }
 
     /// Implement one of the `Fn()` family for a fn pointer.
@@ -1902,8 +1840,8 @@ fn candidate_should_be_dropped_in_favor_of<'o>(
                          when there are other valid candidates");
                 }
                 ImplCandidate(..) |
-                ClosureCandidate(..) |
-                GeneratorCandidate(..) |
+                ClosureCandidate |
+                GeneratorCandidate |
                 FnPointerCandidate |
                 BuiltinObjectCandidate |
                 BuiltinUnsizeCandidate |
@@ -2245,15 +2183,13 @@ fn confirm_candidate(&mut self,
                 Ok(VtableImpl(self.confirm_impl_candidate(obligation, impl_def_id)))
             }
 
-            ClosureCandidate(closure_def_id, substs, kind) => {
-                let vtable_closure =
-                    self.confirm_closure_candidate(obligation, closure_def_id, substs, kind)?;
+            ClosureCandidate => {
+                let vtable_closure = self.confirm_closure_candidate(obligation)?;
                 Ok(VtableClosure(vtable_closure))
             }
 
-            GeneratorCandidate(closure_def_id, substs) => {
-                let vtable_generator =
-                    self.confirm_generator_candidate(obligation, closure_def_id, substs)?;
+            GeneratorCandidate => {
+                let vtable_generator = self.confirm_generator_candidate(obligation)?;
                 Ok(VtableGenerator(vtable_generator))
             }
 
@@ -2590,21 +2526,34 @@ fn confirm_fn_pointer_candidate(&mut self, obligation: &TraitObligation<'tcx>)
     }
 
     fn confirm_generator_candidate(&mut self,
-                                 obligation: &TraitObligation<'tcx>,
-                                 closure_def_id: DefId,
-                                 substs: ty::ClosureSubsts<'tcx>)
-                                 -> Result<VtableGeneratorData<'tcx, PredicateObligation<'tcx>>,
+                                   obligation: &TraitObligation<'tcx>)
+                                   -> Result<VtableGeneratorData<'tcx, PredicateObligation<'tcx>>,
                                            SelectionError<'tcx>>
     {
+        // ok to skip binder because the substs on generator types never
+        // touch bound regions, they just capture the in-scope
+        // type/region parameters
+        let self_ty = self.infcx.shallow_resolve(obligation.self_ty().skip_binder());
+        let (closure_def_id, substs) = match self_ty.sty {
+            ty::TyGenerator(id, substs, _) => (id, substs),
+            _ => bug!("closure candidate for non-closure {:?}", obligation)
+        };
+
         debug!("confirm_generator_candidate({:?},{:?},{:?})",
                obligation,
                closure_def_id,
                substs);
 
+        let trait_ref =
+            self.generator_trait_ref_unnormalized(obligation, closure_def_id, substs);
         let Normalized {
             value: trait_ref,
             obligations
-        } = self.generator_trait_ref(obligation, closure_def_id, substs);
+        } = normalize_with_depth(self,
+                                 obligation.param_env,
+                                 obligation.cause.clone(),
+                                 obligation.recursion_depth+1,
+                                 &trait_ref);
 
         debug!("confirm_generator_candidate(closure_def_id={:?}, trait_ref={:?}, obligations={:?})",
                closure_def_id,
@@ -2624,22 +2573,36 @@ fn confirm_generator_candidate(&mut self,
     }
 
     fn confirm_closure_candidate(&mut self,
-                                 obligation: &TraitObligation<'tcx>,
-                                 closure_def_id: DefId,
-                                 substs: ty::ClosureSubsts<'tcx>,
-                                 kind: ty::ClosureKind)
+                                 obligation: &TraitObligation<'tcx>)
                                  -> Result<VtableClosureData<'tcx, PredicateObligation<'tcx>>,
                                            SelectionError<'tcx>>
     {
-        debug!("confirm_closure_candidate({:?},{:?},{:?})",
-               obligation,
-               closure_def_id,
-               substs);
+        debug!("confirm_closure_candidate({:?})", obligation);
 
+        let kind = match self.tcx().lang_items.fn_trait_kind(obligation.predicate.0.def_id()) {
+            Some(k) => k,
+            None => bug!("closure candidate for non-fn trait {:?}", obligation)
+        };
+
+        // ok to skip binder because the substs on closure types never
+        // touch bound regions, they just capture the in-scope
+        // type/region parameters
+        let self_ty = self.infcx.shallow_resolve(obligation.self_ty().skip_binder());
+        let (closure_def_id, substs) = match self_ty.sty {
+            ty::TyClosure(id, substs) => (id, substs),
+            _ => bug!("closure candidate for non-closure {:?}", obligation)
+        };
+
+        let trait_ref =
+            self.closure_trait_ref_unnormalized(obligation, closure_def_id, substs);
         let Normalized {
             value: trait_ref,
             mut obligations
-        } = self.closure_trait_ref(obligation, closure_def_id, substs);
+        } = normalize_with_depth(self,
+                                 obligation.param_env,
+                                 obligation.cause.clone(),
+                                 obligation.recursion_depth+1,
+                                 &trait_ref);
 
         debug!("confirm_closure_candidate(closure_def_id={:?}, trait_ref={:?}, obligations={:?})",
                closure_def_id,
@@ -3106,24 +3069,6 @@ fn closure_trait_ref_unnormalized(&mut self,
         ty::Binder(trait_ref)
     }
 
-    fn closure_trait_ref(&mut self,
-                         obligation: &TraitObligation<'tcx>,
-                         closure_def_id: DefId,
-                         substs: ty::ClosureSubsts<'tcx>)
-                         -> Normalized<'tcx, ty::PolyTraitRef<'tcx>>
-    {
-        let trait_ref = self.closure_trait_ref_unnormalized(
-            obligation, closure_def_id, substs);
-
-        // A closure signature can contain associated types which
-        // must be normalized.
-        normalize_with_depth(self,
-                             obligation.param_env,
-                             obligation.cause.clone(),
-                             obligation.recursion_depth+1,
-                             &trait_ref)
-    }
-
     fn generator_trait_ref_unnormalized(&mut self,
                                       obligation: &TraitObligation<'tcx>,
                                       closure_def_id: DefId,
@@ -3145,24 +3090,6 @@ fn generator_trait_ref_unnormalized(&mut self,
         ty::Binder(trait_ref)
     }
 
-    fn generator_trait_ref(&mut self,
-                         obligation: &TraitObligation<'tcx>,
-                         closure_def_id: DefId,
-                         substs: ty::ClosureSubsts<'tcx>)
-                         -> Normalized<'tcx, ty::PolyTraitRef<'tcx>>
-    {
-        let trait_ref = self.generator_trait_ref_unnormalized(
-            obligation, closure_def_id, substs);
-
-        // A generator signature can contain associated types which
-        // must be normalized.
-        normalize_with_depth(self,
-                             obligation.param_env,
-                             obligation.cause.clone(),
-                             obligation.recursion_depth+1,
-                             &trait_ref)
-    }
-
     /// Returns the obligations that are implied by instantiating an
     /// impl or trait. The obligations are substituted and fully
     /// normalized. This is used when confirming an impl or default
index 7aaf4e598d6bf273b0f06ece3b10347986a58ae9..d9b0f4ac8a6c0e4e22fd988ac3fd20adefbfb9d7 100644 (file)
@@ -302,6 +302,12 @@ pub fn set_continue_after_error(&self, continue_after_error: bool) {
         self.continue_after_error.set(continue_after_error);
     }
 
+    // NOTE: DO NOT call this function from rustc, as it relies on `err_count` being non-zero
+    // if an error happened to avoid ICEs. This function should only be called from tools.
+    pub fn reset_err_count(&self) {
+        self.err_count.set(0);
+    }
+
     pub fn struct_dummy<'a>(&'a self) -> DiagnosticBuilder<'a> {
         DiagnosticBuilder::new(self, Level::Cancelled, "")
     }
index 033ef988571dd263a5ad4c82161a3a522b89b2a3..9a3c8a5079a2f2b9c8dc6f42089f0f6b2b36f745 100644 (file)
@@ -383,6 +383,18 @@ pub fn trans_intrinsic_call<'a, 'tcx>(bcx: &Builder<'a, 'tcx>,
                 _ => C_null(llret_ty)
             }
         }
+
+        "align_offset" => {
+            // `ptr as usize`
+            let ptr_val = bcx.ptrtoint(llargs[0], bcx.ccx.int_type());
+            // `ptr_val % align`
+            let offset = bcx.urem(ptr_val, llargs[1]);
+            let zero = C_null(bcx.ccx.int_type());
+            // `offset == 0`
+            let is_zero = bcx.icmp(llvm::IntPredicate::IntEQ, offset, zero);
+            // `if offset == 0 { 0 } else { offset - align }`
+            bcx.select(is_zero, zero, bcx.sub(offset, llargs[1]))
+        }
         name if name.starts_with("simd_") => {
             generic_simd_intrinsic(bcx, name,
                                    callee_ty,
index 96643ae72abadc5c7beaade2ff465137743c0a17..6fee7e58633fcf12e560c189f648b324e192b978 100644 (file)
@@ -313,6 +313,11 @@ pub fn check_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
                 (0, vec![tcx.mk_fn_ptr(fn_ty), mut_u8, mut_u8], tcx.types.i32)
             }
 
+            "align_offset" => {
+                let ptr_ty = tcx.mk_imm_ptr(tcx.mk_nil());
+                (0, vec![ptr_ty, tcx.types.usize], tcx.types.usize)
+            },
+
             ref other => {
                 struct_span_err!(tcx.sess, it.span, E0093,
                                 "unrecognized intrinsic function: `{}`",
index db383b6305b4a424fd60490907442f62c2e92363..08ec3bf74a71daf355cd41ff0cac98f22b32ec59 100644 (file)
@@ -232,24 +232,6 @@ fn fresh_receiver_substs(&mut self,
                 })
             }
 
-            probe::ExtensionImplPick(impl_def_id) => {
-                // The method being invoked is the method as defined on the trait,
-                // so return the substitutions from the trait. Consider:
-                //
-                //     impl<A,B,C> Trait<A,B> for Foo<C> { ... }
-                //
-                // If we instantiate A, B, and C with $A, $B, and $C
-                // respectively, then we want to return the type
-                // parameters from the trait ([$A,$B]), not those from
-                // the impl ([$A,$B,$C]) not the receiver type ([$C]).
-                let impl_polytype = self.impl_self_ty(self.span, impl_def_id);
-                let impl_trait_ref =
-                    self.instantiate_type_scheme(self.span,
-                                                 impl_polytype.substs,
-                                                 &self.tcx.impl_trait_ref(impl_def_id).unwrap());
-                impl_trait_ref.substs
-            }
-
             probe::TraitPick => {
                 let trait_def_id = pick.item.container.id();
 
index 819f48a1b57e80855c4a314c43c056af34a84729..31ceed5b965bff71377c4b3f9e121c702167fecd 100644 (file)
@@ -52,10 +52,6 @@ pub enum MethodError<'tcx> {
     // Multiple methods might apply.
     Ambiguity(Vec<CandidateSource>),
 
-    // Using a `Fn`/`FnMut`/etc method on a raw closure type before we have inferred its kind.
-    ClosureAmbiguity(// DefId of fn trait
-                     DefId),
-
     // Found an applicable method, but it is not visible. The second argument contains a list of
     // not-in-scope traits which may work.
     PrivateMatch(Def, Vec<DefId>),
@@ -63,6 +59,9 @@ pub enum MethodError<'tcx> {
     // Found a `Self: Sized` bound where `Self` is a trait object, also the caller may have
     // forgotten to import a trait.
     IllegalSizedBound(Vec<DefId>),
+
+    // Found a match, but the return type is wrong
+    BadReturnType,
 }
 
 // Contains a list of static methods that may apply, a list of unsatisfied trait predicates which
@@ -113,9 +112,12 @@ pub fn method_exists(&self,
             Ok(..) => true,
             Err(NoMatch(..)) => false,
             Err(Ambiguity(..)) => true,
-            Err(ClosureAmbiguity(..)) => true,
             Err(PrivateMatch(..)) => allow_private,
             Err(IllegalSizedBound(..)) => true,
+            Err(BadReturnType) => {
+                bug!("no return type expectations but got BadReturnType")
+            }
+
         }
     }
 
index 5e5a27f2ba13719a7b640fb69d9df737856cbbb7..228b6c88f24db77cbca22040d670d251369c8ce5 100644 (file)
@@ -18,7 +18,7 @@
 use hir::def::Def;
 use rustc::ty::subst::{Subst, Substs};
 use rustc::traits::{self, ObligationCause};
-use rustc::ty::{self, Ty, ToPolyTraitRef, TraitRef, TypeFoldable};
+use rustc::ty::{self, Ty, ToPolyTraitRef, ToPredicate, TraitRef, TypeFoldable};
 use rustc::infer::type_variable::TypeVariableOrigin;
 use rustc::util::nodemap::FxHashSet;
 use rustc::infer::{self, InferOk};
 use self::CandidateKind::*;
 pub use self::PickKind::*;
 
-pub enum LookingFor<'tcx> {
-    /// looking for methods with the given name; this is the normal case
-    MethodName(ast::Name),
-
-    /// looking for methods that return a given type; this is used to
-    /// assemble suggestions
-    ReturnType(Ty<'tcx>),
-}
-
 /// Boolean flag used to indicate if this search is for a suggestion
 /// or not.  If true, we can allow ambiguity and so forth.
 pub struct IsSuggestion(pub bool);
@@ -49,9 +40,9 @@ struct ProbeContext<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
     fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
     span: Span,
     mode: Mode,
-    looking_for: LookingFor<'tcx>,
+    method_name: Option<ast::Name>,
+    return_type: Option<Ty<'tcx>>,
     steps: Rc<Vec<CandidateStep<'tcx>>>,
-    opt_simplified_steps: Option<Vec<ty::fast_reject::SimplifiedType>>,
     inherent_candidates: Vec<Candidate<'tcx>>,
     extension_candidates: Vec<Candidate<'tcx>>,
     impl_dups: FxHashSet<DefId>,
@@ -85,6 +76,7 @@ struct CandidateStep<'tcx> {
 #[derive(Debug)]
 struct Candidate<'tcx> {
     xform_self_ty: Ty<'tcx>,
+    xform_ret_ty: Option<Ty<'tcx>>,
     item: ty::AssociatedItem,
     kind: CandidateKind<'tcx>,
     import_id: Option<ast::NodeId>,
@@ -95,17 +87,19 @@ enum CandidateKind<'tcx> {
     InherentImplCandidate(&'tcx Substs<'tcx>,
                           // Normalize obligations
                           Vec<traits::PredicateObligation<'tcx>>),
-    ExtensionImplCandidate(// Impl
-                           DefId,
-                           &'tcx Substs<'tcx>,
-                           // Normalize obligations
-                           Vec<traits::PredicateObligation<'tcx>>),
     ObjectCandidate,
-    TraitCandidate,
+    TraitCandidate(ty::TraitRef<'tcx>),
     WhereClauseCandidate(// Trait
                          ty::PolyTraitRef<'tcx>),
 }
 
+#[derive(Debug, PartialEq, Eq, Copy, Clone)]
+enum ProbeResult {
+    NoMatch,
+    BadReturnType,
+    Match,
+}
+
 #[derive(Debug, PartialEq, Eq, Clone)]
 pub struct Pick<'tcx> {
     pub item: ty::AssociatedItem,
@@ -133,8 +127,6 @@ pub struct Pick<'tcx> {
 #[derive(Clone, Debug, PartialEq, Eq)]
 pub enum PickKind<'tcx> {
     InherentImplPick,
-    ExtensionImplPick(// Impl
-                      DefId),
     ObjectPick,
     TraitPick,
     WhereClausePick(// Trait
@@ -183,19 +175,19 @@ pub fn probe_for_return_type(&self,
                return_type,
                scope_expr_id);
         let method_names =
-            self.probe_op(span, mode, LookingFor::ReturnType(return_type), IsSuggestion(true),
+            self.probe_op(span, mode, None, Some(return_type), IsSuggestion(true),
                           self_ty, scope_expr_id, ProbeScope::TraitsInScope,
                           |probe_cx| Ok(probe_cx.candidate_method_names()))
                 .unwrap_or(vec![]);
-        method_names
-            .iter()
-            .flat_map(|&method_name| {
-                match self.probe_for_name(span, mode, method_name, IsSuggestion(true), self_ty,
-                                          scope_expr_id, ProbeScope::TraitsInScope) {
-                    Ok(pick) => Some(pick.item),
-                    Err(_) => None,
-                }
-            })
+         method_names
+             .iter()
+             .flat_map(|&method_name| {
+                 self.probe_op(
+                     span, mode, Some(method_name), Some(return_type),
+                     IsSuggestion(true), self_ty, scope_expr_id,
+                     ProbeScope::TraitsInScope, |probe_cx| probe_cx.pick()
+                 ).ok().map(|pick| pick.item)
+             })
             .collect()
     }
 
@@ -214,7 +206,8 @@ pub fn probe_for_name(&self,
                scope_expr_id);
         self.probe_op(span,
                       mode,
-                      LookingFor::MethodName(item_name),
+                      Some(item_name),
+                      None,
                       is_suggestion,
                       self_ty,
                       scope_expr_id,
@@ -225,7 +218,8 @@ pub fn probe_for_name(&self,
     fn probe_op<OP,R>(&'a self,
                       span: Span,
                       mode: Mode,
-                      looking_for: LookingFor<'tcx>,
+                      method_name: Option<ast::Name>,
+                      return_type: Option<Ty<'tcx>>,
                       is_suggestion: IsSuggestion,
                       self_ty: Ty<'tcx>,
                       scope_expr_id: ast::NodeId,
@@ -259,24 +253,6 @@ fn probe_op<OP,R>(&'a self,
                  }]
         };
 
-        // Create a list of simplified self types, if we can.
-        let mut simplified_steps = Vec::new();
-        for step in &steps {
-            match ty::fast_reject::simplify_type(self.tcx, step.self_ty, true) {
-                None => {
-                    break;
-                }
-                Some(simplified_type) => {
-                    simplified_steps.push(simplified_type);
-                }
-            }
-        }
-        let opt_simplified_steps = if simplified_steps.len() < steps.len() {
-            None // failed to convert at least one of the steps
-        } else {
-            Some(simplified_steps)
-        };
-
         debug!("ProbeContext: steps for self_ty={:?} are {:?}",
                self_ty,
                steps);
@@ -285,8 +261,7 @@ fn probe_op<OP,R>(&'a self,
         // that we create during the probe process are removed later
         self.probe(|_| {
             let mut probe_cx =
-                ProbeContext::new(self, span, mode, looking_for,
-                                  steps, opt_simplified_steps);
+                ProbeContext::new(self, span, mode, method_name, return_type, steps);
 
             probe_cx.assemble_inherent_candidates();
             match scope {
@@ -356,20 +331,20 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
     fn new(fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
            span: Span,
            mode: Mode,
-           looking_for: LookingFor<'tcx>,
-           steps: Vec<CandidateStep<'tcx>>,
-           opt_simplified_steps: Option<Vec<ty::fast_reject::SimplifiedType>>)
+           method_name: Option<ast::Name>,
+           return_type: Option<Ty<'tcx>>,
+           steps: Vec<CandidateStep<'tcx>>)
            -> ProbeContext<'a, 'gcx, 'tcx> {
         ProbeContext {
             fcx,
             span,
             mode,
-            looking_for,
+            method_name,
+            return_type,
             inherent_candidates: Vec::new(),
             extension_candidates: Vec::new(),
             impl_dups: FxHashSet(),
             steps: Rc::new(steps),
-            opt_simplified_steps,
             static_candidates: Vec::new(),
             private_candidate: None,
             unsatisfied_predicates: Vec::new(),
@@ -387,33 +362,25 @@ fn reset(&mut self) {
     ///////////////////////////////////////////////////////////////////////////
     // CANDIDATE ASSEMBLY
 
-    fn push_inherent_candidate(&mut self, xform_self_ty: Ty<'tcx>, item: ty::AssociatedItem,
-                               kind: CandidateKind<'tcx>, import_id: Option<ast::NodeId>) {
-        let is_accessible = if let LookingFor::MethodName(name) = self.looking_for {
-            let def_scope = self.tcx.adjust(name, item.container.id(), self.body_id).1;
-            item.vis.is_accessible_from(def_scope, self.tcx)
-        } else {
-            true
-        };
-        if is_accessible {
-            self.inherent_candidates.push(Candidate { xform_self_ty, item, kind, import_id });
-        } else if self.private_candidate.is_none() {
-            self.private_candidate = Some(item.def());
-        }
-    }
-
-    fn push_extension_candidate(&mut self, xform_self_ty: Ty<'tcx>, item: ty::AssociatedItem,
-                               kind: CandidateKind<'tcx>, import_id: Option<ast::NodeId>) {
-        let is_accessible = if let LookingFor::MethodName(name) = self.looking_for {
+    fn push_candidate(&mut self,
+                      candidate: Candidate<'tcx>,
+                      is_inherent: bool)
+    {
+        let is_accessible = if let Some(name) = self.method_name {
+            let item = candidate.item;
             let def_scope = self.tcx.adjust(name, item.container.id(), self.body_id).1;
             item.vis.is_accessible_from(def_scope, self.tcx)
         } else {
             true
         };
         if is_accessible {
-            self.extension_candidates.push(Candidate { xform_self_ty, item, kind, import_id });
+            if is_inherent {
+                self.inherent_candidates.push(candidate);
+            } else {
+                self.extension_candidates.push(candidate);
+            }
         } else if self.private_candidate.is_none() {
-            self.private_candidate = Some(item.def());
+            self.private_candidate = Some(candidate.item.def());
         }
     }
 
@@ -551,19 +518,22 @@ fn assemble_inherent_impl_probe(&mut self, impl_def_id: DefId) {
             let impl_ty = impl_ty.subst(self.tcx, impl_substs);
 
             // Determine the receiver type that the method itself expects.
-            let xform_self_ty = self.xform_self_ty(&item, impl_ty, impl_substs);
+            let xform_tys = self.xform_self_ty(&item, impl_ty, impl_substs);
 
             // We can't use normalize_associated_types_in as it will pollute the
             // fcx's fulfillment context after this probe is over.
             let cause = traits::ObligationCause::misc(self.span, self.body_id);
             let selcx = &mut traits::SelectionContext::new(self.fcx);
-            let traits::Normalized { value: xform_self_ty, obligations } =
-                traits::normalize(selcx, self.param_env, cause, &xform_self_ty);
-            debug!("assemble_inherent_impl_probe: xform_self_ty = {:?}",
-                   xform_self_ty);
-
-            self.push_inherent_candidate(xform_self_ty, item,
-                                         InherentImplCandidate(impl_substs, obligations), None);
+            let traits::Normalized { value: (xform_self_ty, xform_ret_ty), obligations } =
+                traits::normalize(selcx, self.param_env, cause, &xform_tys);
+            debug!("assemble_inherent_impl_probe: xform_self_ty = {:?}/{:?}",
+                   xform_self_ty, xform_ret_ty);
+
+            self.push_candidate(Candidate {
+                xform_self_ty, xform_ret_ty, item,
+                kind: InherentImplCandidate(impl_substs, obligations),
+                import_id: None
+            }, true);
         }
     }
 
@@ -584,10 +554,13 @@ fn assemble_inherent_candidates_from_object(&mut self,
         self.elaborate_bounds(&[trait_ref], |this, new_trait_ref, item| {
             let new_trait_ref = this.erase_late_bound_regions(&new_trait_ref);
 
-            let xform_self_ty =
+            let (xform_self_ty, xform_ret_ty) =
                 this.xform_self_ty(&item, new_trait_ref.self_ty(), new_trait_ref.substs);
-
-            this.push_inherent_candidate(xform_self_ty, item, ObjectCandidate, None);
+            this.push_candidate(Candidate {
+                xform_self_ty, xform_ret_ty, item,
+                kind: ObjectCandidate,
+                import_id: None
+            }, true);
         });
     }
 
@@ -624,7 +597,8 @@ fn assemble_inherent_candidates_from_param(&mut self,
         self.elaborate_bounds(&bounds, |this, poly_trait_ref, item| {
             let trait_ref = this.erase_late_bound_regions(&poly_trait_ref);
 
-            let xform_self_ty = this.xform_self_ty(&item, trait_ref.self_ty(), trait_ref.substs);
+            let (xform_self_ty, xform_ret_ty) =
+                this.xform_self_ty(&item, trait_ref.self_ty(), trait_ref.substs);
 
             // Because this trait derives from a where-clause, it
             // should not contain any inference variables or other
@@ -633,8 +607,11 @@ fn assemble_inherent_candidates_from_param(&mut self,
             // `WhereClausePick`.
             assert!(!trait_ref.substs.needs_infer());
 
-            this.push_inherent_candidate(xform_self_ty, item,
-                                         WhereClauseCandidate(poly_trait_ref), None);
+            this.push_candidate(Candidate {
+                xform_self_ty, xform_ret_ty, item,
+                kind: WhereClauseCandidate(poly_trait_ref),
+                import_id: None
+            }, true);
         });
     }
 
@@ -687,17 +664,27 @@ fn assemble_extension_candidates_for_all_traits(&mut self) -> Result<(), MethodE
         Ok(())
     }
 
-    pub fn matches_return_type(&self, method: &ty::AssociatedItem,
-                               expected: ty::Ty<'tcx>) -> bool {
+    pub fn matches_return_type(&self,
+                               method: &ty::AssociatedItem,
+                               self_ty: Option<Ty<'tcx>>,
+                               expected: Ty<'tcx>) -> bool {
         match method.def() {
             Def::Method(def_id) => {
                 let fty = self.tcx.fn_sig(def_id);
                 self.probe(|_| {
                     let substs = self.fresh_substs_for_item(self.span, method.def_id);
-                    let output = fty.output().subst(self.tcx, substs);
-                    let (output, _) = self.replace_late_bound_regions_with_fresh_var(
-                        self.span, infer::FnCall, &output);
-                    self.can_sub(self.param_env, output, expected).is_ok()
+                    let fty = fty.subst(self.tcx, substs);
+                    let (fty, _) = self.replace_late_bound_regions_with_fresh_var(
+                        self.span, infer::FnCall, &fty);
+
+                    if let Some(self_ty) = self_ty {
+                        if let Err(_) = self.at(&ObligationCause::dummy(), self.param_env)
+                            .sup(fty.inputs()[0], self_ty)
+                        {
+                            return false
+                        }
+                    }
+                    self.can_sub(self.param_env, fty.output(), expected).is_ok()
                 })
             }
             _ => false,
@@ -710,6 +697,8 @@ fn assemble_extension_candidates_for_trait(&mut self,
                                                -> Result<(), MethodError<'tcx>> {
         debug!("assemble_extension_candidates_for_trait(trait_def_id={:?})",
                trait_def_id);
+        let trait_substs = self.fresh_item_substs(trait_def_id);
+        let trait_ref = ty::TraitRef::new(trait_def_id, trait_substs);
 
         for item in self.impl_or_trait_item(trait_def_id) {
             // Check whether `trait_def_id` defines a method with suitable name:
@@ -719,326 +708,31 @@ fn assemble_extension_candidates_for_trait(&mut self,
                 continue;
             }
 
-            self.assemble_builtin_candidates(import_id, trait_def_id, item.clone());
-
-            self.assemble_extension_candidates_for_trait_impls(import_id, trait_def_id,
-                                                               item.clone());
-
-            self.assemble_closure_candidates(import_id, trait_def_id, item.clone())?;
-
-            self.assemble_generator_candidates(import_id, trait_def_id, item.clone())?;
-
-            self.assemble_projection_candidates(import_id, trait_def_id, item.clone());
-
-            self.assemble_where_clause_candidates(import_id, trait_def_id, item.clone());
-        }
-
-        Ok(())
-    }
-
-    fn assemble_builtin_candidates(&mut self,
-                                   import_id: Option<ast::NodeId>,
-                                   trait_def_id: DefId,
-                                   item: ty::AssociatedItem) {
-        if Some(trait_def_id) == self.tcx.lang_items.clone_trait() {
-            self.assemble_builtin_clone_candidates(import_id, trait_def_id, item);
-        }
-    }
-
-    fn assemble_builtin_clone_candidates(&mut self,
-                                         import_id: Option<ast::NodeId>,
-                                         trait_def_id: DefId,
-                                         item: ty::AssociatedItem) {
-        for step in Rc::clone(&self.steps).iter() {
-            match step.self_ty.sty {
-                ty::TyInfer(ty::IntVar(_)) | ty::TyInfer(ty::FloatVar(_)) |
-                ty::TyUint(_) | ty::TyInt(_) | ty::TyBool | ty::TyFloat(_) |
-                ty::TyFnDef(..) | ty::TyFnPtr(_) | ty::TyChar |
-                ty::TyRawPtr(..) | ty::TyError | ty::TyNever |
-                ty::TyRef(_, ty::TypeAndMut { ty: _, mutbl: hir::MutImmutable }) |
-                ty::TyArray(..) | ty::TyTuple(..) => {
-                    ()
-                }
-
-                _ => continue,
-            };
-
-            let substs = Substs::for_item(self.tcx,
-                                          trait_def_id,
-                                          |def, _| self.region_var_for_def(self.span, def),
-                                          |def, substs| {
-                if def.index == 0 {
-                    step.self_ty
-                } else {
-                    self.type_var_for_def(self.span, def, substs)
-                }
-            });
-
-            let xform_self_ty = self.xform_self_ty(&item, step.self_ty, substs);
-            self.push_inherent_candidate(xform_self_ty, item, TraitCandidate, import_id);
-        }
-    }
-
-    fn assemble_extension_candidates_for_trait_impls(&mut self,
-                                                     import_id: Option<ast::NodeId>,
-                                                     trait_def_id: DefId,
-                                                     item: ty::AssociatedItem) {
-        // FIXME(arielb1): can we use for_each_relevant_impl here?
-        self.tcx.for_each_impl(trait_def_id, |impl_def_id| {
-            debug!("assemble_extension_candidates_for_trait_impl: trait_def_id={:?} \
-                                                                  impl_def_id={:?}",
-                   trait_def_id,
-                   impl_def_id);
-
-            if !self.impl_can_possibly_match(impl_def_id) {
-                return;
-            }
-
-            let (_, impl_substs) = self.impl_ty_and_substs(impl_def_id);
-
-            debug!("impl_substs={:?}", impl_substs);
-
-            let impl_trait_ref = self.tcx.impl_trait_ref(impl_def_id)
-                .unwrap() // we know this is a trait impl
-                .subst(self.tcx, impl_substs);
-
-            debug!("impl_trait_ref={:?}", impl_trait_ref);
-
-            // Determine the receiver type that the method itself expects.
-            let xform_self_ty =
-                self.xform_self_ty(&item, impl_trait_ref.self_ty(), impl_trait_ref.substs);
-
-            // Normalize the receiver. We can't use normalize_associated_types_in
-            // as it will pollute the fcx's fulfillment context after this probe
-            // is over.
-            let cause = traits::ObligationCause::misc(self.span, self.body_id);
-            let selcx = &mut traits::SelectionContext::new(self.fcx);
-            let traits::Normalized { value: xform_self_ty, obligations } =
-                traits::normalize(selcx, self.param_env, cause, &xform_self_ty);
-
-            debug!("xform_self_ty={:?}", xform_self_ty);
-
-            self.push_extension_candidate(xform_self_ty, item,
-                        ExtensionImplCandidate(impl_def_id, impl_substs, obligations), import_id);
-        });
-    }
-
-    fn impl_can_possibly_match(&self, impl_def_id: DefId) -> bool {
-        let simplified_steps = match self.opt_simplified_steps {
-            Some(ref simplified_steps) => simplified_steps,
-            None => {
-                return true;
-            }
-        };
-
-        let impl_type = self.tcx.type_of(impl_def_id);
-        let impl_simplified_type =
-            match ty::fast_reject::simplify_type(self.tcx, impl_type, false) {
-                Some(simplified_type) => simplified_type,
-                None => {
-                    return true;
-                }
-            };
-
-        simplified_steps.contains(&impl_simplified_type)
-    }
-
-    fn assemble_closure_candidates(&mut self,
-                                   import_id: Option<ast::NodeId>,
-                                   trait_def_id: DefId,
-                                   item: ty::AssociatedItem)
-                                   -> Result<(), MethodError<'tcx>> {
-        // Check if this is one of the Fn,FnMut,FnOnce traits.
-        let tcx = self.tcx;
-        let kind = if Some(trait_def_id) == tcx.lang_items.fn_trait() {
-            ty::ClosureKind::Fn
-        } else if Some(trait_def_id) == tcx.lang_items.fn_mut_trait() {
-            ty::ClosureKind::FnMut
-        } else if Some(trait_def_id) == tcx.lang_items.fn_once_trait() {
-            ty::ClosureKind::FnOnce
-        } else {
-            return Ok(());
-        };
-
-        // Check if there is an unboxed-closure self-type in the list of receivers.
-        // If so, add "synthetic impls".
-        let steps = self.steps.clone();
-        for step in steps.iter() {
-            let closure_id = match step.self_ty.sty {
-                ty::TyClosure(def_id, _) => {
-                    if let Some(id) = self.tcx.hir.as_local_node_id(def_id) {
-                        self.tcx.hir.node_to_hir_id(id)
-                    } else {
-                        continue;
-                    }
-                }
-                _ => continue,
-            };
-
-            let closure_kind = {
-                match self.tables.borrow().closure_kinds().get(closure_id) {
-                    Some(&(k, _)) => k,
-                    None => {
-                        return Err(MethodError::ClosureAmbiguity(trait_def_id));
-                    }
-                }
-            };
-
-            // this closure doesn't implement the right kind of `Fn` trait
-            if !closure_kind.extends(kind) {
-                continue;
-            }
-
-            // create some substitutions for the argument/return type;
-            // for the purposes of our method lookup, we only take
-            // receiver type into account, so we can just substitute
-            // fresh types here to use during substitution and subtyping.
-            let substs = Substs::for_item(self.tcx,
-                                          trait_def_id,
-                                          |def, _| self.region_var_for_def(self.span, def),
-                                          |def, substs| {
-                if def.index == 0 {
-                    step.self_ty
-                } else {
-                    self.type_var_for_def(self.span, def, substs)
-                }
-            });
-
-            let xform_self_ty = self.xform_self_ty(&item, step.self_ty, substs);
-            self.push_inherent_candidate(xform_self_ty, item, TraitCandidate, import_id);
-        }
-
-        Ok(())
-    }
-
-    fn assemble_generator_candidates(&mut self,
-                                   import_id: Option<ast::NodeId>,
-                                   trait_def_id: DefId,
-                                   item: ty::AssociatedItem)
-                                   -> Result<(), MethodError<'tcx>> {
-        // Check if this is the Generator trait.
-        let tcx = self.tcx;
-        if Some(trait_def_id) != tcx.lang_items.gen_trait() {
-            return Ok(());
-        }
-
-        // Check if there is an generator self-type in the list of receivers.
-        // If so, add "synthetic impls".
-        let steps = self.steps.clone();
-        for step in steps.iter() {
-            match step.self_ty.sty {
-                ty::TyGenerator(..) => (),
-                _ => continue,
-            };
-
-            // create some substitutions for the argument/return type;
-            // for the purposes of our method lookup, we only take
-            // receiver type into account, so we can just substitute
-            // fresh types here to use during substitution and subtyping.
-            let substs = Substs::for_item(self.tcx,
-                                          trait_def_id,
-                                          |def, _| self.region_var_for_def(self.span, def),
-                                          |def, substs| {
-                if def.index == 0 {
-                    step.self_ty
-                } else {
-                    self.type_var_for_def(self.span, def, substs)
-                }
-            });
-
-            let xform_self_ty = self.xform_self_ty(&item, step.self_ty, substs);
-            self.push_inherent_candidate(xform_self_ty, item, TraitCandidate, import_id);
+            let (xform_self_ty, xform_ret_ty) =
+                self.xform_self_ty(&item, trait_ref.self_ty(), trait_substs);
+            self.push_candidate(Candidate {
+                xform_self_ty, xform_ret_ty, item, import_id,
+                kind: TraitCandidate(trait_ref),
+            }, false);
         }
-
         Ok(())
     }
 
-    fn assemble_projection_candidates(&mut self,
-                                      import_id: Option<ast::NodeId>,
-                                      trait_def_id: DefId,
-                                      item: ty::AssociatedItem) {
-        debug!("assemble_projection_candidates(\
-               trait_def_id={:?}, \
-               item={:?})",
-               trait_def_id,
-               item);
-
-        for step in Rc::clone(&self.steps).iter() {
-            debug!("assemble_projection_candidates: step={:?}", step);
-
-            let (def_id, substs) = match step.self_ty.sty {
-                ty::TyProjection(ref data) => {
-                    let trait_ref = data.trait_ref(self.tcx);
-                    (trait_ref.def_id, trait_ref.substs)
-                },
-                ty::TyAnon(def_id, substs) => (def_id, substs),
-                _ => continue,
-            };
-
-            debug!("assemble_projection_candidates: def_id={:?} substs={:?}",
-                   def_id,
-                   substs);
-
-            let trait_predicates = self.tcx.predicates_of(def_id);
-            let bounds = trait_predicates.instantiate(self.tcx, substs);
-            let predicates = bounds.predicates;
-            debug!("assemble_projection_candidates: predicates={:?}",
-                   predicates);
-            for poly_bound in traits::elaborate_predicates(self.tcx, predicates)
-                .filter_map(|p| p.to_opt_poly_trait_ref())
-                .filter(|b| b.def_id() == trait_def_id) {
-                let bound = self.erase_late_bound_regions(&poly_bound);
-
-                debug!("assemble_projection_candidates: def_id={:?} substs={:?} bound={:?}",
-                       def_id,
-                       substs,
-                       bound);
-
-                if self.can_eq(self.param_env, step.self_ty, bound.self_ty()).is_ok() {
-                    let xform_self_ty = self.xform_self_ty(&item, bound.self_ty(), bound.substs);
-
-                    debug!("assemble_projection_candidates: bound={:?} xform_self_ty={:?}",
-                           bound,
-                           xform_self_ty);
-
-                    self.push_extension_candidate(xform_self_ty, item, TraitCandidate, import_id);
-                }
-            }
-        }
-    }
-
-    fn assemble_where_clause_candidates(&mut self,
-                                        import_id: Option<ast::NodeId>,
-                                        trait_def_id: DefId,
-                                        item: ty::AssociatedItem) {
-        debug!("assemble_where_clause_candidates(trait_def_id={:?})",
-               trait_def_id);
-
-        let caller_predicates = self.param_env.caller_bounds.to_vec();
-        for poly_bound in traits::elaborate_predicates(self.tcx, caller_predicates)
-            .filter_map(|p| p.to_opt_poly_trait_ref())
-            .filter(|b| b.def_id() == trait_def_id) {
-            let bound = self.erase_late_bound_regions(&poly_bound);
-            let xform_self_ty = self.xform_self_ty(&item, bound.self_ty(), bound.substs);
-
-            debug!("assemble_where_clause_candidates: bound={:?} xform_self_ty={:?}",
-                   bound,
-                   xform_self_ty);
-
-            self.push_extension_candidate(xform_self_ty, item,
-                                          WhereClauseCandidate(poly_bound), import_id);
-        }
-    }
-
     fn candidate_method_names(&self) -> Vec<ast::Name> {
         let mut set = FxHashSet();
-        let mut names: Vec<_> =
-            self.inherent_candidates
-                .iter()
-                .chain(&self.extension_candidates)
-                .map(|candidate| candidate.item.name)
-                .filter(|&name| set.insert(name))
-                .collect();
+        let mut names: Vec<_> = self.inherent_candidates
+            .iter()
+            .chain(&self.extension_candidates)
+            .filter(|candidate| {
+                if let Some(return_ty) = self.return_type {
+                    self.matches_return_type(&candidate.item, None, return_ty)
+                } else {
+                    true
+                }
+            })
+            .map(|candidate| candidate.item.name)
+            .filter(|&name| set.insert(name))
+            .collect();
 
         // sort them by the name so we have a stable result
         names.sort_by_key(|n| n.as_str());
@@ -1049,10 +743,7 @@ fn candidate_method_names(&self) -> Vec<ast::Name> {
     // THE ACTUAL SEARCH
 
     fn pick(mut self) -> PickResult<'tcx> {
-        assert!(match self.looking_for {
-            LookingFor::MethodName(_) => true,
-            LookingFor::ReturnType(_) => false,
-        });
+        assert!(self.method_name.is_some());
 
         if let Some(r) = self.pick_core() {
             return r;
@@ -1095,10 +786,6 @@ fn pick(mut self) -> PickResult<'tcx> {
                 assert!(others.is_empty());
                 vec![]
             }
-            Some(Err(MethodError::ClosureAmbiguity(..))) => {
-                // this error only occurs when assembling candidates
-                span_bug!(span, "encountered ClosureAmbiguity from pick_core");
-            }
             _ => vec![],
         };
 
@@ -1116,21 +803,17 @@ fn pick_core(&mut self) -> Option<PickResult<'tcx>> {
         let steps = self.steps.clone();
 
         // find the first step that works
-        steps.iter().filter_map(|step| self.pick_step(step)).next()
-    }
-
-    fn pick_step(&mut self, step: &CandidateStep<'tcx>) -> Option<PickResult<'tcx>> {
-        debug!("pick_step: step={:?}", step);
-
-        if step.self_ty.references_error() {
-            return None;
-        }
-
-        if let Some(result) = self.pick_by_value_method(step) {
-            return Some(result);
-        }
-
-        self.pick_autorefd_method(step)
+        steps
+            .iter()
+            .filter(|step| {
+                debug!("pick_core: step={:?}", step);
+                !step.self_ty.references_error()
+            }).flat_map(|step| {
+                self.pick_by_value_method(step).or_else(|| {
+                self.pick_autorefd_method(step, hir::MutImmutable).or_else(|| {
+                self.pick_autorefd_method(step, hir::MutMutable)
+            })})})
+            .next()
     }
 
     fn pick_by_value_method(&mut self, step: &CandidateStep<'tcx>) -> Option<PickResult<'tcx>> {
@@ -1161,36 +844,30 @@ fn pick_by_value_method(&mut self, step: &CandidateStep<'tcx>) -> Option<PickRes
         })
     }
 
-    fn pick_autorefd_method(&mut self, step: &CandidateStep<'tcx>) -> Option<PickResult<'tcx>> {
+    fn pick_autorefd_method(&mut self, step: &CandidateStep<'tcx>, mutbl: hir::Mutability)
+                            -> Option<PickResult<'tcx>> {
         let tcx = self.tcx;
 
         // In general, during probing we erase regions. See
         // `impl_self_ty()` for an explanation.
         let region = tcx.types.re_erased;
 
-        // Search through mutabilities in order to find one where pick works:
-        [hir::MutImmutable, hir::MutMutable]
-            .iter()
-            .filter_map(|&m| {
-                let autoref_ty = tcx.mk_ref(region,
-                                            ty::TypeAndMut {
-                                                ty: step.self_ty,
-                                                mutbl: m,
-                                            });
-                self.pick_method(autoref_ty).map(|r| {
-                    r.map(|mut pick| {
-                        pick.autoderefs = step.autoderefs;
-                        pick.autoref = Some(m);
-                        pick.unsize = if step.unsize {
-                            Some(step.self_ty)
-                        } else {
-                            None
-                        };
-                        pick
-                    })
-                })
+        let autoref_ty = tcx.mk_ref(region,
+                                    ty::TypeAndMut {
+                                        ty: step.self_ty, mutbl
+                                    });
+        self.pick_method(autoref_ty).map(|r| {
+            r.map(|mut pick| {
+                pick.autoderefs = step.autoderefs;
+                pick.autoref = Some(mutbl);
+                pick.unsize = if step.unsize {
+                    Some(step.self_ty)
+                } else {
+                    None
+                };
+                pick
             })
-            .nth(0)
+        })
     }
 
     fn pick_method(&mut self, self_ty: Ty<'tcx>) -> Option<PickResult<'tcx>> {
@@ -1221,7 +898,10 @@ fn consider_candidates(&self,
                            possibly_unsatisfied_predicates: &mut Vec<TraitRef<'tcx>>)
                            -> Option<PickResult<'tcx>> {
         let mut applicable_candidates: Vec<_> = probes.iter()
-            .filter(|&probe| self.consider_probe(self_ty, probe, possibly_unsatisfied_predicates))
+            .map(|probe| {
+                (probe, self.consider_probe(self_ty, probe, possibly_unsatisfied_predicates))
+            })
+            .filter(|&(_, status)| status != ProbeResult::NoMatch)
             .collect();
 
         debug!("applicable_candidates: {:?}", applicable_candidates);
@@ -1236,18 +916,60 @@ fn consider_candidates(&self,
         }
 
         if applicable_candidates.len() > 1 {
-            let sources = probes.iter().map(|p| p.to_source()).collect();
+            let sources = probes.iter()
+                .map(|p| self.candidate_source(p, self_ty))
+                .collect();
             return Some(Err(MethodError::Ambiguity(sources)));
         }
 
-        applicable_candidates.pop().map(|probe| Ok(probe.to_unadjusted_pick()))
+        applicable_candidates.pop().map(|(probe, status)| {
+            if status == ProbeResult::Match {
+                Ok(probe.to_unadjusted_pick())
+            } else {
+                Err(MethodError::BadReturnType)
+            }
+        })
+    }
+
+    fn select_trait_candidate(&self, trait_ref: ty::TraitRef<'tcx>)
+                              -> traits::SelectionResult<'tcx, traits::Selection<'tcx>>
+    {
+        let cause = traits::ObligationCause::misc(self.span, self.body_id);
+        let predicate =
+            trait_ref.to_poly_trait_ref().to_poly_trait_predicate();
+        let obligation = traits::Obligation::new(cause, self.param_env, predicate);
+        traits::SelectionContext::new(self).select(&obligation)
+    }
+
+    fn candidate_source(&self, candidate: &Candidate<'tcx>, self_ty: Ty<'tcx>)
+                        -> CandidateSource
+    {
+        match candidate.kind {
+            InherentImplCandidate(..) => ImplSource(candidate.item.container.id()),
+            ObjectCandidate |
+            WhereClauseCandidate(_) => TraitSource(candidate.item.container.id()),
+            TraitCandidate(trait_ref) => self.probe(|_| {
+                let _ = self.at(&ObligationCause::dummy(), self.param_env)
+                    .sup(candidate.xform_self_ty, self_ty);
+                match self.select_trait_candidate(trait_ref) {
+                    Ok(Some(traits::Vtable::VtableImpl(ref impl_data))) => {
+                        // If only a single impl matches, make the error message point
+                        // to that impl.
+                        ImplSource(impl_data.impl_def_id)
+                    }
+                    _ => {
+                        TraitSource(candidate.item.container.id())
+                    }
+                }
+            })
+        }
     }
 
     fn consider_probe(&self,
                       self_ty: Ty<'tcx>,
                       probe: &Candidate<'tcx>,
                       possibly_unsatisfied_predicates: &mut Vec<TraitRef<'tcx>>)
-                      -> bool {
+                      -> ProbeResult {
         debug!("consider_probe: self_ty={:?} probe={:?}", self_ty, probe);
 
         self.probe(|_| {
@@ -1257,60 +979,102 @@ fn consider_probe(&self,
                 Ok(InferOk { obligations, value: () }) => obligations,
                 Err(_) => {
                     debug!("--> cannot relate self-types");
-                    return false;
+                    return ProbeResult::NoMatch;
                 }
             };
 
+            let mut result = ProbeResult::Match;
+            let selcx = &mut traits::SelectionContext::new(self);
+            let cause = traits::ObligationCause::misc(self.span, self.body_id);
+
             // If so, impls may carry other conditions (e.g., where
             // clauses) that must be considered. Make sure that those
             // match as well (or at least may match, sometimes we
             // don't have enough information to fully evaluate).
-            let (impl_def_id, substs, ref_obligations) = match probe.kind {
+            let candidate_obligations : Vec<_> = match probe.kind {
                 InherentImplCandidate(ref substs, ref ref_obligations) => {
-                    (probe.item.container.id(), substs, ref_obligations)
-                }
-
-                ExtensionImplCandidate(impl_def_id, ref substs, ref ref_obligations) => {
-                    (impl_def_id, substs, ref_obligations)
+                    // Check whether the impl imposes obligations we have to worry about.
+                    let impl_def_id = probe.item.container.id();
+                    let impl_bounds = self.tcx.predicates_of(impl_def_id);
+                    let impl_bounds = impl_bounds.instantiate(self.tcx, substs);
+                    let traits::Normalized { value: impl_bounds, obligations: norm_obligations } =
+                        traits::normalize(selcx, self.param_env, cause.clone(), &impl_bounds);
+
+                    // Convert the bounds into obligations.
+                    let impl_obligations = traits::predicates_for_generics(
+                        cause.clone(), self.param_env, &impl_bounds);
+
+                    debug!("impl_obligations={:?}", impl_obligations);
+                    impl_obligations.into_iter()
+                        .chain(norm_obligations.into_iter())
+                        .chain(ref_obligations.iter().cloned())
+                        .collect()
                 }
 
                 ObjectCandidate |
-                TraitCandidate |
                 WhereClauseCandidate(..) => {
                     // These have no additional conditions to check.
-                    return true;
+                    vec![]
                 }
-            };
 
-            let selcx = &mut traits::SelectionContext::new(self);
-            let cause = traits::ObligationCause::misc(self.span, self.body_id);
-
-            // Check whether the impl imposes obligations we have to worry about.
-            let impl_bounds = self.tcx.predicates_of(impl_def_id);
-            let impl_bounds = impl_bounds.instantiate(self.tcx, substs);
-            let traits::Normalized { value: impl_bounds, obligations: norm_obligations } =
-                traits::normalize(selcx, self.param_env, cause.clone(), &impl_bounds);
+                TraitCandidate(trait_ref) => {
+                    let predicate = trait_ref.to_predicate();
+                    let obligation =
+                        traits::Obligation::new(cause.clone(), self.param_env, predicate);
+                    if !selcx.evaluate_obligation(&obligation) {
+                        if self.probe(|_| self.select_trait_candidate(trait_ref).is_err()) {
+                            // This candidate's primary obligation doesn't even
+                            // select - don't bother registering anything in
+                            // `potentially_unsatisfied_predicates`.
+                            return ProbeResult::NoMatch;
+                        } else {
+                            // Some nested subobligation of this predicate
+                            // failed.
+                            //
+                            // FIXME: try to find the exact nested subobligation
+                            // and point at it rather than reporting the entire
+                            // trait-ref?
+                            result = ProbeResult::NoMatch;
+                            let trait_ref = self.resolve_type_vars_if_possible(&trait_ref);
+                            possibly_unsatisfied_predicates.push(trait_ref);
+                        }
+                    }
+                    vec![]
+                }
+            };
 
-            // Convert the bounds into obligations.
-            let obligations = traits::predicates_for_generics(cause.clone(),
-                                                              self.param_env,
-                                                              &impl_bounds);
-            debug!("impl_obligations={:?}", obligations);
+            debug!("consider_probe - candidate_obligations={:?} sub_obligations={:?}",
+                   candidate_obligations, sub_obligations);
 
             // Evaluate those obligations to see if they might possibly hold.
-            let mut all_true = true;
-            for o in obligations.iter()
-                .chain(sub_obligations.iter())
-                .chain(norm_obligations.iter())
-                .chain(ref_obligations.iter()) {
-                if !selcx.evaluate_obligation(o) {
-                    all_true = false;
+            for o in candidate_obligations.into_iter().chain(sub_obligations) {
+                let o = self.resolve_type_vars_if_possible(&o);
+                if !selcx.evaluate_obligation(&o) {
+                    result = ProbeResult::NoMatch;
                     if let &ty::Predicate::Trait(ref pred) = &o.predicate {
                         possibly_unsatisfied_predicates.push(pred.0.trait_ref);
                     }
                 }
             }
-            all_true
+
+            if let ProbeResult::Match = result {
+                if let (Some(return_ty), Some(xform_ret_ty)) =
+                    (self.return_type, probe.xform_ret_ty)
+                {
+                    let xform_ret_ty = self.resolve_type_vars_if_possible(&xform_ret_ty);
+                    debug!("comparing return_ty {:?} with xform ret ty {:?}",
+                           return_ty,
+                           probe.xform_ret_ty);
+                    if self.at(&ObligationCause::dummy(), self.param_env)
+                        .sup(return_ty, xform_ret_ty)
+                        .is_err()
+                    {
+                        return ProbeResult::BadReturnType;
+                    }
+                }
+            }
+
+            result
         })
     }
 
@@ -1331,22 +1095,25 @@ fn consider_probe(&self,
     ///
     /// Now imagine the receiver is `Vec<_>`. It doesn't really matter at this time which impl we
     /// use, so it's ok to just commit to "using the method from the trait Foo".
-    fn collapse_candidates_to_trait_pick(&self, probes: &[&Candidate<'tcx>]) -> Option<Pick<'tcx>> {
+    fn collapse_candidates_to_trait_pick(&self, probes: &[(&Candidate<'tcx>, ProbeResult)])
+                                         -> Option<Pick<'tcx>>
+    {
         // Do all probes correspond to the same trait?
-        let container = probes[0].item.container;
+        let container = probes[0].0.item.container;
         match container {
             ty::TraitContainer(_) => {}
             ty::ImplContainer(_) => return None,
         }
-        if probes[1..].iter().any(|p| p.item.container != container) {
+        if probes[1..].iter().any(|&(p, _)| p.item.container != container) {
             return None;
         }
 
+        // FIXME: check the return type here somehow.
         // If so, just use this trait and call it a day.
         Some(Pick {
-            item: probes[0].item.clone(),
+            item: probes[0].0.item.clone(),
             kind: TraitPick,
-            import_id: probes[0].import_id,
+            import_id: probes[0].0.import_id,
             autoderefs: 0,
             autoref: None,
             unsize: None,
@@ -1384,23 +1151,23 @@ fn xform_self_ty(&self,
                      item: &ty::AssociatedItem,
                      impl_ty: Ty<'tcx>,
                      substs: &Substs<'tcx>)
-                     -> Ty<'tcx> {
+                     -> (Ty<'tcx>, Option<Ty<'tcx>>) {
         if item.kind == ty::AssociatedKind::Method && self.mode == Mode::MethodCall {
-            self.xform_method_self_ty(item.def_id, impl_ty, substs)
+            let sig = self.xform_method_sig(item.def_id, substs);
+            (sig.inputs()[0], Some(sig.output()))
         } else {
-            impl_ty
+            (impl_ty, None)
         }
     }
 
-    fn xform_method_self_ty(&self,
-                            method: DefId,
-                            impl_ty: Ty<'tcx>,
-                            substs: &Substs<'tcx>)
-                            -> Ty<'tcx> {
-        let self_ty = self.tcx.fn_sig(method).input(0);
-        debug!("xform_self_ty(impl_ty={:?}, self_ty={:?}, substs={:?})",
-               impl_ty,
-               self_ty,
+    fn xform_method_sig(&self,
+                        method: DefId,
+                        substs: &Substs<'tcx>)
+                        -> ty::FnSig<'tcx>
+    {
+        let fn_sig = self.tcx.fn_sig(method);
+        debug!("xform_self_ty(fn_sig={:?}, substs={:?})",
+               fn_sig,
                substs);
 
         assert!(!substs.has_escaping_regions());
@@ -1416,10 +1183,10 @@ fn xform_method_self_ty(&self,
 
         // Erase any late-bound regions from the method and substitute
         // in the values from the substitution.
-        let xform_self_ty = self.erase_late_bound_regions(&self_ty);
+        let xform_fn_sig = self.erase_late_bound_regions(&fn_sig);
 
         if generics.types.is_empty() && generics.regions.is_empty() {
-            xform_self_ty.subst(self.tcx, substs)
+            xform_fn_sig.subst(self.tcx, substs)
         } else {
             let substs = Substs::for_item(self.tcx, method, |def, _| {
                 let i = def.index as usize;
@@ -1438,22 +1205,22 @@ fn xform_method_self_ty(&self,
                     self.type_var_for_def(self.span, def, cur_substs)
                 }
             });
-            xform_self_ty.subst(self.tcx, substs)
+            xform_fn_sig.subst(self.tcx, substs)
         }
     }
 
     /// Get the type of an impl and generate substitutions with placeholders.
     fn impl_ty_and_substs(&self, impl_def_id: DefId) -> (Ty<'tcx>, &'tcx Substs<'tcx>) {
-        let impl_ty = self.tcx.type_of(impl_def_id);
-
-        let substs = Substs::for_item(self.tcx,
-                                      impl_def_id,
-                                      |_, _| self.tcx.types.re_erased,
-                                      |_, _| self.next_ty_var(
-                                        TypeVariableOrigin::SubstitutionPlaceholder(
-                                            self.tcx.def_span(impl_def_id))));
+        (self.tcx.type_of(impl_def_id), self.fresh_item_substs(impl_def_id))
+    }
 
-        (impl_ty, substs)
+    fn fresh_item_substs(&self, def_id: DefId) -> &'tcx Substs<'tcx> {
+        Substs::for_item(self.tcx,
+                         def_id,
+                         |_, _| self.tcx.types.re_erased,
+                         |_, _| self.next_ty_var(
+                             TypeVariableOrigin::SubstitutionPlaceholder(
+                                 self.tcx.def_span(def_id))))
     }
 
     /// Replace late-bound-regions bound by `value` with `'static` using
@@ -1482,17 +1249,10 @@ fn erase_late_bound_regions<T>(&self, value: &ty::Binder<T>) -> T
 
     /// Find the method with the appropriate name (or return type, as the case may be).
     fn impl_or_trait_item(&self, def_id: DefId) -> Vec<ty::AssociatedItem> {
-        match self.looking_for {
-            LookingFor::MethodName(name) => {
-                self.fcx.associated_item(def_id, name).map_or(Vec::new(), |x| vec![x])
-            }
-            LookingFor::ReturnType(return_ty) => {
-                self.tcx
-                    .associated_items(def_id)
-                    .map(|did| self.tcx.associated_item(did.def_id))
-                    .filter(|m| self.matches_return_type(m, return_ty))
-                    .collect()
-            }
+        if let Some(name) = self.method_name {
+            self.fcx.associated_item(def_id, name).map_or(Vec::new(), |x| vec![x])
+        } else {
+            self.tcx.associated_items(def_id).collect()
         }
     }
 }
@@ -1503,9 +1263,8 @@ fn to_unadjusted_pick(&self) -> Pick<'tcx> {
             item: self.item.clone(),
             kind: match self.kind {
                 InherentImplCandidate(..) => InherentImplPick,
-                ExtensionImplCandidate(def_id, ..) => ExtensionImplPick(def_id),
                 ObjectCandidate => ObjectPick,
-                TraitCandidate => TraitPick,
+                TraitCandidate(_) => TraitPick,
                 WhereClauseCandidate(ref trait_ref) => {
                     // Only trait derived from where-clauses should
                     // appear here, so they should not contain any
@@ -1523,14 +1282,4 @@ fn to_unadjusted_pick(&self) -> Pick<'tcx> {
             unsize: None,
         }
     }
-
-    fn to_source(&self) -> CandidateSource {
-        match self.kind {
-            InherentImplCandidate(..) => ImplSource(self.item.container.id()),
-            ExtensionImplCandidate(def_id, ..) => ImplSource(def_id),
-            ObjectCandidate |
-            TraitCandidate |
-            WhereClauseCandidate(_) => TraitSource(self.item.container.id()),
-        }
-    }
 }
index c8b828f3a434dca6cb9d3a103665d20e5b3cb0c1..f2d7842e473f61df3e943edf823badbbc8d3f147 100644 (file)
@@ -296,22 +296,6 @@ macro_rules! report_function {
                 err.emit();
             }
 
-            MethodError::ClosureAmbiguity(trait_def_id) => {
-                let msg = format!("the `{}` method from the `{}` trait cannot be explicitly \
-                                   invoked on this closure as we have not yet inferred what \
-                                   kind of closure it is",
-                                  item_name,
-                                  self.tcx.item_path_str(trait_def_id));
-                let msg = if let Some(callee) = rcvr_expr {
-                    format!("{}; use overloaded call notation instead (e.g., `{}()`)",
-                            msg,
-                            self.tcx.hir.node_to_pretty_string(callee.id))
-                } else {
-                    msg
-                };
-                self.sess().span_err(span, &msg);
-            }
-
             MethodError::PrivateMatch(def, out_of_scope_traits) => {
                 let mut err = struct_span_err!(self.tcx.sess, span, E0624,
                                                "{} `{}` is private", def.kind_name(), item_name);
@@ -337,6 +321,10 @@ macro_rules! report_function {
                 }
                 err.emit();
             }
+
+            MethodError::BadReturnType => {
+                bug!("no return type expectations but got BadReturnType")
+            }
         }
     }
 
index dc362b4d953d2ad6676cd041e8cf38fad0feb3bf..bd362c3535373d622c0ae2a13a3b857b435e06e3 100644 (file)
@@ -2818,6 +2818,7 @@ fn expected_inputs_for_expected_output(&self,
                                            formal_ret: Ty<'tcx>,
                                            formal_args: &[Ty<'tcx>])
                                            -> Vec<Ty<'tcx>> {
+        let formal_ret = self.resolve_type_vars_with_obligations(formal_ret);
         let expected_args = expected_ret.only_has_type(self).and_then(|ret_ty| {
             self.fudge_regions_if_ok(&RegionVariableOrigin::Coercion(call_span), || {
                 // Attempt to apply a subtyping relationship between the formal
@@ -3978,6 +3979,7 @@ fn check_expr_kind(&self,
           }
           hir::ExprTup(ref elts) => {
             let flds = expected.only_has_type(self).and_then(|ty| {
+                let ty = self.resolve_type_vars_with_obligations(ty);
                 match ty.sty {
                     ty::TyTuple(ref flds, _) => Some(&flds[..]),
                     _ => None
index 988890ffedcdd92d0e0cbb15d5e8645d558ce371..10a3878073e97534da24dbe9268891d2a3f28d64 100644 (file)
@@ -228,7 +228,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         }
 
         if end_newline {
-            //add a space so stripping <br> tags and breaking spaces still renders properly
+            // add a space so stripping <br> tags and breaking spaces still renders properly
             if f.alternate() {
                 clause.push(' ');
             } else {
index eb27fa3abfa19a240bd9d123e9af1288c11be5d7..e6b236deac4eed88911e35dbd9870f11ed038fb3 100644 (file)
@@ -172,6 +172,21 @@ pub fn new(lexer: lexer::StringReader<'a>, codemap: &'a CodeMap) -> Classifier<'
         }
     }
 
+    /// Gets the next token out of the lexer, emitting fatal errors if lexing fails.
+    fn try_next_token(&mut self) -> io::Result<TokenAndSpan> {
+        match self.lexer.try_next_token() {
+            Ok(tas) => Ok(tas),
+            Err(_) => {
+                self.lexer.emit_fatal_errors();
+                self.lexer.sess.span_diagnostic
+                    .struct_warn("Backing out of syntax highlighting")
+                    .note("You probably did not intend to render this as a rust code-block")
+                    .emit();
+                Err(io::Error::new(io::ErrorKind::Other, ""))
+            }
+        }
+    }
+
     /// Exhausts the `lexer` writing the output into `out`.
     ///
     /// The general structure for this method is to iterate over each token,
@@ -183,18 +198,7 @@ pub fn write_source<W: Writer>(&mut self,
                                    out: &mut W)
                                    -> io::Result<()> {
         loop {
-            let next = match self.lexer.try_next_token() {
-                Ok(tas) => tas,
-                Err(_) => {
-                    self.lexer.emit_fatal_errors();
-                    self.lexer.sess.span_diagnostic
-                        .struct_warn("Backing out of syntax highlighting")
-                        .note("You probably did not intend to render this as a rust code-block")
-                        .emit();
-                    return Err(io::Error::new(io::ErrorKind::Other, ""));
-                }
-            };
-
+            let next = self.try_next_token()?;
             if next.tok == token::Eof {
                 break;
             }
@@ -255,13 +259,37 @@ fn write_token<W: Writer>(&mut self,
                 }
             }
 
-            // This is the start of an attribute. We're going to want to
+            // This might be the start of an attribute. We're going to want to
             // continue highlighting it as an attribute until the ending ']' is
             // seen, so skip out early. Down below we terminate the attribute
             // span when we see the ']'.
             token::Pound => {
-                self.in_attribute = true;
-                out.enter_span(Class::Attribute)?;
+                // We can't be sure that our # begins an attribute (it could
+                // just be appearing in a macro) until we read either `#![` or
+                // `#[` from the input stream.
+                //
+                // We don't want to start highlighting as an attribute until
+                // we're confident there is going to be a ] coming up, as
+                // otherwise # tokens in macros highlight the rest of the input
+                // as an attribute.
+
+                // Case 1: #![inner_attribute]
+                if self.lexer.peek().tok == token::Not {
+                    self.try_next_token()?; // NOTE: consumes `!` token!
+                    if self.lexer.peek().tok == token::OpenDelim(token::Bracket) {
+                        self.in_attribute = true;
+                        out.enter_span(Class::Attribute)?;
+                    }
+                    out.string("#", Class::None, None)?;
+                    out.string("!", Class::None, None)?;
+                    return Ok(());
+                }
+
+                // Case 2: #[outer_attribute]
+                if self.lexer.peek().tok == token::OpenDelim(token::Bracket) {
+                    self.in_attribute = true;
+                    out.enter_span(Class::Attribute)?;
+                }
                 out.string("#", Class::None, None)?;
                 return Ok(());
             }
index 5457f69cb6dab02bf9c3d420a83f94533909e4c7..5b8c7503a791d83d15e46c7ed153afe4011c2a76 100644 (file)
@@ -1523,8 +1523,7 @@ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
                 } else {
                     write!(fmt, "Module ")?;
                 },
-            clean::FunctionItem(..) | clean::ForeignFunctionItem(..) =>
-                write!(fmt, "Function ")?,
+            clean::FunctionItem(..) | clean::ForeignFunctionItem(..) => write!(fmt, "Function ")?,
             clean::TraitItem(..) => write!(fmt, "Trait ")?,
             clean::StructItem(..) => write!(fmt, "Struct ")?,
             clean::UnionItem(..) => write!(fmt, "Union ")?,
@@ -1532,8 +1531,7 @@ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
             clean::TypedefItem(..) => write!(fmt, "Type Definition ")?,
             clean::MacroItem(..) => write!(fmt, "Macro ")?,
             clean::PrimitiveItem(..) => write!(fmt, "Primitive Type ")?,
-            clean::StaticItem(..) | clean::ForeignStaticItem(..) =>
-                write!(fmt, "Static ")?,
+            clean::StaticItem(..) | clean::ForeignStaticItem(..) => write!(fmt, "Static ")?,
             clean::ConstantItem(..) => write!(fmt, "Constant ")?,
             _ => {
                 // We don't generate pages for any other type.
index 4a3286b421ae9e40f87be7297b217da86639ee2c..312dfce8d39c27215ca9b31cdfddd8fd7ef1d2cf 100644 (file)
@@ -329,6 +329,10 @@ h4 > code, h3 > code, .invisible > code {
        display: inline-block;
 }
 
+.in-band > code {
+       display: inline-block;
+}
+
 #main { position: relative; }
 #main > .since {
        top: inherit;
@@ -447,7 +451,8 @@ a {
 }
 
 .in-band:hover > .anchor {
-       display: initial;
+       display: inline-block;
+       position: absolute;
 }
 .anchor {
        display: none;
index 36c06dc0b58d057d8a0f6df048b48e8837ebfafd..9ef19cd64b386f89a6ff7be7e60e8b795d16c80e 100644 (file)
@@ -705,30 +705,74 @@ fn hash<H: hash::Hasher>(&self, s: &mut H) {
 ///
 /// # Examples
 ///
+/// Creating a [`SocketAddr`] iterator that yields one item:
+///
+/// ```
+/// use std::net::{ToSocketAddrs, SocketAddr};
+///
+/// let addr = SocketAddr::from(([127, 0, 0, 1], 443));
+/// let mut addrs_iter = addr.to_socket_addrs().unwrap();
+///
+/// assert_eq!(Some(addr), addrs_iter.next());
+/// assert!(addrs_iter.next().is_none());
+/// ```
+///
+/// Creating a [`SocketAddr`] iterator from a hostname:
+///
 /// ```no_run
-/// use std::net::{SocketAddrV4, TcpStream, UdpSocket, TcpListener, Ipv4Addr};
-///
-/// fn main() {
-///     let ip = Ipv4Addr::new(127, 0, 0, 1);
-///     let port = 12345;
-///
-///     // The following lines are equivalent modulo possible "localhost" name
-///     // resolution differences
-///     let tcp_s = TcpStream::connect(SocketAddrV4::new(ip, port));
-///     let tcp_s = TcpStream::connect((ip, port));
-///     let tcp_s = TcpStream::connect(("127.0.0.1", port));
-///     let tcp_s = TcpStream::connect(("localhost", port));
-///     let tcp_s = TcpStream::connect("127.0.0.1:12345");
-///     let tcp_s = TcpStream::connect("localhost:12345");
-///
-///     // TcpListener::bind(), UdpSocket::bind() and UdpSocket::send_to()
-///     // behave similarly
-///     let tcp_l = TcpListener::bind("localhost:12345");
-///
-///     let mut udp_s = UdpSocket::bind(("127.0.0.1", port)).unwrap();
-///     udp_s.send_to(&[7], (ip, 23451)).unwrap();
-/// }
+/// use std::net::{SocketAddr, ToSocketAddrs};
+///
+/// // assuming 'localhost' resolves to 127.0.0.1
+/// let mut addrs_iter = "localhost:443".to_socket_addrs().unwrap();
+/// assert_eq!(addrs_iter.next(), Some(SocketAddr::from(([127, 0, 0, 1], 443))));
+/// assert!(addrs_iter.next().is_none());
+///
+/// // assuming 'foo' does not resolve
+/// assert!("foo:443".to_socket_addrs().is_err());
 /// ```
+///
+/// Creating a [`SocketAddr`] iterator that yields multiple items:
+///
+/// ```
+/// use std::net::{SocketAddr, ToSocketAddrs};
+///
+/// let addr1 = SocketAddr::from(([0, 0, 0, 0], 80));
+/// let addr2 = SocketAddr::from(([127, 0, 0, 1], 443));
+/// let addrs = vec![addr1, addr2];
+///
+/// let mut addrs_iter = (&addrs[..]).to_socket_addrs().unwrap();
+///
+/// assert_eq!(Some(addr1), addrs_iter.next());
+/// assert_eq!(Some(addr2), addrs_iter.next());
+/// assert!(addrs_iter.next().is_none());
+/// ```
+///
+/// Attempting to create a [`SocketAddr`] iterator from an improperly formatted
+/// socket address `&str` (missing the port):
+///
+/// ```
+/// use std::io;
+/// use std::net::ToSocketAddrs;
+///
+/// let err = "127.0.0.1".to_socket_addrs().unwrap_err();
+/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
+/// ```
+///
+/// [`TcpStream::connect`] is an example of an function that utilizes
+/// `ToSocketsAddr` as a trait bound on its parameter in order to accept
+/// different types:
+///
+/// ```no_run
+/// use std::net::{TcpStream, Ipv4Addr};
+///
+/// let stream = TcpStream::connect(("127.0.0.1", 443));
+/// // or
+/// let stream = TcpStream::connect("127.0.0.1.443");
+/// // or
+/// let stream = TcpStream::connect((Ipv4Addr::new(127, 0, 0, 1), 443));
+/// ```
+///
+/// [`TcpStream::connect`]: ../../std/net/struct.TcpStream.html#method.connect
 #[stable(feature = "rust1", since = "1.0.0")]
 pub trait ToSocketAddrs {
     /// Returned iterator over socket addresses which this type may correspond
diff --git a/src/test/compile-fail/unboxed-closures-infer-explicit-call-too-early.rs b/src/test/compile-fail/unboxed-closures-infer-explicit-call-too-early.rs
deleted file mode 100644 (file)
index 62f6ee5..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-fn main() {
-    let mut zero = || {};
-    let () = zero.call_mut(());
-    //~^ ERROR we have not yet inferred what kind of closure it is
-}
diff --git a/src/test/run-pass/method-argument-inference-associated-type.rs b/src/test/run-pass/method-argument-inference-associated-type.rs
new file mode 100644 (file)
index 0000000..76b8cf9
--- /dev/null
@@ -0,0 +1,37 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+pub struct ClientMap;
+pub struct ClientMap2;
+
+pub trait Service {
+    type Request;
+    fn call(&self, _req: Self::Request);
+}
+
+pub struct S<T>(T);
+
+impl Service for ClientMap {
+    type Request = S<Box<Fn(i32)>>;
+    fn call(&self, _req: Self::Request) {}
+}
+
+
+impl Service for ClientMap2 {
+    type Request = (Box<Fn(i32)>,);
+    fn call(&self, _req: Self::Request) {}
+}
+
+
+fn main() {
+    ClientMap.call(S { 0: Box::new(|_msgid| ()) });
+    ClientMap.call(S(Box::new(|_msgid| ())));
+    ClientMap2.call((Box::new(|_msgid| ()),));
+}
diff --git a/src/test/run-pass/unboxed-closures-infer-explicit-call-early.rs b/src/test/run-pass/unboxed-closures-infer-explicit-call-early.rs
new file mode 100644 (file)
index 0000000..028f2e9
--- /dev/null
@@ -0,0 +1,17 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(fn_traits)]
+
+fn main() {
+    let mut zero = || 0;
+    let x = zero.call_mut(());
+    assert_eq!(x, 0);
+}
index 3933b8bcbb8fdf6762cacf3b3be0ff0f84b4cb5e..991cf4cf2b375aae64d296717d4bc304f7fe79f1 100644 (file)
 // @!has - 'space'
 // @!has - 'comment'
 // @has - '# <span class="ident">single'
-// @has - '#<span class="attribute"># <span class="ident">double</span>'
-// @has - '#<span class="attribute">#<span class="attribute"># <span class="ident">triple</span>'
+// @has - '## <span class="ident">double</span>'
+// @has - '### <span class="ident">triple</span>'
+// @has - '<span class="attribute">#[<span class="ident">outer</span>]</span>'
+// @has - '<span class="attribute">#![<span class="ident">inner</span>]</span>'
 
 /// ```no_run
 /// # # space
@@ -21,5 +23,7 @@
 /// ## single
 /// ### double
 /// #### triple
+/// ##[outer]
+/// ##![inner]
 /// ```
 pub struct Foo;
index fc441f94842731d7b65ca8044615e91d6c1168e4..23f115858cd5e6b53c66cb24ddf3d9b7e4d58668 100644 (file)
@@ -8,6 +8,8 @@ error[E0599]: no method named `method` found for type `u32` in the current scope
    = note: the following traits are implemented but not in scope, perhaps add a `use` for one of them:
            candidate #1: `use foo::Bar;`
            candidate #2: `use no_method_suggested_traits::foo::PubPub;`
+           candidate #3: `use no_method_suggested_traits::qux::PrivPub;`
+           candidate #4: `use no_method_suggested_traits::Reexported;`
 
 error[E0599]: no method named `method` found for type `std::rc::Rc<&mut std::boxed::Box<&u32>>` in the current scope
   --> $DIR/no-method-suggested-traits.rs:38:44
@@ -19,6 +21,8 @@ error[E0599]: no method named `method` found for type `std::rc::Rc<&mut std::box
    = note: the following traits are implemented but not in scope, perhaps add a `use` for one of them:
            candidate #1: `use foo::Bar;`
            candidate #2: `use no_method_suggested_traits::foo::PubPub;`
+           candidate #3: `use no_method_suggested_traits::qux::PrivPub;`
+           candidate #4: `use no_method_suggested_traits::Reexported;`
 
 error[E0599]: no method named `method` found for type `char` in the current scope
   --> $DIR/no-method-suggested-traits.rs:44:9
index 174f7dfa0d0f5f897b93ecbb14edb66735457329..e2e2019307f34f53160b3148a781273cf12ccae2 100644 (file)
@@ -5,8 +5,8 @@ error[E0599]: no method named `count` found for type `std::iter::Filter<std::ite
    |                                                       ^^^^^
    |
    = note: the method `count` exists but the following trait bounds were not satisfied:
-           `[closure@$DIR/issue-36053-2.rs:17:39: 17:53] : std::ops::FnMut<(&_,)>`
            `std::iter::Filter<std::iter::Fuse<std::iter::Once<&str>>, [closure@$DIR/issue-36053-2.rs:17:39: 17:53]> : std::iter::Iterator`
+           `&mut std::iter::Filter<std::iter::Fuse<std::iter::Once<&str>>, [closure@$DIR/issue-36053-2.rs:17:39: 17:53]> : std::iter::Iterator`
 
 error[E0281]: type mismatch: `[closure@$DIR/issue-36053-2.rs:17:39: 17:53]` implements the trait `for<'r> std::ops::FnMut<(&'r str,)>`, but the trait `for<'r> std::ops::FnMut<(&'r &str,)>` is required
   --> $DIR/issue-36053-2.rs:17:32
diff --git a/src/test/ui/suggestions/issue-43420-no-over-suggest.rs b/src/test/ui/suggestions/issue-43420-no-over-suggest.rs
new file mode 100644 (file)
index 0000000..d504b7c
--- /dev/null
@@ -0,0 +1,19 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// check that we substitute type parameters before we suggest anything - otherwise
+// we would suggest function such as `as_slice` for the `&[u16]`.
+
+fn foo(b: &[u16]) {}
+
+fn main() {
+    let a: Vec<u8> = Vec::new();
+    foo(&a);
+}
diff --git a/src/test/ui/suggestions/issue-43420-no-over-suggest.stderr b/src/test/ui/suggestions/issue-43420-no-over-suggest.stderr
new file mode 100644 (file)
index 0000000..bcad9ce
--- /dev/null
@@ -0,0 +1,11 @@
+error[E0308]: mismatched types
+  --> $DIR/issue-43420-no-over-suggest.rs:18:9
+   |
+18 |     foo(&a);
+   |         ^^ expected slice, found struct `std::vec::Vec`
+   |
+   = note: expected type `&[u16]`
+              found type `&std::vec::Vec<u8>`
+
+error: aborting due to previous error
+
index 7a09ae48b91bd746aec2d28749d1b7081142eab3..e2be021e7cc39357608a9e9d67ee89a3b0d81cc2 100644 (file)
@@ -239,7 +239,12 @@ fn build_manifest(&mut self) -> Manifest {
         self.package("rust-std", &mut manifest.pkg, TARGETS);
         self.package("rust-docs", &mut manifest.pkg, TARGETS);
         self.package("rust-src", &mut manifest.pkg, &["*"]);
-        self.package("rls", &mut manifest.pkg, HOSTS);
+        let rls_package_name = if self.rust_release == "nightly" {
+            "rls"
+        } else {
+            "rls-preview"
+        };
+        self.package(rls_package_name, &mut manifest.pkg, HOSTS);
         self.package("rust-analysis", &mut manifest.pkg, TARGETS);
 
         let mut pkg = Package {
@@ -276,7 +281,7 @@ fn build_manifest(&mut self) -> Manifest {
             }
 
             extensions.push(Component {
-                pkg: "rls".to_string(),
+                pkg: rls_package_name.to_string(),
                 target: host.to_string(),
             });
             extensions.push(Component {
@@ -353,7 +358,7 @@ fn filename(&self, component: &str, target: &str) -> String {
             format!("rust-src-{}.tar.gz", self.rust_release)
         } else if component == "cargo" {
             format!("cargo-{}-{}.tar.gz", self.cargo_release, target)
-        } else if component == "rls" {
+        } else if component == "rls" || component == "rls-preview" {
             format!("rls-{}-{}.tar.gz", self.rls_release, target)
         } else {
             format!("{}-{}-{}.tar.gz", component, self.rust_release, target)
@@ -363,7 +368,7 @@ fn filename(&self, component: &str, target: &str) -> String {
     fn cached_version(&self, component: &str) -> &str {
         if component == "cargo" {
             &self.cargo_version
-        } else if component == "rls" {
+        } else if component == "rls" || component == "rls-preview" {
             &self.rls_version
         } else {
             &self.rust_version
index 0d6b350a1d431b1b5ff2eb2c6c1ea9b58db72f25..cee7e52c7f3c63f23edce8c7bde77322cc90ab26 100644 (file)
@@ -83,117 +83,117 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 
 #[derive(Clone)]
 pub struct Config {
-    // The library paths required for running the compiler
+    /// The library paths required for running the compiler
     pub compile_lib_path: PathBuf,
 
-    // The library paths required for running compiled programs
+    /// The library paths required for running compiled programs
     pub run_lib_path: PathBuf,
 
-    // The rustc executable
+    /// The rustc executable
     pub rustc_path: PathBuf,
 
-    // The rustdoc executable
+    /// The rustdoc executable
     pub rustdoc_path: Option<PathBuf>,
 
-    // The python executable to use for LLDB
+    /// The python executable to use for LLDB
     pub lldb_python: String,
 
-    // The python executable to use for htmldocck
+    /// The python executable to use for htmldocck
     pub docck_python: String,
 
-    // The llvm FileCheck binary path
+    /// The llvm FileCheck binary path
     pub llvm_filecheck: Option<PathBuf>,
 
-    // The valgrind path
+    /// The valgrind path
     pub valgrind_path: Option<String>,
 
-    // Whether to fail if we can't run run-pass-valgrind tests under valgrind
-    // (or, alternatively, to silently run them like regular run-pass tests).
+    /// Whether to fail if we can't run run-pass-valgrind tests under valgrind
+    /// (or, alternatively, to silently run them like regular run-pass tests).
     pub force_valgrind: bool,
 
-    // The directory containing the tests to run
+    /// The directory containing the tests to run
     pub src_base: PathBuf,
 
-    // The directory where programs should be built
+    /// The directory where programs should be built
     pub build_base: PathBuf,
 
-    // The name of the stage being built (stage1, etc)
+    /// The name of the stage being built (stage1, etc)
     pub stage_id: String,
 
-    // The test mode, compile-fail, run-fail, run-pass
+    /// The test mode, compile-fail, run-fail, run-pass
     pub mode: Mode,
 
-    // Run ignored tests
+    /// Run ignored tests
     pub run_ignored: bool,
 
-    // Only run tests that match this filter
+    /// Only run tests that match this filter
     pub filter: Option<String>,
 
-    // Exactly match the filter, rather than a substring
+    /// Exactly match the filter, rather than a substring
     pub filter_exact: bool,
 
-    // Write out a parseable log of tests that were run
+    /// Write out a parseable log of tests that were run
     pub logfile: Option<PathBuf>,
 
-    // A command line to prefix program execution with,
-    // for running under valgrind
+    /// A command line to prefix program execution with,
+    /// for running under valgrind
     pub runtool: Option<String>,
 
-    // Flags to pass to the compiler when building for the host
+    /// Flags to pass to the compiler when building for the host
     pub host_rustcflags: Option<String>,
 
-    // Flags to pass to the compiler when building for the target
+    /// Flags to pass to the compiler when building for the target
     pub target_rustcflags: Option<String>,
 
-    // Target system to be tested
+    /// Target system to be tested
     pub target: String,
 
-    // Host triple for the compiler being invoked
+    /// Host triple for the compiler being invoked
     pub host: String,
 
-    // Path to / name of the GDB executable
+    /// Path to / name of the GDB executable
     pub gdb: Option<String>,
 
-    // Version of GDB, encoded as ((major * 1000) + minor) * 1000 + patch
+    /// Version of GDB, encoded as ((major * 1000) + minor) * 1000 + patch
     pub gdb_version: Option<u32>,
 
-    // Whether GDB has native rust support
+    /// Whether GDB has native rust support
     pub gdb_native_rust: bool,
 
-    // Version of LLDB
+    /// Version of LLDB
     pub lldb_version: Option<String>,
 
-    // Version of LLVM
+    /// Version of LLVM
     pub llvm_version: Option<String>,
 
-    // Is LLVM a system LLVM
+    /// Is LLVM a system LLVM
     pub system_llvm: bool,
 
-    // Path to the android tools
+    /// Path to the android tools
     pub android_cross_path: PathBuf,
 
-    // Extra parameter to run adb on arm-linux-androideabi
+    /// Extra parameter to run adb on arm-linux-androideabi
     pub adb_path: String,
 
-    // Extra parameter to run test suite on arm-linux-androideabi
+    /// Extra parameter to run test suite on arm-linux-androideabi
     pub adb_test_dir: String,
 
-    // status whether android device available or not
+    /// status whether android device available or not
     pub adb_device_status: bool,
 
-    // the path containing LLDB's Python module
+    /// the path containing LLDB's Python module
     pub lldb_python_dir: Option<String>,
 
-    // Explain what's going on
+    /// Explain what's going on
     pub verbose: bool,
 
-    // Print one character per test instead of one line
+    /// Print one character per test instead of one line
     pub quiet: bool,
 
-    // Whether to use colors in test.
+    /// Whether to use colors in test.
     pub color: ColorConfig,
 
-    // where to find the remote test client process, if we're using it
+    /// where to find the remote test client process, if we're using it
     pub remote_test_client: Option<PathBuf>,
 
     // Configuration for various run-make tests frobbing things like C compilers