Skip to content

Commit 5d24303

Browse files
authored
Rollup merge of #149057 - lnicola:sync-from-ra, r=lnicola
`rust-analyzer` subtree update Subtree update of `rust-analyzer` to rust-lang/rust-analyzer@afcfe14. Created using https://github.com/rust-lang/josh-sync. r? `@ghost`
2 parents 642300e + 6da4a35 commit 5d24303

Some content is hidden

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

73 files changed

+5994
-408
lines changed

src/tools/rust-analyzer/Cargo.lock

Lines changed: 542 additions & 26 deletions
Large diffs are not rendered by default.

src/tools/rust-analyzer/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[workspace]
2-
members = ["xtask/", "lib/*", "crates/*"]
2+
members = ["xtask/", "lib/*", "lib/ungrammar/ungrammar2json", "crates/*"]
33
exclude = ["crates/proc-macro-srv/proc-macro-test/imp"]
44
resolver = "2"
55

@@ -42,7 +42,7 @@ debug = 2
4242
# lsp-server = { path = "lib/lsp-server" }
4343

4444

45-
# ungrammar = { path = "../ungrammar" }
45+
# ungrammar = { path = "lin/ungrammar" }
4646

4747
# salsa = { path = "../salsa" }
4848
# salsa-macros = { path = "../salsa/components/salsa-macros" }

