@@ -1958,26 +1958,6 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
19581958 if predicate. references_error ( ) {
19591959 return ;
19601960 }
1961- // Typically, this ambiguity should only happen if
1962- // there are unresolved type inference variables
1963- // (otherwise it would suggest a coherence
1964- // failure). But given #21974 that is not necessarily
1965- // the case -- we can have multiple where clauses that
1966- // are only distinguished by a region, which results
1967- // in an ambiguity even when all types are fully
1968- // known, since we don't dispatch based on region
1969- // relationships.
1970-
1971- // Pick the first substitution that still contains inference variables as the one
1972- // we're going to emit an error for. If there are none (see above), fall back to
1973- // the substitution for `Self`.
1974- let subst = {
1975- let substs = data. trait_ref . substs ;
1976- substs
1977- . iter ( )
1978- . find ( |s| s. has_infer_types_or_consts ( ) )
1979- . unwrap_or_else ( || substs[ 0 ] )
1980- } ;
19811961
19821962 // This is kind of a hack: it frequently happens that some earlier
19831963 // error prevents types from being fully inferred, and then we get
@@ -1999,27 +1979,54 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
19991979 self . emit_inference_failure_err (
20001980 body_id,
20011981 span,
2002- subst ,
1982+ trait_ref . self_ty ( ) . skip_binder ( ) . into ( ) ,
20031983 vec ! [ ] ,
20041984 ErrorCode :: E0282 ,
1985+ false ,
20051986 )
20061987 . emit ( ) ;
20071988 }
20081989 return ;
20091990 }
20101991
2011- let impl_candidates = self
2012- . find_similar_impl_candidates ( trait_ref)
2013- . into_iter ( )
2014- . map ( |candidate| candidate. trait_ref )
2015- . collect ( ) ;
2016- let mut err = self . emit_inference_failure_err (
2017- body_id,
2018- span,
2019- subst,
2020- impl_candidates,
2021- ErrorCode :: E0283 ,
2022- ) ;
1992+ // Typically, this ambiguity should only happen if
1993+ // there are unresolved type inference variables
1994+ // (otherwise it would suggest a coherence
1995+ // failure). But given #21974 that is not necessarily
1996+ // the case -- we can have multiple where clauses that
1997+ // are only distinguished by a region, which results
1998+ // in an ambiguity even when all types are fully
1999+ // known, since we don't dispatch based on region
2000+ // relationships.
2001+
2002+ // Pick the first substitution that still contains inference variables as the one
2003+ // we're going to emit an error for. If there are none (see above), fall back to
2004+ // a more general error.
2005+ let subst = data. trait_ref . substs . iter ( ) . find ( |s| s. has_infer_types_or_consts ( ) ) ;
2006+
2007+ let mut err = if let Some ( subst) = subst {
2008+ let impl_candidates = self
2009+ . find_similar_impl_candidates ( trait_ref)
2010+ . into_iter ( )
2011+ . map ( |candidate| candidate. trait_ref )
2012+ . collect ( ) ;
2013+ self . emit_inference_failure_err (
2014+ body_id,
2015+ span,
2016+ subst,
2017+ impl_candidates,
2018+ ErrorCode :: E0283 ,
2019+ true ,
2020+ )
2021+ } else {
2022+ struct_span_err ! (
2023+ self . tcx. sess,
2024+ span,
2025+ E0283 ,
2026+ "type annotations needed: cannot satisfy `{}`" ,
2027+ predicate,
2028+ )
2029+ } ;
20232030
20242031 let obligation = Obligation :: new (
20252032 obligation. cause . clone ( ) ,
@@ -2110,7 +2117,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
21102117 return ;
21112118 }
21122119
2113- self . emit_inference_failure_err ( body_id, span, arg, vec ! [ ] , ErrorCode :: E0282 )
2120+ self . emit_inference_failure_err ( body_id, span, arg, vec ! [ ] , ErrorCode :: E0282 , false )
21142121 }
21152122
21162123 ty:: PredicateKind :: Subtype ( data) => {
@@ -2124,26 +2131,38 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
21242131 let SubtypePredicate { a_is_expected : _, a, b } = data;
21252132 // both must be type variables, or the other would've been instantiated
21262133 assert ! ( a. is_ty_var( ) && b. is_ty_var( ) ) ;
2127- self . emit_inference_failure_err ( body_id, span, a. into ( ) , vec ! [ ] , ErrorCode :: E0282 )
2134+ self . emit_inference_failure_err (
2135+ body_id,
2136+ span,
2137+ a. into ( ) ,
2138+ vec ! [ ] ,
2139+ ErrorCode :: E0282 ,
2140+ true ,
2141+ )
21282142 }
21292143 ty:: PredicateKind :: Projection ( data) => {
2130- let self_ty = data. projection_ty . self_ty ( ) ;
2131- let term = data. term ;
21322144 if predicate. references_error ( ) || self . is_tainted_by_errors ( ) {
21332145 return ;
21342146 }
2135- if self_ty. needs_infer ( ) && term. needs_infer ( ) {
2136- // We do this for the `foo.collect()?` case to produce a suggestion.
2147+ let subst = data
2148+ . projection_ty
2149+ . substs
2150+ . iter ( )
2151+ . chain ( Some ( data. term . into_arg ( ) ) )
2152+ . find ( |g| g. has_infer_types_or_consts ( ) ) ;
2153+ if let Some ( subst) = subst {
21372154 let mut err = self . emit_inference_failure_err (
21382155 body_id,
21392156 span,
2140- self_ty . into ( ) ,
2157+ subst ,
21412158 vec ! [ ] ,
21422159 ErrorCode :: E0284 ,
2160+ true ,
21432161 ) ;
21442162 err. note ( & format ! ( "cannot satisfy `{}`" , predicate) ) ;
21452163 err
21462164 } else {
2165+ // If we can't find a substitution, just print a generic error
21472166 let mut err = struct_span_err ! (
21482167 self . tcx. sess,
21492168 span,
0 commit comments