Skip to content

Commit e669f6b

Browse files
committed
Adapt to the new standard library directory layout
Refs: rust-lang/rust#73265
1 parent 2bda0ad commit e669f6b

File tree

6 files changed

+71
-35
lines changed

6 files changed

+71
-35
lines changed

rust-toolchain

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
nightly-2020-09-02
1+
nightly-2020-09-02

src/racer/fileres.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ pub fn get_std_file(name: &str, session: &Session<'_>) -> Option<PathBuf> {
1818
if filepath.exists() || session.contains_file(&filepath) {
1919
return Some(filepath);
2020
}
21+
// If not found, try using the new standard library directory layout
22+
let filepath = std_path.join(name).join("src").join("lib.rs");
23+
if filepath.exists() || session.contains_file(&filepath) {
24+
return Some(filepath);
25+
}
2126
}
2227
return None;
2328
}

src/racer/nameres.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ fn test_do_file_search_std() {
765765
let matches = do_file_search("std", path, &session);
766766
assert!(matches
767767
.into_iter()
768-
.any(|m| m.filepath.ends_with("src/libstd/lib.rs")));
768+
.any(|m| m.filepath.ends_with("std/src/lib.rs")));
769769
}
770770

771771
#[test]
@@ -804,6 +804,7 @@ pub fn do_file_search(searchstr: &str, currentdir: &Path, session: &Session<'_>)
804804
Some(fname) => fname,
805805
None => continue,
806806
};
807+
// Firstly, try the original layout, e.g. libstd/lib.rs
807808
if fname.starts_with(&format!("lib{}", searchstr)) {
808809
let filepath = fpath_buf.join("lib.rs");
809810
if filepath.exists() || session.contains_file(&filepath) {
@@ -820,6 +821,23 @@ pub fn do_file_search(searchstr: &str, currentdir: &Path, session: &Session<'_>)
820821
out.push(m);
821822
}
822823
}
824+
// Secondly, try the new standard library layout, e.g. std/src/lib.rs
825+
if fname.starts_with(searchstr) {
826+
let filepath = fpath_buf.join("src").join("lib.rs");
827+
if filepath.exists() || session.contains_file(&filepath) {
828+
let m = Match {
829+
matchstr: fname.to_owned(),
830+
filepath: filepath.to_path_buf(),
831+
point: BytePos::ZERO,
832+
coords: Some(Coordinate::start()),
833+
local: false,
834+
mtype: MatchType::Module,
835+
contextstr: fname.to_owned(),
836+
docs: String::new(),
837+
};
838+
out.push(m);
839+
}
840+
}
823841

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

13641382
// find the prelude file from the search path and scan it
13651383
if let Some(ref std_path) = *RUST_SRC_PATH {
1366-
let filepath = std_path.join("libstd").join("prelude").join("v1.rs");
1384+
let filepath = std_path.join("std").join("src").join("prelude").join("v1.rs");
13671385
if filepath.exists() || session.contains_file(&filepath) {
13681386
let msrc = session.load_source_file(&filepath);
13691387
let is_local = true;
@@ -2441,10 +2459,10 @@ fn get_std_macros(
24412459
searchstr
24422460
};
24432461
for macro_file in &[
2444-
"libstd/macros.rs",
2445-
"libcore/macros.rs",
2446-
"libcore/macros/mod.rs",
2447-
"liballoc/macros.rs",
2462+
"std/src/macros.rs",
2463+
"core/src/macros.rs",
2464+
"core/src/macros/mod.rs",
2465+
"alloc/src/macros.rs",
24482466
] {
24492467
let macro_path = std_path.join(macro_file);
24502468
if !macro_path.exists() {
@@ -2453,7 +2471,7 @@ fn get_std_macros(
24532471
get_std_macros_(
24542472
&macro_path,
24552473
searchstr,
2456-
macro_file == &"libcore/macros.rs",
2474+
macro_file == &"core/src/macros.rs",
24572475
search_type,
24582476
session,
24592477
out,

src/racer/primitive.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use crate::nameres::{self, RUST_SRC_PATH};
44
use rustc_ast::ast::{IntTy, LitIntType, UintTy};
55
use std::path::PathBuf;
66

7-
const PRIM_DOC: &str = "libstd/primitive_docs.rs";
8-
const KEY_DOC: &str = "libstd/keyword_docs.rs";
7+
const PRIM_DOC: &str = "std/src/primitive_docs.rs";
8+
const KEY_DOC: &str = "std/src/keyword_docs.rs";
99

1010
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
1111
pub enum PrimKind {
@@ -83,27 +83,27 @@ impl PrimKind {
8383
match self {
8484
PrimKind::Bool => None,
8585
PrimKind::Never => None,
86-
PrimKind::Char => Some(&["libcore/char/methods.rs"]),
86+
PrimKind::Char => Some(&["core/src/char/methods.rs"]),
8787
PrimKind::Unit => None,
88-
PrimKind::Pointer => Some(&["libcore/ptr.rs"]),
88+
PrimKind::Pointer => Some(&["core/src/ptr.rs"]),
8989
PrimKind::Array => None,
90-
PrimKind::Slice => Some(&["libcore/slice/mod.rs", "liballoc/slice.rs"]),
91-
PrimKind::Str => Some(&["libcore/str/mod.rs", "liballoc/str.rs"]),
90+
PrimKind::Slice => Some(&["core/src/slice/mod.rs", "alloc/src/slice.rs"]),
91+
PrimKind::Str => Some(&["core/src/str/mod.rs", "alloc/src/str.rs"]),
9292
PrimKind::Tuple => None,
93-
PrimKind::F32 => Some(&["libstd/f32.rs", "libcore/num/f32.rs"]),
94-
PrimKind::F64 => Some(&["libstd/f64.rs", "libcore/num/f64.rs"]),
95-
PrimKind::I8 => Some(&["libcore/num/mod.rs"]),
96-
PrimKind::I16 => Some(&["libcore/num/mod.rs"]),
97-
PrimKind::I32 => Some(&["libcore/num/mod.rs"]),
98-
PrimKind::I64 => Some(&["libcore/num/mod.rs"]),
99-
PrimKind::I128 => Some(&["libcore/num/mod.rs"]),
100-
PrimKind::U8 => Some(&["libcore/num/mod.rs"]),
101-
PrimKind::U16 => Some(&["libcore/num/mod.rs"]),
102-
PrimKind::U32 => Some(&["libcore/num/mod.rs"]),
103-
PrimKind::U64 => Some(&["libcore/num/mod.rs"]),
104-
PrimKind::U128 => Some(&["libcore/num/mod.rs"]),
105-
PrimKind::Isize => Some(&["libcore/num/mod.rs"]),
106-
PrimKind::Usize => Some(&["libcore/num/mod.rs"]),
93+
PrimKind::F32 => Some(&["std/src/f32.rs", "core/src/num/f32.rs"]),
94+
PrimKind::F64 => Some(&["std/src/f64.rs", "core/src/num/f64.rs"]),
95+
PrimKind::I8 => Some(&["core/src/num/mod.rs"]),
96+
PrimKind::I16 => Some(&["core/src/num/mod.rs"]),
97+
PrimKind::I32 => Some(&["core/src/num/mod.rs"]),
98+
PrimKind::I64 => Some(&["core/src/num/mod.rs"]),
99+
PrimKind::I128 => Some(&["core/src/num/mod.rs"]),
100+
PrimKind::U8 => Some(&["core/src/num/mod.rs"]),
101+
PrimKind::U16 => Some(&["core/src/num/mod.rs"]),
102+
PrimKind::U32 => Some(&["core/src/num/mod.rs"]),
103+
PrimKind::U64 => Some(&["core/src/num/mod.rs"]),
104+
PrimKind::U128 => Some(&["core/src/num/mod.rs"]),
105+
PrimKind::Isize => Some(&["core/src/num/mod.rs"]),
106+
PrimKind::Usize => Some(&["core/src/num/mod.rs"]),
107107
PrimKind::Ref => None,
108108
PrimKind::Fn => None,
109109
PrimKind::Await => None,

src/racer/util.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,12 @@ fn check_rust_sysroot() -> Option<path::PathBuf> {
455455
if srcpath.exists() {
456456
return Some(srcpath);
457457
}
458+
// See if the toolchain is sufficiently new, after the libstd
459+
// has been internally reorganized
460+
let srcpath = sysroot.join("lib/rustlib/src/rust/library");
461+
if srcpath.exists() {
462+
return Some(srcpath);
463+
}
458464
}
459465
}
460466
None
@@ -508,7 +514,7 @@ pub fn get_rust_src_path() -> Result<path::PathBuf, RustSrcPathError> {
508514
}
509515
};
510516

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

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

532538
fn validate_rust_src_path(path: path::PathBuf) -> Result<path::PathBuf, RustSrcPathError> {
533539
if !path.exists() {
534-
Err(RustSrcPathError::DoesNotExist(path.to_path_buf()))
535-
} else if !path.join("libstd").exists() {
536-
Err(RustSrcPathError::NotRustSourceTree(path.join("libstd")))
537-
} else {
540+
return Err(RustSrcPathError::DoesNotExist(path));
541+
}
542+
// Historically, the Rust standard library was distributed under "libstd"
543+
// but was later renamed to "std" when the library was moved under "library/"
544+
// in https://github.com/rust-lang/rust/pull/73265.
545+
if path.join("libstd").exists() || path.join("std").join("src").exists() {
538546
Ok(path)
547+
} else {
548+
549+
Err(RustSrcPathError::NotRustSourceTree(path.join("libstd")))
539550
}
540551
}
541552

testutils/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,10 @@ pub fn get_one_completion(src: &str, dir: Option<TmpDir>) -> Match {
208208
/// Panics if there is not exactly one completion.
209209
pub fn get_only_completion(src: &str, dir: Option<TmpDir>) -> Match {
210210
let mut all = get_all_completions(src, dir);
211-
assert_eq!(all.len(), 1, "all: {:?}", all);
212-
all.pop().unwrap()
211+
match (all.pop(), all.as_slice()) {
212+
(Some(head), &[]) => head,
213+
(head, tail) => panic!("head: {:?}, tail: {:?}", head, tail),
214+
}
213215
}
214216

215217
/// Return the definition for the given source.

0 commit comments

Comments
 (0)