From 67e6a813159b5438507d4c24201e452a110a8284 Mon Sep 17 00:00:00 2001 From: Bernhard Schuster Date: Fri, 9 Apr 2021 16:35:40 +0200 Subject: [PATCH] add track_path::path fn for proc-macro usage Ref #73921 --- compiler/rustc_expand/src/proc_macro_server.rs | 4 ++++ compiler/rustc_interface/src/passes.rs | 16 +++++++++++++--- compiler/rustc_session/src/parse.rs | 3 +++ library/proc_macro/src/bridge/mod.rs | 1 + library/proc_macro/src/lib.rs | 14 ++++++++++++++ src/test/run-make/track-path-dep-info/Makefile | 13 +++++++++++++ src/test/run-make/track-path-dep-info/emojis.txt | 1 + .../run-make/track-path-dep-info/macro_def.rs | 11 +++++++++++ .../run-make/track-path-dep-info/macro_use.rs | 6 ++++++ 9 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 src/test/run-make/track-path-dep-info/Makefile create mode 100644 src/test/run-make/track-path-dep-info/emojis.txt create mode 100644 src/test/run-make/track-path-dep-info/macro_def.rs create mode 100644 src/test/run-make/track-path-dep-info/macro_use.rs diff --git a/compiler/rustc_expand/src/proc_macro_server.rs b/compiler/rustc_expand/src/proc_macro_server.rs index 92315c4d4f6c7..1d73002710d38 100644 --- a/compiler/rustc_expand/src/proc_macro_server.rs +++ b/compiler/rustc_expand/src/proc_macro_server.rs @@ -411,6 +411,10 @@ impl server::FreeFunctions for Rustc<'_> { fn track_env_var(&mut self, var: &str, value: Option<&str>) { self.sess.env_depinfo.borrow_mut().insert((Symbol::intern(var), value.map(Symbol::intern))); } + + fn track_path(&mut self, path: &str) { + self.sess.file_depinfo.borrow_mut().insert(Symbol::intern(path)); + } } impl server::TokenStream for Rustc<'_> { diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 9ea1f88b43f79..c0f7ea8df49e4 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -28,18 +28,18 @@ use rustc_passes::{self, hir_stats, layout_test}; use rustc_plugin_impl as plugin; use rustc_query_impl::Queries as TcxQueries; use rustc_resolve::{Resolver, ResolverArenas}; +use rustc_serialize::json; use rustc_session::config::{CrateType, Input, OutputFilenames, OutputType, PpMode, PpSourceMode}; use rustc_session::lint; use rustc_session::output::{filename_for_input, filename_for_metadata}; use rustc_session::search_paths::PathKind; use rustc_session::Session; use rustc_span::symbol::{Ident, Symbol}; +use rustc_span::FileName; use rustc_trait_selection::traits; use rustc_typeck as typeck; -use tracing::{info, warn}; - -use rustc_serialize::json; use tempfile::Builder as TempFileBuilder; +use tracing::{info, warn}; use std::any::Any; use std::cell::RefCell; @@ -594,6 +594,16 @@ fn write_out_deps( .map(|fmap| escape_dep_filename(&fmap.name.prefer_local().to_string())) .collect(); + // Account for explicitly marked-to-track files + // (e.g. accessed in proc macros). + let file_depinfo = sess.parse_sess.file_depinfo.borrow(); + let extra_tracked_files = file_depinfo.iter().map(|path_sym| { + let path = PathBuf::from(&*path_sym.as_str()); + let file = FileName::from(path); + escape_dep_filename(&file.prefer_local().to_string()) + }); + files.extend(extra_tracked_files); + if let Some(ref backend) = sess.opts.debugging_opts.codegen_backend { files.push(backend.to_string()); } diff --git a/compiler/rustc_session/src/parse.rs b/compiler/rustc_session/src/parse.rs index 7b7878e9c7f42..226fde2343aab 100644 --- a/compiler/rustc_session/src/parse.rs +++ b/compiler/rustc_session/src/parse.rs @@ -133,6 +133,8 @@ pub struct ParseSess { pub reached_eof: Lock, /// Environment variables accessed during the build and their values when they exist. pub env_depinfo: Lock)>>, + /// File paths accessed during the build. + pub file_depinfo: Lock>, /// All the type ascriptions expressions that have had a suggestion for likely path typo. pub type_ascription_path_suggestions: Lock>, /// Whether cfg(version) should treat the current release as incomplete @@ -165,6 +167,7 @@ impl ParseSess { symbol_gallery: SymbolGallery::default(), reached_eof: Lock::new(false), env_depinfo: Default::default(), + file_depinfo: Default::default(), type_ascription_path_suggestions: Default::default(), assume_incomplete_release: false, proc_macro_quoted_spans: Default::default(), diff --git a/library/proc_macro/src/bridge/mod.rs b/library/proc_macro/src/bridge/mod.rs index a2953b68564a8..b968d44fe488d 100644 --- a/library/proc_macro/src/bridge/mod.rs +++ b/library/proc_macro/src/bridge/mod.rs @@ -55,6 +55,7 @@ macro_rules! with_api { FreeFunctions { fn drop($self: $S::FreeFunctions); fn track_env_var(var: &str, value: Option<&str>); + fn track_path(path: &str); }, TokenStream { fn drop($self: $S::TokenStream); diff --git a/library/proc_macro/src/lib.rs b/library/proc_macro/src/lib.rs index 7586229504c22..7fbd5c6a69914 100644 --- a/library/proc_macro/src/lib.rs +++ b/library/proc_macro/src/lib.rs @@ -1235,3 +1235,17 @@ pub mod tracked_env { value } } + +/// Tracked access to additional files. +#[unstable(feature = "track_path", issue = "73921")] +pub mod tracked_path { + + /// Track a file explicitly. + /// + /// Commonly used for tracking asset preprocessing. + #[unstable(feature = "track_path", issue = "73921")] + pub fn path>(path: P) { + let path: &str = path.as_ref(); + crate::bridge::client::FreeFunctions::track_path(path); + } +} diff --git a/src/test/run-make/track-path-dep-info/Makefile b/src/test/run-make/track-path-dep-info/Makefile new file mode 100644 index 0000000000000..465d3744789b0 --- /dev/null +++ b/src/test/run-make/track-path-dep-info/Makefile @@ -0,0 +1,13 @@ +-include ../../run-make-fulldeps/tools.mk + +# FIXME(eddyb) provide `HOST_RUSTC` and `TARGET_RUSTC` +# instead of hardcoding them everywhere they're needed. +ifeq ($(IS_MUSL_HOST),1) +ADDITIONAL_ARGS := $(RUSTFLAGS) +endif + +all: + # Proc macro + $(BARE_RUSTC) $(ADDITIONAL_ARGS) --out-dir $(TMPDIR) macro_def.rs + EXISTING_PROC_MACRO_ENV=1 $(RUSTC) --emit dep-info macro_use.rs + $(CGREP) "emojis.txt:" < $(TMPDIR)/macro_use.d diff --git a/src/test/run-make/track-path-dep-info/emojis.txt b/src/test/run-make/track-path-dep-info/emojis.txt new file mode 100644 index 0000000000000..e1a728461f3c3 --- /dev/null +++ b/src/test/run-make/track-path-dep-info/emojis.txt @@ -0,0 +1 @@ +👾👾👾👾👾👾 diff --git a/src/test/run-make/track-path-dep-info/macro_def.rs b/src/test/run-make/track-path-dep-info/macro_def.rs new file mode 100644 index 0000000000000..8777ce21f8b82 --- /dev/null +++ b/src/test/run-make/track-path-dep-info/macro_def.rs @@ -0,0 +1,11 @@ +#![feature(track_path)] +#![crate_type = "proc-macro"] + +extern crate proc_macro; +use proc_macro::*; + +#[proc_macro] +pub fn access_tracked_paths(_: TokenStream) -> TokenStream { + tracked_path::path("emojis.txt"); + TokenStream::new() +} diff --git a/src/test/run-make/track-path-dep-info/macro_use.rs b/src/test/run-make/track-path-dep-info/macro_use.rs new file mode 100644 index 0000000000000..3c49fd05dd9e0 --- /dev/null +++ b/src/test/run-make/track-path-dep-info/macro_use.rs @@ -0,0 +1,6 @@ +#[macro_use] +extern crate macro_def; + +access_tracked_paths!(); + +fn main() {}