Skip to content

Commit a15500c

Browse files
authored
Rollup merge of #100049 - lnicola:rust-analyzer-2022-08-02, r=lnicola
⬆️ rust-analyzer r? `@ghost`
2 parents 4fbe53f + 30a3706 commit a15500c

File tree

45 files changed

+765
-241
lines changed

Some content is hidden

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

45 files changed

+765
-241
lines changed

src/tools/rust-analyzer/.github/workflows/publish.yml

+15-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,21 @@ jobs:
3434
git config --global user.email "[email protected]"
3535
git config --global user.name "Github Action"
3636
rm Cargo.lock
37+
# Fix names for crates that were published before switch to kebab-case.
38+
cargo workspaces rename --from base-db base_db
39+
cargo workspaces rename --from hir-def hir_def
40+
cargo workspaces rename --from hir-expand hir_expand
41+
cargo workspaces rename --from hir-ty hir_ty
42+
cargo workspaces rename --from ide-assists ide_assists
43+
cargo workspaces rename --from ide-completion ide_completion
44+
cargo workspaces rename --from ide-db ide_db
45+
cargo workspaces rename --from ide-diagnostics ide_diagnostics
46+
cargo workspaces rename --from ide-ssr ide_ssr
47+
cargo workspaces rename --from proc-macro-api proc_macro_api
48+
cargo workspaces rename --from proc-macro-srv proc_macro_srv
49+
cargo workspaces rename --from project-model project_model
50+
cargo workspaces rename --from test-utils test_utils
51+
cargo workspaces rename --from text-edit text_edit
3752
cargo workspaces rename ra_ap_%n
3853
find crates/rust-analyzer -type f -name '*.rs' -exec sed -i 's/rust_analyzer/ra_ap_rust_analyzer/g' {} +
39-
# Fix names for crates that were published before switch to kebab-case.
40-
find crates -name 'Cargo.toml' -exec sed -i "s/ra_ap_base-db/ra_ap_base_db/g; s/ra_ap_hir-def/ra_ap_hir_def/g; s/ra_ap_hir-expand/ra_ap_hir_expand/g; s/ra_ap_hir-ty/ra_ap_hir_ty/g; s/ra_ap_ide-assists/ra_ap_ide_assists/g; s/ra_ap_ide-completion/ra_ap_ide_completion/g; s/ra_ap_ide-db/ra_ap_ide_db/g; s/ra_ap_ide-diagnostics/ra_ap_ide_diagnostics/g; s/ra_ap_ide-ssr/ra_ap_ide_ssr/g; s/ra_ap_proc-macro-api/ra_ap_proc_macro_api/g; s/ra_ap_proc-macro-srv/ra_ap_proc_macro_srv/g; s/ra_ap_project-model/ra_ap_project_model/g; s/ra_ap_test-utils/ra_ap_test_utils/g; s/ra_ap_text-edit/ra_ap_text_edit/g" {} +
4154
cargo workspaces publish --yes --force '*' --exact --no-git-commit --allow-dirty --skip-published custom 0.0.$PATCH

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

