From b871062e329301683083a1a2ff5eb476e6c397c7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 15 Jan 2019 20:43:37 +0300 Subject: remove Cancelable from Ty --- crates/ra_hir/src/adt.rs | 5 +- crates/ra_hir/src/code_model_api.rs | 2 +- crates/ra_hir/src/db.rs | 4 +- crates/ra_hir/src/ty.rs | 108 ++++++++++------------- crates/ra_hir/src/ty/method_resolution.rs | 2 +- crates/ra_ide_api/src/completion/complete_dot.rs | 7 +- 6 files changed, 57 insertions(+), 71 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index 57d112f74..d87fe7049 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs @@ -1,6 +1,5 @@ use std::sync::Arc; -use ra_db::Cancelable; use ra_syntax::{ SyntaxNode, ast::{self, NameOwner, StructFlavor, AstNode} @@ -18,8 +17,8 @@ impl Struct { Struct { def_id } } - pub(crate) fn variant_data(&self, db: &impl HirDatabase) -> Cancelable> { - Ok(db.struct_data(self.def_id).variant_data.clone()) + pub(crate) fn variant_data(&self, db: &impl HirDatabase) -> Arc { + db.struct_data(self.def_id).variant_data.clone() } } diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 7ccd29e2f..d0c455d0a 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -160,7 +160,7 @@ impl StructField { pub fn name(&self) -> &Name { &self.name } - pub fn ty(&self, db: &impl HirDatabase) -> Cancelable> { + pub fn ty(&self, db: &impl HirDatabase) -> Option { db.type_for_field(self.struct_.def_id, self.name.clone()) } } diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 161a5e714..4a2b0b3dc 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -57,12 +57,12 @@ pub trait HirDatabase: SyntaxDatabase use fn crate::ty::infer; } - fn type_for_def(def_id: DefId) -> Cancelable { + fn type_for_def(def_id: DefId) -> Ty { type TypeForDefQuery; use fn crate::ty::type_for_def; } - fn type_for_field(def_id: DefId, field: Name) -> Cancelable> { + fn type_for_field(def_id: DefId, field: Name) -> Option { type TypeForFieldQuery; use fn crate::ty::type_for_field; } diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index e5f8ffc2e..37fc8643a 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -271,28 +271,28 @@ impl Ty { module: &Module, impl_block: Option<&ImplBlock>, type_ref: &TypeRef, - ) -> Cancelable { - Ok(match type_ref { + ) -> Self { + match type_ref { TypeRef::Never => Ty::Never, TypeRef::Tuple(inner) => { let inner_tys = inner .iter() .map(|tr| Ty::from_hir(db, module, impl_block, tr)) - .collect::>>()?; + .collect::>(); Ty::Tuple(inner_tys.into()) } - TypeRef::Path(path) => Ty::from_hir_path(db, module, impl_block, path)?, + TypeRef::Path(path) => Ty::from_hir_path(db, module, impl_block, path), TypeRef::RawPtr(inner, mutability) => { - let inner_ty = Ty::from_hir(db, module, impl_block, inner)?; + let inner_ty = Ty::from_hir(db, module, impl_block, inner); Ty::RawPtr(Arc::new(inner_ty), *mutability) } TypeRef::Array(_inner) => Ty::Unknown, // TODO TypeRef::Slice(inner) => { - let inner_ty = Ty::from_hir(db, module, impl_block, inner)?; + let inner_ty = Ty::from_hir(db, module, impl_block, inner); Ty::Slice(Arc::new(inner_ty)) } TypeRef::Reference(inner, mutability) => { - let inner_ty = Ty::from_hir(db, module, impl_block, inner)?; + let inner_ty = Ty::from_hir(db, module, impl_block, inner); Ty::Ref(Arc::new(inner_ty), *mutability) } TypeRef::Placeholder => Ty::Unknown, @@ -300,7 +300,7 @@ impl Ty { let mut inner_tys = params .iter() .map(|tr| Ty::from_hir(db, module, impl_block, tr)) - .collect::>>()?; + .collect::>(); let return_ty = inner_tys .pop() .expect("TypeRef::Fn should always have at least return type"); @@ -311,7 +311,7 @@ impl Ty { Ty::FnPtr(Arc::new(sig)) } TypeRef::Error => Ty::Unknown, - }) + } } pub(crate) fn from_hir_opt( @@ -319,10 +319,8 @@ impl Ty { module: &Module, impl_block: Option<&ImplBlock>, type_ref: Option<&TypeRef>, - ) -> Cancelable { - type_ref - .map(|t| Ty::from_hir(db, module, impl_block, t)) - .unwrap_or(Ok(Ty::Unknown)) + ) -> Self { + type_ref.map_or(Ty::Unknown, |t| Ty::from_hir(db, module, impl_block, t)) } pub(crate) fn from_hir_path( @@ -330,19 +328,19 @@ impl Ty { module: &Module, impl_block: Option<&ImplBlock>, path: &Path, - ) -> Cancelable { + ) -> Self { if let Some(name) = path.as_ident() { if let Some(int_ty) = primitive::UncertainIntTy::from_name(name) { - return Ok(Ty::Int(int_ty)); + return Ty::Int(int_ty); } else if let Some(float_ty) = primitive::UncertainFloatTy::from_name(name) { - return Ok(Ty::Float(float_ty)); + return Ty::Float(float_ty); } else if name.as_known_name() == Some(KnownName::SelfType) { return Ty::from_hir_opt(db, module, None, impl_block.map(|i| i.target_type())); } else if let Some(known) = name.as_known_name() { match known { - KnownName::Bool => return Ok(Ty::Bool), - KnownName::Char => return Ok(Ty::Char), - KnownName::Str => return Ok(Ty::Str), + KnownName::Bool => return Ty::Bool, + KnownName::Char => return Ty::Char, + KnownName::Str => return Ty::Str, _ => {} } } @@ -352,10 +350,9 @@ impl Ty { let resolved = if let Some(r) = module.resolve_path(db, path).take_types() { r } else { - return Ok(Ty::Unknown); + return Ty::Unknown; }; - let ty = db.type_for_def(resolved)?; - Ok(ty) + db.type_for_def(resolved) } pub fn unit() -> Self { @@ -445,7 +442,7 @@ impl fmt::Display for Ty { /// Compute the declared type of a function. This should not need to look at the /// function body. -fn type_for_fn(db: &impl HirDatabase, f: Function) -> Cancelable { +fn type_for_fn(db: &impl HirDatabase, f: Function) -> Ty { let signature = f.signature(db); let module = f.module(db); let impl_block = f.impl_block(db); @@ -454,38 +451,38 @@ fn type_for_fn(db: &impl HirDatabase, f: Function) -> Cancelable { .params() .iter() .map(|tr| Ty::from_hir(db, &module, impl_block.as_ref(), tr)) - .collect::>>()?; - let output = Ty::from_hir(db, &module, impl_block.as_ref(), signature.ret_type())?; + .collect::>(); + let output = Ty::from_hir(db, &module, impl_block.as_ref(), signature.ret_type()); let sig = FnSig { input, output }; - Ok(Ty::FnPtr(Arc::new(sig))) + Ty::FnPtr(Arc::new(sig)) } -fn type_for_struct(db: &impl HirDatabase, s: Struct) -> Cancelable { - Ok(Ty::Adt { +fn type_for_struct(db: &impl HirDatabase, s: Struct) -> Ty { + Ty::Adt { def_id: s.def_id(), name: s.name(db).unwrap_or_else(Name::missing), - }) + } } -pub(crate) fn type_for_enum(db: &impl HirDatabase, s: Enum) -> Cancelable { - Ok(Ty::Adt { +pub(crate) fn type_for_enum(db: &impl HirDatabase, s: Enum) -> Ty { + Ty::Adt { def_id: s.def_id(), name: s.name(db).unwrap_or_else(Name::missing), - }) + } } -pub(crate) fn type_for_enum_variant(db: &impl HirDatabase, ev: EnumVariant) -> Cancelable { +pub(crate) fn type_for_enum_variant(db: &impl HirDatabase, ev: EnumVariant) -> Ty { let enum_parent = ev.parent_enum(db); type_for_enum(db, enum_parent) } -pub(super) fn type_for_def(db: &impl HirDatabase, def_id: DefId) -> Cancelable { +pub(super) fn type_for_def(db: &impl HirDatabase, def_id: DefId) -> Ty { let def = def_id.resolve(db); match def { Def::Module(..) => { log::debug!("trying to get type for module {:?}", def_id); - Ok(Ty::Unknown) + Ty::Unknown } Def::Function(f) => type_for_fn(db, f), Def::Struct(s) => type_for_struct(db, s), @@ -497,19 +494,15 @@ pub(super) fn type_for_def(db: &impl HirDatabase, def_id: DefId) -> Cancelable Cancelable> { +pub(super) fn type_for_field(db: &impl HirDatabase, def_id: DefId, field: Name) -> Option { let def = def_id.resolve(db); let variant_data = match def { - Def::Struct(s) => s.variant_data(db)?, + Def::Struct(s) => s.variant_data(db), Def::EnumVariant(ev) => ev.variant_data(db), // TODO: unions _ => panic!( @@ -519,13 +512,8 @@ pub(super) fn type_for_field( }; let module = def_id.module(db); let impl_block = def_id.impl_block(db); - let type_ref = ctry!(variant_data.get_field_type_ref(&field)); - Ok(Some(Ty::from_hir( - db, - &module, - impl_block.as_ref(), - &type_ref, - )?)) + let type_ref = variant_data.get_field_type_ref(&field)?; + Some(Ty::from_hir(db, &module, impl_block.as_ref(), &type_ref)) } /// The result of type inference: A mapping from expressions and patterns to types. @@ -702,7 +690,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { self.type_of_pat.insert(pat, ty); } - fn make_ty(&self, type_ref: &TypeRef) -> Cancelable { + fn make_ty(&self, type_ref: &TypeRef) -> Ty { Ty::from_hir(self.db, &self.module, self.impl_block.as_ref(), type_ref) } @@ -861,7 +849,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { // resolve in module let resolved = ctry!(self.module.resolve_path(self.db, &path).take_values()); - let ty = self.db.type_for_def(resolved)?; + let ty = self.db.type_for_def(resolved); let ty = self.insert_type_vars(ty); Ok(Some(ty)) } @@ -879,11 +867,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { }; Ok(match def_id.resolve(self.db) { Def::Struct(s) => { - let ty = type_for_struct(self.db, s)?; + let ty = type_for_struct(self.db, s); (ty, Some(def_id)) } Def::EnumVariant(ev) => { - let ty = type_for_enum_variant(self.db, ev)?; + let ty = type_for_enum_variant(self.db, ev); (ty, Some(def_id)) } _ => (Ty::Unknown, None), @@ -964,7 +952,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { let method_ty = match resolved { Some(def_id) => { self.write_method_resolution(expr, def_id); - self.db.type_for_def(def_id)? + self.db.type_for_def(def_id) } None => Ty::Unknown, }; @@ -1024,7 +1012,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { for field in fields { let field_ty = if let Some(def_id) = def_id { self.db - .type_for_field(def_id, field.name.clone())? + .type_for_field(def_id, field.name.clone()) .unwrap_or(Ty::Unknown) } else { Ty::Unknown @@ -1047,7 +1035,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { i.and_then(|i| fields.get(i).cloned()).map(Ok) } Ty::Adt { def_id, .. } => { - transpose(self.db.type_for_field(def_id, name.clone())) + transpose(Ok(self.db.type_for_field(def_id, name.clone()))) } _ => None, }) @@ -1061,7 +1049,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { Expr::Cast { expr, type_ref } => { let _inner_ty = self.infer_expr(*expr, &Expectation::none())?; let cast_ty = - Ty::from_hir(self.db, &self.module, self.impl_block.as_ref(), type_ref)?; + Ty::from_hir(self.db, &self.module, self.impl_block.as_ref(), type_ref); let cast_ty = self.insert_type_vars(cast_ty); // TODO check the cast... cast_ty @@ -1154,7 +1142,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { &self.module, self.impl_block.as_ref(), type_ref.as_ref(), - )?; + ); let decl_ty = self.insert_type_vars(decl_ty); let ty = if let Some(expr) = initializer { let expr_ty = self.infer_expr(*expr, &Expectation::has_type(decl_ty))?; @@ -1181,12 +1169,12 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { fn collect_fn_signature(&mut self, signature: &FnSignature) -> Cancelable<()> { let body = Arc::clone(&self.body); // avoid borrow checker problem for (type_ref, pat) in signature.params().iter().zip(body.params()) { - let ty = self.make_ty(type_ref)?; + let ty = self.make_ty(type_ref); let ty = self.insert_type_vars(ty); self.write_pat_ty(*pat, ty); } self.return_ty = { - let ty = self.make_ty(signature.ret_type())?; + let ty = self.make_ty(signature.ret_type()); let ty = self.insert_type_vars(ty); ty }; diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index 94c5124a9..0676a989d 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs @@ -65,7 +65,7 @@ impl CrateImplBlocks { // ignore for now } else { let target_ty = - Ty::from_hir(db, &module, Some(&impl_block), impl_data.target_type())?; + Ty::from_hir(db, &module, Some(&impl_block), impl_data.target_type()); if let Some(target_ty_fp) = TyFingerprint::for_impl(&target_ty) { self.impls .entry(target_ty_fp) diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs index cb86ba9a3..30a0a3924 100644 --- a/crates/ra_ide_api/src/completion/complete_dot.rs +++ b/crates/ra_ide_api/src/completion/complete_dot.rs @@ -17,13 +17,13 @@ pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) -> Ca }; let receiver_ty = infer_result[expr].clone(); if !ctx.is_call { - complete_fields(acc, ctx, receiver_ty.clone())?; + complete_fields(acc, ctx, receiver_ty.clone()); } complete_methods(acc, ctx, receiver_ty)?; Ok(()) } -fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) -> Cancelable<()> { +fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) { for receiver in receiver.autoderef(ctx.db) { match receiver { Ty::Adt { def_id, .. } => { @@ -35,7 +35,7 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) field.name().to_string(), ) .kind(CompletionItemKind::Field) - .set_detail(field.ty(ctx.db)?.map(|ty| ty.to_string())) + .set_detail(field.ty(ctx.db).map(|ty| ty.to_string())) .add_to(acc); } } @@ -53,7 +53,6 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) _ => {} }; } - Ok(()) } fn complete_methods( -- cgit v1.2.3 From 8ba9c2d4cedbcf8f1d2c644733d2b06fa1984d22 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 15 Jan 2019 20:54:18 +0300 Subject: remove Cancelable from type inference --- crates/ra_hir/src/code_model_api.rs | 2 +- crates/ra_hir/src/db.rs | 6 +- crates/ra_hir/src/lib.rs | 9 -- crates/ra_hir/src/ty.rs | 128 ++++++++++------------- crates/ra_hir/src/ty/method_resolution.rs | 37 +++---- crates/ra_hir/src/ty/tests.rs | 6 +- crates/ra_ide_api/src/completion/complete_dot.rs | 15 +-- crates/ra_ide_api/src/goto_definition.rs | 2 +- crates/ra_ide_api/src/hover.rs | 2 +- 9 files changed, 88 insertions(+), 119 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index d0c455d0a..0cf45944f 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -318,7 +318,7 @@ impl Function { db.fn_signature(self.def_id) } - pub fn infer(&self, db: &impl HirDatabase) -> Cancelable> { + pub fn infer(&self, db: &impl HirDatabase) -> Arc { db.infer(self.def_id) } } diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 4a2b0b3dc..0a0994f5f 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -1,7 +1,7 @@ use std::sync::Arc; use ra_syntax::{SyntaxNode, TreeArc, SourceFile}; -use ra_db::{SourceRootId, LocationIntener, SyntaxDatabase, Cancelable}; +use ra_db::{SourceRootId, LocationIntener, SyntaxDatabase}; use crate::{ DefLoc, DefId, MacroCallLoc, MacroCallId, Name, HirFileId, @@ -52,7 +52,7 @@ pub trait HirDatabase: SyntaxDatabase use fn crate::adt::EnumVariantData::enum_variant_data_query; } - fn infer(def_id: DefId) -> Cancelable> { + fn infer(def_id: DefId) -> Arc { type InferQuery; use fn crate::ty::infer; } @@ -102,7 +102,7 @@ pub trait HirDatabase: SyntaxDatabase use fn crate::impl_block::impls_in_module; } - fn impls_in_crate(krate: Crate) -> Cancelable> { + fn impls_in_crate(krate: Crate) -> Arc { type ImplsInCrateQuery; use fn crate::ty::method_resolution::CrateImplBlocks::impls_in_crate_query; } diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 45dda4f7f..ef7d049ee 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -5,15 +5,6 @@ //! to a particular crate instance. That is, it has cfg flags and features //! applied. So, the relation between syntax and HIR is many-to-one. -macro_rules! ctry { - ($expr:expr) => { - match $expr { - None => return Ok(None), - Some(it) => it, - } - }; -} - pub mod db; #[cfg(test)] mod mock; diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 37fc8643a..dbbbce795 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -30,8 +30,6 @@ use ra_arena::map::ArenaMap; use join_to_string::join; use rustc_hash::FxHashMap; -use ra_db::Cancelable; - use crate::{ Def, DefId, Module, Function, Struct, Enum, EnumVariant, Path, Name, ImplBlock, FnSignature, FnScopes, @@ -41,14 +39,6 @@ use crate::{ expr::{Body, Expr, Literal, ExprId, PatId, UnaryOp, BinaryOp, Statement}, }; -fn transpose(x: Cancelable>) -> Option> { - match x { - Ok(Some(t)) => Some(Ok(t)), - Ok(None) => None, - Err(e) => Some(Err(e)), - } -} - /// The ID of a type variable. #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)] pub struct TypeVarId(u32); @@ -836,36 +826,36 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { }) } - fn infer_path_expr(&mut self, expr: ExprId, path: &Path) -> Cancelable> { + fn infer_path_expr(&mut self, expr: ExprId, path: &Path) -> Option { if path.is_ident() || path.is_self() { // resolve locally let name = path.as_ident().cloned().unwrap_or_else(Name::self_param); if let Some(scope_entry) = self.scopes.resolve_local_name(expr, name) { - let ty = ctry!(self.type_of_pat.get(scope_entry.pat())); + let ty = self.type_of_pat.get(scope_entry.pat())?; let ty = self.resolve_ty_as_possible(ty.clone()); - return Ok(Some(ty)); + return Some(ty); }; }; // resolve in module - let resolved = ctry!(self.module.resolve_path(self.db, &path).take_values()); + let resolved = self.module.resolve_path(self.db, &path).take_values()?; let ty = self.db.type_for_def(resolved); let ty = self.insert_type_vars(ty); - Ok(Some(ty)) + Some(ty) } - fn resolve_variant(&self, path: Option<&Path>) -> Cancelable<(Ty, Option)> { + fn resolve_variant(&self, path: Option<&Path>) -> (Ty, Option) { let path = if let Some(path) = path { path } else { - return Ok((Ty::Unknown, None)); + return (Ty::Unknown, None); }; let def_id = if let Some(def_id) = self.module.resolve_path(self.db, &path).take_types() { def_id } else { - return Ok((Ty::Unknown, None)); + return (Ty::Unknown, None); }; - Ok(match def_id.resolve(self.db) { + match def_id.resolve(self.db) { Def::Struct(s) => { let ty = type_for_struct(self.db, s); (ty, Some(def_id)) @@ -875,10 +865,10 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { (ty, Some(def_id)) } _ => (Ty::Unknown, None), - }) + } } - fn infer_expr(&mut self, expr: ExprId, expected: &Expectation) -> Cancelable { + fn infer_expr(&mut self, expr: ExprId, expected: &Expectation) -> Ty { let body = Arc::clone(&self.body); // avoid borrow checker problem let ty = match &body[expr] { Expr::Missing => Ty::Unknown, @@ -888,11 +878,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { else_branch, } => { // if let is desugared to match, so this is always simple if - self.infer_expr(*condition, &Expectation::has_type(Ty::Bool))?; - let then_ty = self.infer_expr(*then_branch, expected)?; + self.infer_expr(*condition, &Expectation::has_type(Ty::Bool)); + let then_ty = self.infer_expr(*then_branch, expected); match else_branch { Some(else_branch) => { - self.infer_expr(*else_branch, expected)?; + self.infer_expr(*else_branch, expected); } None => { // no else branch -> unit @@ -901,31 +891,31 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { }; then_ty } - Expr::Block { statements, tail } => self.infer_block(statements, *tail, expected)?, + Expr::Block { statements, tail } => self.infer_block(statements, *tail, expected), Expr::Loop { body } => { - self.infer_expr(*body, &Expectation::has_type(Ty::unit()))?; + self.infer_expr(*body, &Expectation::has_type(Ty::unit())); // TODO handle break with value Ty::Never } Expr::While { condition, body } => { // while let is desugared to a match loop, so this is always simple while - self.infer_expr(*condition, &Expectation::has_type(Ty::Bool))?; - self.infer_expr(*body, &Expectation::has_type(Ty::unit()))?; + self.infer_expr(*condition, &Expectation::has_type(Ty::Bool)); + self.infer_expr(*body, &Expectation::has_type(Ty::unit())); Ty::unit() } Expr::For { iterable, body, .. } => { let _iterable_ty = self.infer_expr(*iterable, &Expectation::none()); // TODO write type for pat - self.infer_expr(*body, &Expectation::has_type(Ty::unit()))?; + self.infer_expr(*body, &Expectation::has_type(Ty::unit())); Ty::unit() } Expr::Lambda { body, .. } => { // TODO write types for args, infer lambda type etc. - let _body_ty = self.infer_expr(*body, &Expectation::none())?; + let _body_ty = self.infer_expr(*body, &Expectation::none()); Ty::Unknown } Expr::Call { callee, args } => { - let callee_ty = self.infer_expr(*callee, &Expectation::none())?; + let callee_ty = self.infer_expr(*callee, &Expectation::none()); let (param_tys, ret_ty) = match &callee_ty { Ty::FnPtr(sig) => (&sig.input[..], sig.output.clone()), _ => { @@ -938,7 +928,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { self.infer_expr( *arg, &Expectation::has_type(param_tys.get(i).cloned().unwrap_or(Ty::Unknown)), - )?; + ); } ret_ty } @@ -947,8 +937,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { args, method_name, } => { - let receiver_ty = self.infer_expr(*receiver, &Expectation::none())?; - let resolved = receiver_ty.clone().lookup_method(self.db, method_name)?; + let receiver_ty = self.infer_expr(*receiver, &Expectation::none()); + let resolved = receiver_ty.clone().lookup_method(self.db, method_name); let method_ty = match resolved { Some(def_id) => { self.write_method_resolution(expr, def_id); @@ -974,32 +964,32 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { self.infer_expr( *arg, &Expectation::has_type(param_tys.get(i).cloned().unwrap_or(Ty::Unknown)), - )?; + ); } ret_ty } Expr::Match { expr, arms } => { - let _ty = self.infer_expr(*expr, &Expectation::none())?; + let _ty = self.infer_expr(*expr, &Expectation::none()); for arm in arms { // TODO type the bindings in pats // TODO type the guard - let _ty = self.infer_expr(arm.expr, &Expectation::none())?; + let _ty = self.infer_expr(arm.expr, &Expectation::none()); } // TODO unify all the match arm types Ty::Unknown } - Expr::Path(p) => self.infer_path_expr(expr, p)?.unwrap_or(Ty::Unknown), + Expr::Path(p) => self.infer_path_expr(expr, p).unwrap_or(Ty::Unknown), Expr::Continue => Ty::Never, Expr::Break { expr } => { if let Some(expr) = expr { // TODO handle break with value - self.infer_expr(*expr, &Expectation::none())?; + self.infer_expr(*expr, &Expectation::none()); } Ty::Never } Expr::Return { expr } => { if let Some(expr) = expr { - self.infer_expr(*expr, &Expectation::has_type(self.return_ty.clone()))?; + self.infer_expr(*expr, &Expectation::has_type(self.return_ty.clone())); } Ty::Never } @@ -1008,7 +998,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { fields, spread, } => { - let (ty, def_id) = self.resolve_variant(path.as_ref())?; + let (ty, def_id) = self.resolve_variant(path.as_ref()); for field in fields { let field_ty = if let Some(def_id) = def_id { self.db @@ -1017,37 +1007,35 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } else { Ty::Unknown }; - self.infer_expr(field.expr, &Expectation::has_type(field_ty))?; + self.infer_expr(field.expr, &Expectation::has_type(field_ty)); } if let Some(expr) = spread { - self.infer_expr(*expr, &Expectation::has_type(ty.clone()))?; + self.infer_expr(*expr, &Expectation::has_type(ty.clone())); } ty } Expr::Field { expr, name } => { - let receiver_ty = self.infer_expr(*expr, &Expectation::none())?; + let receiver_ty = self.infer_expr(*expr, &Expectation::none()); let ty = receiver_ty .autoderef(self.db) .find_map(|derefed_ty| match derefed_ty { // this is more complicated than necessary because type_for_field is cancelable Ty::Tuple(fields) => { let i = name.to_string().parse::().ok(); - i.and_then(|i| fields.get(i).cloned()).map(Ok) - } - Ty::Adt { def_id, .. } => { - transpose(Ok(self.db.type_for_field(def_id, name.clone()))) + i.and_then(|i| fields.get(i).cloned()) } + Ty::Adt { def_id, .. } => self.db.type_for_field(def_id, name.clone()), _ => None, }) - .unwrap_or(Ok(Ty::Unknown))?; + .unwrap_or(Ty::Unknown); self.insert_type_vars(ty) } Expr::Try { expr } => { - let _inner_ty = self.infer_expr(*expr, &Expectation::none())?; + let _inner_ty = self.infer_expr(*expr, &Expectation::none()); Ty::Unknown } Expr::Cast { expr, type_ref } => { - let _inner_ty = self.infer_expr(*expr, &Expectation::none())?; + let _inner_ty = self.infer_expr(*expr, &Expectation::none()); let cast_ty = Ty::from_hir(self.db, &self.module, self.impl_block.as_ref(), type_ref); let cast_ty = self.insert_type_vars(cast_ty); @@ -1056,12 +1044,12 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } Expr::Ref { expr, mutability } => { // TODO pass the expectation down - let inner_ty = self.infer_expr(*expr, &Expectation::none())?; + let inner_ty = self.infer_expr(*expr, &Expectation::none()); // TODO reference coercions etc. Ty::Ref(Arc::new(inner_ty), *mutability) } Expr::UnaryOp { expr, op } => { - let inner_ty = self.infer_expr(*expr, &Expectation::none())?; + let inner_ty = self.infer_expr(*expr, &Expectation::none()); match op { Some(UnaryOp::Deref) => { if let Some(derefed_ty) = inner_ty.builtin_deref() { @@ -1082,11 +1070,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } _ => Expectation::none(), }; - let lhs_ty = self.infer_expr(*lhs, &lhs_expectation)?; + let lhs_ty = self.infer_expr(*lhs, &lhs_expectation); // TODO: find implementation of trait corresponding to operation // symbol and resolve associated `Output` type let rhs_expectation = binary_op_rhs_expectation(*op, lhs_ty); - let rhs_ty = self.infer_expr(*rhs, &Expectation::has_type(rhs_expectation))?; + let rhs_ty = self.infer_expr(*rhs, &Expectation::has_type(rhs_expectation)); // TODO: similar as above, return ty is often associated trait type binary_op_return_ty(*op, rhs_ty) @@ -1096,7 +1084,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { Expr::Tuple { exprs } => { let mut ty_vec = Vec::with_capacity(exprs.len()); for arg in exprs.iter() { - ty_vec.push(self.infer_expr(*arg, &Expectation::none())?); + ty_vec.push(self.infer_expr(*arg, &Expectation::none())); } Ty::Tuple(Arc::from(ty_vec)) @@ -1121,7 +1109,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { self.unify(&ty, &expected.ty); let ty = self.resolve_ty_as_possible(ty); self.write_expr_ty(expr, ty.clone()); - Ok(ty) + ty } fn infer_block( @@ -1129,7 +1117,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { statements: &[Statement], tail: Option, expected: &Expectation, - ) -> Cancelable { + ) -> Ty { for stmt in statements { match stmt { Statement::Let { @@ -1145,7 +1133,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { ); let decl_ty = self.insert_type_vars(decl_ty); let ty = if let Some(expr) = initializer { - let expr_ty = self.infer_expr(*expr, &Expectation::has_type(decl_ty))?; + let expr_ty = self.infer_expr(*expr, &Expectation::has_type(decl_ty)); expr_ty } else { decl_ty @@ -1154,19 +1142,19 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { self.write_pat_ty(*pat, ty); } Statement::Expr(expr) => { - self.infer_expr(*expr, &Expectation::none())?; + self.infer_expr(*expr, &Expectation::none()); } } } let ty = if let Some(expr) = tail { - self.infer_expr(expr, expected)? + self.infer_expr(expr, expected) } else { Ty::unit() }; - Ok(ty) + ty } - fn collect_fn_signature(&mut self, signature: &FnSignature) -> Cancelable<()> { + fn collect_fn_signature(&mut self, signature: &FnSignature) { let body = Arc::clone(&self.body); // avoid borrow checker problem for (type_ref, pat) in signature.params().iter().zip(body.params()) { let ty = self.make_ty(type_ref); @@ -1178,19 +1166,17 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { let ty = self.insert_type_vars(ty); ty }; - Ok(()) } - fn infer_body(&mut self) -> Cancelable<()> { + fn infer_body(&mut self) { self.infer_expr( self.body.body_expr(), &Expectation::has_type(self.return_ty.clone()), - )?; - Ok(()) + ); } } -pub fn infer(db: &impl HirDatabase, def_id: DefId) -> Cancelable> { +pub fn infer(db: &impl HirDatabase, def_id: DefId) -> Arc { db.check_canceled(); let function = Function::new(def_id); // TODO: consts also need inference let body = function.body(db); @@ -1200,9 +1186,9 @@ pub fn infer(db: &impl HirDatabase, def_id: DefId) -> Cancelable impl Iterator> + 'a { + ) -> impl Iterator + 'a { let fingerprint = TyFingerprint::for_impl(ty); fingerprint .and_then(|f| self.impls.get(&f)) @@ -50,11 +50,11 @@ impl CrateImplBlocks { .flat_map(|i| i.iter()) .map(move |(module_id, impl_id)| { let module_impl_blocks = db.impls_in_module(self.source_root_id, *module_id); - Ok(ImplBlock::from_id(module_impl_blocks, *impl_id)) + ImplBlock::from_id(module_impl_blocks, *impl_id) }) } - fn collect_recursive(&mut self, db: &impl HirDatabase, module: Module) -> Cancelable<()> { + fn collect_recursive(&mut self, db: &impl HirDatabase, module: Module) { let module_id = module.def_id.loc(db).module_id; let module_impl_blocks = db.impls_in_module(self.source_root_id, module_id); @@ -76,16 +76,14 @@ impl CrateImplBlocks { } for child in module.children(db) { - self.collect_recursive(db, child)?; + self.collect_recursive(db, child); } - - Ok(()) } pub(crate) fn impls_in_crate_query( db: &impl HirDatabase, krate: Crate, - ) -> Cancelable> { + ) -> Arc { let crate_graph = db.crate_graph(); let file_id = crate_graph.crate_root(krate.crate_id); let source_root_id = db.file_source_root(file_id); @@ -94,9 +92,9 @@ impl CrateImplBlocks { impls: FxHashMap::default(), }; if let Some(module) = krate.root_module(db) { - crate_impl_blocks.collect_recursive(db, module)?; + crate_impl_blocks.collect_recursive(db, module); } - Ok(Arc::new(crate_impl_blocks)) + Arc::new(crate_impl_blocks) } } @@ -111,13 +109,13 @@ impl Ty { // TODO: cache this as a query? // - if so, what signature? (TyFingerprint, Name)? // - or maybe cache all names and def_ids of methods per fingerprint? - pub fn lookup_method(self, db: &impl HirDatabase, name: &Name) -> Cancelable> { + pub fn lookup_method(self, db: &impl HirDatabase, name: &Name) -> Option { self.iterate_methods(db, |f| { let sig = f.signature(db); if sig.name() == name && sig.has_self_param() { - Ok(Some(f.def_id())) + Some(f.def_id()) } else { - Ok(None) + None } }) } @@ -127,8 +125,8 @@ impl Ty { pub fn iterate_methods( self, db: &impl HirDatabase, - mut callback: impl FnMut(Function) -> Cancelable>, - ) -> Cancelable> { + mut callback: impl FnMut(Function) -> Option, + ) -> Option { // For method calls, rust first does any number of autoderef, and then one // autoref (i.e. when the method takes &self or &mut self). We just ignore // the autoref currently -- when we find a method matching the given name, @@ -143,15 +141,14 @@ impl Ty { Some(krate) => krate, None => continue, }; - let impls = db.impls_in_crate(krate)?; + let impls = db.impls_in_crate(krate); for impl_block in impls.lookup_impl_blocks(db, &derefed_ty) { - let impl_block = impl_block?; for item in impl_block.items() { match item { ImplItem::Method(f) => { - if let Some(result) = callback(f.clone())? { - return Ok(Some(result)); + if let Some(result) = callback(f.clone()) { + return Some(result); } } _ => {} @@ -159,6 +156,6 @@ impl Ty { } } } - Ok(None) + None } } diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index b44ac9987..929fee04c 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -321,7 +321,7 @@ fn infer(content: &str) -> String { .filter_map(ast::FnDef::cast) { let func = source_binder::function_from_source(&db, file_id, fn_def).unwrap(); - let inference_result = func.infer(&db).unwrap(); + let inference_result = func.infer(&db); let body_syntax_mapping = func.body_syntax_mapping(&db); let mut types = Vec::new(); for (pat, ty) in inference_result.type_of_pat.iter() { @@ -405,7 +405,7 @@ fn typing_whitespace_inside_a_function_should_not_invalidate_types() { let func = source_binder::function_from_position(&db, pos).unwrap(); { let events = db.log_executed(|| { - func.infer(&db).unwrap(); + func.infer(&db); }); assert!(format!("{:?}", events).contains("infer")) } @@ -424,7 +424,7 @@ fn typing_whitespace_inside_a_function_should_not_invalidate_types() { { let events = db.log_executed(|| { - func.infer(&db).unwrap(); + func.infer(&db); }); assert!(!format!("{:?}", events).contains("infer"), "{:#?}", events) } diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs index 30a0a3924..31a2478d1 100644 --- a/crates/ra_ide_api/src/completion/complete_dot.rs +++ b/crates/ra_ide_api/src/completion/complete_dot.rs @@ -9,7 +9,7 @@ pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) -> Ca (Some(function), Some(receiver)) => (function, receiver), _ => return Ok(()), }; - let infer_result = function.infer(ctx.db)?; + let infer_result = function.infer(ctx.db); let syntax_mapping = function.body_syntax_mapping(ctx.db); let expr = match syntax_mapping.node_expr(receiver) { Some(expr) => expr, @@ -19,7 +19,7 @@ pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) -> Ca if !ctx.is_call { complete_fields(acc, ctx, receiver_ty.clone()); } - complete_methods(acc, ctx, receiver_ty)?; + complete_methods(acc, ctx, receiver_ty); Ok(()) } @@ -55,11 +55,7 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) } } -fn complete_methods( - acc: &mut Completions, - ctx: &CompletionContext, - receiver: Ty, -) -> Cancelable<()> { +fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) { receiver.iterate_methods(ctx.db, |func| { let sig = func.signature(ctx.db); if sig.has_self_param() { @@ -68,9 +64,8 @@ fn complete_methods( .kind(CompletionItemKind::Method) .add_to(acc); } - Ok(None::<()>) - })?; - Ok(()) + None::<()> + }); } #[cfg(test)] diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index cdd8e211d..e0f3deb0b 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs @@ -63,7 +63,7 @@ pub(crate) fn reference_definition( .parent() .and_then(ast::MethodCallExpr::cast) { - let infer_result = function.infer(db)?; + let infer_result = function.infer(db); let syntax_mapping = function.body_syntax_mapping(db); let expr = ast::Expr::cast(method_call.syntax()).unwrap(); if let Some(def_id) = syntax_mapping diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs index 0e9c48421..6b5887bda 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs @@ -73,7 +73,7 @@ pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Cancelable