Skip to content

Commit

Permalink
Auto merge of #125887 - lnicola:sync-from-ra, r=lnicola
Browse files Browse the repository at this point in the history
Subtree update of `rust-analyzer`

r? `@ghost`
  • Loading branch information
bors committed Jun 2, 2024
2 parents 13e2d72 + 4717bdf commit 8bec878
Show file tree
Hide file tree
Showing 22 changed files with 1,715 additions and 191 deletions.
98 changes: 60 additions & 38 deletions src/tools/rust-analyzer/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,6 @@ dependencies = [
"tracing",
]

[[package]]
name = "command-group"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5080df6b0f0ecb76cab30808f00d937ba725cebe266a3da8cd89dff92f2a9916"
dependencies = [
"nix 0.26.4",
"winapi",
]

[[package]]
name = "countme"
version = "3.0.1"
Expand Down Expand Up @@ -292,7 +282,7 @@ version = "3.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "672465ae37dc1bc6380a6547a8883d5dd397b0f1faaad4f265726cc7042a5345"
dependencies = [
"nix 0.28.0",
"nix",
"windows-sys 0.52.0",
]

Expand Down Expand Up @@ -432,9 +422,9 @@ name = "flycheck"
version = "0.0.0"
dependencies = [
"cargo_metadata",
"command-group",
"crossbeam-channel",
"paths",
"process-wrap",
"rustc-hash",
"serde",
"serde_json",
Expand Down Expand Up @@ -1121,17 +1111,6 @@ dependencies = [
"windows-sys 0.48.0",
]

[[package]]
name = "nix"
version = "0.26.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "598beaf3cc6fdd9a5dfb1630c2800c7acd31df7aaf0f565796fba2b53ca1af1b"
dependencies = [
"bitflags 1.3.2",
"cfg-if",
"libc",
]

[[package]]
name = "nix"
version = "0.28.0"
Expand Down Expand Up @@ -1397,6 +1376,18 @@ dependencies = [
"unicode-ident",
]

[[package]]
name = "process-wrap"
version = "8.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38ee68ae331824036479c84060534b18254c864fa73366c58d86db3b7b811619"
dependencies = [
"indexmap",
"nix",
"tracing",
"windows",
]

[[package]]
name = "profile"
version = "0.0.0"
Expand Down Expand Up @@ -2375,35 +2366,66 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"

[[package]]
name = "winapi"
version = "0.3.9"
name = "winapi-util"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b"
dependencies = [
"windows-sys 0.52.0",
]

[[package]]
name = "windows"
version = "0.56.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
checksum = "1de69df01bdf1ead2f4ac895dc77c9351aefff65b2f3db429a343f9cbf05e132"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
"windows-core",
"windows-targets 0.52.5",
]

