about summary refs log tree commit diff
path: root/crates/hir-def/src
diff options
context:
space:
mode:
authorLukas Wirth <lukastw97@gmail.com>2023-05-26 22:41:32 +0200
committerLukas Wirth <lukastw97@gmail.com>2023-05-28 14:18:44 +0200
commitbbd9e41606cba8efb02e356f78b7f1c8cfc04a94 (patch)
treef9d9d0745b037b6016944a6194db97a8c1937366 /crates/hir-def/src
parent7c81fff52015b044a0d5dfadcd1e8caa2f237ea3 (diff)
downloadrust-bbd9e41606cba8efb02e356f78b7f1c8cfc04a94.tar.gz
rust-bbd9e41606cba8efb02e356f78b7f1c8cfc04a94.zip
Don't add --all-targets to runnables for no-std crates
Diffstat (limited to 'crates/hir-def/src')
-rw-r--r--crates/hir-def/src/nameres.rs10
-rw-r--r--crates/hir-def/src/nameres/collector.rs20
2 files changed, 25 insertions, 5 deletions
diff --git a/crates/hir-def/src/nameres.rs b/crates/hir-def/src/nameres.rs
index 8aceb4952a5..ccb9bed5c50 100644
--- a/crates/hir-def/src/nameres.rs
+++ b/crates/hir-def/src/nameres.rs
@@ -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>,
@@ -294,6 +296,8 @@ impl DefMap {
             unstable_features: FxHashSet::default(),
             diagnostics: Vec::new(),
             rustc_coherence_is_core: false,
+            no_core: false,
+            no_std: false,
         }
     }
 
@@ -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
     }
@@ -528,6 +536,8 @@ impl DefMap {
             prelude: _,
             root: _,
             rustc_coherence_is_core: _,
+            no_core: _,
+            no_std: _,
         } = self;
 
         extern_prelude.shrink_to_fit();
diff --git a/crates/hir-def/src/nameres/collector.rs b/crates/hir-def/src/nameres/collector.rs
index c49bb248a7b..64caf26299c 100644
--- a/crates/hir-def/src/nameres/collector.rs
+++ b/crates/hir-def/src/nameres/collector.rs
@@ -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() {
@@ -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;
@@ -359,6 +367,8 @@ impl DefCollector<'_> {
             }
         }
 
+        self.inject_prelude();
+
         ModCollector {
             def_collector: self,
             macro_depth: 0,
@@ -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];