-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #103252 - lcnr:recompute_applicable_impls, r=jackh726
selection failure: recompute applicable impls The way we currently skip errors for ambiguous trait obligations seems pretty fragile so we get some duplicate errors because of this. Removing this info from selection errors changes this system to be closer to my image of our new trait solver and is also making it far easier to change overflow errors to be non-fatal ✨ r? types cc `@estebank`
- Loading branch information
Showing
21 changed files
with
170 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
compiler/rustc_trait_selection/src/traits/error_reporting/ambiguity.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use rustc_hir::def_id::DefId; | ||
use rustc_infer::infer::InferCtxt; | ||
use rustc_infer::traits::{Obligation, ObligationCause, TraitObligation}; | ||
use rustc_span::DUMMY_SP; | ||
|
||
use crate::traits::ObligationCtxt; | ||
|
||
pub fn recompute_applicable_impls<'tcx>( | ||
infcx: &InferCtxt<'tcx>, | ||
obligation: &TraitObligation<'tcx>, | ||
) -> Vec<DefId> { | ||
let tcx = infcx.tcx; | ||
let param_env = obligation.param_env; | ||
let dummy_cause = ObligationCause::dummy(); | ||
let impl_may_apply = |impl_def_id| { | ||
let ocx = ObligationCtxt::new_in_snapshot(infcx); | ||
let placeholder_obligation = | ||
infcx.replace_bound_vars_with_placeholders(obligation.predicate); | ||
let obligation_trait_ref = | ||
ocx.normalize(dummy_cause.clone(), param_env, placeholder_obligation.trait_ref); | ||
|
||
let impl_substs = infcx.fresh_substs_for_item(DUMMY_SP, impl_def_id); | ||
let impl_trait_ref = tcx.bound_impl_trait_ref(impl_def_id).unwrap().subst(tcx, impl_substs); | ||
let impl_trait_ref = ocx.normalize(ObligationCause::dummy(), param_env, impl_trait_ref); | ||
|
||
if let Err(_) = ocx.eq(&dummy_cause, param_env, obligation_trait_ref, impl_trait_ref) { | ||
return false; | ||
} | ||
|
||
let impl_predicates = tcx.predicates_of(impl_def_id).instantiate(tcx, impl_substs); | ||
ocx.register_obligations( | ||
impl_predicates | ||
.predicates | ||
.iter() | ||
.map(|&predicate| Obligation::new(dummy_cause.clone(), param_env, predicate)), | ||
); | ||
|
||
ocx.select_where_possible().is_empty() | ||
}; | ||
|
||
let mut impls = Vec::new(); | ||
tcx.for_each_relevant_impl( | ||
obligation.predicate.def_id(), | ||
obligation.predicate.skip_binder().trait_ref.self_ty(), | ||
|impl_def_id| { | ||
if infcx.probe(move |_snapshot| impl_may_apply(impl_def_id)) { | ||
impls.push(impl_def_id) | ||
} | ||
}, | ||
); | ||
impls | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
fn main() { | ||
let x = "hello".chars().rev().collect(); //~ ERROR E0282 | ||
let x = "hello".chars().rev().collect(); | ||
//~^ ERROR E0282 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
fn main() { | ||
let x = |a: (), b: ()| { | ||
Err(a)?; | ||
Ok(b) //~ ERROR type annotations needed | ||
Ok(b) | ||
//~^ ERROR type annotations needed | ||
}; | ||
} |
Oops, something went wrong.