Skip to content

Commit e59cf24

Browse files
Add more tests
1 parent 069e9d2 commit e59cf24

10 files changed

+168
-1
lines changed

Diff for: compiler/rustc_hir_typeck/src/method/probe.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
13431343
// We collapse to a subtrait pick *after* filtering unstable candidates
13441344
// to make sure we don't prefer a unstable subtrait method over a stable
13451345
// supertrait method.
1346-
if self.tcx.features().supertrait_item_shadowing {
1346+
if self.tcx.features().supertrait_item_shadowing() {
13471347
if let Some(pick) =
13481348
self.collapse_candidates_to_subtrait_pick(self_ty, &applicable_candidates)
13491349
{

Diff for: tests/ui/methods/supertrait-shadowing/assoc-const.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//@ run-pass
2+
//@ check-run-results
3+
4+
#![feature(supertrait_item_shadowing)]
5+
#![allow(dead_code)]
6+
7+
trait A {
8+
const CONST: i32;
9+
}
10+
impl<T> A for T {
11+
const CONST: i32 = 1;
12+
}
13+
14+
trait B: A {
15+
const CONST: i32;
16+
}
17+
impl<T> B for T {
18+
const CONST: i32 = 2;
19+
}
20+
21+
fn main() {
22+
println!("{}", i32::CONST);
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![feature(supertrait_item_shadowing)]
2+
3+
struct W<T>(T);
4+
5+
trait Upstream {
6+
fn hello(&self) {}
7+
}
8+
impl<T> Upstream for T {}
9+
10+
trait Downstream: Upstream {
11+
fn hello(&self) {}
12+
}
13+
impl<T> Downstream for W<T> where T: Foo {}
14+
15+
trait Foo {}
16+
17+
fn main() {
18+
let x = W(Default::default());
19+
x.hello();
20+
//~^ ERROR the trait bound `i32: Foo` is not satisfied
21+
//~| WARN trait method `hello` from `Downstream` shadows identically named method from supertrait
22+
let _: i32 = x.0;
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
warning: trait method `hello` from `Downstream` shadows identically named method from supertrait
2+
--> $DIR/false-subtrait-after-inference.rs:19:7
3+
|
4+
LL | x.hello();
5+
| ^^^^^
6+
|
7+
note: method from `Downstream` shadows a supertrait method
8+
--> $DIR/false-subtrait-after-inference.rs:11:5
9+
|
10+
LL | fn hello(&self) {}
11+
| ^^^^^^^^^^^^^^^
12+
note: method from `Upstream` is shadowed by a subtrait method
13+
--> $DIR/false-subtrait-after-inference.rs:6:5
14+
|
15+
LL | fn hello(&self) {}
16+
| ^^^^^^^^^^^^^^^
17+
= note: `#[warn(supertrait_item_shadowing)]` on by default
18+
19+
error[E0277]: the trait bound `i32: Foo` is not satisfied
20+
--> $DIR/false-subtrait-after-inference.rs:19:7
21+
|
22+
LL | x.hello();
23+
| ^^^^^ the trait `Foo` is not implemented for `i32`, which is required by `W<_>: Downstream`
24+
|
25+
help: this trait has no implementations, consider adding one
26+
--> $DIR/false-subtrait-after-inference.rs:15:1
27+
|
28+
LL | trait Foo {}
29+
| ^^^^^^^^^
30+
note: required for `W<i32>` to implement `Downstream`
31+
--> $DIR/false-subtrait-after-inference.rs:13:9
32+
|
33+
LL | impl<T> Downstream for W<T> where T: Foo {}
34+
| ^^^^^^^^^^ ^^^^ --- unsatisfied trait bound introduced here
35+
36+
error: aborting due to 1 previous error; 1 warning emitted
37+
38+
For more information about this error, try `rustc --explain E0277`.
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//@ run-pass
2+
//@ check-run-results
3+
4+
#![feature(supertrait_item_shadowing)]
5+
#![allow(dead_code)]
6+
7+
mod out_of_scope {
8+
pub trait Subtrait: super::Supertrait {
9+
fn hello(&self) {
10+
println!("subtrait");
11+
}
12+
}
13+
impl<T> Subtrait for T {}
14+
}
15+
16+
trait Supertrait {
17+
fn hello(&self) {
18+
println!("supertrait");
19+
}
20+
}
21+
impl<T> Supertrait for T {}
22+
23+
fn main() {
24+
().hello();
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
supertrait
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//@ check-pass
2+
3+
// Make sure we don't prefer a subtrait that we would've otherwise eliminated
4+
// in `consider_probe` during method probing.
5+
6+
#![feature(supertrait_item_shadowing)]
7+
#![allow(dead_code)]
8+
9+
struct W<T>(T);
10+
11+
trait Upstream {
12+
fn hello(&self) {}
13+
}
14+
impl<T> Upstream for T {}
15+
16+
trait Downstream: Upstream {
17+
fn hello(&self) {}
18+
}
19+
impl<T> Downstream for W<T> where T: Foo {}
20+
21+
trait Foo {}
22+
23+
fn main() {
24+
let x = W(1i32);
25+
x.hello();
26+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//@ run-pass
2+
//@ check-run-results
3+
4+
// Makes sure we can shadow with type-dependent method syntax.
5+
6+
#![feature(supertrait_item_shadowing)]
7+
#![allow(dead_code)]
8+
9+
trait A {
10+
fn hello() {
11+
println!("A");
12+
}
13+
}
14+
impl<T> A for T {}
15+
16+
trait B: A {
17+
fn hello() {
18+
println!("B");
19+
}
20+
}
21+
impl<T> B for T {}
22+
23+
fn foo<T>() {
24+
T::hello();
25+
}
26+
27+
fn main() {
28+
foo::<()>();
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
B

0 commit comments

Comments
 (0)