From e669f6b86c8df77eaa64cf00c0d14e30a9458c9e Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Thu, 3 Sep 2020 22:18:01 +0200 Subject: [PATCH 1/2] Adapt to the new standard library directory layout Refs: https://github.com/rust-lang/rust/pull/73265 --- rust-toolchain | 2 +- src/racer/fileres.rs | 5 +++++ src/racer/nameres.rs | 32 +++++++++++++++++++++++++------- src/racer/primitive.rs | 40 ++++++++++++++++++++-------------------- src/racer/util.rs | 21 ++++++++++++++++----- testutils/src/lib.rs | 6 ++++-- 6 files changed, 71 insertions(+), 35 deletions(-) diff --git a/rust-toolchain b/rust-toolchain index df08a6fe..0294f6de 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -nightly-2020-09-02 \ No newline at end of file +nightly-2020-09-02 diff --git a/src/racer/fileres.rs b/src/racer/fileres.rs index 12e9bb07..4d1a5646 100644 --- a/src/racer/fileres.rs +++ b/src/racer/fileres.rs @@ -18,6 +18,11 @@ pub fn get_std_file(name: &str, session: &Session<'_>) -> Option { 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; } diff --git a/src/racer/nameres.rs b/src/racer/nameres.rs index 88faf70a..6a6e615e 100644 --- a/src/racer/nameres.rs +++ b/src/racer/nameres.rs @@ -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] @@ -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) { @@ -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"] { @@ -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; @@ -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() { @@ -2453,7 +2471,7 @@ fn get_std_macros( get_std_macros_( ¯o_path, searchstr, - macro_file == &"libcore/macros.rs", + macro_file == &"core/src/macros.rs", search_type, session, out, diff --git a/src/racer/primitive.rs b/src/racer/primitive.rs index cb1489be..e9e75922 100644 --- a/src/racer/primitive.rs +++ b/src/racer/primitive.rs @@ -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"; -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 { @@ -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, diff --git a/src/racer/util.rs b/src/racer/util.rs index 183e9793..ebd59f8f 100644 --- a/src/racer/util.rs +++ b/src/racer/util.rs @@ -455,6 +455,12 @@ fn check_rust_sysroot() -> Option { 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 @@ -508,7 +514,7 @@ pub fn get_rust_src_path() -> Result { } }; - 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); @@ -531,11 +537,16 @@ pub fn get_rust_src_path() -> Result { fn validate_rust_src_path(path: path::PathBuf) -> Result { 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"))) } } diff --git a/testutils/src/lib.rs b/testutils/src/lib.rs index 6b7a2725..f7877040 100644 --- a/testutils/src/lib.rs +++ b/testutils/src/lib.rs @@ -208,8 +208,10 @@ pub fn get_one_completion(src: &str, dir: Option) -> Match { /// Panics if there is not exactly one completion. pub fn get_only_completion(src: &str, dir: Option) -> 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. From db803aba1019b665fdbad0a4164e5f0c431668d1 Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Thu, 3 Sep 2020 23:20:46 +0200 Subject: [PATCH 2/2] ci: Update RUST_SRC_PATH to account for new std src layout --- .appveyor.yml | 2 +- .travis.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.appveyor.yml b/.appveyor.yml index 3fff6a87..75e5f86d 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -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 diff --git a/.travis.yml b/.travis.yml index 309cf7c8..ba69d7fd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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