Skip to content

Commit e0d7cb4

Browse files
committed
Rust: Add example where method resolution depends on trait visibility
1 parent 919ed3c commit e0d7cb4

File tree

2 files changed

+4265
-4198
lines changed

2 files changed

+4265
-4198
lines changed

rust/ql/test/library-tests/type-inference/main.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,51 @@ mod trait_impl {
121121
}
122122
}
123123

124+
mod trait_visibility {
125+
// In this test the correct method target depends on which trait is visible.
126+
127+
mod m {
128+
pub trait Foo {
129+
// Foo::a_method
130+
fn a_method(&self) {
131+
println!("foo!");
132+
}
133+
}
134+
135+
pub trait Bar {
136+
// Bar::a_method
137+
fn a_method(&self) {
138+
println!("quux!");
139+
}
140+
}
141+
142+
pub struct X;
143+
impl Foo for X {}
144+
impl Bar for X {}
145+
}
146+
147+
use m::X;
148+
149+
fn main() {
150+
let x = X;
151+
{
152+
use m::Foo;
153+
x.a_method(); // $ target=Foo::a_method SPURIOUS: target=Bar::a_method
154+
}
155+
{
156+
use m::Bar;
157+
x.a_method(); // $ target=Bar::a_method SPURIOUS: target=Foo::a_method
158+
}
159+
{
160+
use m::Bar;
161+
use m::Foo;
162+
// x.a_method(); // This would be ambiguous
163+
Foo::a_method(&x); // $ target=Foo::a_method
164+
Bar::a_method(&x); // $ target=Bar::a_method
165+
}
166+
}
167+
}
168+
124169
mod method_non_parametric_impl {
125170
#[derive(Debug)]
126171
struct MyThing<A> {

0 commit comments

Comments
 (0)