]> git.lizzy.rs Git - rust.git/commitdiff
Use std::future::Future trait from stdlib
authorEvgenii P <eupn@protonmail.com>
Sat, 3 Aug 2019 10:07:20 +0000 (17:07 +0700)
committerEvgenii P <eupn@protonmail.com>
Sat, 3 Aug 2019 10:07:20 +0000 (17:07 +0700)
crates/ra_hir/src/source_binder.rs
crates/ra_ide_api/src/completion/complete_dot.rs

index 7720329e33a81904da2461709e4463b80d3f43a4..6a9f228b82d0a80656f7f5689c64ca5c6ae0b38e 100644 (file)
@@ -7,28 +7,29 @@
 /// purely for "IDE needs".
 use std::sync::Arc;
 
-use ra_db::{FileId, FilePosition};
-use ra_syntax::{
-    algo::find_node_at_offset,
-    ast::{self, AstNode, NameOwner},
-    AstPtr,
-    SyntaxKind::*,
-    SyntaxNode, SyntaxNodePtr, TextRange, TextUnit,
-};
-use rustc_hash::{FxHashMap, FxHashSet};
-
 use crate::{
-    expr,
     expr::{
+        self,
         scope::{ExprScopes, ScopeId},
         BodySourceMap,
     },
     ids::LocationCtx,
-    lang_item::LangItemTarget,
+    name,
+    path::{PathKind, PathSegment},
     ty::method_resolution::implements_trait,
     AsName, AstId, Const, Crate, DefWithBody, Either, Enum, Function, HirDatabase, HirFileId,
-    MacroDef, Module, Name, Path, PerNs, Resolver, Static, Struct, Trait, Ty,
+    MacroDef, Module, ModuleDef, Name, Path, PerNs, Resolution, Resolver, Static, Struct, Trait,
+    Ty,
+};
+use ra_db::{FileId, FilePosition};
+use ra_syntax::{
+    algo::find_node_at_offset,
+    ast::{self, AstNode, NameOwner},
+    AstPtr,
+    SyntaxKind::*,
+    SyntaxNode, SyntaxNodePtr, TextRange, TextUnit,
 };
+use rustc_hash::{FxHashMap, FxHashSet};
 
 /// Locates the module by `FileId`. Picks topmost module in the file.
 pub fn module_from_file_id(db: &impl HirDatabase, file_id: FileId) -> Option<Module> {
@@ -411,18 +412,32 @@ pub fn autoderef<'a>(
         crate::ty::autoderef(db, &self.resolver, canonical).map(|canonical| canonical.value)
     }
 
-    /// Checks that particular type `ty` implements `Future` trait (`future_trait` lang item).
+    /// Checks that particular type `ty` implements `std::future::Future`.
     /// This function is used in `.await` syntax completion.
     pub fn impls_future(&self, db: &impl HirDatabase, ty: Ty) -> bool {
-        let krate = self.resolver.krate();
-        if let Some(krate) = krate {
-            let future_trait = match db.lang_item(krate, "future_trait".into()) {
-                Some(LangItemTarget::Trait(t)) => t,
-                _ => return false,
+        let std_future_path = Path {
+            kind: PathKind::Abs,
+            segments: vec![
+                PathSegment { name: name::STD, args_and_bindings: None },
+                PathSegment { name: name::FUTURE_MOD, args_and_bindings: None },
+                PathSegment { name: name::FUTURE_TYPE, args_and_bindings: None },
+            ],
+        };
+
+        let std_future_trait =
+            match self.resolver.resolve_path_segments(db, &std_future_path).into_fully_resolved() {
+                PerNs { types: Some(Resolution::Def(ModuleDef::Trait(trait_))), .. } => {
+                    Some(trait_)
+                }
+                _ => None,
             };
 
-            let canonical_ty = crate::ty::Canonical { value: ty, num_vars: 0 };
-            return implements_trait(&canonical_ty, db, &self.resolver, krate, future_trait);
+        let krate = self.resolver.krate();
+        if let Some(krate) = krate {
+            if let Some(trait_) = std_future_trait {
+                let canonical_ty = crate::ty::Canonical { value: ty, num_vars: 0 };
+                return implements_trait(&canonical_ty, db, &self.resolver, krate, trait_);
+            }
         }
 
         false
index e8fd37bca465fc300e594fcf7e53ed9a9a4dcc8c..9a3b353a92602a8453dd9619af830fc1dd6a7700 100644 (file)
@@ -425,28 +425,25 @@ fn test_completion_await_impls_future() {
         assert_debug_snapshot_matches!(
         do_completion(
             r###"
-            // Mock Future trait from stdlib
-            pub mod std {
-                pub mod future {
-                    #[lang = "future_trait"]
-                    pub trait Future {}
-                }
-            }
-
+            //- /main.rs
             use std::future::*;
             struct A {}
             impl Future for A {}
-
             fn foo(a: A) {
                 a.<|>
             }
+
+            //- /std/lib.rs
+            pub mod future {
+                pub trait Future {}
+            }
             "###, CompletionKind::Keyword),
         @r###"
        ⋮[
        ⋮    CompletionItem {
        ⋮        label: "await",
-       ⋮        source_range: [358; 358),
-       ⋮        delete: [358; 358),
+       ⋮        source_range: [74; 74),
+       ⋮        delete: [74; 74),
        ⋮        insert: "await",
        ⋮        detail: "expr.await",
        ⋮    },