-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Prefer imports starting with std #2917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prefer imports starting with std #2917
Conversation
|
I'd like to write a test on it, but could not reproduce the Apparently, I'm doing something wrong, but I have no clue on how to make it proper. |
|
@SomeoneToIgnore could you post an exact test that does not work? |
crates/ra_hir_def/src/find_path.rs
Outdated
| best_path | ||
| } | ||
|
|
||
| fn prefer_new_path(old_path_len: usize, old_path: Option<&ModPath>, new_path: &ModPath) -> bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we express this as a key function? like (path.starts_with_std(), path.len())?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, way more readable now, thanks.
I think I should stop writing the code late in the night (I wish I have any other time to do so for RA though... ) For some reason, I've thought that lines I'll try to dig it more, thank you for reaching out to help me here. |
They very much do something: https://github.com/rust-analyzer/rust-analyzer/blob/0d6e5a986cc7c3a434c2107edf748d1c26ac3f69/crates/ra_db/src/fixture.rs#L152-L163 |
|
Then I'm fully confused by their behavior (I know I should look in the test code better, but did not have the possibility to do that properly yet). I try to replicate the Arc location in both crates and import it. This test panics: #[test]
fn why_does_it_panic() {
let code = r#"
//- /main.rs crate:main deps:std
<|>
//- /sync/mod.rs crate:std deps:alloc
pub use alloc::sync::Arc;
//- /sync.rs crate:alloc
pub struct Arc;
"#;
check_found_path(code, "std::sync::Arc");
}its simplified version also: #[test]
fn why_does_it_panic() {
let code = r#"
//- /main.rs crate:main deps:std
<|>
//- /sync/mod.rs crate:std
pub struct Arc;
"#;
check_found_path(code, "std::sync::Arc");
}When I try to use #[test]
fn zzzzz() {
let code = r#"
//- /main.rs crate:main deps:std
<|>
//- /sync/mod.rs crate:std deps:alloc
pub mod sync {
pub use alloc::sync::Arc;
}
//- /sync.rs crate:alloc
pub mod sync {
pub struct Arc;
}
"#;
check_found_path(code, "std::sync::Arc");
}but that is odd, since I basically duplicate module names in the file and in the And, anyway, when I put the debug prints into the |
|
Ah, well, it works exactly as expected! (but it took me a long time to realize this). You want something like this: let code = r#"
//- /main.rs crate:main deps:std
<|>
//- /std.rs crate:std deps:alloc
pub use alloc::sync
//- /alloc.rs crate:alloc
pub mod sync {
pub struct Arc;
}
"#;files with |
|
Ok, so that's basically my 2nd option that does not go into the |
|
Yeah, to clarify, if you write The file name matters for finding child modules, but that's it |
|
Also note that if your main crate in the test only has the dependency to |
|
Well, simply adding it to the std dependencies did not help to enter the required branch, but thanks a lot, that's what I've been missing from the very start when tried to make it work in the night. |
|
And now it's ready to be reviewed, thank you again for the help. |
matklad
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bors r+
| tested_by!(prefer_std_paths); | ||
| old_path | ||
| } else if new_path.starts_with_std() && old_path.should_start_with_std() { | ||
| tested_by!(prefer_std_paths); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Judging by the look of the checks, bors decided to ignore this one :) |
|
bors ping |
|
pong |
|
bors merge |
Build succeeded
|
|
Bors doesn't seem to take commands from review comments anymore. |
Closes #2915