Skip to content

Commit c48dcf7

Browse files
Merge #3549
3549: Implement env! macro r=matklad a=edwin0cheng This PR implements `env!` macro by adding following things: 1. Added `additional_outdirs` settings in vscode. (naming to be bikeshed) 2. Added `ExternSourceId` which is a wrapping for SourceRootId but only used in extern sources. It is because `OUT_DIR` is not belonged to any crate and we have to access it behind an `AstDatabase`. 3. This PR does not implement the `OUT_DIR` parsing from `cargo check`. I don't have general design about this, @kiljacken could we reuse some cargo watch code for that ? ~~Block on [#3536]~~ PS: After this PR , we (kind of) completed the `include!(concat!(env!('OUT_DIR'), "foo.rs")` macro call combo. [Exodia Obliterate!](https://www.youtube.com/watch?v=RfqNH3FoGi0) Co-authored-by: Edwin Cheng <edwin0cheng@gmail.com>
2 parents 7b323b4 + 6ea7c31 commit c48dcf7

File tree

20 files changed

+320
-72
lines changed

20 files changed

+320
-72
lines changed

crates/ra_db/src/fixture.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,14 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId
6161
};
6262

6363
let mut crate_graph = CrateGraph::default();
64-
crate_graph.add_crate_root(file_id, meta.edition, meta.krate, meta.cfg, meta.env);
64+
crate_graph.add_crate_root(
65+
file_id,
66+
meta.edition,
67+
meta.krate,
68+
meta.cfg,
69+
meta.env,
70+
Default::default(),
71+
);
6572
crate_graph
6673
} else {
6774
let mut crate_graph = CrateGraph::default();
@@ -71,6 +78,7 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId
7178
None,
7279
CfgOptions::default(),
7380
Env::default(),
81+
Default::default(),
7482
);
7583
crate_graph
7684
};
@@ -119,6 +127,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
119127
Some(krate.clone()),
120128
meta.cfg,
121129
meta.env,
130+
Default::default(),
122131
);
123132
let prev = crates.insert(krate.clone(), crate_id);
124133
assert!(prev.is_none());
@@ -155,6 +164,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
155164
None,
156165
CfgOptions::default(),
157166
Env::default(),
167+
Default::default(),
158168
);
159169
} else {
160170
for (from, to) in crate_deps {

crates/ra_db/src/input.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ pub struct CrateData {
113113
pub display_name: Option<String>,
114114
pub cfg_options: CfgOptions,
115115
pub env: Env,
116+
pub extern_source: ExternSource,
116117
pub dependencies: Vec<Dependency>,
117118
}
118119

@@ -122,11 +123,22 @@ pub enum Edition {
122123
Edition2015,
123124
}
124125

126+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
127+
pub struct ExternSourceId(pub u32);
128+
125129
#[derive(Default, Debug, Clone, PartialEq, Eq)]
126130
pub struct Env {
127131
entries: FxHashMap<String, String>,
128132
}
129133

134+
// FIXME: Redesign vfs for solve the following limitation ?
135+
// Note: Some env variables (e.g. OUT_DIR) are located outside of the
136+
// crate. We store a map to allow remap it to ExternSourceId
137+
#[derive(Default, Debug, Clone, PartialEq, Eq)]
138+
pub struct ExternSource {
139+
extern_paths: FxHashMap<String, ExternSourceId>,
140+
}
141+
130142
#[derive(Debug, Clone, PartialEq, Eq)]
131143
pub struct Dependency {
132144
pub crate_id: CrateId,
@@ -141,13 +153,15 @@ impl CrateGraph {
141153
display_name: Option<String>,
142154
cfg_options: CfgOptions,
143155
env: Env,
156+
extern_source: ExternSource,
144157
) -> CrateId {
145158
let data = CrateData {
146159
root_file_id: file_id,
147160
edition,
148161
display_name,
149162
cfg_options,
150163
env,
164+
extern_source,
151165
dependencies: Vec::new(),
152166
};
153167
let crate_id = CrateId(self.arena.len() as u32);
@@ -271,6 +285,27 @@ impl Env {
271285
}
272286
}
273287

288+
impl ExternSource {
289+
pub fn extern_path(&self, path: &str) -> Option<(ExternSourceId, RelativePathBuf)> {
290+
self.extern_paths.iter().find_map(|(root_path, id)| {
291+
if path.starts_with(root_path) {
292+
let mut rel_path = &path[root_path.len()..];
293+
if rel_path.starts_with("/") {
294+
rel_path = &rel_path[1..];
295+
}
296+
let rel_path = RelativePathBuf::from_path(rel_path).ok()?;
297+
Some((id.clone(), rel_path))
298+
} else {
299+
None
300+
}
301+
})
302+
}
303+
304+
pub fn set_extern_path(&mut self, root_path: &str, root: ExternSourceId) {
305+
self.extern_paths.insert(root_path.to_owned(), root);
306+
}
307+
}
308+
274309
#[derive(Debug)]
275310
pub struct ParseEditionError {
276311
invalid_input: String,
@@ -300,20 +335,23 @@ mod tests {
300335
None,
301336
CfgOptions::default(),
302337
Env::default(),
338+
Default::default(),
303339
);
304340
let crate2 = graph.add_crate_root(
305341
FileId(2u32),
306342
Edition2018,
307343
None,
308344
CfgOptions::default(),
309345
Env::default(),
346+
Default::default(),
310347
);
311348
let crate3 = graph.add_crate_root(
312349
FileId(3u32),
313350
Edition2018,
314351
None,
315352
CfgOptions::default(),
316353
Env::default(),
354+
Default::default(),
317355
);
318356
assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok());
319357
assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok());
@@ -329,20 +367,23 @@ mod tests {
329367
None,
330368
CfgOptions::default(),
331369
Env::default(),
370+
Default::default(),
332371
);
333372
let crate2 = graph.add_crate_root(
334373
FileId(2u32),
335374
Edition2018,
336375
None,
337376
CfgOptions::default(),
338377
Env::default(),
378+
Default::default(),
339379
);
340380
let crate3 = graph.add_crate_root(
341381
FileId(3u32),
342382
Edition2018,
343383
None,
344384
CfgOptions::default(),
345385
Env::default(),
386+
Default::default(),
346387
);
347388
assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok());
348389
assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok());
@@ -357,13 +398,15 @@ mod tests {
357398
None,
358399
CfgOptions::default(),
359400
Env::default(),
401+
Default::default(),
360402
);
361403
let crate2 = graph.add_crate_root(
362404
FileId(2u32),
363405
Edition2018,
364406
None,
365407
CfgOptions::default(),
366408
Env::default(),
409+
Default::default(),
367410
);
368411
assert!(graph
369412
.add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2)

