Skip to content

Commit 1bff1ff

Browse files
committed
auto merge of #15356 : pcwalton/rust/wrong-implementor, r=alexcrichton
parameters. This can break code that mistakenly used type parameters in place of `Self`. For example, this will break: trait Foo { fn bar<X>(u: X) -> Self { u } } Change this code to not contain a type error. For example: trait Foo { fn bar<X>(_: X) -> Self { self } } Closes #15172. [breaking-change] r? @alexcrichton
2 parents d623a8b + c3ae64a commit 1bff1ff

File tree

4 files changed

+80
-1
lines changed

4 files changed

+80
-1
lines changed

src/librustc/middle/typeck/infer/combine.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,8 @@ pub fn super_tys<C:Combine>(this: &C, a: ty::t, b: ty::t) -> cres<ty::t> {
445445
}
446446
}
447447

448-
(&ty::ty_param(ref a_p), &ty::ty_param(ref b_p)) if a_p.idx == b_p.idx => {
448+
(&ty::ty_param(ref a_p), &ty::ty_param(ref b_p)) if
449+
a_p.idx == b_p.idx && a_p.space == b_p.space => {
449450
Ok(a)
450451
}
451452

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
use std::num::Num;
12+
13+
trait BrokenAdd: Num {
14+
fn broken_add<T>(&self, rhs: T) -> Self {
15+
*self + rhs //~ ERROR mismatched types
16+
}
17+
}
18+
19+
impl<T: Num> BrokenAdd for T {}
20+
21+
pub fn main() {
22+
let foo: u8 = 0u8;
23+
let x: u8 = foo.broken_add("hello darkness my old friend".to_string());
24+
println!("{}", x);
25+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
trait Tr<T> {
12+
fn op(T) -> Self;
13+
}
14+
15+
// these compile as if Self: Tr<U>, even tho only Self: Tr<Self or T>
16+
trait A: Tr<Self> {
17+
fn test<U>(u: U) -> Self {
18+
Tr::op(u) //~ ERROR expected Tr<U>, but found Tr<Self>
19+
}
20+
}
21+
trait B<T>: Tr<T> {
22+
fn test<U>(u: U) -> Self {
23+
Tr::op(u) //~ ERROR expected Tr<U>, but found Tr<T>
24+
}
25+
}
26+
27+
impl<T> Tr<T> for T {
28+
fn op(t: T) -> T { t }
29+
}
30+
impl<T> A for T {}
31+
32+
fn main() {
33+
std::io::println(A::test((&7306634593706211700, 8)));
34+
}
35+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
trait Tr {
12+
fn test<X>(u: X) -> Self {
13+
u //~ ERROR mismatched types
14+
}
15+
}
16+
17+
fn main() {}
18+

0 commit comments

Comments
 (0)