Skip to content

Commit 15d9ea2

Browse files
committed
Keep track of crate edition
1 parent c62c24a commit 15d9ea2

File tree

8 files changed

+44
-24
lines changed

8 files changed

+44
-24
lines changed

crates/ra_db/src/input.rs

+19-12
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,22 @@ pub struct CyclicDependencies;
5656
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
5757
pub struct CrateId(pub u32);
5858

59+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
60+
pub enum Edition {
61+
Edition2018,
62+
Edition2015,
63+
}
64+
5965
#[derive(Debug, Clone, PartialEq, Eq)]
6066
struct CrateData {
6167
file_id: FileId,
68+
edition: Edition,
6269
dependencies: Vec<Dependency>,
6370
}
6471

6572
impl CrateData {
66-
fn new(file_id: FileId) -> CrateData {
67-
CrateData { file_id, dependencies: Vec::new() }
73+
fn new(file_id: FileId, edition: Edition) -> CrateData {
74+
CrateData { file_id, edition, dependencies: Vec::new() }
6875
}
6976

7077
fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) {
@@ -85,9 +92,9 @@ impl Dependency {
8592
}
8693

8794
impl CrateGraph {
88-
pub fn add_crate_root(&mut self, file_id: FileId) -> CrateId {
95+
pub fn add_crate_root(&mut self, file_id: FileId, edition: Edition) -> CrateId {
8996
let crate_id = CrateId(self.arena.len() as u32);
90-
let prev = self.arena.insert(crate_id, CrateData::new(file_id));
97+
let prev = self.arena.insert(crate_id, CrateData::new(file_id, edition));
9198
assert!(prev.is_none());
9299
crate_id
93100
}
@@ -159,14 +166,14 @@ impl CrateGraph {
159166

160167
#[cfg(test)]
161168
mod tests {
162-
use super::{CrateGraph, FileId, SmolStr};
169+
use super::{CrateGraph, FileId, SmolStr, Edition::Edition2018};
163170

164171
#[test]
165-
fn it_should_painc_because_of_cycle_dependencies() {
172+
fn it_should_panic_because_of_cycle_dependencies() {
166173
let mut graph = CrateGraph::default();
167-
let crate1 = graph.add_crate_root(FileId(1u32));
168-
let crate2 = graph.add_crate_root(FileId(2u32));
169-
let crate3 = graph.add_crate_root(FileId(3u32));
174+
let crate1 = graph.add_crate_root(FileId(1u32), Edition2018);
175+
let crate2 = graph.add_crate_root(FileId(2u32), Edition2018);
176+
let crate3 = graph.add_crate_root(FileId(3u32), Edition2018);
170177
assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok());
171178
assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok());
172179
assert!(graph.add_dep(crate3, SmolStr::new("crate1"), crate1).is_err());
@@ -175,9 +182,9 @@ mod tests {
175182
#[test]
176183
fn it_works() {
177184
let mut graph = CrateGraph::default();
178-
let crate1 = graph.add_crate_root(FileId(1u32));
179-
let crate2 = graph.add_crate_root(FileId(2u32));
180-
let crate3 = graph.add_crate_root(FileId(3u32));
185+
let crate1 = graph.add_crate_root(FileId(1u32), Edition2018);
186+
let crate2 = graph.add_crate_root(FileId(2u32), Edition2018);
187+
let crate3 = graph.add_crate_root(FileId(3u32), Edition2018);
181188
assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok());
182189
assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok());
183190
}

crates/ra_db/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub use ::salsa as salsa;
1414
pub use crate::{
1515
cancellation::Canceled,
1616
input::{
17-
FileId, CrateId, SourceRoot, SourceRootId, CrateGraph, Dependency,
17+
FileId, CrateId, SourceRoot, SourceRootId, CrateGraph, Dependency, Edition,
1818
},
1919
loc2id::LocationIntener,
2020
};

crates/ra_hir/src/mock.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::{sync::Arc, panic};
33
use parking_lot::Mutex;
44
use ra_db::{
55
FilePosition, FileId, CrateGraph, SourceRoot, SourceRootId, SourceDatabase, salsa,
6+
Edition,
67
};
78
use relative_path::RelativePathBuf;
89
use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset};
@@ -60,7 +61,7 @@ impl MockDatabase {
6061
let mut crate_graph = CrateGraph::default();
6162
for (crate_name, (crate_root, _)) in graph.0.iter() {
6263
let crate_root = self.file_id_of(&crate_root);
63-
let crate_id = crate_graph.add_crate_root(crate_root);
64+
let crate_id = crate_graph.add_crate_root(crate_root, Edition::Edition2018);
6465
ids.insert(crate_name, crate_id);
6566
}
6667
for (crate_name, (_, deps)) in graph.0.iter() {
@@ -144,7 +145,7 @@ impl MockDatabase {
144145

145146
if is_crate_root {
146147
let mut crate_graph = CrateGraph::default();
147-
crate_graph.add_crate_root(file_id);
148+
crate_graph.add_crate_root(file_id, Edition::Edition2018);
148149
self.set_crate_graph(Arc::new(crate_graph));
149150
}
150151
file_id

crates/ra_ide_api/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ pub use ra_ide_api_light::{
6262
LineIndex, LineCol, translate_offset_with_edit,
6363
};
6464
pub use ra_db::{
65-
Canceled, CrateGraph, CrateId, FileId, FilePosition, FileRange, SourceRootId
65+
Canceled, CrateGraph, CrateId, FileId, FilePosition, FileRange, SourceRootId,
66+
Edition
6667
};
6768
pub use hir::Documentation;
6869

crates/ra_ide_api/src/mock_analysis.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::Arc;
33
use relative_path::RelativePathBuf;
44
use test_utils::{extract_offset, extract_range, parse_fixture, CURSOR_MARKER};
55

6-
use crate::{Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, FilePosition, FileRange, SourceRootId};
6+
use crate::{Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, FilePosition, FileRange, SourceRootId, Edition::Edition2018};
77

88
/// Mock analysis is used in test to bootstrap an AnalysisHost/Analysis
99
/// from a set of in-memory files.
@@ -89,9 +89,9 @@ impl MockAnalysis {
8989
let path = RelativePathBuf::from_path(&path[1..]).unwrap();
9090
let file_id = FileId(i as u32 + 1);
9191
if path == "/lib.rs" || path == "/main.rs" {
92-
root_crate = Some(crate_graph.add_crate_root(file_id));
92+
root_crate = Some(crate_graph.add_crate_root(file_id, Edition2018));
9393
} else if path.ends_with("/lib.rs") {
94-
let other_crate = crate_graph.add_crate_root(file_id);
94+
let other_crate = crate_graph.add_crate_root(file_id, Edition2018);
9595
let crate_name = path.parent().unwrap().file_name().unwrap();
9696
if let Some(root_crate) = root_crate {
9797
crate_graph.add_dep(root_crate, crate_name.into(), other_crate).unwrap();

crates/ra_ide_api/tests/test/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use insta::assert_debug_snapshot_matches;
22
use ra_ide_api::{
33
mock_analysis::{single_file, single_file_with_position, MockAnalysis},
4-
AnalysisChange, CrateGraph, FileId, Query,
4+
AnalysisChange, CrateGraph, Edition::Edition2018, FileId, Query,
55
};
66
use ra_syntax::TextRange;
77

@@ -36,7 +36,7 @@ fn test_resolve_crate_root() {
3636
assert!(host.analysis().crate_for(mod_file).unwrap().is_empty());
3737

3838
let mut crate_graph = CrateGraph::default();
39-
let crate_id = crate_graph.add_crate_root(root_file);
39+
let crate_id = crate_graph.add_crate_root(root_file, Edition2018);
4040
let mut change = AnalysisChange::new();
4141
change.set_crate_graph(crate_graph);
4242
host.apply_change(change);

crates/ra_project_model/src/cargo_workspace.rs

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct PackageData {
3535
targets: Vec<Target>,
3636
is_member: bool,
3737
dependencies: Vec<PackageDependency>,
38+
edition: String,
3839
}
3940

4041
#[derive(Debug, Clone)]
@@ -84,6 +85,9 @@ impl Package {
8485
pub fn root(self, ws: &CargoWorkspace) -> &Path {
8586
ws.packages[self].manifest.parent().unwrap()
8687
}
88+
pub fn edition(self, ws: &CargoWorkspace) -> &str {
89+
&ws.packages[self].edition
90+
}
8791
pub fn targets<'a>(self, ws: &'a CargoWorkspace) -> impl Iterator<Item = Target> + 'a {
8892
ws.packages[self].targets.iter().cloned()
8993
}
@@ -135,6 +139,7 @@ impl CargoWorkspace {
135139
manifest: meta_pkg.manifest_path.clone(),
136140
targets: Vec::new(),
137141
is_member,
142+
edition: meta_pkg.edition,
138143
dependencies: Vec::new(),
139144
});
140145
let pkg_data = &mut packages[pkg];

crates/ra_project_model/src/lib.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::path::{Path, PathBuf};
66
use failure::bail;
77
use rustc_hash::FxHashMap;
88

9-
use ra_db::{CrateGraph, FileId};
9+
use ra_db::{CrateGraph, FileId, Edition};
1010

1111
pub use crate::{
1212
cargo_workspace::{CargoWorkspace, Package, Target, TargetKind},
@@ -36,7 +36,8 @@ impl ProjectWorkspace {
3636
let mut sysroot_crates = FxHashMap::default();
3737
for krate in self.sysroot.crates() {
3838
if let Some(file_id) = load(krate.root(&self.sysroot)) {
39-
sysroot_crates.insert(krate, crate_graph.add_crate_root(file_id));
39+
sysroot_crates
40+
.insert(krate, crate_graph.add_crate_root(file_id, Edition::Edition2015));
4041
}
4142
}
4243
for from in self.sysroot.crates() {
@@ -62,7 +63,12 @@ impl ProjectWorkspace {
6263
for tgt in pkg.targets(&self.cargo) {
6364
let root = tgt.root(&self.cargo);
6465
if let Some(file_id) = load(root) {
65-
let crate_id = crate_graph.add_crate_root(file_id);
66+
let edition = if pkg.edition(&self.cargo) == "2015" {
67+
Edition::Edition2015
68+
} else {
69+
Edition::Edition2018
70+
};
71+
let crate_id = crate_graph.add_crate_root(file_id, edition);
6672
if tgt.kind(&self.cargo) == TargetKind::Lib {
6773
lib_tgt = Some(crate_id);
6874
pkg_to_lib_crate.insert(pkg, crate_id);

0 commit comments

Comments
 (0)