Dont consider predicates that may hold as impossible in is_impossible_associated_item

This commit is contained in:
Michael Goulet 2024-10-17 12:31:48 -04:00
parent 3a85d3fa78
commit 8ff8f78e4c
2 changed files with 22 additions and 12 deletions

View File

@ -562,11 +562,20 @@ fn visit_const(&mut self, ct: ty::Const<'tcx>) -> Self::Result {
let generics = tcx.generics_of(trait_item_def_id); let generics = tcx.generics_of(trait_item_def_id);
let predicates = tcx.predicates_of(trait_item_def_id); let predicates = tcx.predicates_of(trait_item_def_id);
// Be conservative in cases where we have `W<T: ?Sized>` and a method like `Self: Sized`,
// since that method *may* have some substitutions where the predicates hold.
//
// This replicates the logic we use in coherence.
let infcx =
tcx.infer_ctxt().ignoring_regions().with_next_trait_solver(true).intercrate(true).build();
let param_env = ty::ParamEnv::empty();
let fresh_args = infcx.fresh_args_for_item(tcx.def_span(impl_def_id), impl_def_id);
let impl_trait_ref = tcx let impl_trait_ref = tcx
.impl_trait_ref(impl_def_id) .impl_trait_ref(impl_def_id)
.expect("expected impl to correspond to trait") .expect("expected impl to correspond to trait")
.instantiate_identity(); .instantiate(tcx, fresh_args);
let param_env = tcx.param_env(impl_def_id);
let mut visitor = ReferencesOnlyParentGenerics { tcx, generics, trait_item_def_id }; let mut visitor = ReferencesOnlyParentGenerics { tcx, generics, trait_item_def_id };
let predicates_for_trait = predicates.predicates.iter().filter_map(|(pred, span)| { let predicates_for_trait = predicates.predicates.iter().filter_map(|(pred, span)| {
@ -580,16 +589,9 @@ fn visit_const(&mut self, ct: ty::Const<'tcx>) -> Self::Result {
}) })
}); });
let infcx = tcx.infer_ctxt().ignoring_regions().build(); let ocx = ObligationCtxt::new(&infcx);
for obligation in predicates_for_trait { ocx.register_obligations(predicates_for_trait);
// Ignore overflow error, to be conservative. !ocx.select_where_possible().is_empty()
if let Ok(result) = infcx.evaluate_obligation(&obligation)
&& !result.may_apply()
{
return true;
}
}
false
} }
pub fn provide(providers: &mut Providers) { pub fn provide(providers: &mut Providers) {

View File

@ -18,3 +18,11 @@ fn no_needs_sized(&self) {}
pub struct Bar([u8]); pub struct Bar([u8]);
impl Foo for Bar {} impl Foo for Bar {}
//@ has foo/struct.Generic.html '//*[@id="method.needs_sized"]//h4[@class="code-header"]' \
// "fn needs_sized"
//@ has foo/struct.Generic.html '//*[@id="method.no_needs_sized"]//h4[@class="code-header"]' \
// "fn no_needs_sized"
pub struct Generic<T: ?Sized>(T);
impl<T: ?Sized> Foo for Generic<T> {}