From 3a9934e2c3280864877a90c5ced777bad898d73a Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 10 Feb 2019 22:34:29 +0100 Subject: Keep track of crate edition --- crates/ra_db/src/input.rs | 31 ++++++++++++++++---------- crates/ra_db/src/lib.rs | 2 +- crates/ra_hir/src/mock.rs | 5 +++-- crates/ra_ide_api/src/lib.rs | 3 ++- crates/ra_ide_api/src/mock_analysis.rs | 6 ++--- crates/ra_ide_api/tests/test/main.rs | 4 ++-- crates/ra_project_model/src/cargo_workspace.rs | 5 +++++ crates/ra_project_model/src/lib.rs | 12 +++++++--- 8 files changed, 44 insertions(+), 24 deletions(-) diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index 8decc65c5..76998ea30 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -56,15 +56,22 @@ pub struct CyclicDependencies; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct CrateId(pub u32); +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum Edition { + Edition2018, + Edition2015, +} + #[derive(Debug, Clone, PartialEq, Eq)] struct CrateData { file_id: FileId, + edition: Edition, dependencies: Vec, } impl CrateData { - fn new(file_id: FileId) -> CrateData { - CrateData { file_id, dependencies: Vec::new() } + fn new(file_id: FileId, edition: Edition) -> CrateData { + CrateData { file_id, edition, dependencies: Vec::new() } } fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) { @@ -85,9 +92,9 @@ impl Dependency { } 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 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()); crate_id } @@ -159,14 +166,14 @@ impl CrateGraph { #[cfg(test)] mod tests { - use super::{CrateGraph, FileId, SmolStr}; + use super::{CrateGraph, FileId, SmolStr, Edition::Edition2018}; #[test] - fn it_should_painc_because_of_cycle_dependencies() { + fn it_should_panic_because_of_cycle_dependencies() { let mut graph = CrateGraph::default(); - let crate1 = graph.add_crate_root(FileId(1u32)); - let crate2 = graph.add_crate_root(FileId(2u32)); - let crate3 = graph.add_crate_root(FileId(3u32)); + let crate1 = graph.add_crate_root(FileId(1u32), Edition2018); + let crate2 = graph.add_crate_root(FileId(2u32), Edition2018); + let crate3 = graph.add_crate_root(FileId(3u32), Edition2018); 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(crate3, SmolStr::new("crate1"), crate1).is_err()); @@ -175,9 +182,9 @@ mod tests { #[test] fn it_works() { let mut graph = CrateGraph::default(); - let crate1 = graph.add_crate_root(FileId(1u32)); - let crate2 = graph.add_crate_root(FileId(2u32)); - let crate3 = graph.add_crate_root(FileId(3u32)); + let crate1 = graph.add_crate_root(FileId(1u32), Edition2018); + let crate2 = graph.add_crate_root(FileId(2u32), Edition2018); + let crate3 = graph.add_crate_root(FileId(3u32), Edition2018); assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok()); assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok()); } diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index 31442713d..e006c6d27 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs @@ -14,7 +14,7 @@ pub use ::salsa as salsa; pub use crate::{ cancellation::Canceled, input::{ - FileId, CrateId, SourceRoot, SourceRootId, CrateGraph, Dependency, + FileId, CrateId, SourceRoot, SourceRootId, CrateGraph, Dependency, Edition, }, loc2id::LocationIntener, }; diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs index 5ca870867..145ed39a1 100644 --- a/crates/ra_hir/src/mock.rs +++ b/crates/ra_hir/src/mock.rs @@ -3,6 +3,7 @@ use std::{sync::Arc, panic}; use parking_lot::Mutex; use ra_db::{ FilePosition, FileId, CrateGraph, SourceRoot, SourceRootId, SourceDatabase, salsa, + Edition, }; use relative_path::RelativePathBuf; use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset}; @@ -60,7 +61,7 @@ impl MockDatabase { let mut crate_graph = CrateGraph::default(); for (crate_name, (crate_root, _)) in graph.0.iter() { 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); } for (crate_name, (_, deps)) in graph.0.iter() { @@ -144,7 +145,7 @@ impl MockDatabase { if is_crate_root { 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)); } file_id diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index de3ec4e0a..d77a56ce8 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -62,7 +62,8 @@ pub use ra_ide_api_light::{ LineIndex, LineCol, translate_offset_with_edit, }; pub use ra_db::{ - Canceled, CrateGraph, CrateId, FileId, FilePosition, FileRange, SourceRootId + Canceled, CrateGraph, CrateId, FileId, FilePosition, FileRange, SourceRootId, + Edition }; pub use hir::Documentation; diff --git a/crates/ra_ide_api/src/mock_analysis.rs b/crates/ra_ide_api/src/mock_analysis.rs index 017ac5de3..550d69641 100644 --- a/crates/ra_ide_api/src/mock_analysis.rs +++ b/crates/ra_ide_api/src/mock_analysis.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use relative_path::RelativePathBuf; 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 /// from a set of in-memory files. @@ -89,9 +89,9 @@ impl MockAnalysis { let path = RelativePathBuf::from_path(&path[1..]).unwrap(); let file_id = FileId(i as u32 + 1); 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") { - 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(); if let Some(root_crate) = root_crate { crate_graph.add_dep(root_crate, crate_name.into(), other_crate).unwrap(); diff --git a/crates/ra_ide_api/tests/test/main.rs b/crates/ra_ide_api/tests/test/main.rs index 4cf842452..0526f7584 100644 --- a/crates/ra_ide_api/tests/test/main.rs +++ b/crates/ra_ide_api/tests/test/main.rs @@ -1,7 +1,7 @@ use insta::assert_debug_snapshot_matches; use ra_ide_api::{ 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}; @@ -36,7 +36,7 @@ fn test_resolve_crate_root() { assert!(host.analysis().crate_for(mod_file).unwrap().is_empty()); 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(); change.set_crate_graph(crate_graph); host.apply_change(change); diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs index 5866be519..e28aca259 100644 --- a/crates/ra_project_model/src/cargo_workspace.rs +++ b/crates/ra_project_model/src/cargo_workspace.rs @@ -35,6 +35,7 @@ struct PackageData { targets: Vec, is_member: bool, dependencies: Vec, + edition: String, } #[derive(Debug, Clone)] @@ -84,6 +85,9 @@ impl Package { pub fn root(self, ws: &CargoWorkspace) -> &Path { 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 + 'a { ws.packages[self].targets.iter().cloned() } @@ -135,6 +139,7 @@ impl CargoWorkspace { manifest: meta_pkg.manifest_path.clone(), targets: Vec::new(), is_member, + edition: meta_pkg.edition, dependencies: Vec::new(), }); let pkg_data = &mut packages[pkg]; diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 3b1e07149..e5c93fd85 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -6,7 +6,7 @@ use std::path::{Path, PathBuf}; use failure::bail; use rustc_hash::FxHashMap; -use ra_db::{CrateGraph, FileId}; +use ra_db::{CrateGraph, FileId, Edition}; pub use crate::{ cargo_workspace::{CargoWorkspace, Package, Target, TargetKind}, @@ -36,7 +36,8 @@ impl ProjectWorkspace { let mut sysroot_crates = FxHashMap::default(); for krate in self.sysroot.crates() { 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() { @@ -62,7 +63,12 @@ impl ProjectWorkspace { for tgt in pkg.targets(&self.cargo) { let root = tgt.root(&self.cargo); 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 { lib_tgt = Some(crate_id); pkg_to_lib_crate.insert(pkg, crate_id); -- cgit v1.2.3