@@ -1958,26 +1958,6 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
1958
1958
if predicate. references_error ( ) {
1959
1959
return ;
1960
1960
}
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
- } ;
1981
1961
1982
1962
// This is kind of a hack: it frequently happens that some earlier
1983
1963
// 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> {
1999
1979
self . emit_inference_failure_err (
2000
1980
body_id,
2001
1981
span,
2002
- subst ,
1982
+ trait_ref . self_ty ( ) . skip_binder ( ) . into ( ) ,
2003
1983
vec ! [ ] ,
2004
1984
ErrorCode :: E0282 ,
1985
+ false ,
2005
1986
)
2006
1987
. emit ( ) ;
2007
1988
}
2008
1989
return ;
2009
1990
}
2010
1991
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
+ } ;
2023
2030
2024
2031
let obligation = Obligation :: new (
2025
2032
obligation. cause . clone ( ) ,
@@ -2110,7 +2117,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
2110
2117
return ;
2111
2118
}
2112
2119
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 )
2114
2121
}
2115
2122
2116
2123
ty:: PredicateKind :: Subtype ( data) => {
@@ -2124,26 +2131,38 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
2124
2131
let SubtypePredicate { a_is_expected : _, a, b } = data;
2125
2132
// both must be type variables, or the other would've been instantiated
2126
2133
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
+ )
2128
2142
}
2129
2143
ty:: PredicateKind :: Projection ( data) => {
2130
- let self_ty = data. projection_ty . self_ty ( ) ;
2131
- let term = data. term ;
2132
2144
if predicate. references_error ( ) || self . is_tainted_by_errors ( ) {
2133
2145
return ;
2134
2146
}
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 {
2137
2154
let mut err = self . emit_inference_failure_err (
2138
2155
body_id,
2139
2156
span,
2140
- self_ty . into ( ) ,
2157
+ subst ,
2141
2158
vec ! [ ] ,
2142
2159
ErrorCode :: E0284 ,
2160
+ true ,
2143
2161
) ;
2144
2162
err. note ( & format ! ( "cannot satisfy `{}`" , predicate) ) ;
2145
2163
err
2146
2164
} else {
2165
+ // If we can't find a substitution, just print a generic error
2147
2166
let mut err = struct_span_err ! (
2148
2167
self . tcx. sess,
2149
2168
span,
0 commit comments