Skip to content

Commit 76d8650

Browse files
committed
Auto merge of #14912 - Veykril:cargo-alltargets, r=Veykril
Don't add --all-targets to runnables for no-std crates Fixes #14155
2 parents be446c6 + bbd9e41 commit 76d8650

File tree

6 files changed

+47
-13
lines changed

6 files changed

+47
-13
lines changed

crates/hir-def/src/nameres.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ pub struct DefMap {
127127
unstable_features: FxHashSet<SmolStr>,
128128
/// #[rustc_coherence_is_core]
129129
rustc_coherence_is_core: bool,
130+
no_core: bool,
131+
no_std: bool,
130132

131133
edition: Edition,
132134
recursion_limit: Option<u32>,
@@ -294,6 +296,8 @@ impl DefMap {
294296
unstable_features: FxHashSet::default(),
295297
diagnostics: Vec::new(),
296298
rustc_coherence_is_core: false,
299+
no_core: false,
300+
no_std: false,
297301
}
298302
}
299303

@@ -331,6 +335,10 @@ impl DefMap {
331335
self.rustc_coherence_is_core
332336
}
333337

338+
pub fn is_no_std(&self) -> bool {
339+
self.no_std || self.no_core
340+
}
341+
334342
pub fn root(&self) -> LocalModuleId {
335343
self.root
336344
}
@@ -528,6 +536,8 @@ impl DefMap {
528536
prelude: _,
529537
root: _,
530538
rustc_coherence_is_core: _,
539+
no_core: _,
540+
no_std: _,
531541
} = self;
532542

533543
extern_prelude.shrink_to_fit();

crates/hir-def/src/nameres/collector.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,6 @@ impl DefCollector<'_> {
291291

292292
let attrs = item_tree.top_level_attrs(self.db, self.def_map.krate);
293293

294-
self.inject_prelude(&attrs);
295-
296294
// Process other crate-level attributes.
297295
for attr in &*attrs {
298296
if let Some(cfg) = attr.cfg() {
@@ -321,6 +319,16 @@ impl DefCollector<'_> {
321319
continue;
322320
}
323321

322+
if *attr_name == hir_expand::name![no_core] {
323+
self.def_map.no_core = true;
324+
continue;
325+
}
326+
327+
if *attr_name == hir_expand::name![no_std] {
328+
self.def_map.no_std = true;
329+
continue;
330+
}
331+
324332
if attr_name.as_text().as_deref() == Some("rustc_coherence_is_core") {
325333
self.def_map.rustc_coherence_is_core = true;
326334
continue;
@@ -359,6 +367,8 @@ impl DefCollector<'_> {
359367
}
360368
}
361369

370+
self.inject_prelude();
371+
362372
ModCollector {
363373
def_collector: self,
364374
macro_depth: 0,
@@ -517,15 +527,15 @@ impl DefCollector<'_> {
517527
}
518528
}
519529

520-
fn inject_prelude(&mut self, crate_attrs: &Attrs) {
530+
fn inject_prelude(&mut self) {
521531
// See compiler/rustc_builtin_macros/src/standard_library_imports.rs
522532

523-
if crate_attrs.by_key("no_core").exists() {
533+
if self.def_map.no_core {
524534
// libcore does not get a prelude.
525535
return;
526536
}
527537

528-
let krate = if crate_attrs.by_key("no_std").exists() {
538+
let krate = if self.def_map.no_std {
529539
name![core]
530540
} else {
531541
let std = name![std];

crates/hir-expand/src/name.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,8 @@ pub mod known {
366366
crate_type,
367367
derive,
368368
global_allocator,
369+
no_core,
370+
no_std,
369371
test,
370372
test_case,
371373
recursion_limit,

crates/ide/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,11 @@ impl Analysis {
531531
self.with_db(|db| db.crate_graph()[crate_id].edition)
532532
}
533533

534+
/// Returns true if this crate has `no_std` or `no_core` specified.
535+
pub fn is_crate_no_std(&self, crate_id: CrateId) -> Cancellable<bool> {
536+
self.with_db(|db| hir::db::DefDatabase::crate_def_map(db, crate_id).is_no_std())
537+
}
538+
534539
/// Returns the root file of the given crate.
535540
pub fn crate_root(&self, crate_id: CrateId) -> Cancellable<FileId> {
536541
self.with_db(|db| db.crate_graph()[crate_id].root_file_id)

crates/rust-analyzer/src/cargo_target_spec.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::mem;
44

55
use cfg::{CfgAtom, CfgExpr};
6-
use ide::{Cancellable, FileId, RunnableKind, TestId};
6+
use ide::{Cancellable, CrateId, FileId, RunnableKind, TestId};
77
use project_model::{self, CargoFeatures, ManifestPath, TargetKind};
88
use rustc_hash::FxHashSet;
99
use vfs::AbsPathBuf;
@@ -21,6 +21,7 @@ pub(crate) struct CargoTargetSpec {
2121
pub(crate) package: String,
2222
pub(crate) target: String,
2323
pub(crate) target_kind: TargetKind,
24+
pub(crate) crate_id: CrateId,
2425
pub(crate) required_features: Vec<String>,
2526
pub(crate) features: FxHashSet<String>,
2627
}
@@ -142,6 +143,7 @@ impl CargoTargetSpec {
142143
target_kind: target_data.kind,
143144
required_features: target_data.required_features.clone(),
144145
features: package_data.features.keys().cloned().collect(),
146+
crate_id,
145147
};
146148

147149
Ok(Some(res))

crates/rust-analyzer/src/handlers/request.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -768,20 +768,25 @@ pub(crate) fn handle_runnables(
768768
let config = snap.config.runnables();
769769
match cargo_spec {
770770
Some(spec) => {
771+
let all_targets = !snap.analysis.is_crate_no_std(spec.crate_id)?;
771772
for cmd in ["check", "test"] {
773+
let mut cargo_args =
774+
vec![cmd.to_owned(), "--package".to_owned(), spec.package.clone()];
775+
if all_targets {
776+
cargo_args.push("--all-targets".to_owned());
777+
}
772778
res.push(lsp_ext::Runnable {
773-
label: format!("cargo {cmd} -p {} --all-targets", spec.package),
779+
label: format!(
780+
"cargo {cmd} -p {}{all_targets}",
781+
spec.package,
782+
all_targets = if all_targets { " --all-targets" } else { "" }
783+
),
774784
location: None,
775785
kind: lsp_ext::RunnableKind::Cargo,
776786
args: lsp_ext::CargoRunnable {
777787
workspace_root: Some(spec.workspace_root.clone().into()),
778788
override_cargo: config.override_cargo.clone(),
779-
cargo_args: vec![
780-
cmd.to_string(),
781-
"--package".to_string(),
782-
spec.package.clone(),
783-
"--all-targets".to_string(),
784-
],
789+
cargo_args,
785790
cargo_extra_args: config.cargo_extra_args.clone(),
786791
executable_args: Vec::new(),
787792
expect_test: None,

0 commit comments

Comments
 (0)