Skip to content

builtin dyn impl no guide inference #141352

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 12 additions & 1 deletion compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1917,12 +1917,23 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
// impl `impl<T: ?Sized> Any for T { .. }`. This really shouldn't exist but is
// necessary due to #57893. We again arbitrarily prefer the applicable candidate
// with the lowest index.
//
// We do not want to use these impls to guide inference in case a user-written impl
// may also apply.
let object_bound = candidates
.iter()
.filter_map(|c| if let ObjectCandidate(i) = c.candidate { Some(i) } else { None })
.try_reduce(|c1, c2| if has_non_region_infer { None } else { Some(c1.min(c2)) });
match object_bound {
Some(Some(index)) => return Some(ObjectCandidate(index)),
Some(Some(index)) => {
return if has_non_region_infer
&& candidates.iter().any(|c| matches!(c.candidate, ImplCandidate(_)))
{
None
} else {
Some(ObjectCandidate(index))
};
}
Some(None) => {}
None => return None,
}
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/traits/object/no-incomplete-inference.current.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0283]: type annotations needed
--> $DIR/no-incomplete-inference.rs:16:5
|
LL | impls_equals::<dyn Equals<u32>, _>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `U` declared on the function `impls_equals`
|
= note: cannot satisfy `dyn Equals<u32>: Equals<_>`
note: required by a bound in `impls_equals`
--> $DIR/no-incomplete-inference.rs:13:20
|
LL | fn impls_equals<T: Equals<U> + ?Sized, U: ?Sized>() {}
| ^^^^^^^^^ required by this bound in `impls_equals`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0283`.
16 changes: 16 additions & 0 deletions tests/ui/traits/object/no-incomplete-inference.next.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0283]: type annotations needed
--> $DIR/no-incomplete-inference.rs:16:5
|
LL | impls_equals::<dyn Equals<u32>, _>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `U` declared on the function `impls_equals`
|
= note: cannot satisfy `dyn Equals<u32>: Equals<_>`
note: required by a bound in `impls_equals`
--> $DIR/no-incomplete-inference.rs:13:20
|
LL | fn impls_equals<T: Equals<U> + ?Sized, U: ?Sized>() {}
| ^^^^^^^^^ required by this bound in `impls_equals`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0283`.
18 changes: 18 additions & 0 deletions tests/ui/traits/object/no-incomplete-inference.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//@ revisions: current next
//@ ignore-compare-mode-next-solver (explicit revisions)
//@[next] compile-flags: -Znext-solver


// Make sure that having an applicable user-written
// and builtin impl is ambiguous.

trait Equals<T: ?Sized> {}

impl<T: ?Sized> Equals<T> for T {}

fn impls_equals<T: Equals<U> + ?Sized, U: ?Sized>() {}

fn main() {
impls_equals::<dyn Equals<u32>, _>();
//~^ ERROR type annotations needed
}
Loading