Replace ra_hir_expand::either with crate

This commit is contained in:
ice1000 2019-12-03 11:07:56 -05:00
parent 15f143f0c3
commit 009437f5d9
23 changed files with 69 additions and 112 deletions

4
Cargo.lock generated
View file

@ -944,6 +944,7 @@ dependencies = [
name = "ra_hir"
version = "0.1.0"
dependencies = [
"either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"ra_db 0.1.0",
"ra_hir_def 0.1.0",
@ -957,6 +958,7 @@ dependencies = [
name = "ra_hir_def"
version = "0.1.0"
dependencies = [
"either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"insta 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"once_cell 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -976,6 +978,7 @@ dependencies = [
name = "ra_hir_expand"
version = "0.1.0"
dependencies = [
"either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"ra_arena 0.1.0",
"ra_db 0.1.0",
@ -1012,6 +1015,7 @@ dependencies = [
name = "ra_ide"
version = "0.1.0"
dependencies = [
"either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"format-buf 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"fst 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"insta 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",

View file

@ -10,6 +10,7 @@ doctest = false
[dependencies]
log = "0.4.5"
rustc-hash = "1.0"
either = "1.5"
ra_syntax = { path = "../ra_syntax" }
ra_db = { path = "../ra_db" }

View file

@ -4,6 +4,7 @@ pub(crate) mod src;
use std::sync::Arc;
use either::Either;
use hir_def::{
adt::VariantData,
body::{Body, BodySourceMap},
@ -30,7 +31,7 @@ use crate::{
db::{DefDatabase, HirDatabase},
ty::display::HirFormatter,
ty::{self, InEnvironment, InferenceResult, TraitEnvironment, Ty, TyDefId, TypeCtor, TypeWalk},
CallableDef, Either, HirDisplay, InFile, Name,
CallableDef, HirDisplay, InFile, Name,
};
/// hir::Crate describes a single crate. It's the main interface with which
@ -905,7 +906,9 @@ impl Local {
let (_body, source_map) = db.body_with_source_map(self.parent.into());
let src = source_map.pat_syntax(self.pat_id).unwrap(); // Hmm...
let root = src.file_syntax(db);
src.map(|ast| ast.map(|it| it.cast().unwrap().to_node(&root), |it| it.to_node(&root)))
src.map(|ast| {
ast.map_left(|it| it.cast().unwrap().to_node(&root)).map_right(|it| it.to_node(&root))
})
}
}

View file

@ -1,10 +1,10 @@
//! FIXME: write short doc here
use either::Either;
use hir_def::{
src::{HasChildSource, HasSource as _},
AstItemDef, Lookup, VariantId,
};
use hir_expand::either::Either;
use ra_syntax::ast;
use crate::{
@ -27,8 +27,8 @@ impl Module {
let def_map = db.crate_def_map(self.id.krate);
let src = def_map[self.id.local_id].definition_source(db);
src.map(|it| match it {
Either::A(it) => ModuleSource::SourceFile(it),
Either::B(it) => ModuleSource::Module(it),
Either::Left(it) => ModuleSource::SourceFile(it),
Either::Right(it) => ModuleSource::Module(it),
})
}
@ -46,8 +46,8 @@ impl HasSource for StructField {
let var = VariantId::from(self.parent);
let src = var.child_source(db);
src.map(|it| match it[self.id].clone() {
Either::A(it) => FieldSource::Pos(it),
Either::B(it) => FieldSource::Named(it),
Either::Left(it) => FieldSource::Pos(it),
Either::Right(it) => FieldSource::Named(it),
})
}
}
@ -126,6 +126,6 @@ impl HasSource for Import {
let (_, source_map) = db.raw_items_with_source_map(src.file_id);
let root = db.parse_or_expand(src.file_id).unwrap();
let ptr = source_map.get(self.id);
src.with_value(ptr.map(|it| it.to_node(&root), |it| it.to_node(&root)))
src.with_value(ptr.map_left(|it| it.to_node(&root)).map_right(|it| it.to_node(&root)))
}
}

View file

@ -63,5 +63,5 @@ pub use hir_def::{
type_ref::Mutability,
};
pub use hir_expand::{
either::Either, name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, MacroDefId, MacroFile,
name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, MacroDefId, MacroFile,
};

View file

@ -7,6 +7,7 @@
//! purely for "IDE needs".
use std::sync::Arc;
use either::Either;
use hir_def::{
body::{
scope::{ExprScopes, ScopeId},
@ -33,8 +34,8 @@ use crate::{
method_resolution::{self, implements_trait},
InEnvironment, TraitEnvironment, Ty,
},
Adt, AssocItem, Const, DefWithBody, Either, Enum, EnumVariant, FromSource, Function,
GenericParam, Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Type, TypeAlias,
Adt, AssocItem, Const, DefWithBody, Enum, EnumVariant, FromSource, Function, GenericParam,
Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Type, TypeAlias,
};
fn try_get_resolver_for_node(db: &impl HirDatabase, node: InFile<&SyntaxNode>) -> Option<Resolver> {
@ -349,7 +350,7 @@ impl SourceAnalyzer {
// should switch to general reference search infra there.
pub fn find_all_refs(&self, pat: &ast::BindPat) -> Vec<ReferenceDescriptor> {
let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap();
let ptr = Either::A(AstPtr::new(&ast::Pat::from(pat.clone())));
let ptr = Either::Left(AstPtr::new(&ast::Pat::from(pat.clone())));
fn_def
.syntax()
.descendants()

View file

@ -11,6 +11,7 @@ doctest = false
log = "0.4.5"
once_cell = "1.0.1"
rustc-hash = "1.0"
either = "1.5"
ra_arena = { path = "../ra_arena" }
ra_db = { path = "../ra_db" }

View file

@ -2,8 +2,8 @@
use std::sync::Arc;
use either::Either;
use hir_expand::{
either::Either,
name::{AsName, Name},
InFile,
};
@ -184,7 +184,7 @@ fn lower_struct(
ast::StructKind::Tuple(fl) => {
for (i, fd) in fl.fields().enumerate() {
trace.alloc(
|| Either::A(fd.clone()),
|| Either::Left(fd.clone()),
|| StructFieldData {
name: Name::new_tuple_field(i),
type_ref: TypeRef::from_ast_opt(fd.type_ref()),
@ -196,7 +196,7 @@ fn lower_struct(
ast::StructKind::Record(fl) => {
for fd in fl.fields() {
trace.alloc(
|| Either::B(fd.clone()),
|| Either::Right(fd.clone()),
|| StructFieldData {
name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing),
type_ref: TypeRef::from_ast_opt(fd.ascribed_type()),

View file

@ -2,7 +2,8 @@
use std::{ops, sync::Arc};
use hir_expand::{either::Either, hygiene::Hygiene, AstId, InFile};
use either::Either;
use hir_expand::{hygiene::Hygiene, AstId, InFile};
use mbe::ast_to_token_tree;
use ra_syntax::{
ast::{self, AstNode, AttrsOwner},
@ -45,8 +46,8 @@ impl Attrs {
AttrDefId::StructFieldId(it) => {
let src = it.parent.child_source(db);
match &src.value[it.local_id] {
Either::A(_tuple) => Attrs::default(),
Either::B(record) => Attrs::from_attrs_owner(db, src.with_value(record)),
Either::Left(_tuple) => Attrs::default(),
Either::Right(record) => Attrs::from_attrs_owner(db, src.with_value(record)),
}
}
AttrDefId::EnumVariantId(var_id) => {

View file

@ -5,9 +5,8 @@ pub mod scope;
use std::{ops::Index, sync::Arc};
use hir_expand::{
either::Either, hygiene::Hygiene, AstId, HirFileId, InFile, MacroDefId, MacroFileKind,
};
use either::Either;
use hir_expand::{hygiene::Hygiene, AstId, HirFileId, InFile, MacroDefId, MacroFileKind};
use ra_arena::{map::ArenaMap, Arena};
use ra_syntax::{ast, AstNode, AstPtr};
use rustc_hash::FxHashMap;
@ -210,7 +209,7 @@ impl BodySourceMap {
}
pub fn node_expr(&self, node: InFile<&ast::Expr>) -> Option<ExprId> {
let src = node.map(|it| Either::A(AstPtr::new(it)));
let src = node.map(|it| Either::Left(AstPtr::new(it)));
self.expr_map.get(&src).cloned()
}
@ -219,7 +218,7 @@ impl BodySourceMap {
}
pub fn node_pat(&self, node: InFile<&ast::Pat>) -> Option<PatId> {
let src = node.map(|it| Either::A(AstPtr::new(it)));
let src = node.map(|it| Either::Left(AstPtr::new(it)));
self.pat_map.get(&src).cloned()
}

View file

@ -1,10 +1,8 @@
//! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr`
//! representation.
use hir_expand::{
either::Either,
name::{self, AsName, Name},
};
use either::Either;
use hir_expand::name::{self, AsName, Name};
use ra_arena::Arena;
use ra_syntax::{
ast::{
@ -74,7 +72,7 @@ where
mode: BindingAnnotation::Unannotated,
subpat: None,
},
Either::B(ptr),
Either::Right(ptr),
);
self.body.params.push(param_pat);
}
@ -94,7 +92,7 @@ where
}
fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId {
let ptr = Either::A(ptr);
let ptr = Either::Left(ptr);
let id = self.body.exprs.alloc(expr);
let src = self.expander.to_source(ptr);
self.source_map.expr_map.insert(src, id);
@ -107,7 +105,7 @@ where
self.body.exprs.alloc(expr)
}
fn alloc_expr_field_shorthand(&mut self, expr: Expr, ptr: AstPtr<ast::RecordField>) -> ExprId {
let ptr = Either::B(ptr);
let ptr = Either::Right(ptr);
let id = self.body.exprs.alloc(expr);
let src = self.expander.to_source(ptr);
self.source_map.expr_map.insert(src, id);
@ -277,7 +275,7 @@ where
ast::Expr::ParenExpr(e) => {
let inner = self.collect_expr_opt(e.expr());
// make the paren expr point to the inner expression as well
let src = self.expander.to_source(Either::A(syntax_ptr));
let src = self.expander.to_source(Either::Left(syntax_ptr));
self.source_map.expr_map.insert(src, inner);
inner
}
@ -550,7 +548,7 @@ where
ast::Pat::SlicePat(_) | ast::Pat::RangePat(_) => Pat::Missing,
};
let ptr = AstPtr::new(&pat);
self.alloc_pat(pattern, Either::A(ptr))
self.alloc_pat(pattern, Either::Left(ptr))
}
fn collect_pat_opt(&mut self, pat: Option<ast::Pat>) -> PatId {

View file

@ -5,7 +5,7 @@
use std::sync::Arc;
use hir_expand::either::Either;
use either::Either;
use ra_syntax::ast;
use crate::{
@ -46,8 +46,8 @@ impl Documentation {
AttrDefId::StructFieldId(it) => {
let src = it.parent.child_source(db);
match &src.value[it.local_id] {
Either::A(_tuple) => None,
Either::B(record) => docs_from_ast(record),
Either::Left(_tuple) => None,
Either::Right(record) => docs_from_ast(record),
}
}
AttrDefId::AdtId(it) => match it {

View file

@ -57,9 +57,9 @@ mod tests;
use std::sync::Arc;
use either::Either;
use hir_expand::{
ast_id_map::FileAstId, diagnostics::DiagnosticSink, either::Either, name::Name, InFile,
MacroDefId,
ast_id_map::FileAstId, diagnostics::DiagnosticSink, name::Name, InFile, MacroDefId,
};
use once_cell::sync::Lazy;
use ra_arena::Arena;
@ -287,10 +287,10 @@ impl ModuleData {
) -> InFile<Either<ast::SourceFile, ast::Module>> {
if let Some(file_id) = self.definition {
let sf = db.parse(file_id).tree();
return InFile::new(file_id.into(), Either::A(sf));
return InFile::new(file_id.into(), Either::Left(sf));
}
let decl = self.declaration.unwrap();
InFile::new(decl.file_id, Either::B(decl.to_node(db)))
InFile::new(decl.file_id, Either::Right(decl.to_node(db)))
}
/// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.

View file

@ -7,10 +7,10 @@
use std::{ops::Index, sync::Arc};
use either::Either;
use hir_expand::{
ast_id_map::AstIdMap,
db::AstDatabase,
either::Either,
hygiene::Hygiene,
name::{AsName, Name},
};
@ -324,7 +324,7 @@ impl RawItemsCollector {
is_extern_crate: false,
is_macro_use: false,
};
buf.push((import_data, Either::A(AstPtr::new(use_tree))));
buf.push((import_data, Either::Left(AstPtr::new(use_tree))));
},
);
for (import_data, ptr) in buf {
@ -355,7 +355,7 @@ impl RawItemsCollector {
current_module,
attrs,
import_data,
Either::B(AstPtr::new(&extern_crate)),
Either::Right(AstPtr::new(&extern_crate)),
);
}
}

View file

@ -2,8 +2,8 @@
use std::{iter, sync::Arc};
use either::Either;
use hir_expand::{
either::Either,
hygiene::Hygiene,
name::{self, AsName, Name},
};
@ -111,7 +111,7 @@ impl Path {
ast::PathSegmentKind::Name(name_ref) => {
// FIXME: this should just return name
match hygiene.name_ref_to_name(name_ref) {
Either::A(name) => {
Either::Left(name) => {
let args = segment
.type_arg_list()
.and_then(GenericArgs::from_ast)
@ -125,7 +125,7 @@ impl Path {
let segment = PathSegment { name, args_and_bindings: args };
segments.push(segment);
}
Either::B(crate_id) => {
Either::Right(crate_id) => {
kind = PathKind::DollarCrate(crate_id);
break;
}
@ -347,7 +347,7 @@ fn convert_path(prefix: Option<Path>, path: ast::Path, hygiene: &Hygiene) -> Opt
let res = match segment.kind()? {
ast::PathSegmentKind::Name(name_ref) => {
match hygiene.name_ref_to_name(name_ref) {
Either::A(name) => {
Either::Left(name) => {
// no type args in use
let mut res = prefix.unwrap_or_else(|| Path {
kind: PathKind::Plain,
@ -359,7 +359,7 @@ fn convert_path(prefix: Option<Path>, path: ast::Path, hygiene: &Hygiene) -> Opt
});
res
}
Either::B(crate_id) => {
Either::Right(crate_id) => {
return Some(Path::from_simple_segments(
PathKind::DollarCrate(crate_id),
iter::empty(),

View file

@ -9,6 +9,7 @@ doctest = false
[dependencies]
log = "0.4.5"
either = "1.5"
ra_arena = { path = "../ra_arena" }
ra_db = { path = "../ra_db" }

View file

@ -1,54 +0,0 @@
//! FIXME: write short doc here
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Either<A, B> {
A(A),
B(B),
}
impl<A, B> Either<A, B> {
pub fn either<R, F1, F2>(self, f1: F1, f2: F2) -> R
where
F1: FnOnce(A) -> R,
F2: FnOnce(B) -> R,
{
match self {
Either::A(a) => f1(a),
Either::B(b) => f2(b),
}
}
pub fn map<U, V, F1, F2>(self, f1: F1, f2: F2) -> Either<U, V>
where
F1: FnOnce(A) -> U,
F2: FnOnce(B) -> V,
{
match self {
Either::A(a) => Either::A(f1(a)),
Either::B(b) => Either::B(f2(b)),
}
}
pub fn map_a<U, F>(self, f: F) -> Either<U, B>
where
F: FnOnce(A) -> U,
{
self.map(f, |it| it)
}
pub fn a(self) -> Option<A> {
match self {
Either::A(it) => Some(it),
Either::B(_) => None,
}
}
pub fn b(self) -> Option<B> {
match self {
Either::A(_) => None,
Either::B(it) => Some(it),
}
}
pub fn as_ref(&self) -> Either<&A, &B> {
match self {
Either::A(it) => Either::A(it),
Either::B(it) => Either::B(it),
}
}
}

View file

@ -2,12 +2,12 @@
//!
//! Specifically, `ast` + `Hygiene` allows you to create a `Name`. Note that, at
//! this moment, this is horribly incomplete and handles only `$crate`.
use either::Either;
use ra_db::CrateId;
use ra_syntax::ast;
use crate::{
db::AstDatabase,
either::Either,
name::{AsName, Name},
HirFileId, HirFileIdRepr, MacroDefKind,
};
@ -41,9 +41,9 @@ impl Hygiene {
pub fn name_ref_to_name(&self, name_ref: ast::NameRef) -> Either<Name, CrateId> {
if let Some(def_crate) = self.def_crate {
if name_ref.text() == "$crate" {
return Either::B(def_crate);
return Either::Right(def_crate);
}
}
Either::A(name_ref.as_name())
Either::Left(name_ref.as_name())
}
}

View file

@ -6,7 +6,6 @@
pub mod db;
pub mod ast_id_map;
pub mod either;
pub mod name;
pub mod hygiene;
pub mod diagnostics;

View file

@ -97,7 +97,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
let (_, source_map) = db.body_with_source_map(self.func.into());
if let Some(source_ptr) = source_map.expr_syntax(id) {
if let Some(expr) = source_ptr.value.a() {
if let Some(expr) = source_ptr.value.left() {
let root = source_ptr.file_syntax(db);
if let ast::Expr::RecordLit(record_lit) = expr.to_node(&root) {
if let Some(field_list) = record_lit.record_field_list() {
@ -142,7 +142,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
let (_, source_map) = db.body_with_source_map(self.func.into());
if let Some(source_ptr) = source_map.expr_syntax(id) {
if let Some(expr) = source_ptr.value.a() {
if let Some(expr) = source_ptr.value.left() {
self.sink.push(MissingOkInTailExpr { file: source_ptr.file_id, expr });
}
}

View file

@ -11,6 +11,7 @@ doctest = false
wasm = []
[dependencies]
either = "1.5"
format-buf = "1.0.0"
itertools = "0.8.0"
join_to_string = "0.1.3"

View file

@ -1,6 +1,7 @@
//! FIXME: write short doc here
use hir::{Adt, Either, HasSource, PathResolution};
use either::Either;
use hir::{Adt, HasSource, PathResolution};
use ra_syntax::AstNode;
use test_utils::tested_by;
@ -27,7 +28,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
}
if Some(module) == ctx.module {
if let Some(import) = import {
if let Either::A(use_tree) = import.source(ctx.db).value {
if let Either::Left(use_tree) = import.source(ctx.db).value {
if use_tree.syntax().text_range().contains_inclusive(ctx.offset) {
// for `use self::foo<|>`, don't suggest `foo` as a completion
tested_by!(dont_complete_current_use);

View file

@ -1,6 +1,7 @@
//! FIXME: write short doc here
use hir::{AssocItem, Either, FieldSource, HasSource, InFile, ModuleSource};
use either::Either;
use hir::{AssocItem, FieldSource, HasSource, InFile, ModuleSource};
use ra_db::{FileId, SourceDatabase};
use ra_syntax::{
ast::{self, DocCommentsOwner, NameOwner},
@ -342,10 +343,10 @@ impl ToNav for hir::Local {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
let src = self.source(db);
let (full_range, focus_range) = match src.value {
Either::A(it) => {
Either::Left(it) => {
(it.syntax().text_range(), it.name().map(|it| it.syntax().text_range()))
}
Either::B(it) => (it.syntax().text_range(), Some(it.self_kw_token().text_range())),
Either::Right(it) => (it.syntax().text_range(), Some(it.self_kw_token().text_range())),
};
let name = match self.name(db) {
Some(it) => it.to_string().into(),