Auto merge of #131840 - compiler-errors:impossible-maybe, r=lcnr

Dont consider predicates that may hold as impossible in `is_impossible_associated_item`

Use infer vars to account for ambiguities when considering if methods are impossible to instantiate for a given self type. Also while we're at it, let's use the new trait solver instead of `evaluate` since this is used in rustdoc.

r? lcnr
Fixes #131839
This commit is contained in:
bors 2024-10-21 22:58:44 +00:00
commit 814df6e50e
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 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
.impl_trait_ref(impl_def_id)
.expect("expected impl to correspond to trait")
.instantiate_identity();
let param_env = tcx.param_env(impl_def_id);
.instantiate(tcx, fresh_args);
let mut visitor = ReferencesOnlyParentGenerics { tcx, generics, trait_item_def_id };
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();
for obligation in predicates_for_trait {
// Ignore overflow error, to be conservative.
if let Ok(result) = infcx.evaluate_obligation(&obligation)
&& !result.may_apply()
{
return true;
}
}
false
let ocx = ObligationCtxt::new(&infcx);
ocx.register_obligations(predicates_for_trait);
!ocx.select_where_possible().is_empty()
}
pub fn provide(providers: &mut Providers) {

View File

@ -18,3 +18,11 @@ fn no_needs_sized(&self) {}
pub struct Bar([u8]);
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> {}