Skip to content

Commit 6ea7c31

Browse files
committed
Add extern source
1 parent 5a29230 commit 6ea7c31

File tree

8 files changed

+49
-11
lines changed

8 files changed

+49
-11
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: 20 additions & 4 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

@@ -128,9 +129,13 @@ pub struct ExternSourceId(pub u32);
128129
#[derive(Default, Debug, Clone, PartialEq, Eq)]
129130
pub struct Env {
130131
entries: FxHashMap<String, String>,
132+
}
131133

132-
// Note: Some env variables (e.g. OUT_DIR) are located outside of the
133-
// crate. We store a map to allow remap it to ExternSourceId
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 {
134139
extern_paths: FxHashMap<String, ExternSourceId>,
135140
}
136141

@@ -148,13 +153,15 @@ impl CrateGraph {
148153
display_name: Option<String>,
149154
cfg_options: CfgOptions,
150155
env: Env,
156+
extern_source: ExternSource,
151157
) -> CrateId {
152158
let data = CrateData {
153159
root_file_id: file_id,
154160
edition,
155161
display_name,
156162
cfg_options,
157163
env,
164+
extern_source,
158165
dependencies: Vec::new(),
159166
};
160167
let crate_id = CrateId(self.arena.len() as u32);
@@ -276,7 +283,9 @@ impl Env {
276283
pub fn get(&self, env: &str) -> Option<String> {
277284
self.entries.get(env).cloned()
278285
}
286+
}
279287

288+
impl ExternSource {
280289
pub fn extern_path(&self, path: &str) -> Option<(ExternSourceId, RelativePathBuf)> {
281290
self.extern_paths.iter().find_map(|(root_path, id)| {
282291
if path.starts_with(root_path) {
@@ -292,8 +301,7 @@ impl Env {
292301
})
293302
}
294303

295-
pub fn set_extern_path(&mut self, env: &str, root_path: &str, root: ExternSourceId) {
296-
self.entries.insert(env.to_owned(), root_path.to_owned());
304+
pub fn set_extern_path(&mut self, root_path: &str, root: ExternSourceId) {
297305
self.extern_paths.insert(root_path.to_owned(), root);
298306
}
299307
}
@@ -327,20 +335,23 @@ mod tests {
327335
None,
328336
CfgOptions::default(),
329337
Env::default(),
338+
Default::default(),
330339
);
331340
let crate2 = graph.add_crate_root(
332341
FileId(2u32),
333342
Edition2018,
334343
None,
335344
CfgOptions::default(),
336345
Env::default(),
346+
Default::default(),
337347
);
338348
let crate3 = graph.add_crate_root(
339349
FileId(3u32),
340350
Edition2018,
341351
None,
342352
CfgOptions::default(),
343353
Env::default(),
354+
Default::default(),
344355
);
345356
assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok());
346357
assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok());
@@ -356,20 +367,23 @@ mod tests {
356367
None,
357368
CfgOptions::default(),
358369
Env::default(),
370+
Default::default(),
359371
);
360372
let crate2 = graph.add_crate_root(
361373
FileId(2u32),
362374
Edition2018,
363375
None,
364376
CfgOptions::default(),
365377
Env::default(),
378+
Default::default(),
366379
);
367380
let crate3 = graph.add_crate_root(
368381
FileId(3u32),
369382
Edition2018,
370383
None,
371384
CfgOptions::default(),
372385
Env::default(),
386+
Default::default(),
373387
);
374388
assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok());
375389
assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok());
@@ -384,13 +398,15 @@ mod tests {
384398
None,
385399
CfgOptions::default(),
386400
Env::default(),
401+
Default::default(),
387402
);
388403
let crate2 = graph.add_crate_root(
389404
FileId(2u32),
390405
Edition2018,
391406
None,
392407
CfgOptions::default(),
393408
Env::default(),
409+
Default::default(),
394410
);
395411
assert!(graph
396412
.add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2)

crates/ra_db/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +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, ExternSourceId, FileId,
15-
SourceRoot, SourceRootId,
14+
CrateGraph, CrateId, CrateName, Dependency, Edition, Env, ExternSource, ExternSourceId,
15+
FileId, SourceRoot, SourceRootId,
1616
},
1717
};
1818
pub use relative_path::{RelativePath, RelativePathBuf};

