rustc_trait_selection::traits::coherence

Function impl_intersection_has_negative_obligation

source
fn impl_intersection_has_negative_obligation(
    tcx: TyCtxt<'_>,
    impl1_def_id: DefId,
    impl2_def_id: DefId,
) -> bool
Expand description

Check if both impls can be satisfied by a common type by considering whether any of first impl’s obligations is known not to hold via a negative predicate.

For example, given these two impls: struct MyCustomBox<T: ?Sized>(Box<T>); impl From<&str> for MyCustomBox<dyn Error> (in my crate) impl<E> From<E> for MyCustomBox<dyn Error> where E: Error (in my crate)

After replacing the second impl’s header with inference vars, we get: MyCustomBox<dyn Error>: From<&str> MyCustomBox<dyn Error>: From<?E>

This gives us ?E = &str. We then try to prove the first impl’s predicates after negating, giving us &str: !Error. This is a negative impl provided by libstd, and therefore we can guarantee for certain that libstd will never add a positive impl for &str: Error (without it being a breaking change).