Skip to content
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

Special case inference around AsRef<Vec<T>> to support existing code without reverting #95098 #96446

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 18 additions & 1 deletion compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1767,7 +1767,24 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
!needs_infer
}
Some(_) => true,
None => false,
None => {
if let Some(other) = tcx.impl_trait_ref(other_def)
&& let Some(victim) = tcx.impl_trait_ref(victim_def)
&& tcx.is_diagnostic_item(sym::AsRef, other.def_id)
&& tcx.is_diagnostic_item(sym::AsRef, victim.def_id)
Comment on lines +1773 to +1774
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These would have to be lang_item checks instead (to not lie about AsRef and Vec not being language items after this).

&& other.substs.len() > 1
&& victim.substs.len() > 1
Comment on lines +1775 to +1776
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are likely redundant with the two lines above.

&& let ty::Slice(other) = other.substs.type_at(1).kind()
&& let ty::Adt(def, substs) = victim.substs.type_at(1).kind()
&& tcx.is_diagnostic_item(sym::Vec, def.did())
{
// If this is an `AsRef` implementation that can go either way for
// `AsRef<[T]>` or `AsRef<Vec[T]>`, prefer the former.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// `AsRef<[T]>` or `AsRef<Vec[T]>`, prefer the former.
// `AsRef<[T]>` or `AsRef<Vec<T>>`, prefer the former.

other == &substs.type_at(0)
} else {
false
}
}
}
} else {
false
Expand Down
14 changes: 14 additions & 0 deletions src/test/ui/inference/vec-from-array-ref-impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// check-pass

#[derive(Clone)]
struct Constraint;

fn constraints<C>(constraints: C)
where C: Into<Vec<Constraint>>
{
let _: Vec<Constraint> = constraints.into();
}

fn main() {
constraints(vec![Constraint].as_ref());
}