[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
name = "windows-core"
version = "0.56.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
checksum = "4698e52ed2d08f8658ab0c39512a7c00ee5fe2688c65f8c0a4f06750d729f2a6"
dependencies = [
"windows-implement",
"windows-interface",
"windows-result",
"windows-targets 0.52.5",
]

[[package]]
name = "winapi-util"
version = "0.1.8"
name = "windows-implement"
version = "0.56.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b"
checksum = "f6fc35f58ecd95a9b71c4f2329b911016e6bec66b3f2e6a4aad86bd2e99e2f9b"
dependencies = [
"windows-sys 0.52.0",
"proc-macro2",
"quote",
"syn",
]

[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
name = "windows-interface"
version = "0.56.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
checksum = "08990546bf4edef8f431fa6326e032865f27138718c587dc21bc0265bbcb57cc"
dependencies = [
"proc-macro2",
"quote",
"syn",
]

[[package]]
name = "windows-result"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "749f0da9cc72d82e600d8d2e44cadd0b9eedb9038f71a1c58556ac1c5791813b"
dependencies = [
"windows-targets 0.52.5",
]

[[package]]
name = "windows-sys"
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ chalk-solve = { version = "0.97.0", default-features = false }
chalk-ir = "0.97.0"
chalk-recursive = { version = "0.97.0", default-features = false }
chalk-derive = "0.97.0"
command-group = "2.0.1"
crossbeam-channel = "0.5.8"
dissimilar = "1.0.7"
dot = "0.1.4"
Expand All @@ -132,6 +131,7 @@ object = { version = "0.33.0", default-features = false, features = [
"macho",
"pe",
] }
process-wrap = { version = "8.0.2", features = ["std"] }
pulldown-cmark-to-cmark = "10.0.4"
pulldown-cmark = { version = "0.9.0", default-features = false }
rayon = "1.8.0"
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/crates/flycheck/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ tracing.workspace = true
rustc-hash.workspace = true
serde_json.workspace = true
serde.workspace = true
command-group.workspace = true
process-wrap.workspace = true

# local deps
paths.workspace = true
Expand Down
16 changes: 11 additions & 5 deletions src/tools/rust-analyzer/crates/flycheck/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use std::{
process::{ChildStderr, ChildStdout, Command, Stdio},
};

use command_group::{CommandGroup, GroupChild};
use crossbeam_channel::Sender;
use process_wrap::std::{StdChildWrapper, StdCommandWrap};
use stdx::process::streaming_output;

/// Cargo output is structured as a one JSON per line. This trait abstracts parsing one line of
Expand Down Expand Up @@ -85,7 +85,7 @@ impl<T: ParseFromLine> CargoActor<T> {
}
}

struct JodGroupChild(GroupChild);
struct JodGroupChild(Box<dyn StdChildWrapper>);

impl Drop for JodGroupChild {
fn drop(&mut self) {
Expand Down Expand Up @@ -119,14 +119,20 @@ impl<T> fmt::Debug for CommandHandle<T> {
impl<T: ParseFromLine> CommandHandle<T> {
pub(crate) fn spawn(mut command: Command, sender: Sender<T>) -> std::io::Result<Self> {
command.stdout(Stdio::piped()).stderr(Stdio::piped()).stdin(Stdio::null());
let mut child = command.group_spawn().map(JodGroupChild)?;

let program = command.get_program().into();
let arguments = command.get_args().map(|arg| arg.into()).collect::<Vec<OsString>>();
let current_dir = command.get_current_dir().map(|arg| arg.to_path_buf());

let stdout = child.0.inner().stdout.take().unwrap();
let stderr = child.0.inner().stderr.take().unwrap();
let mut child = StdCommandWrap::from(command);
#[cfg(unix)]
child.wrap(process_wrap::std::ProcessSession);
#[cfg(windows)]
child.wrap(process_wrap::std::JobObject);
let mut child = child.spawn().map(JodGroupChild)?;

let stdout = child.0.stdout().take().unwrap();
let stderr = child.0.stderr().take().unwrap();

let actor = CargoActor::<T>::new(sender, stdout, stderr);
let thread = stdx::thread::Builder::new(stdx::thread::ThreadIntent::Worker)
Expand Down
27 changes: 27 additions & 0 deletions src/tools/rust-analyzer/crates/flycheck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ pub enum Message {
/// Request adding a diagnostic with fixes included to a file
AddDiagnostic { id: usize, workspace_root: AbsPathBuf, diagnostic: Diagnostic },

/// Request clearing all previous diagnostics
ClearDiagnostics { id: usize },

/// Request check progress notification to client
Progress {
/// Flycheck instance ID
Expand All @@ -180,6 +183,9 @@ impl fmt::Debug for Message {
.field("workspace_root", workspace_root)
.field("diagnostic_code", &diagnostic.code.as_ref().map(|it| &it.code))
.finish(),
Message::ClearDiagnostics { id } => {
f.debug_struct("ClearDiagnostics").field("id", id).finish()
}
Message::Progress { id, progress } => {
f.debug_struct("Progress").field("id", id).field("progress", progress).finish()
}
Expand Down Expand Up @@ -220,13 +226,22 @@ struct FlycheckActor {
command_handle: Option<CommandHandle<CargoCheckMessage>>,
/// The receiver side of the channel mentioned above.
command_receiver: Option<Receiver<CargoCheckMessage>>,

status: FlycheckStatus,
}

enum Event {
RequestStateChange(StateChange),
CheckEvent(Option<CargoCheckMessage>),
}

#[derive(PartialEq)]
enum FlycheckStatus {
Started,
DiagnosticSent,
Finished,
}

const SAVED_FILE_PLACEHOLDER: &str = "$saved_file";

impl FlycheckActor {
Expand All @@ -248,6 +263,7 @@ impl FlycheckActor {
manifest_path,
command_handle: None,
command_receiver: None,
status: FlycheckStatus::Finished,
}
}

Expand Down Expand Up @@ -298,12 +314,14 @@ impl FlycheckActor {
self.command_handle = Some(command_handle);
self.command_receiver = Some(receiver);
self.report_progress(Progress::DidStart);
self.status = FlycheckStatus::Started;
}
Err(error) => {
self.report_progress(Progress::DidFailToRestart(format!(
"Failed to run the following command: {} error={}",
formatted_command, error
)));
self.status = FlycheckStatus::Finished;
}
}
}
Expand All @@ -323,7 +341,11 @@ impl FlycheckActor {
error
);
}
if self.status == FlycheckStatus::Started {
self.send(Message::ClearDiagnostics { id: self.id });
}
self.report_progress(Progress::DidFinish(res));
self.status = FlycheckStatus::Finished;
}
Event::CheckEvent(Some(message)) => match message {
CargoCheckMessage::CompilerArtifact(msg) => {
Expand All @@ -341,11 +363,15 @@ impl FlycheckActor {
message = msg.message,
"diagnostic received"
);
if self.status == FlycheckStatus::Started {
self.send(Message::ClearDiagnostics { id: self.id });
}
self.send(Message::AddDiagnostic {
id: self.id,
workspace_root: self.root.clone(),
diagnostic: msg,
});
self.status = FlycheckStatus::DiagnosticSent;
}
},
}
Expand All @@ -362,6 +388,7 @@ impl FlycheckActor {
);
command_handle.cancel();
self.report_progress(Progress::DidCancel);
self.status = FlycheckStatus::Finished;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/crates/hir-expand/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ pub fn expand_speculative(
// prefer tokens of the same kind and text
// Note the inversion of the score here, as we want to prefer the first token in case
// of all tokens having the same score
(t.kind() != token_to_map.kind()) as u8 + (t.text() != token_to_map.text()) as u8
(t.kind() != token_to_map.kind()) as u8 + 2 * ((t.text() != token_to_map.text()) as u8)
})?;
Some((node.syntax_node(), token))
}
Expand Down
22 changes: 9 additions & 13 deletions src/tools/rust-analyzer/crates/hir-expand/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,24 +153,20 @@ impl<FileId: Copy, N: AstNode> InFileWrapper<FileId, N> {
// region:specific impls

impl InFile<&SyntaxNode> {
/// Skips the attributed item that caused the macro invocation we are climbing up
pub fn ancestors_with_macros_skip_attr_item(
/// Traverse up macro calls and skips the macro invocation node
pub fn ancestors_with_macros(
self,
db: &dyn db::ExpandDatabase,
) -> impl Iterator<Item = InFile<SyntaxNode>> + '_ {
let succ = move |node: &InFile<SyntaxNode>| match node.value.parent() {
Some(parent) => Some(node.with_value(parent)),
None => {
let macro_file_id = node.file_id.macro_file()?;
let parent_node = macro_file_id.call_node(db);
if macro_file_id.is_attr_macro(db) {
// macro call was an attributed item, skip it
// FIXME: does this fail if this is a direct expansion of another macro?
parent_node.map(|node| node.parent()).transpose()
} else {
Some(parent_node)
}
}
None => db
.lookup_intern_macro_call(node.file_id.macro_file()?.macro_call_id)
.to_node_item(db)
.syntax()
.cloned()
.map(|node| node.parent())
.transpose(),
};
iter::successors(succ(&self.cloned()), succ)
}
Expand Down
16 changes: 14 additions & 2 deletions src/tools/rust-analyzer/crates/hir-expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ use std::{fmt, hash::Hash};
use base_db::{salsa::impl_intern_value_trivial, CrateId, FileId};
use either::Either;
use span::{
Edition, ErasedFileAstId, FileRange, HirFileIdRepr, Span, SpanAnchor, SyntaxContextData,
SyntaxContextId,
Edition, ErasedFileAstId, FileAstId, FileRange, HirFileIdRepr, Span, SpanAnchor,
SyntaxContextData, SyntaxContextId,
};
use syntax::{
ast::{self, AstNode},
Expand Down Expand Up @@ -546,6 +546,18 @@ impl MacroCallLoc {
}
}

pub fn to_node_item(&self, db: &dyn ExpandDatabase) -> InFile<ast::Item> {
match self.kind {
MacroCallKind::FnLike { ast_id, .. } => {
InFile::new(ast_id.file_id, ast_id.map(FileAstId::upcast).to_node(db))
}
MacroCallKind::Derive { ast_id, .. } => {
InFile::new(ast_id.file_id, ast_id.map(FileAstId::upcast).to_node(db))
}
MacroCallKind::Attr { ast_id, .. } => InFile::new(ast_id.file_id, ast_id.to_node(db)),
}
}

fn expand_to(&self) -> ExpandTo {
match self.kind {
MacroCallKind::FnLike { expand_to, .. } => expand_to,
Expand Down
Loading

0 comments on commit 8bec878

Please sign in to comment.