Don't resolve generic impls that may be shadowed by dyn built-in impls by compiler-errors · Pull Request #114941 · rust-lang/rust (original) (raw)

NOTE: This is a hack. This is not trying to be a general fix for the issue that we've allowed overlapping built-in trait object impls and user-written impls for quite a long time, and traits like Any rely on this (#57893) -- this PR specifically aims to mitigate a new unsoundness that is uncovered by the MIR inliner (#114928) that interacts with this pre-existing issue.

Builtin dyn Trait impls may overlap with user-provided blanket impls (impl<T: ?Sized> Trait for T) in generic contexts. This leads to bugs when instances are resolved in polymorphic contexts, since we typically prefer object candidates over impl candidates.

This PR implements a (hacky) heuristic to resolve_associated_item to account for that unfortunate hole in the type system -- we now bail with ambiguity if we try to resolve a non-rigid instance whose self type is not known to be sized. This makes sure we can still inline instances like impl<T: Sized> Trait for T, which can never overlap with dyn Trait's built-in impl, but we avoid inlining an impl that may be shadowed by a dyn Trait.

Fixes #114928