src/tools/rust-analyzer/crates/hir-ty/src/infer/unify.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,10 +674,13 @@ impl<'db> InferenceTable<'db> {
674674
let args = [ty, arg_ty];
675675
let trait_ref = TraitRef::new(self.interner(), fn_trait.into(), args);
676676

677+
let proj_args = self
678+
.infer_ctxt
679+
.fill_rest_fresh_args(output_assoc_type.into(), args.into_iter().map(Into::into));
677680
let projection = Ty::new_alias(
678681
self.interner(),
679682
rustc_type_ir::AliasTyKind::Projection,
680-
AliasTy::new(self.interner(), output_assoc_type.into(), args),
683+
AliasTy::new(self.interner(), output_assoc_type.into(), proj_args),
681684
);
682685

683686
let pred = Predicate::upcast_from(trait_ref, self.interner());

src/tools/rust-analyzer/crates/hir-ty/src/tests/regression/new_solver.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,3 +524,31 @@ fn g(it: *const (dyn Trait)) {
524524
"#,
525525
);
526526
}
527+
528+
#[test]
529+
fn regression_20951() {
530+
check_infer(
531+
r#"
532+
//- minicore: async_fn
533+
trait DoesSomething {
534+
fn do_something(&self) -> impl Future<Output = usize>;
535+
}
536+
537+
impl<F> DoesSomething for F
538+
where
539+
F: AsyncFn() -> usize,
540+
{
541+
fn do_something(&self) -> impl Future<Output = usize> {
542+
self()
543+
}
544+
}
545+
"#,
546+
expect![[r#"
547+
43..47 'self': &'? Self
548+
168..172 'self': &'? F
549+
205..227 '{ ... }': <F as AsyncFnMut<()>>::CallRefFuture<'<erased>>
550+
215..219 'self': &'? F
551+
215..221 'self()': <F as AsyncFnMut<()>>::CallRefFuture<'<erased>>
552+
"#]],
553+
);
554+
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ pub mod term_search;
3535

3636
mod display;
3737

38+
#[doc(hidden)]
39+
pub use hir_def::ModuleId;
40+
3841
use std::{
3942
fmt,
4043
mem::discriminant,
@@ -48,8 +51,8 @@ use hir_def::{
4851
AdtId, AssocItemId, AssocItemLoc, AttrDefId, CallableDefId, ConstId, ConstParamId,
4952
CrateRootModuleId, DefWithBodyId, EnumId, EnumVariantId, ExternBlockId, ExternCrateId,
5053
FunctionId, GenericDefId, GenericParamId, HasModule, ImplId, ItemContainerId, LifetimeParamId,
51-
LocalFieldId, Lookup, MacroExpander, MacroId, ModuleId, StaticId, StructId, SyntheticSyntax,
52-
TupleId, TypeAliasId, TypeOrConstParamId, TypeParamId, UnionId,
54+
LocalFieldId, Lookup, MacroExpander, MacroId, StaticId, StructId, SyntheticSyntax, TupleId,
55+
TypeAliasId, TypeOrConstParamId, TypeParamId, UnionId,
5356
expr_store::{ExpressionStoreDiagnostics, ExpressionStoreSourceMap},
5457
hir::{
5558
BindingAnnotation, BindingId, Expr, ExprId, ExprOrPatId, LabelId, Pat,

src/tools/rust-analyzer/crates/hir/src/symbols.rs

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use hir_ty::{
1818
};
1919
use intern::Symbol;
2020
use rustc_hash::FxHashMap;
21-
use syntax::{AstNode, AstPtr, SmolStr, SyntaxNode, SyntaxNodePtr, ToSmolStr, ast::HasName};
21+
use syntax::{AstNode, AstPtr, SyntaxNode, SyntaxNodePtr, ToSmolStr, ast::HasName};
2222

2323
use crate::{HasCrate, Module, ModuleDef, Semantics};
2424

@@ -29,7 +29,7 @@ pub struct FileSymbol {
2929
pub name: Symbol,
3030
pub def: ModuleDef,
3131
pub loc: DeclarationLocation,
32-
pub container_name: Option<SmolStr>,
32+
pub container_name: Option<Symbol>,
3333
/// Whether this symbol is a doc alias for the original symbol.
3434
pub is_alias: bool,
3535
pub is_assoc: bool,
@@ -65,23 +65,29 @@ pub struct SymbolCollector<'a> {
6565
db: &'a dyn HirDatabase,
6666
symbols: FxIndexSet<FileSymbol>,
6767
work: Vec<SymbolCollectorWork>,
68-
current_container_name: Option<SmolStr>,
68+
current_container_name: Option<Symbol>,
69+
collect_pub_only: bool,
6970
}
7071

7172
/// Given a [`ModuleId`] and a [`HirDatabase`], use the DefMap for the module's crate to collect
7273
/// all symbols that should be indexed for the given module.
7374
impl<'a> SymbolCollector<'a> {
74-
pub fn new(db: &'a dyn HirDatabase) -> Self {
75+
pub fn new(db: &'a dyn HirDatabase, collect_pub_only: bool) -> Self {
7576
SymbolCollector {
7677
db,
7778
symbols: Default::default(),
7879
work: Default::default(),
7980
current_container_name: None,
81+
collect_pub_only,
8082
}
8183
}
8284

83-
pub fn new_module(db: &dyn HirDatabase, module: Module) -> Box<[FileSymbol]> {
84-
let mut symbol_collector = SymbolCollector::new(db);
85+
pub fn new_module(
86+
db: &dyn HirDatabase,
87+
module: Module,
88+
collect_pub_only: bool,
89+
) -> Box<[FileSymbol]> {
90+
let mut symbol_collector = SymbolCollector::new(db, collect_pub_only);
8591
symbol_collector.collect(module);
8692
symbol_collector.finish()
8793
}
@@ -108,12 +114,16 @@ impl<'a> SymbolCollector<'a> {
108114
tracing::info!(?work, "SymbolCollector::do_work");
109115
self.db.unwind_if_revision_cancelled();
110116

111-
let parent_name = work.parent.map(|name| name.as_str().to_smolstr());
117+
let parent_name = work.parent.map(|name| Symbol::intern(name.as_str()));
112118
self.with_container_name(parent_name, |s| s.collect_from_module(work.module_id));
113119
}
114120

115121
fn collect_from_module(&mut self, module_id: ModuleId) {
116-
let push_decl = |this: &mut Self, def, name| {
122+
let collect_pub_only = self.collect_pub_only;
123+
let push_decl = |this: &mut Self, def: ModuleDefId, name, vis| {
124+
if collect_pub_only && vis != Visibility::Public {
125+
return;
126+
}
117127
match def {
118128
ModuleDefId::ModuleId(id) => this.push_module(id, name),
119129
ModuleDefId::FunctionId(id) => {
@@ -125,7 +135,7 @@ impl<'a> SymbolCollector<'a> {
125135
}
126136
ModuleDefId::AdtId(AdtId::EnumId(id)) => {
127137
this.push_decl(id, name, false, None);
128-
let enum_name = this.db.enum_signature(id).name.as_str().to_smolstr();
138+
let enum_name = Symbol::intern(this.db.enum_signature(id).name.as_str());
129139
this.with_container_name(Some(enum_name), |this| {
130140
let variants = id.enum_variants(this.db);
131141
for (variant_id, variant_name, _) in &variants.variants {
@@ -175,6 +185,9 @@ impl<'a> SymbolCollector<'a> {
175185
};
176186

177187
let mut push_import = |this: &mut Self, i: ImportId, name: &Name, def: ModuleDefId, vis| {
188+
if collect_pub_only && vis != Visibility::Public {
189+
return;
190+
}
178191
let source = import_child_source_cache
179192
.entry(i.use_)
180193
.or_insert_with(|| i.use_.child_source(this.db));
@@ -209,6 +222,9 @@ impl<'a> SymbolCollector<'a> {
209222

210223
let push_extern_crate =
211224
|this: &mut Self, i: ExternCrateId, name: &Name, def: ModuleDefId, vis| {
225+
if collect_pub_only && vis != Visibility::Public {
226+
return;
227+
}
212228
let loc = i.lookup(this.db);
213229
let source = loc.source(this.db);
214230
let rename = source.value.rename().and_then(|rename| rename.name());
@@ -258,7 +274,7 @@ impl<'a> SymbolCollector<'a> {
258274
continue;
259275
}
260276
// self is a declaration
261-
push_decl(self, def, name)
277+
push_decl(self, def, name, vis)
262278
}
263279

264280
for (name, Item { def, vis, import }) in scope.macros() {
@@ -271,7 +287,7 @@ impl<'a> SymbolCollector<'a> {
271287
continue;
272288
}
273289
// self is a declaration
274-
push_decl(self, def.into(), name)
290+
push_decl(self, ModuleDefId::MacroId(def), name, vis)
275291
}
276292

277293
for (name, Item { def, vis, import }) in scope.values() {
@@ -283,7 +299,7 @@ impl<'a> SymbolCollector<'a> {
283299
continue;
284300
}
285301
// self is a declaration
286-
push_decl(self, def, name)
302+
push_decl(self, def, name, vis)
287303
}
288304

289305
for const_id in scope.unnamed_consts() {
@@ -304,6 +320,9 @@ impl<'a> SymbolCollector<'a> {
304320
}
305321

306322
fn collect_from_body(&mut self, body_id: impl Into<DefWithBodyId>, name: Option<Name>) {
323+
if self.collect_pub_only {
324+
return;
325+
}
307326
let body_id = body_id.into();
308327
let body = self.db.body(body_id);
309328

@@ -328,23 +347,28 @@ impl<'a> SymbolCollector<'a> {
328347
)
329348
.to_smolstr(),
330349
);
331-
self.with_container_name(impl_name, |s| {
350+
self.with_container_name(impl_name.as_deref().map(Symbol::intern), |s| {
332351
for &(ref name, assoc_item_id) in &impl_id.impl_items(self.db).items {
352+
if s.collect_pub_only && s.db.assoc_visibility(assoc_item_id) != Visibility::Public
353+
{
354+
continue;
355+
}
356+
333357
s.push_assoc_item(assoc_item_id, name, None)
334358
}
335359
})
336360
}
337361

338362
fn collect_from_trait(&mut self, trait_id: TraitId, trait_do_not_complete: Complete) {
339363
let trait_data = self.db.trait_signature(trait_id);
340-
self.with_container_name(Some(trait_data.name.as_str().into()), |s| {
364+
self.with_container_name(Some(Symbol::intern(trait_data.name.as_str())), |s| {
341365
for &(ref name, assoc_item_id) in &trait_id.trait_items(self.db).items {
342366
s.push_assoc_item(assoc_item_id, name, Some(trait_do_not_complete));
343367
}
344368
});
345369
}
346370

347-
fn with_container_name(&mut self, container_name: Option<SmolStr>, f: impl FnOnce(&mut Self)) {
371+
fn with_container_name(&mut self, container_name: Option<Symbol>, f: impl FnOnce(&mut Self)) {
348372
if let Some(container_name) = container_name {
349373
let prev = self.current_container_name.replace(container_name);
350374
f(self);

src/tools/rust-analyzer/crates/ide-assists/src/handlers/add_label_to_loop.rs

Lines changed: 66 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
use ide_db::syntax_helpers::node_ext::for_each_break_and_continue_expr;
1+
use ide_db::{
2+
source_change::SourceChangeBuilder, syntax_helpers::node_ext::for_each_break_and_continue_expr,
3+
};
24
use syntax::{
3-
T,
4-
ast::{self, AstNode, HasLoopBody},
5+
SyntaxToken, T,
6+
ast::{
7+
self, AstNode, HasLoopBody,
8+
make::{self, tokens},
9+
syntax_factory::SyntaxFactory,
10+
},
11+
syntax_editor::{Position, SyntaxEditor},
512
};
613

714
use crate::{AssistContext, AssistId, Assists};
@@ -21,9 +28,9 @@ use crate::{AssistContext, AssistId, Assists};
2128
// ->
2229
// ```
2330
// fn main() {
24-
// 'l: loop {
25-
// break 'l;
26-
// continue 'l;
31+
// ${1:'l}: loop {
32+
// break ${2:'l};
33+
// continue ${0:'l};
2734
// }
2835
// }
2936
// ```
@@ -39,30 +46,56 @@ pub(crate) fn add_label_to_loop(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
3946
"Add Label",
4047
loop_expr.syntax().text_range(),
4148
|builder| {
42-
builder.insert(loop_kw.text_range().start(), "'l: ");
49+
let make = SyntaxFactory::with_mappings();
50+
let mut editor = builder.make_editor(loop_expr.syntax());
51+
52+
let label = make.lifetime("'l");
53+
let elements = vec![
54+
label.syntax().clone().into(),
55+
make::token(T![:]).into(),
56+
tokens::single_space().into(),
57+
];
58+
editor.insert_all(Position::before(&loop_kw), elements);
59+
60+
if let Some(cap) = ctx.config.snippet_cap {
61+
editor.add_annotation(label.syntax(), builder.make_placeholder_snippet(cap));
62+
}
4363

4464
let loop_body = loop_expr.loop_body().and_then(|it| it.stmt_list());
45-
for_each_break_and_continue_expr(
46-
loop_expr.label(),
47-
loop_body,
48-
&mut |expr| match expr {
49-
ast::Expr::BreakExpr(break_expr) => {
50-
if let Some(break_token) = break_expr.break_token() {
51-
builder.insert(break_token.text_range().end(), " 'l")
52-
}
53-
}
54-
ast::Expr::ContinueExpr(continue_expr) => {
55-
if let Some(continue_token) = continue_expr.continue_token() {
56-
builder.insert(continue_token.text_range().end(), " 'l")
57-
}
58-
}
59-
_ => {}
60-
},
61-
);
65+
for_each_break_and_continue_expr(loop_expr.label(), loop_body, &mut |expr| {
66+
let token = match expr {
67+
ast::Expr::BreakExpr(break_expr) => break_expr.break_token(),
68+
ast::Expr::ContinueExpr(continue_expr) => continue_expr.continue_token(),
69+
_ => return,
70+
};
71+
if let Some(token) = token {
72+
insert_label_after_token(&mut editor, &make, &token, ctx, builder);
73+
}
74+
});
75+
76+
editor.add_mappings(make.finish_with_mappings());
77+
builder.add_file_edits(ctx.vfs_file_id(), editor);
78+
builder.rename();
6279
},
6380
)
6481
}
6582

83+
fn insert_label_after_token(
84+
editor: &mut SyntaxEditor,
85+
make: &SyntaxFactory,
86+
token: &SyntaxToken,
87+
ctx: &AssistContext<'_>,
88+
builder: &mut SourceChangeBuilder,
89+
) {
90+
let label = make.lifetime("'l");
91+
let elements = vec![tokens::single_space().into(), label.syntax().clone().into()];
92+
editor.insert_all(Position::after(token), elements);
93+
94+
if let Some(cap) = ctx.config.snippet_cap {
95+
editor.add_annotation(label.syntax(), builder.make_placeholder_snippet(cap));
96+
}
97+
}
98+
6699
#[cfg(test)]
67100
mod tests {
68101
use crate::tests::{check_assist, check_assist_not_applicable};
@@ -82,9 +115,9 @@ fn main() {
82115
}"#,
83116
r#"
84117
fn main() {
85-
'l: loop {
86-
break 'l;
87-
continue 'l;
118+
${1:'l}: loop {
119+
break ${2:'l};
120+
continue ${0:'l};
88121
}
89122
}"#,
90123
);
@@ -107,9 +140,9 @@ fn main() {
107140
}"#,
108141
r#"
109142
fn main() {
110-
'l: loop {
111-
break 'l;
112-
continue 'l;
143+
${1:'l}: loop {
144+
break ${2:'l};
145+
continue ${0:'l};
113146
loop {
114147
break;
115148
continue;
@@ -139,9 +172,9 @@ fn main() {
139172
loop {
140173
break;
141174
continue;
142-
'l: loop {
143-
break 'l;
144-
continue 'l;
175+
${1:'l}: loop {
176+
break ${2:'l};
177+
continue ${0:'l};
145178
}
146179
}
147180
}"#,

0 commit comments

Comments
 (0)