+16-5
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,24 @@ impl RawAttrs {
124124

125125
pub(crate) fn merge(&self, other: Self) -> Self {
126126
// FIXME: This needs to fixup `AttrId`s
127-
match (&self.entries, &other.entries) {
127+
match (&self.entries, other.entries) {
128128
(None, None) => Self::EMPTY,
129-
(Some(entries), None) | (None, Some(entries)) => {
130-
Self { entries: Some(entries.clone()) }
131-
}
129+
(None, entries @ Some(_)) => Self { entries },
130+
(Some(entries), None) => Self { entries: Some(entries.clone()) },
132131
(Some(a), Some(b)) => {
133-
Self { entries: Some(a.iter().chain(b.iter()).cloned().collect()) }
132+
let last_ast_index = a.last().map_or(0, |it| it.id.ast_index + 1);
133+
Self {
134+
entries: Some(
135+
a.iter()
136+
.cloned()
137+
.chain(b.iter().map(|it| {
138+
let mut it = it.clone();
139+
it.id.ast_index += last_ast_index;
140+
it
141+
}))
142+
.collect(),
143+
),
144+
}
134145
}
135146
}
136147
}

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::collections::hash_map::Entry;
55

66
use base_db::CrateId;
77
use hir_expand::{name::Name, AstId, MacroCallId};
8+
use itertools::Itertools;
89
use once_cell::sync::Lazy;
910
use profile::Count;
1011
use rustc_hash::{FxHashMap, FxHashSet};
@@ -97,15 +98,14 @@ pub(crate) enum BuiltinShadowMode {
9798
impl ItemScope {
9899
pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a Name, PerNs)> + 'a {
99100
// FIXME: shadowing
100-
let keys: FxHashSet<_> = self
101-
.types
101+
self.types
102102
.keys()
103103
.chain(self.values.keys())
104104
.chain(self.macros.keys())
105105
.chain(self.unresolved.iter())
106-
.collect();
107-
108-
keys.into_iter().map(move |name| (name, self.get(name)))
106+
.sorted()
107+
.unique()
108+
.map(move |name| (name, self.get(name)))
109109
}
110110

111111
pub fn declarations(&self) -> impl Iterator<Item = ModuleDefId> + '_ {

src/tools/rust-analyzer/crates/hir-def/src/nameres/collector.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ impl DefCollector<'_> {
10551055
};
10561056
let mut res = ReachedFixedPoint::Yes;
10571057
macros.retain(|directive| {
1058-
let resolver2 = |path| {
1058+
let resolver = |path| {
10591059
let resolved_res = self.def_map.resolve_path_fp_with_macro(
10601060
self.db,
10611061
ResolveMode::Other,
@@ -1068,7 +1068,7 @@ impl DefCollector<'_> {
10681068
.take_macros()
10691069
.map(|it| (it, macro_id_to_def_id(self.db, it)))
10701070
};
1071-
let resolver = |path| resolver2(path).map(|(_, it)| it);
1071+
let resolver_def_id = |path| resolver(path).map(|(_, it)| it);
10721072

10731073
match &directive.kind {
10741074
MacroDirectiveKind::FnLike { ast_id, expand_to } => {
@@ -1077,7 +1077,7 @@ impl DefCollector<'_> {
10771077
ast_id,
10781078
*expand_to,
10791079
self.def_map.krate,
1080-
&resolver,
1080+
&resolver_def_id,
10811081
&mut |_err| (),
10821082
);
10831083
if let Ok(Ok(call_id)) = call_id {
@@ -1093,7 +1093,7 @@ impl DefCollector<'_> {
10931093
*derive_attr,
10941094
*derive_pos as u32,
10951095
self.def_map.krate,
1096-
&resolver2,
1096+
&resolver,
10971097
);
10981098

10991099
if let Ok((macro_id, def_id, call_id)) = id {
@@ -1158,7 +1158,7 @@ impl DefCollector<'_> {
11581158
}
11591159
}
11601160

1161-
let def = match resolver(path.clone()) {
1161+
let def = match resolver_def_id(path.clone()) {
11621162
Some(def) if def.is_attribute() => def,
11631163
_ => return true,
11641164
};
@@ -1292,7 +1292,8 @@ impl DefCollector<'_> {
12921292
true
12931293
});
12941294
// Attribute resolution can add unresolved macro invocations, so concatenate the lists.
1295-
self.unresolved_macros.extend(macros);
1295+
macros.extend(mem::take(&mut self.unresolved_macros));
1296+
self.unresolved_macros = macros;
12961297

12971298
for (module_id, depth, container, macro_call_id) in resolved {
12981299
self.collect_macro_expansion(module_id, macro_call_id, depth, container);

src/tools/rust-analyzer/crates/hir-ty/src/chalk_ext.rs

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub trait TyExt {
3434
fn callable_sig(&self, db: &dyn HirDatabase) -> Option<CallableSig>;
3535

3636
fn strip_references(&self) -> &Ty;
37+
fn strip_reference(&self) -> &Ty;
3738

3839
/// If this is a `dyn Trait`, returns that trait.
3940
fn dyn_trait(&self) -> Option<TraitId>;
@@ -182,6 +183,10 @@ impl TyExt for Ty {
182183
t
183184
}
184185

186+
fn strip_reference(&self) -> &Ty {
187+
self.as_reference().map_or(self, |(ty, _, _)| ty)
188+
}
189+
185190
fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option<Vec<QuantifiedWhereClause>> {
186191
match self.kind(Interner) {
187192
TyKind::OpaqueType(opaque_ty_id, subst) => {

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

+4
Original file line numberDiff line numberDiff line change
@@ -2769,6 +2769,10 @@ impl Type {
27692769
self.derived(self.ty.strip_references().clone())
27702770
}
27712771

2772+
pub fn strip_reference(&self) -> Type {
2773+
self.derived(self.ty.strip_reference().clone())
2774+
}
2775+
27722776
pub fn is_unknown(&self) -> bool {
27732777
self.ty.is_unknown()
27742778
}

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

+24-1
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
324324
self.imp.resolve_type(ty)
325325
}
326326

327+
pub fn resolve_trait(&self, trait_: &ast::Path) -> Option<Trait> {
328+
self.imp.resolve_trait(trait_)
329+
}
330+
327331
// FIXME: Figure out a nice interface to inspect adjustments
328332
pub fn is_implicit_reborrow(&self, expr: &ast::Expr) -> Option<Mutability> {
329333
self.imp.is_implicit_reborrow(expr)
@@ -924,7 +928,12 @@ impl<'db> SemanticsImpl<'db> {
924928
}
925929

926930
fn original_ast_node<N: AstNode>(&self, node: N) -> Option<N> {
927-
self.wrap_node_infile(node).original_ast_node(self.db.upcast()).map(|it| it.value)
931+
self.wrap_node_infile(node).original_ast_node(self.db.upcast()).map(
932+
|InFile { file_id, value }| {
933+
self.cache(find_root(value.syntax()), file_id);
934+
value
935+
},
936+
)
928937
}
929938

930939
fn diagnostics_display_range(&self, src: InFile<SyntaxNodePtr>) -> FileRange {
@@ -1009,6 +1018,20 @@ impl<'db> SemanticsImpl<'db> {
10091018
Some(Type::new_with_resolver(self.db, &analyze.resolver, ty))
10101019
}
10111020

1021+
fn resolve_trait(&self, path: &ast::Path) -> Option<Trait> {
1022+
let analyze = self.analyze(path.syntax())?;
1023+
let hygiene = hir_expand::hygiene::Hygiene::new(self.db.upcast(), analyze.file_id);
1024+
let ctx = body::LowerCtx::with_hygiene(self.db.upcast(), &hygiene);
1025+
let hir_path = Path::from_src(path.clone(), &ctx)?;
1026+
match analyze
1027+
.resolver
1028+
.resolve_path_in_type_ns_fully(self.db.upcast(), hir_path.mod_path())?
1029+
{
1030+
TypeNs::TraitId(id) => Some(Trait { id }),
1031+
_ => None,
1032+
}
1033+
}
1034+
10121035
fn is_implicit_reborrow(&self, expr: &ast::Expr) -> Option<Mutability> {
10131036
self.analyze(expr.syntax())?.is_implicit_reborrow(self.db, expr)
10141037
}

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use crate::{
3636
// pub struct Baz;
3737
// }
3838
//
39-
// use foo::{Baz, Bar};
39+
// use foo::{Bar, Baz};
4040
//
4141
// fn qux(bar: Bar, baz: Baz) {}
4242
// ```
@@ -281,7 +281,7 @@ mod foo {
281281
pub fn f() {}
282282
}
283283
284-
use foo::{Baz, Bar, f};
284+
use foo::{Bar, Baz, f};
285285
286286
fn qux(bar: Bar, baz: Baz) {
287287
f();
@@ -351,7 +351,7 @@ mod foo {
351351
pub fn f() {}
352352
}
353353
354-
use foo::{Baz, Bar, f};
354+
use foo::{Bar, Baz, f};
355355
356356
fn qux(bar: Bar, baz: Baz) {
357357
f();
@@ -440,7 +440,7 @@ mod foo {
440440
}
441441
}
442442
443-
use foo::{bar::{Baz, Bar, f}, baz::*};
443+
use foo::{bar::{Bar, Baz, f}, baz::*};
444444
445445
fn qux(bar: Bar, baz: Baz) {
446446
f();
@@ -561,7 +561,7 @@ mod foo {
561561
562562
use foo::{
563563
bar::{*, f},
564-
baz::{g, qux::{q, h}}
564+
baz::{g, qux::{h, q}}
565565
};
566566
567567
fn qux(bar: Bar, baz: Baz) {

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

+46-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use ide_db::{
77
imports::insert_use::remove_path_if_in_use_stmt,
88
path_transform::PathTransform,
99
search::{FileReference, SearchScope},
10-
syntax_helpers::node_ext::expr_as_name_ref,
10+
syntax_helpers::{insert_whitespace_into_node::insert_ws_into, node_ext::expr_as_name_ref},
1111
RootDatabase,
1212
};
1313
use itertools::{izip, Itertools};
@@ -301,7 +301,16 @@ fn inline(
301301
params: &[(ast::Pat, Option<ast::Type>, hir::Param)],
302302
CallInfo { node, arguments, generic_arg_list }: &CallInfo,
303303
) -> ast::Expr {
304-
let body = fn_body.clone_for_update();
304+
let body = if sema.hir_file_for(fn_body.syntax()).is_macro() {
305+
cov_mark::hit!(inline_call_defined_in_macro);
306+
if let Some(body) = ast::BlockExpr::cast(insert_ws_into(fn_body.syntax().clone())) {
307+
body
308+
} else {
309+
fn_body.clone_for_update()
310+
}
311+
} else {
312+
fn_body.clone_for_update()
313+
};
305314
let usages_for_locals = |local| {
306315
Definition::Local(local)
307316
.usages(sema)
@@ -1144,6 +1153,41 @@ fn bar() -> u32 {
11441153
x
11451154
}) + foo()
11461155
}
1156+
"#,
1157+
)
1158+
}
1159+
1160+
#[test]
1161+
fn inline_call_defined_in_macro() {
1162+
cov_mark::check!(inline_call_defined_in_macro);
1163+
check_assist(
1164+
inline_call,
1165+
r#"
1166+
macro_rules! define_foo {
1167+
() => { fn foo() -> u32 {
1168+
let x = 0;
1169+
x
1170+
} };
1171+
}
1172+
define_foo!();
1173+
fn bar() -> u32 {
1174+
foo$0()
1175+
}
1176+
"#,
1177+
r#"
1178+
macro_rules! define_foo {
1179+
() => { fn foo() -> u32 {
1180+
let x = 0;
1181+
x
1182+
} };
1183+
}
1184+
define_foo!();
1185+
fn bar() -> u32 {
1186+
{
1187+
let x = 0;
1188+
x
1189+
}
1190+
}
11471191
"#,
11481192
)
11491193
}

src/tools/rust-analyzer/crates/ide-assists/src/tests/generated.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ mod foo {
535535
pub struct Baz;
536536
}
537537
538-
use foo::{Baz, Bar};
538+
use foo::{Bar, Baz};
539539
540540
fn qux(bar: Bar, baz: Baz) {}
541541
"#####,

src/tools/rust-analyzer/crates/ide-completion/src/completions.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ impl Completions {
400400
) {
401401
if let PathCompletionCtx { kind: PathKind::Pat { pat_ctx }, .. } = path_ctx {
402402
cov_mark::hit!(enum_variant_pattern_path);
403-
self.add_variant_pat(ctx, pat_ctx, variant, local_name);
403+
self.add_variant_pat(ctx, pat_ctx, Some(path_ctx), variant, local_name);
404404
return;
405405
}
406406

@@ -484,12 +484,14 @@ impl Completions {
484484
&mut self,
485485
ctx: &CompletionContext<'_>,
486486
pattern_ctx: &PatternContext,
487+
path_ctx: Option<&PathCompletionCtx>,
487488
variant: hir::Variant,
488489
local_name: Option<hir::Name>,
489490
) {
490491
self.add_opt(render_variant_pat(
491492
RenderContext::new(ctx),
492493
pattern_ctx,
494+
path_ctx,
493495
variant,
494496
local_name.clone(),
495497
None,
@@ -504,7 +506,14 @@ impl Completions {
504506
path: hir::ModPath,
505507
) {
506508
let path = Some(&path);
507-
self.add_opt(render_variant_pat(RenderContext::new(ctx), pattern_ctx, variant, None, path));
509+
self.add_opt(render_variant_pat(
510+
RenderContext::new(ctx),
511+
pattern_ctx,
512+
None,
513+
variant,
514+
None,
515+
path,
516+
));
508517
}
509518

510519
pub(crate) fn add_struct_pat(

src/tools/rust-analyzer/crates/ide-completion/src/completions/attribute.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub(crate) fn complete_attribute_path(
115115
});
116116
acc.add_nameref_keywords_with_colon(ctx);
117117
}
118-
Qualified::Infer | Qualified::With { .. } => {}
118+
Qualified::TypeAnchor { .. } | Qualified::With { .. } => {}
119119
}
120120

121121
let attributes = annotated_item_kind.and_then(|kind| {

src/tools/rust-analyzer/crates/ide-completion/src/completions/attribute/derive.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub(crate) fn complete_derive_path(
9797
});
9898
acc.add_nameref_keywords_with_colon(ctx);
9999
}
100-
Qualified::Infer | Qualified::With { .. } => {}
100+
Qualified::TypeAnchor { .. } | Qualified::With { .. } => {}
101101
}
102102
}
103103

0 commit comments

Comments
 (0)