diff --git a/compiler/rustc_trait_selection/src/traits/mod.rs b/compiler/rustc_trait_selection/src/traits/mod.rs index 0fb795fc184..834363b849c 100644 --- a/compiler/rustc_trait_selection/src/traits/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/mod.rs @@ -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` 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) { diff --git a/tests/rustdoc/impossible-default.rs b/tests/rustdoc/impossible-default.rs index fad64068010..c9444004d5c 100644 --- a/tests/rustdoc/impossible-default.rs +++ b/tests/rustdoc/impossible-default.rs @@ -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); + +impl Foo for Generic {}