Skip to content
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

Adapt to the new standard library directory layout #1130

Merged
merged 2 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ install:
- set PATH=%PATH%;C:\Users\appveyor\.cargo\bin
# the rust-src is needed for tests that depend on the standard library
- rustup component add rust-src
- set RUST_SRC_PATH=C:\Users\appveyor\.rustup\toolchains\%CHANNEL%-%TARGET%\lib\rustlib\src\rust\src
- set RUST_SRC_PATH=C:\Users\appveyor\.rustup\toolchains\%CHANNEL%-%TARGET%\lib\rustlib\src\rust\library
- rustc -Vv
- cargo -V

Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ cache: cargo

before_script:
- rustup component add rust-src
- export RUST_SRC_PATH=`rustc --print sysroot`/lib/rustlib/src/rust/src
- export RUST_SRC_PATH=`rustc --print sysroot`/lib/rustlib/src/rust/library

script:
- cargo build --verbose --all
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nightly-2020-09-02
nightly-2020-09-02
5 changes: 5 additions & 0 deletions src/racer/fileres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ pub fn get_std_file(name: &str, session: &Session<'_>) -> Option<PathBuf> {
if filepath.exists() || session.contains_file(&filepath) {
return Some(filepath);
}
// If not found, try using the new standard library directory layout
let filepath = std_path.join(name).join("src").join("lib.rs");
if filepath.exists() || session.contains_file(&filepath) {
return Some(filepath);
}
}
return None;
}
Expand Down
32 changes: 25 additions & 7 deletions src/racer/nameres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ fn test_do_file_search_std() {
let matches = do_file_search("std", path, &session);
assert!(matches
.into_iter()
.any(|m| m.filepath.ends_with("src/libstd/lib.rs")));
.any(|m| m.filepath.ends_with("std/src/lib.rs")));
}

