Keep track of crate edition

This commit is contained in:
Florian Diebold 2019-02-10 22:34:29 +01:00
parent 1526eb25c9
commit 3a9934e2c3
8 changed files with 44 additions and 24 deletions

View file

@ -56,15 +56,22 @@ pub struct CyclicDependencies;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CrateId(pub u32); pub struct CrateId(pub u32);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Edition {
Edition2018,
Edition2015,
}
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
struct CrateData { struct CrateData {
file_id: FileId, file_id: FileId,
edition: Edition,
dependencies: Vec<Dependency>, dependencies: Vec<Dependency>,
} }
impl CrateData { impl CrateData {
fn new(file_id: FileId) -> CrateData { fn new(file_id: FileId, edition: Edition) -> CrateData {
CrateData { file_id, dependencies: Vec::new() } CrateData { file_id, edition, dependencies: Vec::new() }
} }
fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) { fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) {
@ -85,9 +92,9 @@ impl Dependency {
} }
impl CrateGraph { impl CrateGraph {
pub fn add_crate_root(&mut self, file_id: FileId) -> CrateId { pub fn add_crate_root(&mut self, file_id: FileId, edition: Edition) -> CrateId {
let crate_id = CrateId(self.arena.len() as u32); let crate_id = CrateId(self.arena.len() as u32);
let prev = self.arena.insert(crate_id, CrateData::new(file_id)); let prev = self.arena.insert(crate_id, CrateData::new(file_id, edition));
assert!(prev.is_none()); assert!(prev.is_none());
crate_id crate_id
} }
@ -159,14 +166,14 @@ impl CrateGraph {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{CrateGraph, FileId, SmolStr}; use super::{CrateGraph, FileId, SmolStr, Edition::Edition2018};
#[test] #[test]
fn it_should_painc_because_of_cycle_dependencies() { fn it_should_panic_because_of_cycle_dependencies() {
let mut graph = CrateGraph::default(); let mut graph = CrateGraph::default();
let crate1 = graph.add_crate_root(FileId(1u32)); let crate1 = graph.add_crate_root(FileId(1u32), Edition2018);
let crate2 = graph.add_crate_root(FileId(2u32)); let crate2 = graph.add_crate_root(FileId(2u32), Edition2018);
let crate3 = graph.add_crate_root(FileId(3u32)); let crate3 = graph.add_crate_root(FileId(3u32), Edition2018);
assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok()); assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok());
assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok()); assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok());
assert!(graph.add_dep(crate3, SmolStr::new("crate1"), crate1).is_err()); assert!(graph.add_dep(crate3, SmolStr::new("crate1"), crate1).is_err());
@ -175,9 +182,9 @@ mod tests {
#[test] #[test]
fn it_works() { fn it_works() {
let mut graph = CrateGraph::default(); let mut graph = CrateGraph::default();
let crate1 = graph.add_crate_root(FileId(1u32)); let crate1 = graph.add_crate_root(FileId(1u32), Edition2018);
let crate2 = graph.add_crate_root(FileId(2u32)); let crate2 = graph.add_crate_root(FileId(2u32), Edition2018);
let crate3 = graph.add_crate_root(FileId(3u32)); let crate3 = graph.add_crate_root(FileId(3u32), Edition2018);
assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok()); assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok());
assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok()); assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok());
} }

View file

@ -14,7 +14,7 @@ pub use ::salsa as salsa;
pub use crate::{ pub use crate::{
cancellation::Canceled, cancellation::Canceled,
input::{ input::{
FileId, CrateId, SourceRoot, SourceRootId, CrateGraph, Dependency, FileId, CrateId, SourceRoot, SourceRootId, CrateGraph, Dependency, Edition,
}, },
loc2id::LocationIntener, loc2id::LocationIntener,
}; };

View file

