Skip to content

Commit

Permalink
Auto merge of rust-lang#131840 - compiler-errors:impossible-maybe, r=…
Browse files Browse the repository at this point in the history
…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 rust-lang#131839
  • Loading branch information
bors committed Oct 21, 2024
2 parents 4392847 + 8ff8f78 commit 814df6e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
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> {}

0 comments on commit 814df6e

Please sign in to comment.