Repository: rust-analyzer/rust-analyzer
Pull request #6367 (2020-10-26 15:04:54)

Handle #![cfg] in crate root
Now we correctly skip analysis of winapi on non-Windows platforms.

bors r+ :robot:


Patch:
diff --git a/Cargo.lock b/Cargo.lock
index fe211b9b30..5c7bcb17b0 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -852,9 +852,9 @@ dependencies = [
 
 [[package]]
 name = "lsp-types"
-version = "0.83.0"
+version = "0.82.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "25e0bd4b95038f2c23bda332ba0ca684e8dda765db1f9bdb63dc4c3e01f3b456"
+checksum = "db895abb8527cf59e3de893ab2acf52cf904faeb65e60ea6f373e11fe86464e8"
 dependencies = [
  "base64",
  "bitflags",
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs
index f30172d905..1ff45d2446 100644
--- a/crates/hir_def/src/nameres/collector.rs
+++ b/crates/hir_def/src/nameres/collector.rs
@@ -218,15 +218,18 @@ impl DefCollector<'_> {
         let item_tree = self.db.item_tree(file_id.into());
         let module_id = self.def_map.root;
         self.def_map.modules[module_id].origin = ModuleOrigin::CrateRoot { definition: file_id };
-        ModCollector {
+        let mut root_collector = ModCollector {
             def_collector: &mut *self,
             macro_depth: 0,
             module_id,
             file_id: file_id.into(),
             item_tree: &item_tree,
             mod_dir: ModDir::root(),
+        };
+        if item_tree.top_level_attrs().cfg().map_or(true, |cfg| root_collector.is_cfg_enabled(&cfg))
+        {
+            root_collector.collect(item_tree.top_level_items());
         }
-        .collect(item_tree.top_level_items());
 
         // main name resolution fixed-point loop.
         let mut i = 0;
diff --git a/crates/rust-analyzer/Cargo.toml b/crates/rust-analyzer/Cargo.toml
index d25c4bf837..975b24aaf1 100644
--- a/crates/rust-analyzer/Cargo.toml
+++ b/crates/rust-analyzer/Cargo.toml
@@ -21,7 +21,7 @@ env_logger = { version = "0.8.1", default-features = false }
 itertools = "0.9.0"
 jod-thread = "0.1.0"
 log = "0.4.8"
-lsp-types = { version = "0.83.0", features = ["proposed"] }
+lsp-types = { version = "0.82.0", features = ["proposed"] }
 parking_lot = "0.11.0"
 pico-args = "0.3.1"
 oorandom = "11.1.2"
diff --git a/crates/rust-analyzer/src/caps.rs b/crates/rust-analyzer/src/caps.rs
index ff1ae9575f..c589afeafc 100644
--- a/crates/rust-analyzer/src/caps.rs
+++ b/crates/rust-analyzer/src/caps.rs
@@ -48,7 +48,7 @@ pub fn server_capabilities(client_caps: &ClientCapabilities) -> ServerCapabiliti
         references_provider: Some(OneOf::Left(true)),
         document_highlight_provider: Some(OneOf::Left(true)),
         document_symbol_provider: Some(OneOf::Left(true)),
-        workspace_symbol_provider: Some(OneOf::Left(true)),
+        workspace_symbol_provider: Some(true),
         code_action_provider: Some(code_action_provider),
         code_lens_provider: Some(CodeLensOptions { resolve_provider: Some(true) }),
         document_formatting_provider: Some(OneOf::Left(true)),
@@ -113,7 +113,6 @@ fn code_action_capabilities(client_caps: &ClientCapabilities) -> CodeActionProvi
                     CodeActionKind::REFACTOR_INLINE,
                     CodeActionKind::REFACTOR_REWRITE,
                 ]),
-                resolve_provider: None,
                 work_done_progress_options: Default::default(),
             })
         })
diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs
index 2ed6a0d82e..1b9b24698a 100644
--- a/crates/rust-analyzer/src/config.rs
+++ b/crates/rust-analyzer/src/config.rs
@@ -47,7 +47,6 @@ pub struct Config {
     pub call_info_full: bool,
     pub lens: LensConfig,
     pub hover: HoverConfig,
-    pub semantic_tokens_refresh: bool,
 
     pub with_sysroot: bool,
     pub linked_projects: Vec<LinkedProject>,
@@ -194,7 +193,6 @@ impl Config {
             call_info_full: true,
             lens: LensConfig::default(),
             hover: HoverConfig::default(),
-            semantic_tokens_refresh: false,
             linked_projects: Vec::new(),
             root_path,
         }
@@ -404,14 +402,6 @@ impl Config {
             self.client_caps.hover_actions = get_bool("hoverActions");
             self.client_caps.status_notification = get_bool("statusNotification");
         }
-
-        if let Some(workspace_caps) = caps.workspace.as_ref() {
-            if let Some(refresh_support) =
-                workspace_caps.semantic_tokens.as_ref().and_then(|it| it.refresh_support)
-            {
-                self.semantic_tokens_refresh = refresh_support;
-            }
-        }
     }
 }
 
