Skip to content

Commit 8efa635

Browse files
committed
Auto merge of rust-lang#109069 - lnicola:rust-analyzer-2023-03-13, r=lnicola
⬆️ `rust-analyzer` r? `@ghost`
2 parents cf8d98b + e862901 commit 8efa635

File tree

217 files changed

+12635
-3055
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

217 files changed

+12635
-3055
lines changed

src/tools/rust-analyzer/Cargo.lock

+2
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ dependencies = [
572572
"chalk-recursive",
573573
"chalk-solve",
574574
"cov-mark",
575+
"either",
575576
"ena",
576577
"expect-test",
577578
"hir-def",
@@ -1714,6 +1715,7 @@ name = "syntax"
17141715
version = "0.0.0"
17151716
dependencies = [
17161717
"cov-mark",
1718+
"either",
17171719
"expect-test",
17181720
"indexmap",
17191721
"itertools",

src/tools/rust-analyzer/crates/flycheck/src/lib.rs

+67-42
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl fmt::Display for FlycheckConfig {
7676
#[derive(Debug)]
7777
pub struct FlycheckHandle {
7878
// XXX: drop order is significant
79-
sender: Sender<Restart>,
79+
sender: Sender<StateChange>,
8080
_thread: jod_thread::JoinHandle,
8181
id: usize,
8282
}
@@ -89,7 +89,7 @@ impl FlycheckHandle {
8989
workspace_root: AbsPathBuf,
9090
) -> FlycheckHandle {
9191
let actor = FlycheckActor::new(id, sender, config, workspace_root);
92-
let (sender, receiver) = unbounded::<Restart>();
92+
let (sender, receiver) = unbounded::<StateChange>();
9393
let thread = jod_thread::Builder::new()
9494
.name("Flycheck".to_owned())
9595
.spawn(move || actor.run(receiver))
@@ -99,12 +99,12 @@ impl FlycheckHandle {
9999

100100
/// Schedule a re-start of the cargo check worker.
101101
pub fn restart(&self) {
102-
self.sender.send(Restart::Yes).unwrap();
102+
self.sender.send(StateChange::Restart).unwrap();
103103
}
104104

105105
/// Stop this cargo check worker.
106106
pub fn cancel(&self) {
107-
self.sender.send(Restart::No).unwrap();
107+
self.sender.send(StateChange::Cancel).unwrap();
108108
}
109109

110110
pub fn id(&self) -> usize {
@@ -149,9 +149,9 @@ pub enum Progress {
149149
DidFailToRestart(String),
150150
}
151151

152-
enum Restart {
153-
Yes,
154-
No,
152+
enum StateChange {
153+
Restart,
154+
Cancel,
155155
}
156156

157157
/// A [`FlycheckActor`] is a single check instance of a workspace.
@@ -172,7 +172,7 @@ struct FlycheckActor {
172172
}
173173

174174
enum Event {
175-
Restart(Restart),
175+
RequestStateChange(StateChange),
176176
CheckEvent(Option<CargoMessage>),
177177
}
178178

@@ -191,30 +191,31 @@ impl FlycheckActor {
191191
self.send(Message::Progress { id: self.id, progress });
192192
}
193193

194-
fn next_event(&self, inbox: &Receiver<Restart>) -> Option<Event> {
194+
fn next_event(&self, inbox: &Receiver<StateChange>) -> Option<Event> {
195195
let check_chan = self.cargo_handle.as_ref().map(|cargo| &cargo.receiver);
196196
if let Ok(msg) = inbox.try_recv() {
197197
// give restarts a preference so check outputs don't block a restart or stop
198-
return Some(Event::Restart(msg));
198+
return Some(Event::RequestStateChange(msg));
199199
}
200200
select! {
201-
recv(inbox) -> msg => msg.ok().map(Event::Restart),
201+
recv(inbox) -> msg => msg.ok().map(Event::RequestStateChange),
202202
recv(check_chan.unwrap_or(&never())) -> msg => Some(Event::CheckEvent(msg.ok())),
203203
}
204204
}
205205

206-
fn run(mut self, inbox: Receiver<Restart>) {
206+
fn run(mut self, inbox: Receiver<StateChange>) {
207207
'event: while let Some(event) = self.next_event(&inbox) {
208208
match event {
209-
Event::Restart(Restart::No) => {
209+
Event::RequestStateChange(StateChange::Cancel) => {
210+
tracing::debug!(flycheck_id = self.id, "flycheck cancelled");
210211
self.cancel_check_process();
211212
}
212-
Event::Restart(Restart::Yes) => {
213+
Event::RequestStateChange(StateChange::Restart) => {
213214
// Cancel the previously spawned process
214215
self.cancel_check_process();
215216
while let Ok(restart) = inbox.recv_timeout(Duration::from_millis(50)) {
216217
// restart chained with a stop, so just cancel
217-
if let Restart::No = restart {
218+
if let StateChange::Cancel = restart {
218219
continue 'event;
219220
}
220221
}
@@ -255,10 +256,20 @@ impl FlycheckActor {
255256
}
256257
Event::CheckEvent(Some(message)) => match message {
257258
CargoMessage::CompilerArtifact(msg) => {
259+
tracing::trace!(
260+
flycheck_id = self.id,
261+
artifact = msg.target.name,
262+
"artifact received"
263+
);
258264
self.report_progress(Progress::DidCheckCrate(msg.target.name));
259265
}
260266

261267
CargoMessage::Diagnostic(msg) => {
268+
tracing::trace!(
269+
flycheck_id = self.id,
270+
message = msg.message,
271+
"diagnostic received"
272+
);
262273
self.send(Message::AddDiagnostic {
263274
id: self.id,
264275
workspace_root: self.root.clone(),
@@ -445,42 +456,56 @@ impl CargoActor {
445456
// simply skip a line if it doesn't parse, which just ignores any
446457
// erroneous output.
447458

448-
let mut error = String::new();
449-
let mut read_at_least_one_message = false;
459+
let mut stdout_errors = String::new();
460+
let mut stderr_errors = String::new();
461+
let mut read_at_least_one_stdout_message = false;
462+
let mut read_at_least_one_stderr_message = false;
463+
let process_line = |line: &str, error: &mut String| {
464+
// Try to deserialize a message from Cargo or Rustc.
465+
let mut deserializer = serde_json::Deserializer::from_str(line);
466+
deserializer.disable_recursion_limit();
467+
if let Ok(message) = JsonMessage::deserialize(&mut deserializer) {
468+
match message {
469+
// Skip certain kinds of messages to only spend time on what's useful
470+
JsonMessage::Cargo(message) => match message {
471+
cargo_metadata::Message::CompilerArtifact(artifact) if !artifact.fresh => {
472+
self.sender.send(CargoMessage::CompilerArtifact(artifact)).unwrap();
473+
}
474+
cargo_metadata::Message::CompilerMessage(msg) => {
475+
self.sender.send(CargoMessage::Diagnostic(msg.message)).unwrap();
476+
}
477+
_ => (),
478+
},
479+
JsonMessage::Rustc(message) => {
480+
self.sender.send(CargoMessage::Diagnostic(message)).unwrap();
481+
}
482+
}
483+
return true;
484+
}
485+
486+
error.push_str(line);
487+
error.push('\n');
488+
return false;
489+
};
450490
let output = streaming_output(
451491
self.stdout,
452492
self.stderr,
453493
&mut |line| {
454-
read_at_least_one_message = true;
455-
456-
// Try to deserialize a message from Cargo or Rustc.
457-
let mut deserializer = serde_json::Deserializer::from_str(line);
458-
deserializer.disable_recursion_limit();
459-
if let Ok(message) = JsonMessage::deserialize(&mut deserializer) {
460-
match message {
461-
// Skip certain kinds of messages to only spend time on what's useful
462-
JsonMessage::Cargo(message) => match message {
463-
cargo_metadata::Message::CompilerArtifact(artifact)
464-
if !artifact.fresh =>
465-
{
466-
self.sender.send(CargoMessage::CompilerArtifact(artifact)).unwrap();
467-
}
468-
cargo_metadata::Message::CompilerMessage(msg) => {
469-
self.sender.send(CargoMessage::Diagnostic(msg.message)).unwrap();
470-
}
471-
_ => (),
472-
},
473-
JsonMessage::Rustc(message) => {
474-
self.sender.send(CargoMessage::Diagnostic(message)).unwrap();
475-
}
476-
}
494+
if process_line(line, &mut stdout_errors) {
495+
read_at_least_one_stdout_message = true;
477496
}
478497
},
479498
&mut |line| {
480-
error.push_str(line);
481-
error.push('\n');
499+
if process_line(line, &mut stderr_errors) {
500+
read_at_least_one_stderr_message = true;
501+
}
482502
},
483503
);
504+
505+
let read_at_least_one_message =
506+
read_at_least_one_stdout_message || read_at_least_one_stderr_message;
507+
let mut error = stdout_errors;
508+
error.push_str(&stderr_errors);
484509
match output {
485510
Ok(_) => Ok((read_at_least_one_message, error)),
486511
Err(e) => Err(io::Error::new(e.kind(), format!("{e:?}: {error}"))),

src/tools/rust-analyzer/crates/hir-def/src/attr.rs

+12-36
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ impl AttrsWithOwner {
300300
AdtId::UnionId(it) => attrs_from_item_tree(it.lookup(db).id, db),
301301
},
302302
AttrDefId::TraitId(it) => attrs_from_item_tree(it.lookup(db).id, db),
303+
AttrDefId::TraitAliasId(it) => attrs_from_item_tree(it.lookup(db).id, db),
303304
AttrDefId::MacroId(it) => match it {
304305
MacroId::Macro2Id(it) => attrs_from_item_tree(it.lookup(db).id, db),
305306
MacroId::MacroRulesId(it) => attrs_from_item_tree(it.lookup(db).id, db),
@@ -315,26 +316,14 @@ impl AttrsWithOwner {
315316
let src = it.parent().child_source(db);
316317
RawAttrs::from_attrs_owner(
317318
db.upcast(),
318-
src.with_value(src.value[it.local_id()].as_ref().either(
319-
|it| match it {
320-
ast::TypeOrConstParam::Type(it) => it as _,
321-
ast::TypeOrConstParam::Const(it) => it as _,
322-
},
323-
|it| it as _,
324-
)),
319+
src.with_value(&src.value[it.local_id()]),
325320
)
326321
}
327322
GenericParamId::TypeParamId(it) => {
328323
let src = it.parent().child_source(db);
329324
RawAttrs::from_attrs_owner(
330325
db.upcast(),
331-
src.with_value(src.value[it.local_id()].as_ref().either(
332-
|it| match it {
333-
ast::TypeOrConstParam::Type(it) => it as _,
334-
ast::TypeOrConstParam::Const(it) => it as _,
335-
},
336-
|it| it as _,
337-
)),
326+
src.with_value(&src.value[it.local_id()]),
338327
)
339328
}
340329
GenericParamId::LifetimeParamId(it) => {
@@ -404,6 +393,7 @@ impl AttrsWithOwner {
404393
AttrDefId::StaticId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
405394
AttrDefId::ConstId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
406395
AttrDefId::TraitId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
396+
AttrDefId::TraitAliasId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
407397
AttrDefId::TypeAliasId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
408398
AttrDefId::MacroId(id) => match id {
409399
MacroId::Macro2Id(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
@@ -412,28 +402,14 @@ impl AttrsWithOwner {
412402
},
413403
AttrDefId::ImplId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
414404
AttrDefId::GenericParamId(id) => match id {
415-
GenericParamId::ConstParamId(id) => {
416-
id.parent().child_source(db).map(|source| match &source[id.local_id()] {
417-
Either::Left(ast::TypeOrConstParam::Type(id)) => {
418-
ast::AnyHasAttrs::new(id.clone())
419-
}
420-
Either::Left(ast::TypeOrConstParam::Const(id)) => {
421-
ast::AnyHasAttrs::new(id.clone())
422-
}
423-
Either::Right(id) => ast::AnyHasAttrs::new(id.clone()),
424-
})
425-
}
426-
GenericParamId::TypeParamId(id) => {
427-
id.parent().child_source(db).map(|source| match &source[id.local_id()] {
428-
Either::Left(ast::TypeOrConstParam::Type(id)) => {
429-
ast::AnyHasAttrs::new(id.clone())
430-
}
431-
Either::Left(ast::TypeOrConstParam::Const(id)) => {
432-
ast::AnyHasAttrs::new(id.clone())
433-
}
434-
Either::Right(id) => ast::AnyHasAttrs::new(id.clone()),
435-
})
436-
}
405+
GenericParamId::ConstParamId(id) => id
406+
.parent()
407+
.child_source(db)
408+
.map(|source| ast::AnyHasAttrs::new(source[id.local_id()].clone())),
409+
GenericParamId::TypeParamId(id) => id
410+
.parent()
411+
.child_source(db)
412+
.map(|source| ast::AnyHasAttrs::new(source[id.local_id()].clone())),
437413
GenericParamId::LifetimeParamId(id) => id
438414
.parent
439415
.child_source(db)

src/tools/rust-analyzer/crates/hir-def/src/body.rs

+14-17
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use syntax::{ast, AstPtr, SyntaxNode, SyntaxNodePtr};
2424
use crate::{
2525
attr::Attrs,
2626
db::DefDatabase,
27-
expr::{dummy_expr_id, Expr, ExprId, Label, LabelId, Pat, PatId},
27+
expr::{dummy_expr_id, Binding, BindingId, Expr, ExprId, Label, LabelId, Pat, PatId},
2828
item_scope::BuiltinShadowMode,
2929
macro_id_to_def_id,
3030
nameres::DefMap,
@@ -270,7 +270,7 @@ pub struct Mark {
270270
pub struct Body {
271271
pub exprs: Arena<Expr>,
272272
pub pats: Arena<Pat>,
273-
pub or_pats: FxHashMap<PatId, Arc<[PatId]>>,
273+
pub bindings: Arena<Binding>,
274274
pub labels: Arena<Label>,
275275
/// The patterns for the function's parameters. While the parameter types are
276276
/// part of the function signature, the patterns are not (they don't change
@@ -409,18 +409,6 @@ impl Body {
409409
.map(move |&block| (block, db.block_def_map(block).expect("block ID without DefMap")))
410410
}
411411

412-
pub fn pattern_representative(&self, pat: PatId) -> PatId {
413-
self.or_pats.get(&pat).and_then(|pats| pats.first().copied()).unwrap_or(pat)
414-
}
415-
416-
/// Retrieves all ident patterns this pattern shares the ident with.
417-
pub fn ident_patterns_for<'slf>(&'slf self, pat: &'slf PatId) -> &'slf [PatId] {
418-
match self.or_pats.get(pat) {
419-
Some(pats) => pats,
420-
None => std::slice::from_ref(pat),
421-
}
422-
}
423-
424412
pub fn pretty_print(&self, db: &dyn DefDatabase, owner: DefWithBodyId) -> String {
425413
pretty::print_body_hir(db, self, owner)
426414
}
@@ -435,13 +423,14 @@ impl Body {
435423
}
436424

437425
fn shrink_to_fit(&mut self) {
438-
let Self { _c: _, body_expr: _, block_scopes, or_pats, exprs, labels, params, pats } = self;
426+
let Self { _c: _, body_expr: _, block_scopes, exprs, labels, params, pats, bindings } =
427+
self;
439428
block_scopes.shrink_to_fit();
440-
or_pats.shrink_to_fit();
441429
exprs.shrink_to_fit();
442430
labels.shrink_to_fit();
443431
params.shrink_to_fit();
444432
pats.shrink_to_fit();
433+
bindings.shrink_to_fit();
445434
}
446435
}
447436

@@ -451,7 +440,7 @@ impl Default for Body {
451440
body_expr: dummy_expr_id(),
452441
exprs: Default::default(),
453442
pats: Default::default(),
454-
or_pats: Default::default(),
443+
bindings: Default::default(),
455444
labels: Default::default(),
456445
params: Default::default(),
457446
block_scopes: Default::default(),
@@ -484,6 +473,14 @@ impl Index<LabelId> for Body {
484473
}
485474
}
486475

476+
impl Index<BindingId> for Body {
477+
type Output = Binding;
478+
479+
fn index(&self, b: BindingId) -> &Binding {
480+
&self.bindings[b]
481+
}
482+
}
483+
487484
// FIXME: Change `node_` prefix to something more reasonable.
488485
// Perhaps `expr_syntax` and `expr_id`?
489486
impl BodySourceMap {

0 commit comments

Comments
 (0)