Skip to content

Commit 8904842

Browse files
committed
tests: add two new run-pass tests for method behavior after UFCS.
1 parent e8d0884 commit 8904842

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2015 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+
// Ensure that an user-defined type admits multiple inherent methods
12+
// with the same name, which can be called on values that have a
13+
// precise enough type to allow distinguishing between the methods.
14+
15+
struct Foo<T>(T);
16+
17+
impl Foo<usize> {
18+
fn bar(&self) -> i32 { self.0 as i32 }
19+
}
20+
21+
impl Foo<isize> {
22+
fn bar(&self) -> i32 { -(self.0 as i32) }
23+
}
24+
25+
fn main() {
26+
let foo_u = Foo::<usize>(5);
27+
assert_eq!(foo_u.bar(), 5);
28+
29+
let foo_i = Foo::<isize>(3);
30+
assert_eq!(foo_i.bar(), -3);
31+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2015 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+
struct Foo;
12+
13+
trait Trait {
14+
fn bar(&self);
15+
}
16+
17+
// Inherent impls should be preferred over trait ones.
18+
impl Foo {
19+
fn bar(&self) {}
20+
}
21+
22+
impl Trait {
23+
fn baz(_: &Foo) {}
24+
}
25+
26+
impl Trait for Foo {
27+
fn bar(&self) { panic!("wrong method called!") }
28+
}
29+
30+
fn main() {
31+
Foo.bar();
32+
Foo::bar(&Foo);
33+
<Foo>::bar(&Foo);
34+
35+
// Should work even if Trait::baz doesn't exist.
36+
// N.B: `<Trait>::bar` would be ambiguous.
37+
<Trait>::baz(&Foo);
38+
}

0 commit comments

Comments
 (0)