Skip to content

Commit 881b57a

Browse files
committed
expose error for tracked::path and move unify under mod tracked
1 parent 77e24f9 commit 881b57a

File tree

7 files changed

+71
-27
lines changed

7 files changed

+71
-27
lines changed

compiler/rustc_expand/src/proc_macro_server.rs

+7-2
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

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use rustc_span::source_map::{FilePathMapping, SourceMap};
2121
use rustc_span::{Span, Symbol};
2222

2323
use rustc_ast::attr::AttrIdGenerator;
24+
use std::path;
2425
use std::str;
2526

2627
/// The set of keys (and, optionally, values) that define the compilation
@@ -209,7 +210,9 @@ pub struct ParseSess {
209210
/// Environment variables accessed during the build and their values when they exist.
210211
pub env_depinfo: Lock<FxHashSet<(Symbol, Option<Symbol>)>>,
211212
/// File paths accessed during the build.
212-
pub file_depinfo: Lock<FxHashSet<Symbol>>,
213+
pub file_depinfo: Lock<FxHashSet<path::PathBuf>>,
214+
/// All the type ascriptions expressions that have had a suggestion for likely path typo.
215+
pub type_ascription_path_suggestions: Lock<FxHashSet<Span>>,
213216
/// Whether cfg(version) should treat the current release as incomplete
214217
pub assume_incomplete_release: bool,
215218
/// Spans passed to `proc_macro::quote_span`. Each span has a numerical

library/proc_macro/src/bridge/mod.rs

+3-1
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

+13
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

+30-19
Original file line numberDiff line numberDiff line change
@@ -1458,9 +1458,34 @@ impl fmt::Debug for Literal {
14581458
}
14591459
}
14601460

1461-
/// Tracked access to environment variables.
1462-
#[unstable(feature = "proc_macro_tracked_env", issue = "99515")]
1463-
pub mod tracked_env {
1461+
#[unstable(feature = "proc_macro_tracked_env", issue = "74690")]
1462+
/// Tracked access to env and path.
1463+
pub mod tracked {
1464+
#[unstable(feature = "proc_macro_tracked_path", issue = "73921")]
1465+
use std::path::Path;
1466+
1467+
/// Track a file as if it was a dependency.
1468+
///
1469+
/// The file is located relative to the current file where the proc-macro
1470+
/// is used (similarly to how modules are found). The provided path is
1471+
/// interpreted in a platform-specific way at compile time. So, for
1472+
/// instance, an invocation with a Windows path
1473+
/// containing backslashes `\` would not compile correctly on Unix.
1474+
///
1475+
/// Errors if the provided `Path` cannot be encoded as a `str`
1476+
///
1477+
/// Commonly used for tracking asset preprocessing.
1478+
#[unstable(feature = "proc_macro_tracked_path", issue = "73921")]
1479+
pub fn path<P: AsRef<Path>>(path: P) -> Result<(), ()> {
1480+
let path: &Path = path.as_ref();
1481+
if let Some(path) = path.to_str() {
1482+
crate::bridge::client::FreeFunctions::track_fs_path(path);
1483+
Ok(())
1484+
} else {
1485+
Err(())
1486+
}
1487+
}
1488+
14641489
use std::env::{self, VarError};
14651490
use std::ffi::OsStr;
14661491

@@ -1469,25 +1494,11 @@ pub mod tracked_env {
14691494
/// compilation, and will be able to rerun the build when the value of that variable changes.
14701495
/// Besides the dependency tracking this function should be equivalent to `env::var` from the
14711496
/// standard library, except that the argument must be UTF-8.
1472-
#[unstable(feature = "proc_macro_tracked_env", issue = "99515")]
1473-
pub fn var<K: AsRef<OsStr> + AsRef<str>>(key: K) -> Result<String, VarError> {
1497+
#[unstable(feature = "proc_macro_tracked_env", issue = "74690")]
1498+
pub fn env_var<K: AsRef<OsStr> + AsRef<str>>(key: K) -> Result<String, VarError> {
14741499
let key: &str = key.as_ref();
14751500
let value = env::var(key);
14761501
crate::bridge::client::FreeFunctions::track_env_var(key, value.as_deref().ok());
14771502
value
14781503
}
14791504
}
1480-
1481-
/// Tracked access to additional files.
1482-
#[unstable(feature = "track_path", issue = "99515")]
1483-
pub mod tracked_path {
1484-
1485-
/// Track a file explicitly.
1486-
///
1487-
/// Commonly used for tracking asset preprocessing.
1488-
#[unstable(feature = "track_path", issue = "99515")]
1489-
pub fn path<P: AsRef<str>>(path: P) {
1490-
let path: &str = path.as_ref();
1491-
crate::bridge::client::FreeFunctions::track_path(path);
1492-
}
1493-
}

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

+2-2
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
}
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)