diff --git a/crates/rust-analyzer/src/diagnostics/to_proto.rs b/crates/rust-analyzer/src/diagnostics/to_proto.rs
index b949577c13..33606edda4 100644
--- a/crates/rust-analyzer/src/diagnostics/to_proto.rs
+++ b/crates/rust-analyzer/src/diagnostics/to_proto.rs
@@ -248,12 +248,10 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
                         range: in_macro_location.range,
                         severity,
                         code: code.clone().map(lsp_types::NumberOrString::String),
-                        code_description: None,
                         source: Some(source.clone()),
                         message: message.clone(),
                         related_information: Some(information_for_additional_diagnostic),
                         tags: if tags.is_empty() { None } else { Some(tags.clone()) },
-                        data: None,
                     };
 
                     Some(MappedRustDiagnostic {
@@ -269,7 +267,6 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
                 range: location.range,
                 severity,
                 code: code.clone().map(lsp_types::NumberOrString::String),
-                code_description: None,
                 source: Some(source.clone()),
                 message,
                 related_information: if related_information.is_empty() {
@@ -278,7 +275,6 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
                     Some(related_information.clone())
                 },
                 tags: if tags.is_empty() { None } else { Some(tags.clone()) },
-                data: None,
             };
 
             let main_diagnostic =
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs
index 049c583a42..2680e5f08b 100644
--- a/crates/rust-analyzer/src/handlers.rs
+++ b/crates/rust-analyzer/src/handlers.rs
@@ -1121,12 +1121,10 @@ pub(crate) fn publish_diagnostics(
             range: to_proto::range(&line_index, d.range),
             severity: Some(to_proto::diagnostic_severity(d.severity)),
             code: None,
-            code_description: None,
             source: Some("rust-analyzer".to_string()),
             message: d.message,
             related_information: None,
             tags: if d.unused { Some(vec![DiagnosticTag::Unnecessary]) } else { None },
-            data: None,
         })
         .collect();
     Ok(diagnostics)
diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs
index ff855fe1a6..ed52927330 100644
--- a/crates/rust-analyzer/src/main_loop.rs
+++ b/crates/rust-analyzer/src/main_loop.rs
@@ -330,12 +330,6 @@ impl GlobalState {
                 .collect::<Vec<_>>();
 
             self.update_file_notifications_on_threadpool(subscriptions);
-
-            // Refresh semantic tokens if the client supports it.
-            if self.config.semantic_tokens_refresh {
-                self.semantic_tokens_cache.lock().clear();
-                self.send_request::<lsp_types::request::SemanticTokensRefesh>((), |_, _| ());
-            }
         }
 
         if let Some(diagnostic_changes) = self.diagnostics.take_changes() {
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs
index 92b7c7b681..24a658fc65 100644
--- a/crates/rust-analyzer/src/to_proto.rs
+++ b/crates/rust-analyzer/src/to_proto.rs
@@ -709,16 +709,7 @@ pub(crate) fn call_hierarchy_item(
     let detail = target.description.clone();
     let kind = symbol_kind(target.kind);
     let (uri, range, selection_range) = location_info(snap, target)?;
-    Ok(lsp_types::CallHierarchyItem {
-        name,
-        kind,
-        tags: None,
-        detail,
-        uri,
-        range,
-        selection_range,
-        data: None,
-    })
+    Ok(lsp_types::CallHierarchyItem { name, kind, tags: None, detail, uri, range, selection_range })
 }
 
 pub(crate) fn code_action_kind(kind: AssistKind) -> lsp_types::CodeActionKind {
diff --git a/editors/code/package-lock.json b/editors/code/package-lock.json
index 83ef00058e..33d60d4ddf 100644
--- a/editors/code/package-lock.json
+++ b/editors/code/package-lock.json
@@ -2409,32 +2409,32 @@
             }
         },
         "vscode-jsonrpc": {
-            "version": "6.0.0-next.7",
-            "resolved": "https://registry.npmjs.org/vscode-jsonrpc/-/vscode-jsonrpc-6.0.0-next.7.tgz",
-            "integrity": "sha512-1nG+6cuTtpzmXe7yYfO9GCkYlyV6Ai+jDnwidHiT2T7zhc+bJM+VTtc0T/CdTlDyTNTqIcCj0V1nD4TcVjJ7Ug=="
+            "version": "6.0.0-next.5",
+            "resolved": "https://registry.npmjs.org/vscode-jsonrpc/-/vscode-jsonrpc-6.0.0-next.5.tgz",
+            "integrity": "sha512-IAgsltQPwg/pXOPsdXgbUTCaO9VSKZwirZN5SGtkdYQ/R3VjeC4v00WTVvoNayWMZpoC3O9u0ogqmsKzKhVasQ=="
         },
         "vscode-languageclient": {
-            "version": "7.0.0-next.12",
-            "resolved": "https://registry.npmjs.org/vscode-languageclient/-/vscode-languageclient-7.0.0-next.12.tgz",
-            "integrity": "sha512-OrzvOvhS5o26C0KctTJC7hkwh3avCwkVhllzy42AqwpIUZ3p2aVqkSG2uVxaeodq8ThBb3TLgtg50vxyWs6FEg==",
+            "version": "7.0.0-next.9",
+            "resolved": "https://registry.npmjs.org/vscode-languageclient/-/vscode-languageclient-7.0.0-next.9.tgz",
+            "integrity": "sha512-lFO+rN/i72CM2va6iKXq1lD7pJg8J93KEXf0w0boWVqU+DJhWzLrV3pXl8Xk1nCv//qOAyhlc/nx2KZCTeRF/A==",
             "requires": {
                 "semver": "^6.3.0",
-                "vscode-languageserver-protocol": "3.16.0-next.10"
+                "vscode-languageserver-protocol": "3.16.0-next.7"
             }
         },
         "vscode-languageserver-protocol": {
-            "version": "3.16.0-next.10",
-            "resolved": "https://registry.npmjs.org/vscode-languageserver-protocol/-/vscode-languageserver-protocol-3.16.0-next.10.tgz",
-            "integrity": "sha512-YRTctHUZvts0Z1xXKNYU0ha0o+Tlgtwr+6O8OmDquM086N8exiSKBMwMC+Ra1QtIE+1mfW43Wxsme2FnMkAS9A==",
+            "version": "3.16.0-next.7",
+            "resolved": "https://registry.npmjs.org/vscode-languageserver-protocol/-/vscode-languageserver-protocol-3.16.0-next.7.tgz",
+            "integrity": "sha512-tOjrg+K3RddJ547zpC9/LAgTbzadkPuHlqJFFWIcKjVhiJOh73XyY+Ngcu9wukGaTsuSGjJ0W8rlmwanixa0FQ==",
             "requires": {
-                "vscode-jsonrpc": "6.0.0-next.7",
-                "vscode-languageserver-types": "3.16.0-next.4"
+                "vscode-jsonrpc": "6.0.0-next.5",
+                "vscode-languageserver-types": "3.16.0-next.3"
             }
         },
         "vscode-languageserver-types": {
-            "version": "3.16.0-next.4",
-            "resolved": "https://registry.npmjs.org/vscode-languageserver-types/-/vscode-languageserver-types-3.16.0-next.4.tgz",
-            "integrity": "sha512-NlKJyGcET/ZBCCLBYIPaGo2c37R03bPYeWXozUtnjyye7+9dhlbMSODyoG2INcQf8zFmB4qhm2UOJjgYEgPCNA=="
+            "version": "3.16.0-next.3",
+            "resolved": "https://registry.npmjs.org/vscode-languageserver-types/-/vscode-languageserver-types-3.16.0-next.3.tgz",
+            "integrity": "sha512-s/z5ZqSe7VpoXJ6JQcvwRiPPA3nG0nAcJ/HH03zoU6QaFfnkcgPK+HshC3WKPPnC2G08xA0iRB6h7kmyBB5Adg=="
         },
         "vscode-test": {
             "version": "1.4.0",
diff --git a/editors/code/package.json b/editors/code/package.json
index eccafccdd9..af845d7bc4 100644
--- a/editors/code/package.json
+++ b/editors/code/package.json
@@ -36,7 +36,7 @@
     },
     "dependencies": {
         "node-fetch": "^2.6.1",
-        "vscode-languageclient": "7.0.0-next.12"
+        "vscode-languageclient": "7.0.0-next.9"
     },
     "devDependencies": {
         "@rollup/plugin-commonjs": "^13.0.2",
diff --git a/editors/code/src/util.ts b/editors/code/src/util.ts
index 53492a445c..08159b43c3 100644
--- a/editors/code/src/util.ts
+++ b/editors/code/src/util.ts
@@ -75,11 +75,12 @@ export async function sendRequestWithRetry<TParam, TRet>(
                 log.warn("LSP request timed out", { method: reqType.method, param, error });
                 throw error;
             }
-            if (error.code === lc.LSPErrorCodes.RequestCancelled) {
+
+            if (error.code === lc.ErrorCodes.RequestCancelled) {
                 throw error;
             }
 
-            if (error.code !== lc.LSPErrorCodes.ContentModified) {
+            if (error.code !== lc.ErrorCodes.ContentModified) {
                 log.warn("LSP request failed", { method: reqType.method, param, error });
                 throw error;
             }


Test patch:
diff --git a/crates/hir_def/src/nameres/tests.rs b/crates/hir_def/src/nameres/tests.rs
index 11d84f8082..9c19bf572f 100644
--- a/crates/hir_def/src/nameres/tests.rs
+++ b/crates/hir_def/src/nameres/tests.rs
@@ -691,3 +691,20 @@ mod tr {
         "#]],
     );
 }
+
+#[test]
+fn cfg_the_entire_crate() {
+    check(
+        r#"
+//- /main.rs
+#![cfg(never)]
+
+pub struct S;
+pub enum E {}
+pub fn f() {}
+    "#,
+        expect![[r#"
+            crate
+        "#]],
+    );
+}
diff --git a/crates/rust-analyzer/src/diagnostics/test_data/clippy_pass_by_ref.txt b/crates/rust-analyzer/src/diagnostics/test_data/clippy_pass_by_ref.txt
index 58d47d32a4..d065171260 100644
--- a/crates/rust-analyzer/src/diagnostics/test_data/clippy_pass_by_ref.txt
+++ b/crates/rust-analyzer/src/diagnostics/test_data/clippy_pass_by_ref.txt
@@ -20,7 +20,6 @@
                     "trivially_copy_pass_by_ref",
                 ),
             ),
-            code_description: None,
             source: Some(
                 "clippy",
             ),
@@ -62,7 +61,6 @@
                 ],
             ),
             tags: None,
