Skip to content

Commit 1f6abb7

Browse files
committed
Fix libcore not being included in rust-lang/rust module tree
If you are opening libcore from rust-lang/rust as opposed to e.g. goto definition from some other crate which would use the sysroot instance of libcore, a `#![cfg(not(test))]` would previously have made all the code excluded from the module tree, breaking the editor experience. This puts in a slight hack that checks for the crate name "core" and turns off `#[cfg(test)]`.
1 parent 0d3bad8 commit 1f6abb7

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

crates/cfg/src/lib.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! cfg defines conditional compiling options, `cfg` attibute parser and evaluator
1+
//! cfg defines conditional compiling options, `cfg` attribute parser and evaluator
22
33
mod cfg_expr;
44
mod dnf;
@@ -59,6 +59,20 @@ pub struct CfgDiff {
5959
}
6060

6161
impl CfgDiff {
62+
/// Create a new CfgDiff. Will return None if the same item appears more than once in the set
63+
/// of both.
64+
pub fn new(enable: Vec<CfgAtom>, disable: Vec<CfgAtom>) -> Option<CfgDiff> {
65+
let mut occupied = FxHashSet::default();
66+
for item in enable.iter().chain(disable.iter()) {
67+
if !occupied.insert(item) {
68+
// was present
69+
return None;
70+
}
71+
}
72+
73+
Some(CfgDiff { enable, disable })
74+
}
75+
6276
/// Returns the total number of atoms changed by this diff.
6377
pub fn len(&self) -> usize {
6478
self.enable.len() + self.disable.len()

crates/project_model/src/workspace.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{collections::VecDeque, fmt, fs, path::Path, process::Command};
77
use anyhow::{format_err, Context, Result};
88
use base_db::{CrateDisplayName, CrateGraph, CrateId, CrateName, Edition, Env, FileId, ProcMacro};
99
use cargo_workspace::DepKind;
10-
use cfg::CfgOptions;
10+
use cfg::{CfgAtom, CfgDiff, CfgOptions};
1111
use paths::{AbsPath, AbsPathBuf};
1212
use proc_macro_api::ProcMacroClient;
1313
use rustc_hash::{FxHashMap, FxHashSet};
@@ -425,6 +425,20 @@ fn cargo_to_crate_graph(
425425
let mut has_private = false;
426426
// Next, create crates for each package, target pair
427427
for pkg in cargo.packages() {
428+
let mut cfg_options = &cfg_options;
429+
let mut replaced_cfg_options;
430+
if cargo[pkg].name == "core" {
431+
// FIXME: in the specific case of libcore in rust-lang/rust (i.e. it is not coming from
432+
// a sysroot), there's a `#![cfg(not(test))]` at the top of it that makes its contents
433+
// get ignored by r-a. We should implement a more general solution for this
434+
435+
replaced_cfg_options = cfg_options.clone();
436+
replaced_cfg_options.apply_diff(
437+
CfgDiff::new(Default::default(), vec![CfgAtom::Flag("test".into())]).unwrap(),
438+
);
439+
cfg_options = &replaced_cfg_options;
440+
};
441+
428442
has_private |= cargo[pkg].metadata.rustc_private;
429443
let mut lib_tgt = None;
430444
for &tgt in cargo[pkg].targets.iter() {

0 commit comments

Comments
 (0)