Skip to content

Commit

Permalink
⬆️ rust-analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
lnicola committed Aug 2, 2022
1 parent a1f1b95 commit 9d2cb42
Show file tree
Hide file tree
Showing 45 changed files with 765 additions and 241 deletions.
17 changes: 15 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,21 @@ jobs:
git config --global user.email "[email protected]"
git config --global user.name "Github Action"
rm Cargo.lock
# Fix names for crates that were published before switch to kebab-case.
cargo workspaces rename --from base-db base_db
cargo workspaces rename --from hir-def hir_def
cargo workspaces rename --from hir-expand hir_expand
cargo workspaces rename --from hir-ty hir_ty
cargo workspaces rename --from ide-assists ide_assists
cargo workspaces rename --from ide-completion ide_completion
cargo workspaces rename --from ide-db ide_db
cargo workspaces rename --from ide-diagnostics ide_diagnostics
cargo workspaces rename --from ide-ssr ide_ssr
cargo workspaces rename --from proc-macro-api proc_macro_api
cargo workspaces rename --from proc-macro-srv proc_macro_srv
cargo workspaces rename --from project-model project_model
cargo workspaces rename --from test-utils test_utils
cargo workspaces rename --from text-edit text_edit
cargo workspaces rename ra_ap_%n
find crates/rust-analyzer -type f -name '*.rs' -exec sed -i 's/rust_analyzer/ra_ap_rust_analyzer/g' {} +
# Fix names for crates that were published before switch to kebab-case.
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" {} +
cargo workspaces publish --yes --force '*' --exact --no-git-commit --allow-dirty --skip-published custom 0.0.$PATCH
21 changes: 16 additions & 5 deletions crates/hir-def/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,24 @@ impl RawAttrs {

pub(crate) fn merge(&self, other: Self) -> Self {
// FIXME: This needs to fixup `AttrId`s
match (&self.entries, &other.entries) {
match (&self.entries, other.entries) {
(None, None) => Self::EMPTY,
(Some(entries), None) | (None, Some(entries)) => {
Self { entries: Some(entries.clone()) }
}
(None, entries @ Some(_)) => Self { entries },
(Some(entries), None) => Self { entries: Some(entries.clone()) },
(Some(a), Some(b)) => {
Self { entries: Some(a.iter().chain(b.iter()).cloned().collect()) }
let last_ast_index = a.last().map_or(0, |it| it.id.ast_index + 1);
Self {
entries: Some(
a.iter()
.cloned()
.chain(b.iter().map(|it| {
let mut it = it.clone();
it.id.ast_index += last_ast_index;
it
}))
.collect(),
),
}
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions crates/hir-def/src/item_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::collections::hash_map::Entry;

use base_db::CrateId;
use hir_expand::{name::Name, AstId, MacroCallId};
use itertools::Itertools;
use once_cell::sync::Lazy;
use profile::Count;
use rustc_hash::{FxHashMap, FxHashSet};
Expand Down Expand Up @@ -97,15 +98,14 @@ pub(crate) enum BuiltinShadowMode {
impl ItemScope {
pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a Name, PerNs)> + 'a {
// FIXME: shadowing
let keys: FxHashSet<_> = self
.types
self.types
.keys()
.chain(self.values.keys())
.chain(self.macros.keys())
.chain(self.unresolved.iter())
.collect();

keys.into_iter().map(move |name| (name, self.get(name)))
.sorted()
.unique()
.map(move |name| (name, self.get(name)))
}

pub fn declarations(&self) -> impl Iterator<Item = ModuleDefId> + '_ {
Expand Down
13 changes: 7 additions & 6 deletions crates/hir-def/src/nameres/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,7 @@ impl DefCollector<'_> {
};
let mut res = ReachedFixedPoint::Yes;
macros.retain(|directive| {
let resolver2 = |path| {
let resolver = |path| {
let resolved_res = self.def_map.resolve_path_fp_with_macro(
self.db,
ResolveMode::Other,
Expand All @@ -1068,7 +1068,7 @@ impl DefCollector<'_> {
.take_macros()
.map(|it| (it, macro_id_to_def_id(self.db, it)))
};
let resolver = |path| resolver2(path).map(|(_, it)| it);
let resolver_def_id = |path| resolver(path).map(|(_, it)| it);

match &directive.kind {
MacroDirectiveKind::FnLike { ast_id, expand_to } => {
Expand All @@ -1077,7 +1077,7 @@ impl DefCollector<'_> {
ast_id,
*expand_to,
self.def_map.krate,
&resolver,
&resolver_def_id,
&mut |_err| (),
);
if let Ok(Ok(call_id)) = call_id {
Expand All @@ -1093,7 +1093,7 @@ impl DefCollector<'_> {
*derive_attr,
*derive_pos as u32,
self.def_map.krate,
&resolver2,
&resolver,
);

if let Ok((macro_id, def_id, call_id)) = id {
Expand Down Expand Up @@ -1158,7 +1158,7 @@ impl DefCollector<'_> {
}
}

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

for (module_id, depth, container, macro_call_id) in resolved {
self.collect_macro_expansion(module_id, macro_call_id, depth, container);
Expand Down
5 changes: 5 additions & 0 deletions crates/hir-ty/src/chalk_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub trait TyExt {
fn callable_sig(&self, db: &dyn HirDatabase) -> Option<CallableSig>;

fn strip_references(&self) -> &Ty;
fn strip_reference(&self) -> &Ty;

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

fn strip_reference(&self) -> &Ty {
self.as_reference().map_or(self, |(ty, _, _)| ty)
}

fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option<Vec<QuantifiedWhereClause>> {
match self.kind(Interner) {
TyKind::OpaqueType(opaque_ty_id, subst) => {
Expand Down
4 changes: 4 additions & 0 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2769,6 +2769,10 @@ impl Type {
self.derived(self.ty.strip_references().clone())
}

pub fn strip_reference(&self) -> Type {
self.derived(self.ty.strip_reference().clone())
}

pub fn is_unknown(&self) -> bool {
self.ty.is_unknown()
}
Expand Down
25 changes: 24 additions & 1 deletion crates/hir/src/semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
self.imp.resolve_type(ty)
}

pub fn resolve_trait(&self, trait_: &ast::Path) -> Option<Trait> {
self.imp.resolve_trait(trait_)
}

// FIXME: Figure out a nice interface to inspect adjustments
pub fn is_implicit_reborrow(&self, expr: &ast::Expr) -> Option<Mutability> {
self.imp.is_implicit_reborrow(expr)
Expand Down Expand Up @@ -924,7 +928,12 @@ impl<'db> SemanticsImpl<'db> {
}

fn original_ast_node<N: AstNode>(&self, node: N) -> Option<N> {
self.wrap_node_infile(node).original_ast_node(self.db.upcast()).map(|it| it.value)
self.wrap_node_infile(node).original_ast_node(self.db.upcast()).map(
|InFile { file_id, value }| {
self.cache(find_root(value.syntax()), file_id);
value
},
)
}

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

fn resolve_trait(&self, path: &ast::Path) -> Option<Trait> {
let analyze = self.analyze(path.syntax())?;
let hygiene = hir_expand::hygiene::Hygiene::new(self.db.upcast(), analyze.file_id);
let ctx = body::LowerCtx::with_hygiene(self.db.upcast(), &hygiene);
let hir_path = Path::from_src(path.clone(), &ctx)?;
match analyze
.resolver
.resolve_path_in_type_ns_fully(self.db.upcast(), hir_path.mod_path())?
{
TypeNs::TraitId(id) => Some(Trait { id }),
_ => None,
}
}

fn is_implicit_reborrow(&self, expr: &ast::Expr) -> Option<Mutability> {
self.analyze(expr.syntax())?.is_implicit_reborrow(self.db, expr)
}
Expand Down
10 changes: 5 additions & 5 deletions crates/ide-assists/src/handlers/expand_glob_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use crate::{
// pub struct Baz;
// }
//
// use foo::{Baz, Bar};
// use foo::{Bar, Baz};
//
// fn qux(bar: Bar, baz: Baz) {}
// ```
Expand Down Expand Up @@ -281,7 +281,7 @@ mod foo {
pub fn f() {}
}
use foo::{Baz, Bar, f};
use foo::{Bar, Baz, f};
fn qux(bar: Bar, baz: Baz) {
f();
Expand Down Expand Up @@ -351,7 +351,7 @@ mod foo {
pub fn f() {}
}
use foo::{Baz, Bar, f};
use foo::{Bar, Baz, f};
fn qux(bar: Bar, baz: Baz) {
f();
Expand Down Expand Up @@ -440,7 +440,7 @@ mod foo {
}
}
use foo::{bar::{Baz, Bar, f}, baz::*};
use foo::{bar::{Bar, Baz, f}, baz::*};
fn qux(bar: Bar, baz: Baz) {
f();
Expand Down Expand Up @@ -561,7 +561,7 @@ mod foo {
use foo::{
bar::{*, f},
baz::{g, qux::{q, h}}
baz::{g, qux::{h, q}}
};
fn qux(bar: Bar, baz: Baz) {
Expand Down
48 changes: 46 additions & 2 deletions crates/ide-assists/src/handlers/inline_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ide_db::{
imports::insert_use::remove_path_if_in_use_stmt,
path_transform::PathTransform,
search::{FileReference, SearchScope},
syntax_helpers::node_ext::expr_as_name_ref,
syntax_helpers::{insert_whitespace_into_node::insert_ws_into, node_ext::expr_as_name_ref},
RootDatabase,
};
use itertools::{izip, Itertools};
Expand Down Expand Up @@ -301,7 +301,16 @@ fn inline(
params: &[(ast::Pat, Option<ast::Type>, hir::Param)],
CallInfo { node, arguments, generic_arg_list }: &CallInfo,
) -> ast::Expr {
let body = fn_body.clone_for_update();
let body = if sema.hir_file_for(fn_body.syntax()).is_macro() {
cov_mark::hit!(inline_call_defined_in_macro);
if let Some(body) = ast::BlockExpr::cast(insert_ws_into(fn_body.syntax().clone())) {
body
} else {
fn_body.clone_for_update()
}
} else {
fn_body.clone_for_update()
};
let usages_for_locals = |local| {
Definition::Local(local)
.usages(sema)
Expand Down Expand Up @@ -1144,6 +1153,41 @@ fn bar() -> u32 {
x
}) + foo()
}
"#,
)
}

#[test]
fn inline_call_defined_in_macro() {
cov_mark::check!(inline_call_defined_in_macro);
check_assist(
inline_call,
r#"
macro_rules! define_foo {
() => { fn foo() -> u32 {
let x = 0;
x
} };
}
define_foo!();
fn bar() -> u32 {
foo$0()
}
"#,
r#"
macro_rules! define_foo {
() => { fn foo() -> u32 {
let x = 0;
x
} };
}
define_foo!();
fn bar() -> u32 {
{
let x = 0;
x
}
}
"#,
)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-assists/src/tests/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ mod foo {
pub struct Baz;
}
use foo::{Baz, Bar};
use foo::{Bar, Baz};
fn qux(bar: Bar, baz: Baz) {}
"#####,
Expand Down
13 changes: 11 additions & 2 deletions crates/ide-completion/src/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ impl Completions {
) {
if let PathCompletionCtx { kind: PathKind::Pat { pat_ctx }, .. } = path_ctx {
cov_mark::hit!(enum_variant_pattern_path);
self.add_variant_pat(ctx, pat_ctx, variant, local_name);
self.add_variant_pat(ctx, pat_ctx, Some(path_ctx), variant, local_name);
return;
}

Expand Down Expand Up @@ -484,12 +484,14 @@ impl Completions {
&mut self,
ctx: &CompletionContext<'_>,
pattern_ctx: &PatternContext,
path_ctx: Option<&PathCompletionCtx>,
variant: hir::Variant,
local_name: Option<hir::Name>,
) {
self.add_opt(render_variant_pat(
RenderContext::new(ctx),
pattern_ctx,
path_ctx,
variant,
local_name.clone(),
None,
Expand All @@ -504,7 +506,14 @@ impl Completions {
path: hir::ModPath,
) {
let path = Some(&path);
self.add_opt(render_variant_pat(RenderContext::new(ctx), pattern_ctx, variant, None, path));
self.add_opt(render_variant_pat(
RenderContext::new(ctx),
pattern_ctx,
None,
variant,
None,
path,
));
}

pub(crate) fn add_struct_pat(
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-completion/src/completions/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub(crate) fn complete_attribute_path(
});
acc.add_nameref_keywords_with_colon(ctx);
}
Qualified::Infer | Qualified::With { .. } => {}
Qualified::TypeAnchor { .. } | Qualified::With { .. } => {}
}

let attributes = annotated_item_kind.and_then(|kind| {
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-completion/src/completions/attribute/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub(crate) fn complete_derive_path(
});
acc.add_nameref_keywords_with_colon(ctx);
}
Qualified::Infer | Qualified::With { .. } => {}
Qualified::TypeAnchor { .. } | Qualified::With { .. } => {}
}
}

Expand Down
Loading

0 comments on commit 9d2cb42

Please sign in to comment.