From aab144f6d12101b202b42ceea329dc0ba5dcfe2a Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Thu, 2 Apr 2020 14:05:04 +0200 Subject: [PATCH] update select docs --- src/librustc_middle/traits/select.rs | 32 ++++++++++++++++------------ 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/librustc_middle/traits/select.rs b/src/librustc_middle/traits/select.rs index d316d7659e22..822fbfa8ca66 100644 --- a/src/librustc_middle/traits/select.rs +++ b/src/librustc_middle/traits/select.rs @@ -34,7 +34,7 @@ impl<'tcx> SelectionCache<'tcx> { /// clauses, and so forth that might resolve an obligation. Sometimes /// we'll be able to say definitively that (e.g.) an impl does not /// apply to the obligation: perhaps it is defined for `usize` but the -/// obligation is for `int`. In that case, we drop the impl out of the +/// obligation is for `i32`. In that case, we drop the impl out of the /// list. But the other cases are considered *candidates*. /// /// For selection to succeed, there must be exactly one matching @@ -54,12 +54,14 @@ impl<'tcx> SelectionCache<'tcx> { /// will always be satisfied) picking the blanket impl will be wrong /// for at least *some* substitutions. To make this concrete, if we have /// -/// trait AsDebug { type Out : fmt::Debug; fn debug(self) -> Self::Out; } -/// impl AsDebug for T { -/// type Out = T; -/// fn debug(self) -> fmt::Debug { self } -/// } -/// fn foo(t: T) { println!("{:?}", ::debug(t)); } +/// ```rust, ignore +/// trait AsDebug { type Out: fmt::Debug; fn debug(self) -> Self::Out; } +/// impl AsDebug for T { +/// type Out = T; +/// fn debug(self) -> fmt::Debug { self } +/// } +/// fn foo(t: T) { println!("{:?}", ::debug(t)); } +/// ``` /// /// we can't just use the impl to resolve the `` obligation /// -- a type from another crate (that doesn't implement `fmt::Debug`) could @@ -79,14 +81,16 @@ impl<'tcx> SelectionCache<'tcx> { /// inference variables. The can lead to inference making "leaps of logic", /// for example in this situation: /// -/// pub trait Foo { fn foo(&self) -> T; } -/// impl Foo<()> for T { fn foo(&self) { } } -/// impl Foo for bool { fn foo(&self) -> bool { *self } } +/// ```rust, ignore +/// pub trait Foo { fn foo(&self) -> T; } +/// impl Foo<()> for T { fn foo(&self) { } } +/// impl Foo for bool { fn foo(&self) -> bool { *self } } /// -/// pub fn foo(t: T) where T: Foo { -/// println!("{:?}", >::foo(&t)); -/// } -/// fn main() { foo(false); } +/// pub fn foo(t: T) where T: Foo { +/// println!("{:?}", >::foo(&t)); +/// } +/// fn main() { foo(false); } +/// ``` /// /// Here the obligation `>` can be matched by both the blanket /// impl and the where-clause. We select the where-clause and unify `$0=bool`,