about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-11-27 10:19:38 +0000
committerbors <bors@rust-lang.org>2024-11-27 10:19:38 +0000
commit39cb3386ddc6c71657418be28dbb3987eea4aa4b (patch)
treed9dc869bd9dc664d264a9585d1e247fe07ae99ce
parent5f8a2405a6a7ea0ff85072b3bf90f4cff1144e85 (diff)
parent6798ecaf1000c223378151023b8eb37f3464af29 (diff)
downloadrust-39cb3386ddc6c71657418be28dbb3987eea4aa4b.tar.gz
rust-39cb3386ddc6c71657418be28dbb3987eea4aa4b.zip
Auto merge of #133369 - Zalathar:profiler-builtins-no-core, r=jieyouxu
Allow injecting a profiler runtime into `#![no_core]` crates

An alternative to #133300, allowing `-Cinstrument-coverage` to be used with `-Zbuild-std`.

The incompatibility between `profiler_builtins` and `#![no_core]` crates appears to have been caused by profiler_builtins depending on core, and therefore conflicting with core (or minicore).

But that's a false dependency, because the profiler doesn't contain any actual Rust code. So we can just mark the profiler itself as `#![no_core]`, and remove the incompatibility error.

---

For context, the error was originally added by #79958.
-rw-r--r--compiler/rustc_metadata/messages.ftl3
-rw-r--r--compiler/rustc_metadata/src/creader.rs14
-rw-r--r--compiler/rustc_metadata/src/errors.rs4
-rw-r--r--library/Cargo.lock2
-rw-r--r--library/profiler_builtins/Cargo.toml2
-rw-r--r--library/profiler_builtins/src/lib.rs12
-rw-r--r--tests/coverage/no-core.cov-map9
-rw-r--r--tests/coverage/no-core.coverage13
-rw-r--r--tests/coverage/no-core.rs12
9 files changed, 47 insertions, 24 deletions
diff --git a/compiler/rustc_metadata/messages.ftl b/compiler/rustc_metadata/messages.ftl
index d80aa0cc4f4..6d7d88fa8d7 100644
--- a/compiler/rustc_metadata/messages.ftl
+++ b/compiler/rustc_metadata/messages.ftl
@@ -233,9 +233,6 @@ metadata_prev_alloc_error_handler =
 metadata_prev_global_alloc =
     previous global allocator defined here
 
-metadata_profiler_builtins_needs_core =
-    `profiler_builtins` crate (required by compiler options) is not compatible with crate attribute `#![no_core]`
-
 metadata_raw_dylib_no_nul =
     link name must not contain NUL characters if link kind is `raw-dylib`
 
diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs
index 007d9265165..a18c6baec00 100644
--- a/compiler/rustc_metadata/src/creader.rs
+++ b/compiler/rustc_metadata/src/creader.rs
@@ -799,20 +799,16 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
         self.inject_dependency_if(cnum, "a panic runtime", &|data| data.needs_panic_runtime());
     }
 