-            data: None,
         },
         fixes: [],
     },
diff --git a/crates/rust-analyzer/src/diagnostics/test_data/handles_macro_location.txt b/crates/rust-analyzer/src/diagnostics/test_data/handles_macro_location.txt
index 6aa26bf63a..f5de2f07f3 100644
--- a/crates/rust-analyzer/src/diagnostics/test_data/handles_macro_location.txt
+++ b/crates/rust-analyzer/src/diagnostics/test_data/handles_macro_location.txt
@@ -20,14 +20,12 @@
                     "E0277",
                 ),
             ),
-            code_description: None,
             source: Some(
                 "rustc",
             ),
             message: "can\'t compare `{integer}` with `&str`\nthe trait `std::cmp::PartialEq<&str>` is not implemented for `{integer}`",
             related_information: None,
             tags: None,
-            data: None,
         },
         fixes: [],
     },
diff --git a/crates/rust-analyzer/src/diagnostics/test_data/macro_compiler_error.txt b/crates/rust-analyzer/src/diagnostics/test_data/macro_compiler_error.txt
index 7aaffaba25..00e8da8a70 100644
--- a/crates/rust-analyzer/src/diagnostics/test_data/macro_compiler_error.txt
+++ b/crates/rust-analyzer/src/diagnostics/test_data/macro_compiler_error.txt
@@ -16,7 +16,6 @@
                 Error,
             ),
             code: None,
-            code_description: None,
             source: Some(
                 "rustc",
             ),
@@ -42,7 +41,6 @@
                 ],
             ),
             tags: None,
-            data: None,
         },
         fixes: [],
     },
@@ -63,7 +61,6 @@
                 Error,
             ),
             code: None,
-            code_description: None,
             source: Some(
                 "rustc",
             ),
@@ -89,7 +86,6 @@
                 ],
             ),
             tags: None,
-            data: None,
         },
         fixes: [],
     },
diff --git a/crates/rust-analyzer/src/diagnostics/test_data/rustc_incompatible_type_for_trait.txt b/crates/rust-analyzer/src/diagnostics/test_data/rustc_incompatible_type_for_trait.txt
index 5842134200..fc54440be2 100644
--- a/crates/rust-analyzer/src/diagnostics/test_data/rustc_incompatible_type_for_trait.txt
+++ b/crates/rust-analyzer/src/diagnostics/test_data/rustc_incompatible_type_for_trait.txt
@@ -20,14 +20,12 @@
                     "E0053",
                 ),
             ),
