@@ -4,7 +4,7 @@ use rustc_errors::{Applicability, Diag, E0283, E0284, E0790, MultiSpan, struct_s
44use rustc_hir as hir;
55use rustc_hir:: LangItem ;
66use rustc_hir:: def:: { DefKind , Res } ;
7- use rustc_hir:: def_id:: DefId ;
7+ use rustc_hir:: def_id:: { CRATE_DEF_ID , DefId } ;
88use rustc_hir:: intravisit:: Visitor as _;
99use rustc_infer:: infer:: { BoundRegionConversionTime , InferCtxt } ;
1010use rustc_infer:: traits:: util:: elaborate;
@@ -128,19 +128,26 @@ pub fn compute_applicable_impls_for_diagnostics<'tcx>(
128128 } ,
129129 ) ;
130130
131- let predicates =
132- tcx. predicates_of ( obligation. cause . body_id . to_def_id ( ) ) . instantiate_identity ( tcx) ;
133- for ( pred, span) in elaborate ( tcx, predicates. into_iter ( ) ) {
134- let kind = pred. kind ( ) ;
135- if let ty:: ClauseKind :: Trait ( trait_pred) = kind. skip_binder ( )
136- && param_env_candidate_may_apply ( kind. rebind ( trait_pred) )
137- {
138- if kind. rebind ( trait_pred. trait_ref )
139- == ty:: Binder :: dummy ( ty:: TraitRef :: identity ( tcx, trait_pred. def_id ( ) ) )
131+ // If our `body_id` has been set (and isn't just from a dummy obligation cause),
132+ // then try to look for a param-env clause that would apply. The way we compute
133+ // this is somewhat manual, since we need the spans, so we elaborate this directly
134+ // from `predicates_of` rather than actually looking at the param-env which
135+ // otherwise would be more appropriate.
136+ let body_id = obligation. cause . body_id ;
137+ if body_id != CRATE_DEF_ID {
138+ let predicates = tcx. predicates_of ( body_id. to_def_id ( ) ) . instantiate_identity ( tcx) ;
139+ for ( pred, span) in elaborate ( tcx, predicates. into_iter ( ) ) {
140+ let kind = pred. kind ( ) ;
141+ if let ty:: ClauseKind :: Trait ( trait_pred) = kind. skip_binder ( )
142+ && param_env_candidate_may_apply ( kind. rebind ( trait_pred) )
140143 {
141- ambiguities. push ( CandidateSource :: ParamEnv ( tcx. def_span ( trait_pred. def_id ( ) ) ) )
142- } else {
143- ambiguities. push ( CandidateSource :: ParamEnv ( span) )
144+ if kind. rebind ( trait_pred. trait_ref )
145+ == ty:: Binder :: dummy ( ty:: TraitRef :: identity ( tcx, trait_pred. def_id ( ) ) )
146+ {
147+ ambiguities. push ( CandidateSource :: ParamEnv ( tcx. def_span ( trait_pred. def_id ( ) ) ) )
148+ } else {
149+ ambiguities. push ( CandidateSource :: ParamEnv ( span) )
150+ }
144151 }
145152 }
146153 }
0 commit comments