Skip to content

Commit 6bf0dc8

Browse files
committed
Prefer where clauses to impls in trait resolution (not vice versa).
Fixes #18453.
1 parent 7e66231 commit 6bf0dc8

File tree

2 files changed

+68
-7
lines changed

2 files changed

+68
-7
lines changed

src/librustc/middle/traits/select.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -1104,18 +1104,29 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11041104
* Returns true if `candidate_i` should be dropped in favor of `candidate_j`.
11051105
* This is generally true if either:
11061106
* - candidate i and candidate j are equivalent; or,
1107-
* - candidate i is a where clause bound and candidate j is a concrete impl,
1107+
* - candidate i is a conrete impl and candidate j is a where clause bound,
11081108
* and the concrete impl is applicable to the types in the where clause bound.
11091109
*
1110-
* The last case basically occurs with blanket impls like
1111-
* `impl<T> Foo for T`. In that case, a bound like `T:Foo` is
1112-
* kind of an "false" ambiguity -- both are applicable to any
1113-
* type, but in fact coherence requires that the bound will
1114-
* always be resolved to the impl anyway.
1110+
* The last case refers to cases where there are blanket impls (often conditional
1111+
* blanket impls) as well as a where clause. This can come down to one of two cases:
1112+
*
1113+
* - The impl is truly unconditional (it has no where clauses
1114+
* of its own), in which case the where clause is
1115+
* unnecessary, because coherence requires that we would
1116+
* pick that particular impl anyhow (at least so long as we
1117+
* don't have specialization).
1118+
*
1119+
* - The impl is conditional, in which case we may not have winnowed it out
1120+
* because we don't know if the conditions apply, but the where clause is basically
1121+
* telling us taht there is some impl, though not necessarily the one we see.
1122+
*
1123+
* In both cases we prefer to take the where clause, which is
1124+
* essentially harmless. See issue #18453 for more details of
1125+
* a case where doing the opposite caused us harm.
11151126
*/
11161127

11171128
match (candidate_i, candidate_j) {
1118-
(&ParamCandidate(ref vt), &ImplCandidate(impl_def_id)) => {
1129+
(&ImplCandidate(impl_def_id), &ParamCandidate(ref vt)) => {
11191130
debug!("Considering whether to drop param {} in favor of impl {}",
11201131
candidate_i.repr(self.tcx()),
11211132
candidate_j.repr(self.tcx()));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test that when there is a conditional (but blanket) impl and a
12+
// where clause, we don't get confused in trait resolution.
13+
//
14+
// Issue #18453.
15+
16+
use std::rc::Rc;
17+
18+
pub trait Foo<M> {
19+
fn foo(&mut self, msg: M);
20+
}
21+
22+
pub trait Bar<M> {
23+
fn dummy(&self) -> M;
24+
}
25+
26+
impl<M, F: Bar<M>> Foo<M> for F {
27+
fn foo(&mut self, msg: M) {
28+
}
29+
}
30+
31+
pub struct Both<M, F> {
32+
inner: Rc<(M, F)>,
33+
}
34+
35+
impl<M, F: Foo<M>> Clone for Both<M, F> {
36+
fn clone(&self) -> Both<M, F> {
37+
Both { inner: self.inner.clone() }
38+
}
39+
}
40+
41+
fn repro1<M, F: Foo<M>>(_both: Both<M, F>) {
42+
}
43+
44+
fn repro2<M, F: Foo<M>>(msg: M, foo: F) {
45+
let both = Both { inner: Rc::new((msg, foo)) };
46+
repro1(both.clone()); // <--- This clone causes problem
47+
}
48+
49+
pub fn main() {
50+
}

0 commit comments

Comments
 (0)