-    fn inject_profiler_runtime(&mut self, krate: &ast::Crate) {
-        if self.sess.opts.unstable_opts.no_profiler_runtime
-            || !(self.sess.instrument_coverage() || self.sess.opts.cg.profile_generate.enabled())
-        {
+    fn inject_profiler_runtime(&mut self) {
+        let needs_profiler_runtime =
+            self.sess.instrument_coverage() || self.sess.opts.cg.profile_generate.enabled();
+        if !needs_profiler_runtime || self.sess.opts.unstable_opts.no_profiler_runtime {
             return;
         }
 
         info!("loading profiler");
 
         let name = Symbol::intern(&self.sess.opts.unstable_opts.profiler_runtime);
-        if name == sym::profiler_builtins && attr::contains_name(&krate.attrs, sym::no_core) {
-            self.dcx().emit_err(errors::ProfilerBuiltinsNeedsCore);
-        }
-
         let Some(cnum) = self.resolve_crate(name, DUMMY_SP, CrateDepKind::Implicit) else {
             return;
         };
@@ -1046,7 +1042,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
 
     pub fn postprocess(&mut self, krate: &ast::Crate) {
         self.inject_forced_externs();
-        self.inject_profiler_runtime(krate);
+        self.inject_profiler_runtime();
         self.inject_allocator_crate(krate);
         self.inject_panic_runtime(krate);
 
diff --git a/compiler/rustc_metadata/src/errors.rs b/compiler/rustc_metadata/src/errors.rs
index 16684ae6f26..b6c5619ec18 100644
--- a/compiler/rustc_metadata/src/errors.rs
+++ b/compiler/rustc_metadata/src/errors.rs
@@ -340,10 +340,6 @@ pub struct NoPanicStrategy {
 }
 
 #[derive(Diagnostic)]
-#[diag(metadata_profiler_builtins_needs_core)]
-pub struct ProfilerBuiltinsNeedsCore;
-
-#[derive(Diagnostic)]
 #[diag(metadata_not_profiler_runtime)]
 pub struct NotProfilerRuntime {
     pub crate_name: Symbol,
diff --git a/library/Cargo.lock b/library/Cargo.lock
index 55851daaf2a..197e0a8fedb 100644
--- a/library/Cargo.lock
+++ b/library/Cargo.lock
@@ -235,8 +235,6 @@ name = "profiler_builtins"
 version = "0.0.0"
 dependencies = [
  "cc",
- "compiler_builtins",
- "core",
 ]
 
 [[package]]
diff --git a/library/profiler_builtins/Cargo.toml b/library/profiler_builtins/Cargo.toml
index 9aadefce3b3..c601a41b433 100644
--- a/library/profiler_builtins/Cargo.toml
+++ b/library/profiler_builtins/Cargo.toml
@@ -9,8 +9,6 @@ bench = false
 doc = false
 
 [dependencies]
-core = { path = "../core" }
-compiler_builtins = { version = "0.1.0", features = ['rustc-dep-of-std'] }
 
 [build-dependencies]
 cc = "1.2"
diff --git a/library/profiler_builtins/src/lib.rs b/library/profiler_builtins/src/lib.rs
index ac685b18c29..a258f7d31a1 100644
--- a/library/profiler_builtins/src/lib.rs
+++ b/library/profiler_builtins/src/lib.rs
@@ -1,11 +1,15 @@
-#![no_std]
+// tidy-alphabetical-start
+#![allow(internal_features)]
+#![feature(no_core)]
 #![feature(profiler_runtime)]
+#![feature(staged_api)]
+// tidy-alphabetical-end
+
+// Other attributes:
+#![no_core]
 #![profiler_runtime]
 #![unstable(
     feature = "profiler_runtime_lib",
     reason = "internal implementation detail of rustc right now",
     issue = "none"
 )]
-#![allow(unused_features)]
-#![allow(internal_features)]
-#![feature(staged_api)]
diff --git a/tests/coverage/no-core.cov-map b/tests/coverage/no-core.cov-map
new file mode 100644
index 00000000000..3a1ca4745c7
--- /dev/null
+++ b/tests/coverage/no-core.cov-map
@@ -0,0 +1,9 @@
+Function name: no_core::main
+Raw bytes (9): 0x[01, 01, 00, 01, 01, 0c, 01, 00, 0d]
+Number of files: 1
+- file 0 => global file 1
+Number of expressions: 0
+Number of file 0 mappings: 1
+- Code(Counter(0)) at (prev + 12, 1) to (start + 0, 13)
+Highest counter ID seen: c0
+
diff --git a/tests/coverage/no-core.coverage b/tests/coverage/no-core.coverage
new file mode 100644
index 00000000000..8b890609568
--- /dev/null
+++ b/tests/coverage/no-core.coverage
@@ -0,0 +1,13 @@
+   LL|       |#![feature(no_core)]
+   LL|       |#![no_core]
+   LL|       |//@ edition: 2021
+   LL|       |
+   LL|       |// Test that coverage instrumentation works for `#![no_core]` crates.
+   LL|       |
+   LL|       |// For this test, we pull in std anyway, to avoid having to set up our own
+   LL|       |// no-core or no-std environment. What's important is that the compiler allows
+   LL|       |// coverage for a crate with the `#![no_core]` annotation.
+   LL|       |extern crate std;
+   LL|       |
+   LL|      1|fn main() {}
+
diff --git a/tests/coverage/no-core.rs b/tests/coverage/no-core.rs
new file mode 100644
index 00000000000..206222902fc
--- /dev/null
+++ b/tests/coverage/no-core.rs
@@ -0,0 +1,12 @@
+#![feature(no_core)]
+#![no_core]
+//@ edition: 2021
+
+// Test that coverage instrumentation works for `#![no_core]` crates.
+
+// For this test, we pull in std anyway, to avoid having to set up our own
+// no-core or no-std environment. What's important is that the compiler allows
+// coverage for a crate with the `#![no_core]` annotation.
+extern crate std;
+
+fn main() {}