crates/ra_hir_expand/src/builtin_macro.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,8 @@ fn relative_file(db: &dyn AstDatabase, call_id: MacroCallId, path: &str) -> Opti
262262

263263
// Extern paths ?
264264
let krate = db.relevant_crates(call_site).get(0)?.clone();
265-
let (extern_source_id, relative_file) = db.crate_graph()[krate].env.extern_path(path)?;
265+
let (extern_source_id, relative_file) =
266+
db.crate_graph()[krate].extern_source.extern_path(path)?;
266267

267268
db.resolve_extern_path(extern_source_id, &relative_file)
268269
}

crates/ra_ide/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ impl Analysis {
217217
None,
218218
cfg_options,
219219
Env::default(),
220+
Default::default(),
220221
);
221222
change.add_file(source_root, file_id, "main.rs".into(), Arc::new(text));
222223
change.set_crate_graph(crate_graph);

crates/ra_ide/src/mock_analysis.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ impl MockAnalysis {
102102
None,
103103
cfg_options,
104104
Env::default(),
105+
Default::default(),
105106
));
106107
} else if path.ends_with("/lib.rs") {
107108
let crate_name = path.parent().unwrap().file_name().unwrap();
@@ -111,6 +112,7 @@ impl MockAnalysis {
111112
Some(crate_name.to_owned()),
112113
cfg_options,
113114
Env::default(),
115+
Default::default(),
114116
);
115117
if let Some(root_crate) = root_crate {
116118
crate_graph

crates/ra_ide/src/parent_module.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ mod tests {
136136
None,
137137
CfgOptions::default(),
138138
Env::default(),
139+
Default::default(),
139140
);
140141
let mut change = AnalysisChange::new();
141142
change.set_crate_graph(crate_graph);

crates/ra_project_model/src/lib.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::{
1414

1515
use anyhow::{bail, Context, Result};
1616
use ra_cfg::CfgOptions;
17-
use ra_db::{CrateGraph, CrateName, Edition, Env, ExternSourceId, FileId};
17+
use ra_db::{CrateGraph, CrateName, Edition, Env, ExternSource, ExternSourceId, FileId};
1818
use rustc_hash::FxHashMap;
1919
use serde_json::from_reader;
2020

@@ -197,6 +197,7 @@ impl ProjectWorkspace {
197197
None,
198198
cfg_options,
199199
Env::default(),
200+
Default::default(),
200201
),
201202
);
202203
}
@@ -235,8 +236,10 @@ impl ProjectWorkspace {
235236
};
236237

237238
let mut env = Env::default();
239+
let mut extern_source = ExternSource::default();
238240
if let Some((id, path)) = outdirs.get(krate.name(&sysroot)) {
239-
env.set_extern_path("OUT_DIR", &path, *id);
241+
env.set("OUT_DIR", path.clone());
242+
extern_source.set_extern_path(&path, *id);
240243
}
241244

242245
let crate_id = crate_graph.add_crate_root(
@@ -245,6 +248,7 @@ impl ProjectWorkspace {
245248
Some(krate.name(&sysroot).to_string()),
246249
cfg_options,
247250
env,
251+
extern_source,
248252
);
249253
sysroot_crates.insert(krate, crate_id);
250254
}
@@ -284,15 +288,18 @@ impl ProjectWorkspace {
284288
opts
285289
};
286290
let mut env = Env::default();
291+
let mut extern_source = ExternSource::default();
287292
if let Some((id, path)) = outdirs.get(pkg.name(&cargo)) {
288-
env.set_extern_path("OUT_DIR", &path, *id);
293+
env.set("OUT_DIR", path.clone());
294+
extern_source.set_extern_path(&path, *id);
289295
}
290296
let crate_id = crate_graph.add_crate_root(
291297
file_id,
292298
edition,
293299
Some(pkg.name(&cargo).to_string()),
294300
cfg_options,
295301
env,
302+
extern_source,
296303
);
297304
if tgt.kind(&cargo) == TargetKind::Lib {
298305
lib_tgt = Some(crate_id);

0 commit comments

Comments
 (0)