Skip to content

Commit c1c7127

Browse files
committed
Rust: Add examples where trait visibility affects path and method resolution
1 parent b361b0f commit c1c7127

File tree

6 files changed

+4540
-4380
lines changed

6 files changed

+4540
-4380
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
multipleCallTargets
22
| main.rs:118:9:118:11 | f(...) |
3+
| main.rs:494:13:494:27 | ...::a_method(...) |
4+
| main.rs:498:13:498:27 | ...::a_method(...) |
5+
| main.rs:502:13:502:27 | ...::a_method(...) |
36
| proc_macro.rs:9:5:9:10 | ...::new(...) |

rust/ql/test/library-tests/path-resolution/main.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,58 @@ mod m16 {
460460
} // I83
461461
}
462462

463+
mod trait_visibility {
464+
mod m {
465+
pub trait Foo {
466+
fn a_method(&self); // Foo::a_method
467+
} // Foo
468+
469+
pub trait Bar {
470+
fn a_method(&self); // Bar::a_method
471+
} // Bar
472+
473+
pub struct X;
474+
#[rustfmt::skip]
475+
impl Foo for X { // $ item=Foo item=X
476+
fn a_method(&self) {
477+
println!("foo!");
478+
} // X_Foo::a_method
479+
}
480+
#[rustfmt::skip]
481+
impl Bar for X { // $ item=Bar item=X
482+
fn a_method(&self) {
483+
println!("bar!");
484+
} // X_Bar::a_method
485+
}
486+
}
487+
488+
use m::X; // $ item=X
489+
490+
pub fn f() {
491+
let x = X; // $ item=X
492+
{
493+
// Only the `Foo` trait is visible
494+
use m::Foo; // $ item=Foo
495+
X::a_method(&x); // $ item=X_Foo::a_method SPURIOUS: item=X_Bar::a_method
496+
}
497+
{
498+
// Only the `Bar` trait is visible
499+
use m::Bar; // $ item=Bar
500+
X::a_method(&x); // $ item=X_Bar::a_method SPURIOUS: item=X_Foo::a_method
501+
}
502+
{
503+
// Only the `Bar` trait is visible (but unnameable)
504+
use m::Bar as _; // $ item=Bar
505+
X::a_method(&x); // $ item=X_Bar::a_method SPURIOUS: item=X_Foo::a_method
506+
}
507+
{
508+
// The `Bar` trait is not visible, but we can refer to its method
509+
// with a full path.
510+
m::Bar::a_method(&x); // $ item=Bar::a_method
511+
}
512+
} // trait_visibility::f
513+
}
514+
463515
mod m17 {
464516
trait MyTrait {
465517
fn f(&self); // I1
@@ -730,6 +782,7 @@ fn main() {
730782
m11::f(); // $ item=I63
731783
m15::f(); // $ item=I75
732784
m16::f(); // $ item=I83
785+
trait_visibility::f(); // $ item=trait_visibility::f
733786
m17::f(); // $ item=I99
734787
nested6::f(); // $ item=I116
735788
nested8::f(); // $ item=I119

0 commit comments

Comments
 (0)