Skip to content

Don't add --all-targets to runnables for no-std crates #14912

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

Merged
merged 1 commit into from
May 30, 2023
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
10 changes: 10 additions & 0 deletions crates/hir-def/src/nameres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ pub struct DefMap {
unstable_features: FxHashSet<SmolStr>,
/// #[rustc_coherence_is_core]
rustc_coherence_is_core: bool,
no_core: bool,
no_std: bool,

edition: Edition,
recursion_limit: Option<u32>,
Expand Down Expand Up @@ -294,6 +296,8 @@ impl DefMap {
unstable_features: FxHashSet::default(),
diagnostics: Vec::new(),
rustc_coherence_is_core: false,
no_core: false,
no_std: false,
}
}

Expand Down Expand Up @@ -331,6 +335,10 @@ impl DefMap {
self.rustc_coherence_is_core
}

pub fn is_no_std(&self) -> bool {
self.no_std || self.no_core
}

pub fn root(&self) -> LocalModuleId {
self.root
}
Expand Down Expand Up @@ -528,6 +536,8 @@ impl DefMap {
prelude: _,
root: _,
rustc_coherence_is_core: _,
no_core: _,
no_std: _,
} = self;

extern_prelude.shrink_to_fit();
Expand Down
20 changes: 15 additions & 5 deletions crates/hir-def/src/nameres/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,6 @@ impl DefCollector<'_> {

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

self.inject_prelude(&attrs);

// Process other crate-level attributes.
for attr in &*attrs {
if let Some(cfg) = attr.cfg() {
Expand Down Expand Up @@ -321,6 +319,16 @@ impl DefCollector<'_> {
continue;
}

if *attr_name == hir_expand::name![no_core] {
self.def_map.no_core = true;
continue;
}

if *attr_name == hir_expand::name![no_std] {
self.def_map.no_std = true;
continue;
}

if attr_name.as_text().as_deref() == Some("rustc_coherence_is_core") {
self.def_map.rustc_coherence_is_core = true;
continue;
Expand Down Expand Up @@ -359,6 +367,8 @@ impl DefCollector<'_> {
}
}

self.inject_prelude();

ModCollector {
def_collector: self,
macro_depth: 0,
Expand Down Expand Up @@ -517,15 +527,15 @@ impl DefCollector<'_> {
}
}

fn inject_prelude(&mut self, crate_attrs: &Attrs) {
fn inject_prelude(&mut self) {
// See compiler/rustc_builtin_macros/src/standard_library_imports.rs

if crate_attrs.by_key("no_core").exists() {
if self.def_map.no_core {
// libcore does not get a prelude.
return;
}

let krate = if crate_attrs.by_key("no_std").exists() {
let krate = if self.def_map.no_std {
name![core]
} else {
let std = name![std];
Expand Down
2 changes: 2 additions & 0 deletions crates/hir-expand/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,8 @@ pub mod known {
crate_type,
derive,
global_allocator,
no_core,
no_std,
test,
test_case,
recursion_limit,
Expand Down
5 changes: 5 additions & 0 deletions crates/ide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,11 @@ impl Analysis {
self.with_db(|db| db.crate_graph()[crate_id].edition)
}

/// Returns true if this crate has `no_std` or `no_core` specified.
pub fn is_crate_no_std(&self, crate_id: CrateId) -> Cancellable<bool> {
self.with_db(|db| hir::db::DefDatabase::crate_def_map(db, crate_id).is_no_std())
}

/// Returns the root file of the given crate.
pub fn crate_root(&self, crate_id: CrateId) -> Cancellable<FileId> {
self.with_db(|db| db.crate_graph()[crate_id].root_file_id)
Expand Down
4 changes: 3 additions & 1 deletion crates/rust-analyzer/src/cargo_target_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::mem;

use cfg::{CfgAtom, CfgExpr};
use ide::{Cancellable, FileId, RunnableKind, TestId};
use ide::{Cancellable, CrateId, FileId, RunnableKind, TestId};
use project_model::{self, CargoFeatures, ManifestPath, TargetKind};
use rustc_hash::FxHashSet;
use vfs::AbsPathBuf;
Expand All @@ -21,6 +21,7 @@ pub(crate) struct CargoTargetSpec {
pub(crate) package: String,
pub(crate) target: String,
pub(crate) target_kind: TargetKind,
pub(crate) crate_id: CrateId,
pub(crate) required_features: Vec<String>,
pub(crate) features: FxHashSet<String>,
}
Expand Down Expand Up @@ -142,6 +143,7 @@ impl CargoTargetSpec {
target_kind: target_data.kind,
required_features: target_data.required_features.clone(),
features: package_data.features.keys().cloned().collect(),
crate_id,
};

Ok(Some(res))
Expand Down
19 changes: 12 additions & 7 deletions crates/rust-analyzer/src/handlers/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,20 +768,25 @@ pub(crate) fn handle_runnables(
let config = snap.config.runnables();
match cargo_spec {
Some(spec) => {
let all_targets = !snap.analysis.is_crate_no_std(spec.crate_id)?;
for cmd in ["check", "test"] {
let mut cargo_args =
vec![cmd.to_owned(), "--package".to_owned(), spec.package.clone()];
if all_targets {
cargo_args.push("--all-targets".to_owned());
}
res.push(lsp_ext::Runnable {
label: format!("cargo {cmd} -p {} --all-targets", spec.package),
label: format!(
"cargo {cmd} -p {}{all_targets}",
spec.package,
all_targets = if all_targets { " --all-targets" } else { "" }
),
location: None,
kind: lsp_ext::RunnableKind::Cargo,
args: lsp_ext::CargoRunnable {
workspace_root: Some(spec.workspace_root.clone().into()),
override_cargo: config.override_cargo.clone(),
cargo_args: vec![
cmd.to_string(),
"--package".to_string(),
spec.package.clone(),
"--all-targets".to_string(),
],
cargo_args,
cargo_extra_args: config.cargo_extra_args.clone(),
executable_args: Vec::new(),
expect_test: None,
Expand Down