Skip to content

Dont consider predicates that may hold as impossible in is_impossible_associated_item #131840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions compiler/rustc_trait_selection/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,11 +562,20 @@ fn is_impossible_associated_item(

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)| {
Expand All @@ -580,16 +589,9 @@ fn is_impossible_associated_item(
})
});

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) {
Expand Down
8 changes: 8 additions & 0 deletions tests/rustdoc/impossible-default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ pub trait Foo {
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> {}
Loading