Move Edition into span crate

This commit is contained in:
Lukas Wirth 2024-03-19 16:53:34 +01:00
parent 8d74705b43
commit 255a8aef92
21 changed files with 103 additions and 101 deletions

3
Cargo.lock generated
View file

@ -594,6 +594,7 @@ dependencies = [
"rustc-hash",
"scoped-tls",
"smallvec",
"span",
"stdx",
"syntax",
"test-fixture",
@ -637,6 +638,7 @@ dependencies = [
"pulldown-cmark",
"pulldown-cmark-to-cmark",
"smallvec",
"span",
"stdx",
"syntax",
"test-fixture",
@ -1380,6 +1382,7 @@ dependencies = [
"semver",
"serde",
"serde_json",
"span",
"stdx",
"toolchain",
"tracing",

View file

@ -6,11 +6,12 @@
//! actual IO. See `vfs` and `project_model` in the `rust-analyzer` crate for how
//! actual IO is done and lowered to input.
use std::{fmt, mem, ops, str::FromStr};
use std::{fmt, mem, ops};
use cfg::CfgOptions;
use la_arena::{Arena, Idx, RawIdx};
use rustc_hash::{FxHashMap, FxHashSet};
use span::Edition;
use syntax::SmolStr;
use triomphe::Arc;
use vfs::{file_set::FileSet, AbsPathBuf, AnchoredPath, FileId, VfsPath};
@ -293,42 +294,11 @@ pub struct CrateData {
pub is_proc_macro: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Edition {
Edition2015,
Edition2018,
Edition2021,
Edition2024,
}
impl Edition {
pub const CURRENT: Edition = Edition::Edition2021;
pub const DEFAULT: Edition = Edition::Edition2015;
}
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct Env {
entries: FxHashMap<String, String>,
}
impl Env {
pub fn new_for_test_fixture() -> Self {
Env {
entries: FxHashMap::from_iter([(
String::from("__ra_is_test_fixture"),
String::from("__ra_is_test_fixture"),
)]),
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum DependencyKind {
Normal,
Dev,
Build,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Dependency {
pub crate_id: CrateId,
@ -670,32 +640,6 @@ impl CrateData {
}
}
impl FromStr for Edition {
type Err = ParseEditionError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let res = match s {
"2015" => Edition::Edition2015,
"2018" => Edition::Edition2018,
"2021" => Edition::Edition2021,
"2024" => Edition::Edition2024,
_ => return Err(ParseEditionError { invalid_input: s.to_owned() }),
};
Ok(res)
}
}
impl fmt::Display for Edition {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Edition::Edition2015 => "2015",
Edition::Edition2018 => "2018",
Edition::Edition2021 => "2021",
Edition::Edition2024 => "2024",
})
}
}
impl Extend<(String, String)> for Env {
fn extend<T: IntoIterator<Item = (String, String)>>(&mut self, iter: T) {
self.entries.extend(iter);
@ -722,19 +666,6 @@ impl Env {
}
}
#[derive(Debug)]
pub struct ParseEditionError {
invalid_input: String,
}
impl fmt::Display for ParseEditionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "invalid edition: {:?}", self.invalid_input)
}
}
impl std::error::Error for ParseEditionError {}
#[derive(Debug)]
pub struct CyclicDependenciesError {
path: Vec<(CrateId, Option<CrateDisplayName>)>,

View file

@ -14,9 +14,9 @@ use triomphe::Arc;
pub use crate::{
change::FileChange,
input::{
CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency,
DependencyKind, Edition, Env, LangCrateOrigin, ProcMacroPaths, ReleaseChannel, SourceRoot,
SourceRootId, TargetLayoutLoadResult,
CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency, Env,
LangCrateOrigin, ProcMacroPaths, ReleaseChannel, SourceRoot, SourceRootId,
TargetLayoutLoadResult,
},
};
pub use salsa::{self, Cancelled};

View file

@ -73,7 +73,7 @@ use std::{
use base_db::{
impl_intern_key,
salsa::{self, impl_intern_value_trivial},
CrateId, Edition,
CrateId,
};
use hir_expand::{
builtin_attr_macro::BuiltinAttrExpander,
@ -90,7 +90,7 @@ use hir_expand::{
use item_tree::ExternBlock;
use la_arena::Idx;
use nameres::DefMap;
use span::{AstIdNode, FileAstId, FileId, SyntaxContextId};
use span::{AstIdNode, Edition, FileAstId, FileId, SyntaxContextId};
use stdx::impl_from;
use syntax::{ast, AstNode};

View file

@ -59,14 +59,14 @@ mod tests;
use std::ops::Deref;
use base_db::{CrateId, Edition, FileId};
use base_db::{CrateId, FileId};
use hir_expand::{
name::Name, proc_macro::ProcMacroKind, ErasedAstId, HirFileId, InFile, MacroCallId, MacroDefId,
};
use itertools::Itertools;
use la_arena::Arena;
use rustc_hash::{FxHashMap, FxHashSet};
use span::{FileAstId, ROOT_ERASED_FILE_AST_ID};
use span::{Edition, FileAstId, ROOT_ERASED_FILE_AST_ID};
use stdx::format_to;
use syntax::{ast, SmolStr};
use triomphe::Arc;

View file

@ -5,7 +5,7 @@
use std::{cmp::Ordering, iter, mem, ops::Not};
use base_db::{CrateId, Dependency, Edition, FileId};
use base_db::{CrateId, Dependency, FileId};
use cfg::{CfgExpr, CfgOptions};
use either::Either;
use hir_expand::{
@ -22,7 +22,7 @@ use itertools::{izip, Itertools};
use la_arena::Idx;
use limit::Limit;
use rustc_hash::{FxHashMap, FxHashSet};
use span::{ErasedFileAstId, FileAstId, Span, SyntaxContextId};
use span::{Edition, ErasedFileAstId, FileAstId, Span, SyntaxContextId};
use stdx::always;
use syntax::{ast, SmolStr};
use triomphe::Arc;

View file

@ -10,8 +10,8 @@
//!
//! `ReachedFixedPoint` signals about this.
use base_db::Edition;
use hir_expand::{name::Name, Lookup};
use span::Edition;
use triomphe::Arc;
use crate::{

View file

@ -1,11 +1,11 @@
//! Builtin macro
use base_db::{AnchoredPath, Edition, FileId};
use base_db::{AnchoredPath, FileId};
use cfg::CfgExpr;
use either::Either;
use itertools::Itertools;
use mbe::{parse_exprs_with_sep, parse_to_token_tree};
use span::{Span, SpanAnchor, SyntaxContextId, ROOT_ERASED_FILE_AST_ID};
use span::{Edition, Span, SpanAnchor, SyntaxContextId, ROOT_ERASED_FILE_AST_ID};
use syntax::ast::{self, AstToken};
use crate::{

View file

@ -1,8 +1,8 @@
//! Compiled declarative macro expanders (`macro_rules!`` and `macro`)
use std::sync::OnceLock;
use base_db::{CrateId, Edition, VersionReq};
use span::{MacroCallId, Span};
use base_db::{CrateId, VersionReq};
use span::{Edition, MacroCallId, Span};
use syntax::{ast, AstNode};
use triomphe::Arc;

View file

@ -30,10 +30,11 @@ use triomphe::Arc;
use std::{fmt, hash::Hash};
use base_db::{salsa::impl_intern_value_trivial, CrateId, Edition, FileId};
use base_db::{salsa::impl_intern_value_trivial, CrateId, FileId};
use either::Either;
use span::{
ErasedFileAstId, FileRange, HirFileIdRepr, Span, SpanAnchor, SyntaxContextData, SyntaxContextId,
Edition, ErasedFileAstId, FileRange, HirFileIdRepr, Span, SpanAnchor, SyntaxContextData,
SyntaxContextId,
};
use syntax::{
ast::{self, AstNode},

View file

@ -47,6 +47,7 @@ hir-expand.workspace = true
base-db.workspace = true
syntax.workspace = true
limit.workspace = true
span.workspace = true
[dev-dependencies]
expect-test = "1.4.0"

View file

@ -4,7 +4,7 @@
//! and the corresponding code mostly in rustc_hir_analysis/check/method/probe.rs.
use std::ops::ControlFlow;
use base_db::{CrateId, Edition};
use base_db::CrateId;
use chalk_ir::{cast::Cast, Mutability, TyKind, UniverseIndex, WhereClause};
use hir_def::{
data::{adt::StructFlags, ImplData},
@ -15,6 +15,7 @@ use hir_def::{
use hir_expand::name::Name;
use rustc_hash::{FxHashMap, FxHashSet};
use smallvec::{smallvec, SmallVec};
use span::Edition;
use stdx::never;
use triomphe::Arc;

View file

@ -38,7 +38,7 @@ mod display;
use std::{iter, mem::discriminant, ops::ControlFlow};
use arrayvec::ArrayVec;
use base_db::{CrateDisplayName, CrateId, CrateOrigin, Edition, FileId};
use base_db::{CrateDisplayName, CrateId, CrateOrigin, FileId};
use either::Either;
use hir_def::{
body::{BodyDiagnostic, SyntheticSyntax},
@ -79,6 +79,7 @@ use hir_ty::{
use itertools::Itertools;
use nameres::diagnostics::DefDiagnosticKind;
use rustc_hash::FxHashSet;
use span::Edition;
use stdx::{impl_from, never};
use syntax::{
ast::{self, HasAttrs as _, HasName},

View file

@ -36,6 +36,7 @@ ide-ssr.workspace = true
profile.workspace = true
stdx.workspace = true
syntax.workspace = true
span.workspace = true
text-edit.workspace = true
# ide should depend only on the top-level `hir` package. if you need
# something from some `hir-xxx` subpackage, reexport the API via `hir`.

View file

@ -119,8 +119,8 @@ pub use ide_completion::{
};
pub use ide_db::{
base_db::{
Cancelled, CrateGraph, CrateId, Edition, FileChange, FileId, FilePosition, FileRange,
SourceRoot, SourceRootId,
Cancelled, CrateGraph, CrateId, FileChange, FileId, FilePosition, FileRange, SourceRoot,
SourceRootId,
},
documentation::Documentation,
label::Label,
@ -135,6 +135,7 @@ pub use ide_diagnostics::{
Diagnostic, DiagnosticCode, DiagnosticsConfig, ExprFillDefaultMode, Severity,
};
pub use ide_ssr::SsrError;
pub use span::Edition;
pub use syntax::{TextRange, TextSize};
pub use text_edit::{Indel, TextEdit};

View file

@ -25,6 +25,7 @@ itertools.workspace = true
# local deps
base-db.workspace = true
span.workspace = true
cfg.workspace = true
paths = { workspace = true, features = ["serde1"] }
stdx.workspace = true

View file

@ -4,13 +4,13 @@ use std::ops;
use std::str::from_utf8;
use anyhow::Context;
use base_db::Edition;
use cargo_metadata::{CargoOpt, MetadataCommand};
use la_arena::{Arena, Idx};
use paths::{AbsPath, AbsPathBuf, Utf8PathBuf};
use rustc_hash::{FxHashMap, FxHashSet};
use serde::Deserialize;
use serde_json::from_value;
use span::Edition;
use toolchain::Tool;
use crate::{utf8_stdout, InvocationLocation, ManifestPath, Sysroot};

View file

@ -49,11 +49,12 @@
//! user explores them belongs to that extension (it's totally valid to change
//! rust-project.json over time via configuration request!)
use base_db::{CrateDisplayName, CrateId, CrateName, Dependency, Edition};
use base_db::{CrateDisplayName, CrateId, CrateName, Dependency};
use la_arena::RawIdx;
use paths::{AbsPath, AbsPathBuf, Utf8PathBuf};
use rustc_hash::FxHashMap;
use serde::{de, Deserialize};
use span::Edition;
use crate::cfg_flag::CfgFlag;

View file

@ -6,13 +6,14 @@ use std::{collections::VecDeque, fmt, fs, iter, str::FromStr, sync};
use anyhow::{format_err, Context};
use base_db::{
CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency, Edition, Env,
FileId, LangCrateOrigin, ProcMacroPaths, TargetLayoutLoadResult,
CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency, Env, FileId,
LangCrateOrigin, ProcMacroPaths, TargetLayoutLoadResult,
};
use cfg::{CfgAtom, CfgDiff, CfgOptions};
use paths::{AbsPath, AbsPathBuf};
use rustc_hash::{FxHashMap, FxHashSet};
use semver::Version;
use span::Edition;
use stdx::always;
use toolchain::Tool;
use triomphe::Arc;

View file

@ -16,6 +16,56 @@ pub use self::{
pub use syntax::{TextRange, TextSize};
pub use vfs::FileId;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Edition {
Edition2015,
Edition2018,
Edition2021,
Edition2024,
}
impl Edition {
pub const CURRENT: Edition = Edition::Edition2021;
pub const DEFAULT: Edition = Edition::Edition2015;
}
#[derive(Debug)]
pub struct ParseEditionError {
invalid_input: String,
}
impl std::error::Error for ParseEditionError {}
impl fmt::Display for ParseEditionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "invalid edition: {:?}", self.invalid_input)
}
}
impl std::str::FromStr for Edition {
type Err = ParseEditionError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let res = match s {
"2015" => Edition::Edition2015,
"2018" => Edition::Edition2018,
"2021" => Edition::Edition2021,
"2024" => Edition::Edition2024,
_ => return Err(ParseEditionError { invalid_input: s.to_owned() }),
};
Ok(res)
}
}
impl fmt::Display for Edition {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Edition::Edition2015 => "2015",
Edition::Edition2018 => "2018",
Edition::Edition2021 => "2021",
Edition::Edition2024 => "2024",
})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct FilePosition {
pub file_id: FileId,

View file

@ -2,8 +2,8 @@
use std::{iter, mem, ops::Not, str::FromStr, sync};
use base_db::{
CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency, Edition, Env,
FileChange, FileSet, LangCrateOrigin, SourceDatabaseExt, SourceRoot, Version, VfsPath,
CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency, Env, FileChange,
FileSet, LangCrateOrigin, SourceDatabaseExt, SourceRoot, Version, VfsPath,
};
use cfg::CfgOptions;
use hir_expand::{
@ -14,7 +14,7 @@ use hir_expand::{
},
};
use rustc_hash::FxHashMap;
use span::{FileId, FilePosition, FileRange, Span};
use span::{Edition, FileId, FilePosition, FileRange, Span};
use test_utils::{
extract_range_or_offset, Fixture, FixtureWithProjectMeta, RangeOrOffset, CURSOR_MARKER,
ESCAPED_CURSOR_MARKER,
@ -137,7 +137,10 @@ impl ChangeFixture {
let mut crate_deps = Vec::new();
let mut default_crate_root: Option<FileId> = None;
let mut default_cfg = CfgOptions::default();
let mut default_env = Env::new_for_test_fixture();
let mut default_env = Env::from_iter([(
String::from("__ra_is_test_fixture"),
String::from("__ra_is_test_fixture"),
)]);
let mut file_set = FileSet::default();
let mut current_source_root_kind = SourceRootKind::Local;
@ -262,7 +265,10 @@ impl ChangeFixture {
None,
Default::default(),
Default::default(),
Env::new_for_test_fixture(),
Env::from_iter([(
String::from("__ra_is_test_fixture"),
String::from("__ra_is_test_fixture"),
)]),
false,
CrateOrigin::Lang(LangCrateOrigin::Core),
);
@ -298,7 +304,10 @@ impl ChangeFixture {
None,
Default::default(),
Default::default(),
Env::new_for_test_fixture(),
Env::from_iter([(
String::from("__ra_is_test_fixture"),
String::from("__ra_is_test_fixture"),
)]),
true,
CrateOrigin::Local { repo: None, name: None },
);