crates/ra_db/src/lib.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use ra_syntax::{ast, Parse, SourceFile, TextRange, TextUnit};
1111
pub use crate::{
1212
cancellation::Canceled,
1313
input::{
14-
CrateGraph, CrateId, CrateName, Dependency, Edition, Env, FileId, SourceRoot, SourceRootId,
14+
CrateGraph, CrateId, CrateName, Dependency, Edition, Env, ExternSource, ExternSourceId,
15+
FileId, SourceRoot, SourceRootId,
1516
},
1617
};
1718
pub use relative_path::{RelativePath, RelativePathBuf};
@@ -87,6 +88,12 @@ pub trait FileLoader {
8788
fn resolve_relative_path(&self, anchor: FileId, relative_path: &RelativePath)
8889
-> Option<FileId>;
8990
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>>;
91+
92+
fn resolve_extern_path(
93+
&self,
94+
extern_id: ExternSourceId,
95+
relative_path: &RelativePath,
96+
) -> Option<FileId>;
9097
}
9198

9299
/// Database which stores all significant input facts: source code and project
@@ -164,4 +171,13 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
164171
let source_root = self.0.file_source_root(file_id);
165172
self.0.source_root_crates(source_root)
166173
}
174+
175+
fn resolve_extern_path(
176+
&self,
177+
extern_id: ExternSourceId,
178+
relative_path: &RelativePath,
179+
) -> Option<FileId> {
180+
let source_root = self.0.source_root(SourceRootId(extern_id.0));
181+
source_root.file_by_relative_path(&relative_path)
182+
}
167183
}

crates/ra_hir_def/src/test_db.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66
};
77

88
use crate::db::DefDatabase;
9-
use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath};
9+
use ra_db::{salsa, CrateId, ExternSourceId, FileId, FileLoader, FileLoaderDelegate, RelativePath};
1010

1111
#[salsa::database(
1212
ra_db::SourceDatabaseExtStorage,
@@ -52,6 +52,14 @@ impl FileLoader for TestDB {
5252
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
5353
FileLoaderDelegate(self).relevant_crates(file_id)
5454
}
55+
56+
fn resolve_extern_path(
57+
&self,
58+
extern_id: ExternSourceId,
59+
relative_path: &RelativePath,
60+
) -> Option<FileId> {
61+
FileLoaderDelegate(self).resolve_extern_path(extern_id, relative_path)
62+
}
5563
}
5664

5765
impl TestDB {

0 commit comments

Comments
 (0)