Skip to content
New issue

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

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

Already on GitHub? # to your account

Check xform_ret_ty for WF in the new solver to improve method winnowing #133519

Merged
merged 1 commit into from
Nov 28, 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
22 changes: 22 additions & 0 deletions compiler/rustc_hir_typeck/src/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1642,6 +1642,28 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
}
}

// FIXME(-Znext-solver): See the linked issue below.
// <https://github.com/rust-lang/trait-system-refactor-initiative/issues/134>
//
// In the new solver, check the well-formedness of the return type.
// This emulates, in a way, the predicates that fall out of
// normalizing the return type in the old solver.
//
// We alternatively could check the predicates of the method itself hold,
// but we intentionally do not do this in the old solver b/c of cycles,
// and doing it in the new solver would be stronger. This should be fixed
// in the future, since it likely leads to much better method winnowing.
if let Some(xform_ret_ty) = xform_ret_ty
&& self.infcx.next_trait_solver()
{
ocx.register_obligation(traits::Obligation::new(
self.tcx,
cause.clone(),
self.param_env,
ty::ClauseKind::WellFormed(xform_ret_ty.into()),
));
}

// Evaluate those obligations to see if they might possibly hold.
for error in ocx.select_where_possible() {
result = ProbeResult::NoMatch;
Expand Down
47 changes: 47 additions & 0 deletions tests/ui/traits/next-solver/non-wf-ret.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//@ check-pass
Copy link
Contributor

@lcnr lcnr Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a non-rustc test for this 🤔 or remove the extern type stuff from this one

//@ compile-flags: -Znext-solver

use std::ops::Deref;

pub struct List<T> {
skel: [T],
}

impl<'a, T: Copy> IntoIterator for &'a List<T> {
type Item = T;
type IntoIter = std::iter::Copied<<&'a [T] as IntoIterator>::IntoIter>;

fn into_iter(self) -> Self::IntoIter {
todo!()
}
}

impl<T> Deref for List<T> {
type Target = [T];

fn deref(&self) -> &[T] {
todo!()
}
}

impl<T> List<T> {
fn iter(&self) -> <&Self as IntoIterator>::IntoIter
where
T: Copy,
{
todo!()
}
}

fn test<Q>(t: &List<Q>) {
// Checking that `<&List<Q> as IntoIterator>::IntoIter` is WF
// will disqualify the inherent method, since normalizing it
// requires `Q: Copy` which does not hold. and allow us to fall
// through to the deref'd `<[Q]>::iter` method which works.
//
// In the old solver, the same behavior is achieved by just
// eagerly normalizing the return type.
t.iter();
}

fn main() {}
Loading