Skip to content

Commit 806c6de

Browse files
committed
expose error for tracked::path and move unify under mod tracked
1 parent d627cf0 commit 806c6de

File tree

7 files changed

+71
-27
lines changed

7 files changed

+71
-27
lines changed

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ use rustc_span::symbol::{self, sym, Symbol};
1919
use rustc_span::{BytePos, FileName, Pos, SourceFile, Span};
2020
use smallvec::{smallvec, SmallVec};
2121
use std::ops::{Bound, Range};
22+
use std::{path, ascii, panic};
23+
use pm::bridge::{
24+
server, DelimSpan, ExpnGlobals, Group, Ident, LitKind, Literal, Punct, TokenTree,
25+
};
26+
use pm::{Delimiter, Level, LineColumn};
2227

2328
trait FromInternal<T> {
2429
fn from_internal(x: T) -> Self;
@@ -402,8 +407,8 @@ impl server::FreeFunctions for Rustc<'_, '_> {
402407
.insert((Symbol::intern(var), value.map(Symbol::intern)));
403408
}
404409

405-
fn track_path(&mut self, path: &str) {
406-
self.sess().file_depinfo.borrow_mut().insert(Symbol::intern(path));
410+
fn track_fs_path(&mut self, path: &str) {
411+
self.sess().file_depinfo.borrow_mut().insert(path::PathBuf::from(path));
407412
}
408413

409414
fn literal_from_str(&mut self, s: &str) -> Result<Literal<Self::Span, Self::Symbol>, ()> {

compiler/rustc_session/src/parse.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use rustc_span::source_map::{FilePathMapping, SourceMap};
2323
use rustc_span::{Span, Symbol};
2424

2525
use rustc_ast::attr::AttrIdGenerator;
26+
use std::path;
2627
use std::str;
2728

2829
/// The set of keys (and, optionally, values) that define the compilation
@@ -214,7 +215,9 @@ pub struct ParseSess {
214215
/// Environment variables accessed during the build and their values when they exist.
215216
pub env_depinfo: Lock<FxHashSet<(Symbol, Option<Symbol>)>>,
216217
/// File paths accessed during the build.
217-
pub file_depinfo: Lock<FxHashSet<Symbol>>,
218+
pub file_depinfo: Lock<FxHashSet<path::PathBuf>>,
219+
/// All the type ascriptions expressions that have had a suggestion for likely path typo.
220+
pub type_ascription_path_suggestions: Lock<FxHashSet<Span>>,
218221
/// Whether cfg(version) should treat the current release as incomplete
219222
pub assume_incomplete_release: bool,
220223
/// Spans passed to `proc_macro::quote_span`. Each span has a numerical

library/proc_macro/src/bridge/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use std::mem;
1616
use std::ops::Bound;
1717
use std::ops::Range;
1818
use std::panic;
19+
use std::path;
1920
use std::sync::atomic::AtomicUsize;
2021
use std::sync::Once;
2122
use std::thread;
@@ -56,7 +57,7 @@ macro_rules! with_api {
5657
FreeFunctions {
5758
fn drop($self: $S::FreeFunctions);
5859
fn track_env_var(var: &str, value: Option<&str>);
59-
fn track_path(path: &str);
60+
fn track_fs_path(path: &str);
6061
fn literal_from_str(s: &str) -> Result<Literal<$S::Span, $S::Symbol>, ()>;
6162
fn emit_diagnostic(diagnostic: Diagnostic<$S::Span>);
6263
},
@@ -300,6 +301,7 @@ mark_noop! {
300301
LitKind,
301302
Level,
302303
Spacing,
304+
path::PathBuf,
303305
}
304306

305307
rpc_encode_decode!(

library/proc_macro/src/bridge/rpc.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::any::Any;
44
use std::io::Write;
55
use std::num::NonZeroU32;
6+
use std::path;
67
use std::str;
78

89
pub(super) type Writer = super::buffer::Buffer;
@@ -244,6 +245,18 @@ impl<'a, S, T: for<'s> DecodeMut<'a, 's, S>> DecodeMut<'a, '_, S> for Vec<T> {
244245
}
245246
}
246247

248+
impl<S> Encode<S> for path::PathBuf {
249+
fn encode(self, w: &mut Writer, s: &mut S) {
250+
self.to_str().expect("`PathBuf`s must be valid UTF-8 for now!").encode(w, s);
251+
}
252+
}
253+
254+
impl<S> DecodeMut<'_, '_, S> for path::PathBuf {
255+
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
256+
path::PathBuf::from(<&str>::decode(r, s))
257+
}
258+
}
259+
247260
/// Simplified version of panic payloads, ignoring
248261
/// types other than `&'static str` and `String`.
249262
pub enum PanicMessage {

library/proc_macro/src/lib.rs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,9 +1489,34 @@ impl fmt::Debug for Literal {
14891489
}
14901490
}
14911491

1492-
/// Tracked access to environment variables.
1493-
#[unstable(feature = "proc_macro_tracked_env", issue = "99515")]
1494-
pub mod tracked_env {
1492+
#[unstable(feature = "proc_macro_tracked_env", issue = "74690")]
1493+
/// Tracked access to env and path.
1494+
pub mod tracked {
1495+
#[unstable(feature = "proc_macro_tracked_path", issue = "73921")]
1496+
use std::path::Path;
1497+
1498+
/// Track a file as if it was a dependency.
1499+
///
1500+
/// The file is located relative to the current file where the proc-macro
1501+
/// is used (similarly to how modules are found). The provided path is
1502+
/// interpreted in a platform-specific way at compile time. So, for
1503+
/// instance, an invocation with a Windows path
1504+
/// containing backslashes `\` would not compile correctly on Unix.
1505+
///
1506+
/// Errors if the provided `Path` cannot be encoded as a `str`
1507+
///
1508+
/// Commonly used for tracking asset preprocessing.
1509+
#[unstable(feature = "proc_macro_tracked_path", issue = "73921")]
1510+
pub fn path<P: AsRef<Path>>(path: P) -> Result<(), ()> {
1511+
let path: &Path = path.as_ref();
1512+
if let Some(path) = path.to_str() {
1513+
crate::bridge::client::FreeFunctions::track_fs_path(path);
1514+
Ok(())
1515+
} else {
1516+
Err(())
1517+
}
1518+
}
1519+
14951520
use std::env::{self, VarError};
14961521
use std::ffi::OsStr;
14971522

@@ -1500,25 +1525,11 @@ pub mod tracked_env {
15001525
/// compilation, and will be able to rerun the build when the value of that variable changes.
15011526
/// Besides the dependency tracking this function should be equivalent to `env::var` from the
15021527
/// standard library, except that the argument must be UTF-8.
1503-
#[unstable(feature = "proc_macro_tracked_env", issue = "99515")]
1504-
pub fn var<K: AsRef<OsStr> + AsRef<str>>(key: K) -> Result<String, VarError> {
1528+
#[unstable(feature = "proc_macro_tracked_env", issue = "74690")]
1529+
pub fn env_var<K: AsRef<OsStr> + AsRef<str>>(key: K) -> Result<String, VarError> {
15051530
let key: &str = key.as_ref();
15061531
let value = env::var(key);
15071532
crate::bridge::client::FreeFunctions::track_env_var(key, value.as_deref().ok());
15081533
value
15091534
}
15101535
}
1511-
1512-
/// Tracked access to additional files.
1513-
#[unstable(feature = "track_path", issue = "99515")]
1514-
pub mod tracked_path {
1515-
1516-
/// Track a file explicitly.
1517-
///
1518-
/// Commonly used for tracking asset preprocessing.
1519-
#[unstable(feature = "track_path", issue = "99515")]
1520-
pub fn path<P: AsRef<str>>(path: P) {
1521-
let path: &str = path.as_ref();
1522-
crate::bridge::client::FreeFunctions::track_path(path);
1523-
}
1524-
}

tests/run-make/env-dep-info/macro_def.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use proc_macro::*;
66

77
#[proc_macro]
88
pub fn access_env_vars(_: TokenStream) -> TokenStream {
9-
let _ = tracked_env::var("EXISTING_PROC_MACRO_ENV");
10-
let _ = tracked_env::var("NONEXISTENT_PROC_MACEO_ENV");
9+
let _ = tracked::env_var("EXISTING_PROC_MACRO_ENV");
10+
let _ = tracked::env_var("NONEXISTENT_PROC_MACEO_ENV");
1111
TokenStream::new()
1212
}
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
#![feature(track_path)]
1+
#![feature(proc_macro_tracked_env,proc_macro_tracked_path)]
22
#![crate_type = "proc-macro"]
33

44
extern crate proc_macro;
55
use proc_macro::*;
66

7+
use std::str;
8+
79
#[proc_macro]
810
pub fn access_tracked_paths(_: TokenStream) -> TokenStream {
9-
tracked_path::path("emojis.txt");
11+
assert!(tracked::path("emojis.txt").is_ok());
12+
13+
// currently only valid utf-8 paths are supported
14+
let invalid = [1_u8, 2,123, 254, 0, 0, 1, 1];
15+
let invalid: &str = unsafe {
16+
str::from_utf8_unchecked(&invalid[..])
17+
};
18+
assert!(tracked::path(invalid).is_err());
19+
1020
TokenStream::new()
1121
}

0 commit comments

Comments
 (0)