@ -3,6 +3,7 @@ use std::{sync::Arc, panic};
use parking_lot::Mutex; use parking_lot::Mutex;
use ra_db::{ use ra_db::{
FilePosition, FileId, CrateGraph, SourceRoot, SourceRootId, SourceDatabase, salsa, FilePosition, FileId, CrateGraph, SourceRoot, SourceRootId, SourceDatabase, salsa,
Edition,
}; };
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset}; use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset};
@ -60,7 +61,7 @@ impl MockDatabase {
let mut crate_graph = CrateGraph::default(); let mut crate_graph = CrateGraph::default();
for (crate_name, (crate_root, _)) in graph.0.iter() { for (crate_name, (crate_root, _)) in graph.0.iter() {
let crate_root = self.file_id_of(&crate_root); let crate_root = self.file_id_of(&crate_root);
let crate_id = crate_graph.add_crate_root(crate_root); let crate_id = crate_graph.add_crate_root(crate_root, Edition::Edition2018);
ids.insert(crate_name, crate_id); ids.insert(crate_name, crate_id);
} }
for (crate_name, (_, deps)) in graph.0.iter() { for (crate_name, (_, deps)) in graph.0.iter() {
@ -144,7 +145,7 @@ impl MockDatabase {
if is_crate_root { if is_crate_root {
let mut crate_graph = CrateGraph::default(); let mut crate_graph = CrateGraph::default();
crate_graph.add_crate_root(file_id); crate_graph.add_crate_root(file_id, Edition::Edition2018);
self.set_crate_graph(Arc::new(crate_graph)); self.set_crate_graph(Arc::new(crate_graph));
} }
file_id file_id

View file

@ -62,7 +62,8 @@ pub use ra_ide_api_light::{
LineIndex, LineCol, translate_offset_with_edit, LineIndex, LineCol, translate_offset_with_edit,
}; };
pub use ra_db::{ pub use ra_db::{
Canceled, CrateGraph, CrateId, FileId, FilePosition, FileRange, SourceRootId Canceled, CrateGraph, CrateId, FileId, FilePosition, FileRange, SourceRootId,
Edition
}; };
pub use hir::Documentation; pub use hir::Documentation;

View file

@ -3,7 +3,7 @@ use std::sync::Arc;
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
use test_utils::{extract_offset, extract_range, parse_fixture, CURSOR_MARKER}; use test_utils::{extract_offset, extract_range, parse_fixture, CURSOR_MARKER};
use crate::{Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, FilePosition, FileRange, SourceRootId}; use crate::{Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, FilePosition, FileRange, SourceRootId, Edition::Edition2018};
/// Mock analysis is used in test to bootstrap an AnalysisHost/Analysis /// Mock analysis is used in test to bootstrap an AnalysisHost/Analysis
/// from a set of in-memory files. /// from a set of in-memory files.
@ -89,9 +89,9 @@ impl MockAnalysis {
let path = RelativePathBuf::from_path(&path[1..]).unwrap(); let path = RelativePathBuf::from_path(&path[1..]).unwrap();
let file_id = FileId(i as u32 + 1); let file_id = FileId(i as u32 + 1);
if path == "/lib.rs" || path == "/main.rs" { if path == "/lib.rs" || path == "/main.rs" {
root_crate = Some(crate_graph.add_crate_root(file_id)); root_crate = Some(crate_graph.add_crate_root(file_id, Edition2018));
} else if path.ends_with("/lib.rs") { } else if path.ends_with("/lib.rs") {
let other_crate = crate_graph.add_crate_root(file_id); let other_crate = crate_graph.add_crate_root(file_id, Edition2018);
let crate_name = path.parent().unwrap().file_name().unwrap(); let crate_name = path.parent().unwrap().file_name().unwrap();
if let Some(root_crate) = root_crate { if let Some(root_crate) = root_crate {
crate_graph.add_dep(root_crate, crate_name.into(), other_crate).unwrap(); crate_graph.add_dep(root_crate, crate_name.into(), other_crate).unwrap();

View file

@ -1,7 +1,7 @@
use insta::assert_debug_snapshot_matches; use insta::assert_debug_snapshot_matches;
use ra_ide_api::{ use ra_ide_api::{
mock_analysis::{single_file, single_file_with_position, MockAnalysis}, mock_analysis::{single_file, single_file_with_position, MockAnalysis},
AnalysisChange, CrateGraph, FileId, Query, NavigationTarget, AnalysisChange, CrateGraph, Edition::Edition2018, FileId, Query, NavigationTarget
}; };
use ra_syntax::{TextRange, SmolStr}; use ra_syntax::{TextRange, SmolStr};
@ -36,7 +36,7 @@ fn test_resolve_crate_root() {
assert!(host.analysis().crate_for(mod_file).unwrap().is_empty()); assert!(host.analysis().crate_for(mod_file).unwrap().is_empty());
let mut crate_graph = CrateGraph::default(); let mut crate_graph = CrateGraph::default();
let crate_id = crate_graph.add_crate_root(root_file); let crate_id = crate_graph.add_crate_root(root_file, Edition2018);
let mut change = AnalysisChange::new(); let mut change = AnalysisChange::new();
change.set_crate_graph(crate_graph); change.set_crate_graph(crate_graph);
host.apply_change(change); host.apply_change(change);

View file

@ -35,6 +35,7 @@ struct PackageData {
targets: Vec<Target>, targets: Vec<Target>,
is_member: bool, is_member: bool,
dependencies: Vec<PackageDependency>, dependencies: Vec<PackageDependency>,
edition: String,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -84,6 +85,9 @@ impl Package {
pub fn root(self, ws: &CargoWorkspace) -> &Path { pub fn root(self, ws: &CargoWorkspace) -> &Path {
ws.packages[self].manifest.parent().unwrap() ws.packages[self].manifest.parent().unwrap()
} }
pub fn edition(self, ws: &CargoWorkspace) -> &str {
&ws.packages[self].edition
}
pub fn targets<'a>(self, ws: &'a CargoWorkspace) -> impl Iterator<Item = Target> + 'a { pub fn targets<'a>(self, ws: &'a CargoWorkspace) -> impl Iterator<Item = Target> + 'a {
ws.packages[self].targets.iter().cloned() ws.packages[self].targets.iter().cloned()
} }
@ -135,6 +139,7 @@ impl CargoWorkspace {
manifest: meta_pkg.manifest_path.clone(), manifest: meta_pkg.manifest_path.clone(),
targets: Vec::new(), targets: Vec::new(),
is_member, is_member,
edition: meta_pkg.edition,
dependencies: Vec::new(), dependencies: Vec::new(),
}); });
let pkg_data = &mut packages[pkg]; let pkg_data = &mut packages[pkg];

View file

@ -6,7 +6,7 @@ use std::path::{Path, PathBuf};
use failure::bail; use failure::bail;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use ra_db::{CrateGraph, FileId}; use ra_db::{CrateGraph, FileId, Edition};
pub use crate::{ pub use crate::{
cargo_workspace::{CargoWorkspace, Package, Target, TargetKind}, cargo_workspace::{CargoWorkspace, Package, Target, TargetKind},
@ -36,7 +36,8 @@ impl ProjectWorkspace {
let mut sysroot_crates = FxHashMap::default(); let mut sysroot_crates = FxHashMap::default();
for krate in self.sysroot.crates() { for krate in self.sysroot.crates() {
if let Some(file_id) = load(krate.root(&self.sysroot)) { if let Some(file_id) = load(krate.root(&self.sysroot)) {
sysroot_crates.insert(krate, crate_graph.add_crate_root(file_id)); sysroot_crates
.insert(krate, crate_graph.add_crate_root(file_id, Edition::Edition2015));
} }
} }
for from in self.sysroot.crates() { for from in self.sysroot.crates() {
@ -62,7 +63,12 @@ impl ProjectWorkspace {
for tgt in pkg.targets(&self.cargo) { for tgt in pkg.targets(&self.cargo) {
let root = tgt.root(&self.cargo); let root = tgt.root(&self.cargo);
if let Some(file_id) = load(root) { if let Some(file_id) = load(root) {
let crate_id = crate_graph.add_crate_root(file_id); let edition = if pkg.edition(&self.cargo) == "2015" {
Edition::Edition2015
} else {
Edition::Edition2018
};
let crate_id = crate_graph.add_crate_root(file_id, edition);
if tgt.kind(&self.cargo) == TargetKind::Lib { if tgt.kind(&self.cargo) == TargetKind::Lib {
lib_tgt = Some(crate_id); lib_tgt = Some(crate_id);
pkg_to_lib_crate.insert(pkg, crate_id); pkg_to_lib_crate.insert(pkg, crate_id);