-            code_description: None,
             source: Some(
                 "rustc",
             ),
             message: "method `next` has an incompatible type for trait\nexpected type `fn(&mut ty::list_iter::ListIterator<\'list, M>) -> std::option::Option<&ty::Ref<M>>`\n   found type `fn(&ty::list_iter::ListIterator<\'list, M>) -> std::option::Option<&\'list ty::Ref<M>>`",
             related_information: None,
             tags: None,
-            data: None,
         },
         fixes: [],
     },
diff --git a/crates/rust-analyzer/src/diagnostics/test_data/rustc_mismatched_type.txt b/crates/rust-analyzer/src/diagnostics/test_data/rustc_mismatched_type.txt
index 2610e4e209..c269af218c 100644
--- a/crates/rust-analyzer/src/diagnostics/test_data/rustc_mismatched_type.txt
+++ b/crates/rust-analyzer/src/diagnostics/test_data/rustc_mismatched_type.txt
@@ -20,14 +20,12 @@
                     "E0308",
                 ),
             ),
-            code_description: None,
             source: Some(
                 "rustc",
             ),
             message: "mismatched types\nexpected usize, found u32",
             related_information: None,
             tags: None,
-            data: None,
         },
         fixes: [],
     },
diff --git a/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable.txt b/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable.txt
index 8dc53391e9..74d91bc77c 100644
--- a/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable.txt
+++ b/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable.txt
@@ -20,7 +20,6 @@
                     "unused_variables",
                 ),
             ),
-            code_description: None,
             source: Some(
                 "rustc",
             ),
@@ -31,7 +30,6 @@
                     Unnecessary,
                 ],
             ),
-            data: None,
         },
         fixes: [
             CodeAction {
diff --git a/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_hint.txt b/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_hint.txt
index c8703194c8..8a420c9495 100644
--- a/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_hint.txt
+++ b/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_hint.txt
@@ -20,7 +20,6 @@
                     "unused_variables",
                 ),
             ),
-            code_description: None,
             source: Some(
                 "rustc",
             ),
@@ -31,7 +30,6 @@
                     Unnecessary,
                 ],
             ),
-            data: None,
         },
         fixes: [
             CodeAction {
diff --git a/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_info.txt b/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_info.txt
index dc93227adf..79910660b7 100644
--- a/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_info.txt
+++ b/crates/rust-analyzer/src/diagnostics/test_data/rustc_unused_variable_as_info.txt
@@ -20,7 +20,6 @@
                     "unused_variables",
                 ),
             ),
-            code_description: None,
             source: Some(
                 "rustc",
             ),
@@ -31,7 +30,6 @@
                     Unnecessary,
                 ],
             ),
-            data: None,
         },
         fixes: [
             CodeAction {
diff --git a/crates/rust-analyzer/src/diagnostics/test_data/rustc_wrong_number_of_parameters.txt b/crates/rust-analyzer/src/diagnostics/test_data/rustc_wrong_number_of_parameters.txt
index ba1b98b335..efe37261db 100644
--- a/crates/rust-analyzer/src/diagnostics/test_data/rustc_wrong_number_of_parameters.txt
+++ b/crates/rust-analyzer/src/diagnostics/test_data/rustc_wrong_number_of_parameters.txt
@@ -20,7 +20,6 @@
                     "E0061",
                 ),
             ),
-            code_description: None,
             source: Some(
                 "rustc",
             ),
@@ -46,7 +45,6 @@
                 ],
             ),
             tags: None,
-            data: None,
         },
         fixes: [],
     },
diff --git a/crates/rust-analyzer/src/diagnostics/test_data/snap_multi_line_fix.txt b/crates/rust-analyzer/src/diagnostics/test_data/snap_multi_line_fix.txt
index 81f752672f..4f811ab64b 100644
--- a/crates/rust-analyzer/src/diagnostics/test_data/snap_multi_line_fix.txt
+++ b/crates/rust-analyzer/src/diagnostics/test_data/snap_multi_line_fix.txt
@@ -20,7 +20,6 @@
                     "let_and_return",
                 ),
             ),
-            code_description: None,
             source: Some(
                 "clippy",
             ),
@@ -46,7 +45,6 @@
                 ],
             ),
             tags: None,
-            data: None,
         },
         fixes: [
             CodeAction {