#[test]
Expand Down Expand Up @@ -804,6 +804,7 @@ pub fn do_file_search(searchstr: &str, currentdir: &Path, session: &Session<'_>)
Some(fname) => fname,
None => continue,
};
// Firstly, try the original layout, e.g. libstd/lib.rs
if fname.starts_with(&format!("lib{}", searchstr)) {
let filepath = fpath_buf.join("lib.rs");
if filepath.exists() || session.contains_file(&filepath) {
Expand All @@ -820,6 +821,23 @@ pub fn do_file_search(searchstr: &str, currentdir: &Path, session: &Session<'_>)
out.push(m);
}
}
// Secondly, try the new standard library layout, e.g. std/src/lib.rs
if fname.starts_with(searchstr) {
let filepath = fpath_buf.join("src").join("lib.rs");
if filepath.exists() || session.contains_file(&filepath) {
let m = Match {
matchstr: fname.to_owned(),
filepath: filepath.to_path_buf(),
point: BytePos::ZERO,
coords: Some(Coordinate::start()),
local: false,
mtype: MatchType::Module,
contextstr: fname.to_owned(),
docs: String::new(),
};
out.push(m);
}
}

if fname.starts_with(searchstr) {
for name in &[&format!("{}.rs", fname)[..], "mod.rs", "lib.rs"] {
Expand Down Expand Up @@ -1363,7 +1381,7 @@ pub fn search_prelude_file(

// find the prelude file from the search path and scan it
if let Some(ref std_path) = *RUST_SRC_PATH {
let filepath = std_path.join("libstd").join("prelude").join("v1.rs");
let filepath = std_path.join("std").join("src").join("prelude").join("v1.rs");
if filepath.exists() || session.contains_file(&filepath) {
let msrc = session.load_source_file(&filepath);
let is_local = true;
Expand Down Expand Up @@ -2441,10 +2459,10 @@ fn get_std_macros(
searchstr
};
for macro_file in &[
"libstd/macros.rs",
"libcore/macros.rs",
"libcore/macros/mod.rs",
"liballoc/macros.rs",
"std/src/macros.rs",
"core/src/macros.rs",
"core/src/macros/mod.rs",
"alloc/src/macros.rs",
] {
let macro_path = std_path.join(macro_file);
if !macro_path.exists() {
Expand All @@ -2453,7 +2471,7 @@ fn get_std_macros(
get_std_macros_(
&macro_path,
searchstr,
macro_file == &"libcore/macros.rs",
macro_file == &"core/src/macros.rs",
search_type,
session,
out,
Expand Down
40 changes: 20 additions & 20 deletions src/racer/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::nameres::{self, RUST_SRC_PATH};
use rustc_ast::ast::{IntTy, LitIntType, UintTy};
use std::path::PathBuf;

const PRIM_DOC: &str = "libstd/primitive_docs.rs";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing this to the new directory layout without also checking for the old layout will cause panics on any system that still has the old directory layout.

const KEY_DOC: &str = "libstd/keyword_docs.rs";
const PRIM_DOC: &str = "std/src/primitive_docs.rs";
const KEY_DOC: &str = "std/src/keyword_docs.rs";

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PrimKind {
Expand Down Expand Up @@ -83,27 +83,27 @@ impl PrimKind {
match self {
PrimKind::Bool => None,
PrimKind::Never => None,
PrimKind::Char => Some(&["libcore/char/methods.rs"]),
PrimKind::Char => Some(&["core/src/char/methods.rs"]),
PrimKind::Unit => None,
PrimKind::Pointer => Some(&["libcore/ptr.rs"]),
PrimKind::Pointer => Some(&["core/src/ptr.rs"]),
PrimKind::Array => None,
PrimKind::Slice => Some(&["libcore/slice/mod.rs", "liballoc/slice.rs"]),
PrimKind::Str => Some(&["libcore/str/mod.rs", "liballoc/str.rs"]),
PrimKind::Slice => Some(&["core/src/slice/mod.rs", "alloc/src/slice.rs"]),
PrimKind::Str => Some(&["core/src/str/mod.rs", "alloc/src/str.rs"]),
PrimKind::Tuple => None,
PrimKind::F32 => Some(&["libstd/f32.rs", "libcore/num/f32.rs"]),
PrimKind::F64 => Some(&["libstd/f64.rs", "libcore/num/f64.rs"]),
PrimKind::I8 => Some(&["libcore/num/mod.rs"]),
PrimKind::I16 => Some(&["libcore/num/mod.rs"]),
PrimKind::I32 => Some(&["libcore/num/mod.rs"]),
PrimKind::I64 => Some(&["libcore/num/mod.rs"]),
PrimKind::I128 => Some(&["libcore/num/mod.rs"]),
PrimKind::U8 => Some(&["libcore/num/mod.rs"]),
PrimKind::U16 => Some(&["libcore/num/mod.rs"]),
PrimKind::U32 => Some(&["libcore/num/mod.rs"]),
PrimKind::U64 => Some(&["libcore/num/mod.rs"]),
PrimKind::U128 => Some(&["libcore/num/mod.rs"]),
PrimKind::Isize => Some(&["libcore/num/mod.rs"]),
PrimKind::Usize => Some(&["libcore/num/mod.rs"]),
PrimKind::F32 => Some(&["std/src/f32.rs", "core/src/num/f32.rs"]),
PrimKind::F64 => Some(&["std/src/f64.rs", "core/src/num/f64.rs"]),
PrimKind::I8 => Some(&["core/src/num/mod.rs"]),
PrimKind::I16 => Some(&["core/src/num/mod.rs"]),
PrimKind::I32 => Some(&["core/src/num/mod.rs"]),
PrimKind::I64 => Some(&["core/src/num/mod.rs"]),
PrimKind::I128 => Some(&["core/src/num/mod.rs"]),
PrimKind::U8 => Some(&["core/src/num/mod.rs"]),
PrimKind::U16 => Some(&["core/src/num/mod.rs"]),
PrimKind::U32 => Some(&["core/src/num/mod.rs"]),
PrimKind::U64 => Some(&["core/src/num/mod.rs"]),
PrimKind::U128 => Some(&["core/src/num/mod.rs"]),
PrimKind::Isize => Some(&["core/src/num/mod.rs"]),
PrimKind::Usize => Some(&["core/src/num/mod.rs"]),
PrimKind::Ref => None,
PrimKind::Fn => None,
PrimKind::Await => None,
Expand Down
21 changes: 16 additions & 5 deletions src/racer/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,12 @@ fn check_rust_sysroot() -> Option<path::PathBuf> {
if srcpath.exists() {
return Some(srcpath);
}
// See if the toolchain is sufficiently new, after the libstd
// has been internally reorganized
let srcpath = sysroot.join("lib/rustlib/src/rust/library");
if srcpath.exists() {
return Some(srcpath);
}
}
}
None
Expand Down Expand Up @@ -508,7 +514,7 @@ pub fn get_rust_src_path() -> Result<path::PathBuf, RustSrcPathError> {
}
};

debug!("Nope. Trying rustc --print sysroot and appending lib/rustlib/src/rust/src to that.");
debug!("Nope. Trying rustc --print sysroot and appending lib/rustlib/src/rust/{{src, library}} to that.");

if let Some(path) = check_rust_sysroot() {
return validate_rust_src_path(path);
Expand All @@ -531,11 +537,16 @@ pub fn get_rust_src_path() -> Result<path::PathBuf, RustSrcPathError> {

fn validate_rust_src_path(path: path::PathBuf) -> Result<path::PathBuf, RustSrcPathError> {
if !path.exists() {
Err(RustSrcPathError::DoesNotExist(path.to_path_buf()))
} else if !path.join("libstd").exists() {
Err(RustSrcPathError::NotRustSourceTree(path.join("libstd")))
} else {
return Err(RustSrcPathError::DoesNotExist(path));
}
// Historically, the Rust standard library was distributed under "libstd"
// but was later renamed to "std" when the library was moved under "library/"
// in https://github.com/rust-lang/rust/pull/73265.
if path.join("libstd").exists() || path.join("std").join("src").exists() {
Ok(path)
} else {

Err(RustSrcPathError::NotRustSourceTree(path.join("libstd")))
}
}

Expand Down
6 changes: 4 additions & 2 deletions testutils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,10 @@ pub fn get_one_completion(src: &str, dir: Option<TmpDir>) -> Match {
/// Panics if there is not exactly one completion.
pub fn get_only_completion(src: &str, dir: Option<TmpDir>) -> Match {
let mut all = get_all_completions(src, dir);
assert_eq!(all.len(), 1, "all: {:?}", all);
all.pop().unwrap()
match (all.pop(), all.as_slice()) {
(Some(head), &[]) => head,
(head, tail) => panic!("head: {:?}, tail: {:?}", head, tail),
}
}

/// Return the definition for the given source.
Expand Down