WIP: Find the imports that were used to reach a method

And add tests for some corner cases we have to consider.
This commit is contained in:
Niko Matsakis 2021-06-17 06:10:38 -04:00
parent 56108f67b1
commit dbc9da7962
3 changed files with 118 additions and 6 deletions

View file

@ -0,0 +1,52 @@
// run-rustfix
// edition:2018
// check-pass
#![warn(future_prelude_collision)]
#![allow(dead_code)]
mod m {
pub trait TryIntoU32 {
fn try_into(self) -> Result<u32, ()>;
}
impl TryIntoU32 for u8 {
fn try_into(self) -> Result<u32, ()> {
Ok(self as u32)
}
}
pub trait AnotherTrick {}
}
mod a {
use crate::m::TryIntoU32;
fn main() {
// In this case, we can just use `TryIntoU32`
let _: u32 = 3u8.try_into().unwrap();
}
}
mod b {
use crate::m::AnotherTrick as TryIntoU32;
use crate::m::TryIntoU32 as _;
fn main() {
// In this case, a `TryIntoU32::try_into` rewrite will not work, and we need to use
// the path `crate::m::TryIntoU32` (with which it was imported).
let _: u32 = 3u8.try_into().unwrap();
}
}
mod c {
use super::m::TryIntoU32 as _;
use crate::m::AnotherTrick as TryIntoU32;
fn main() {
// In this case, a `TryIntoU32::try_into` rewrite will not work, and we need to use
// the path `super::m::TryIntoU32` (with which it was imported).
let _: u32 = 3u8.try_into().unwrap();
}
}
fn main() {}

View file

@ -0,0 +1,33 @@
// run-rustfix
// edition:2018
// check-pass
#![warn(future_prelude_collision)]
#![allow(dead_code)]
mod m {
pub trait TryIntoU32 {
fn try_into(self) -> Result<u32, ()>;
}
impl TryIntoU32 for u8 {
fn try_into(self) -> Result<u32, ()> {
Ok(self as u32)
}
}
pub trait AnotherTrick {}
}
mod d {
use crate::m::AnotherTrick as TryIntoU32;
use crate::m::*;
fn main() {
// Here, `TryIntoU32` is imported but shadowed, but in that case we don't permit its methods
// to be available.
let _: u32 = 3u8.try_into().unwrap();
//~^ ERROR no method name `try_into` found
}
}
fn main() {}