Skip to content

Sync rust-analyzer, add rust-analyzer-proc-macro-srv binary to Rustc component #99756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 40 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
7e0bd37
fix: don't replace default members' body
lowr Jul 20, 2022
aeb0774
Spawn a proc-macro-srv instance per workspace
Veykril Jul 23, 2022
50b27e5
Better error messages when the proc-macro-server fails to start
Veykril Jul 23, 2022
fb063d3
Add `ellipsis` to HIR `RecordLit`
lowr Jul 22, 2022
5f3f428
Don't run slow tests in Rust CI, only RA CI
fasterthanlime Jul 22, 2022
0bffdf2
Disable all source-gen tests at compile time
fasterthanlime Jul 23, 2022
b351e11
Move cfg attrs up to the mod definitions to disable sourcegen
fasterthanlime Jul 23, 2022
20eb2dd
Small fixups
jyn514 Jul 23, 2022
74998e4
Fix .gitattributes for test_data
fasterthanlime Jul 24, 2022
894d6a2
Auto merge of #12832 - lowr:fix/impl-default-members-no-codegen, r=Ve…
bors Jul 24, 2022
64758bd
Add info whether it's assignee expr to relevant HIR `Expr` variants
lowr Jul 24, 2022
805ac66
fix: consider assignee expressions in record fields exhaustiveness check
lowr Jul 24, 2022
ff31785
hir-def tests: sort results before comparing, since FxHashSet iterati…
fasterthanlime Jul 24, 2022
537cc1e
Auto merge of #12863 - lowr:fix/missing-fields-on-destructuring-assig…
bors Jul 24, 2022
d8c0d88
Sort in DefMap::dump, since HashMap iteration order isn't defined
fasterthanlime Jul 24, 2022
77acb5c
fix: Autocomplete for struct fields includes receiver
DorianListens Jul 24, 2022
56c369d
Sort when iterating through CrateGraph
fasterthanlime Jul 24, 2022
7e2b983
Auto merge of #12861 - DorianListens:dscheidt/complete-field, r=Doria…
bors Jul 24, 2022
dfe8449
Make macros test order-resistant
fasterthanlime Jul 24, 2022
71225c3
Replace `debug_assert!` with `stdx::always!`
lowr Jul 25, 2022
c7788ad
Auto merge of #12868 - lowr:follow-up-on-12832, r=lnicola
bors Jul 25, 2022
ac86b8e
Auto merge of #12856 - Veykril:multi-proc-srv, r=Veykril
bors Jul 25, 2022
0d04e63
Merge remote-tracking branch 'origin/master' into sync-from-rust-2
fasterthanlime Jul 25, 2022
dc94050
revert nightly rustfmt formatting that accidentally slipped in
fasterthanlime Jul 25, 2022
0b1ed70
Auto merge of #12871 - fasterthanlime:sync-from-rust-2, r=Veykril
bors Jul 25, 2022
5f54ec0
Add a proc-macro-srv-cli crate
fasterthanlime Jul 23, 2022
fba6adf
Add doc comment to pass tidy check
fasterthanlime Jul 23, 2022
fd1b64e
Return result directly
fasterthanlime Jul 24, 2022
4364531
Name the binary 'proc-macro-srv'
fasterthanlime Jul 24, 2022
dadb832
Remember the difference between 'sysroot root' and 'sysroot src root'…
fasterthanlime Jul 25, 2022
74a2fad
Gate and rename binary, use it if it's in the sysroot
fasterthanlime Jul 25, 2022
6967751
Fix mix up in comments
fasterthanlime Jul 25, 2022
2c2520f
Allow specifying sysroot OR sysroot_src
fasterthanlime Jul 25, 2022
7ba94a8
Auto merge of #12858 - fasterthanlime:proc-macro-srv-bin, r=Veykril
bors Jul 26, 2022
4e60db2
feat: Downmap tokens inside derive helpers
Veykril Jul 24, 2022
aa1491e
Record derive helper attributes, resolve them in IDE layer
Veykril Jul 24, 2022
ddad284
Allow name querying for derive helpers
Veykril Jul 24, 2022
e36a20c
Auto merge of #12864 - Veykril:derive-helpers, r=Veykril
bors Jul 26, 2022
8bcd4a2
Merge commit 'e36a20c24f35a4cee82bbdc600289104c9237c22' into ra-sync-…
fasterthanlime Jul 26, 2022
6ea7d82
Ship `rust-analyzer-proc-macro-srv` binary with dist::Rustc
fasterthanlime Jul 26, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/bootstrap/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ impl<'a> Builder<'a> {
tool::Cargo,
tool::Rls,
tool::RustAnalyzer,
tool::RustAnalyzerProcMacroSrv,
tool::RustDemangler,
tool::Rustdoc,
tool::Clippy,
Expand Down
12 changes: 12 additions & 0 deletions src/bootstrap/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,18 @@ impl Step for Rustc {

builder.install(&builder.rustdoc(compiler), &image.join("bin"), 0o755);

let ra_proc_macro_srv = builder
.ensure(tool::RustAnalyzerProcMacroSrv {
compiler: builder.compiler_for(
compiler.stage,
builder.config.build,
compiler.host,
),
target: compiler.host,
})
.expect("rust-analyzer-proc-macro-server always builds");
builder.install(&ra_proc_macro_srv, &image.join("libexec"), 0o755);

let libdir_relative = builder.libdir_relative(compiler);

// Copy runtime DLLs needed by the compiler
Expand Down
44 changes: 44 additions & 0 deletions src/bootstrap/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,50 @@ impl Step for RustAnalyzer {
}
}

#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct RustAnalyzerProcMacroSrv {
pub compiler: Compiler,
pub target: TargetSelection,
}

impl Step for RustAnalyzerProcMacroSrv {
type Output = Option<PathBuf>;
const DEFAULT: bool = true;
const ONLY_HOSTS: bool = false;

fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
let builder = run.builder;
run.path("src/tools/rust-analyzer").default_condition(
builder.config.extended
&& builder
.config
.tools
.as_ref()
.map_or(true, |tools| tools.iter().any(|tool| tool == "rust-analyzer")),
)
}

fn make_run(run: RunConfig<'_>) {
run.builder.ensure(RustAnalyzerProcMacroSrv {
compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
target: run.target,
});
}

fn run(self, builder: &Builder<'_>) -> Option<PathBuf> {
builder.ensure(ToolBuild {
compiler: self.compiler,
target: self.target,
tool: "rust-analyzer-proc-macro-srv",
mode: Mode::ToolStd,
path: "src/tools/rust-analyzer/crates/proc-macro-srv-cli",
extra_features: vec!["proc-macro-srv/sysroot-abi".to_owned()],
is_optional_tool: false,
source_type: SourceType::InTree,
})
}
}

macro_rules! tool_extended {
(($sel:ident, $builder:ident),
$($name:ident,
Expand Down
7 changes: 7 additions & 0 deletions src/tools/rust-analyzer/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,13 @@ dependencies = [
"tt",
]

[[package]]
name = "proc-macro-srv-cli"
version = "0.0.0"
dependencies = [
"proc-macro-srv",
]

[[package]]
name = "proc-macro-test"
version = "0.0.0"
Expand Down
40 changes: 33 additions & 7 deletions src/tools/rust-analyzer/crates/hir-def/src/body/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub(super) fn lower(
expander,
name_to_pat_grouping: Default::default(),
is_lowering_inside_or_pat: false,
is_lowering_assignee_expr: false,
}
.collect(params, body)
}
Expand All @@ -109,6 +110,7 @@ struct ExprCollector<'a> {
// a poor-mans union-find?
name_to_pat_grouping: FxHashMap<Name, Vec<PatId>>,
is_lowering_inside_or_pat: bool,
is_lowering_assignee_expr: bool,
}

impl ExprCollector<'_> {
Expand Down Expand Up @@ -283,7 +285,10 @@ impl ExprCollector<'_> {
} else {
Box::default()
};
self.alloc_expr(Expr::Call { callee, args }, syntax_ptr)
self.alloc_expr(
Expr::Call { callee, args, is_assignee_expr: self.is_lowering_assignee_expr },
syntax_ptr,
)
}
ast::Expr::MethodCallExpr(e) => {
let receiver = self.collect_expr_opt(e.receiver());
Expand Down Expand Up @@ -359,6 +364,7 @@ impl ExprCollector<'_> {
ast::Expr::RecordExpr(e) => {
let path =
e.path().and_then(|path| self.expander.parse_path(self.db, path)).map(Box::new);
let is_assignee_expr = self.is_lowering_assignee_expr;
let record_lit = if let Some(nfl) = e.record_expr_field_list() {
let fields = nfl
.fields()
Expand All @@ -378,9 +384,16 @@ impl ExprCollector<'_> {
})
.collect();
let spread = nfl.spread().map(|s| self.collect_expr(s));
Expr::RecordLit { path, fields, spread }
let ellipsis = nfl.dotdot_token().is_some();
Expr::RecordLit { path, fields, spread, ellipsis, is_assignee_expr }
} else {
Expr::RecordLit { path, fields: Box::default(), spread: None }
Expr::RecordLit {
path,
fields: Box::default(),
spread: None,
ellipsis: false,
is_assignee_expr,
}
};

self.alloc_expr(record_lit, syntax_ptr)
Expand Down Expand Up @@ -458,14 +471,21 @@ impl ExprCollector<'_> {
)
}
ast::Expr::BinExpr(e) => {
let op = e.op_kind();
if let Some(ast::BinaryOp::Assignment { op: None }) = op {
self.is_lowering_assignee_expr = true;
}
let lhs = self.collect_expr_opt(e.lhs());
self.is_lowering_assignee_expr = false;
let rhs = self.collect_expr_opt(e.rhs());
let op = e.op_kind();
self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr)
}
ast::Expr::TupleExpr(e) => {
let exprs = e.fields().map(|expr| self.collect_expr(expr)).collect();
self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr)
self.alloc_expr(
Expr::Tuple { exprs, is_assignee_expr: self.is_lowering_assignee_expr },
syntax_ptr,
)
}
ast::Expr::BoxExpr(e) => {
let expr = self.collect_expr_opt(e.expr());
Expand All @@ -477,8 +497,14 @@ impl ExprCollector<'_> {

match kind {
ArrayExprKind::ElementList(e) => {
let exprs = e.map(|expr| self.collect_expr(expr)).collect();
self.alloc_expr(Expr::Array(Array::ElementList(exprs)), syntax_ptr)
let elements = e.map(|expr| self.collect_expr(expr)).collect();
self.alloc_expr(
Expr::Array(Array::ElementList {
elements,
is_assignee_expr: self.is_lowering_assignee_expr,
}),
syntax_ptr,
)
}
ArrayExprKind::Repeat { initializer, repeat } => {
let initializer = self.collect_expr_opt(initializer);
Expand Down
19 changes: 13 additions & 6 deletions src/tools/rust-analyzer/crates/hir-def/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
db::DefDatabase,
intern::Interned,
item_tree::{self, AssocItem, FnFlags, ItemTree, ItemTreeId, ModItem, Param, TreeId},
nameres::{attr_resolution::ResolvedAttr, DefMap},
nameres::{attr_resolution::ResolvedAttr, proc_macro::ProcMacroKind, DefMap},
type_ref::{TraitRef, TypeBound, TypeRef},
visibility::RawVisibility,
AssocItemId, AstIdWithPath, ConstId, ConstLoc, FunctionId, FunctionLoc, HasModule, ImplId,
Expand Down Expand Up @@ -348,7 +348,8 @@ impl MacroRulesData {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProcMacroData {
pub name: Name,
// FIXME: Record deriver helper here?
/// Derive helpers, if this is a derive
pub helpers: Option<Box<[Name]>>,
}

impl ProcMacroData {
Expand All @@ -360,17 +361,23 @@ impl ProcMacroData {
let item_tree = loc.id.item_tree(db);
let makro = &item_tree[loc.id.value];

let name = if let Some(def) = item_tree
let (name, helpers) = if let Some(def) = item_tree
.attrs(db, loc.container.krate(), ModItem::from(loc.id.value).into())
.parse_proc_macro_decl(&makro.name)
{
def.name
(
def.name,
match def.kind {
ProcMacroKind::CustomDerive { helpers } => Some(helpers),
ProcMacroKind::FnLike | ProcMacroKind::Attr => None,
},
)
} else {
// eeeh...
stdx::never!("proc macro declaration is not a proc macro");
makro.name.clone()
(makro.name.clone(), None)
};
Arc::new(ProcMacroData { name })
Arc::new(ProcMacroData { name, helpers })
}
}

Expand Down
12 changes: 8 additions & 4 deletions src/tools/rust-analyzer/crates/hir-def/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pub enum Expr {
Call {
callee: ExprId,
args: Box<[ExprId]>,
is_assignee_expr: bool,
},
MethodCall {
receiver: ExprId,
Expand Down Expand Up @@ -138,6 +139,8 @@ pub enum Expr {
path: Option<Box<Path>>,
fields: Box<[RecordLitField]>,
spread: Option<ExprId>,
ellipsis: bool,
is_assignee_expr: bool,
},
Field {
expr: ExprId,
Expand Down Expand Up @@ -196,6 +199,7 @@ pub enum Expr {
},
Tuple {
exprs: Box<[ExprId]>,
is_assignee_expr: bool,
},
Unsafe {
body: ExprId,
Expand All @@ -211,7 +215,7 @@ pub enum Expr {

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Array {
ElementList(Box<[ExprId]>),
ElementList { elements: Box<[ExprId]>, is_assignee_expr: bool },
Repeat { initializer: ExprId, repeat: ExprId },
}

Expand Down Expand Up @@ -285,7 +289,7 @@ impl Expr {
f(*iterable);
f(*body);
}
Expr::Call { callee, args } => {
Expr::Call { callee, args, .. } => {
f(*callee);
args.iter().copied().for_each(f);
}
Expand Down Expand Up @@ -339,9 +343,9 @@ impl Expr {
| Expr::Box { expr } => {
f(*expr);
}
Expr::Tuple { exprs } => exprs.iter().copied().for_each(f),
Expr::Tuple { exprs, .. } => exprs.iter().copied().for_each(f),
Expr::Array(a) => match a {
Array::ElementList(exprs) => exprs.iter().copied().for_each(f),
Array::ElementList { elements, .. } => elements.iter().copied().for_each(f),
Array::Repeat { initializer, repeat } => {
f(*initializer);
f(*repeat)
Expand Down
35 changes: 25 additions & 10 deletions src/tools/rust-analyzer/crates/hir-def/src/item_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,14 @@ pub struct ItemScope {
attr_macros: FxHashMap<AstId<ast::Item>, MacroCallId>,
/// The derive macro invocations in this scope, keyed by the owner item over the actual derive attributes
/// paired with the derive macro invocations for the specific attribute.
derive_macros: FxHashMap<
AstId<ast::Adt>,
SmallVec<[(AttrId, MacroCallId, SmallVec<[Option<MacroCallId>; 1]>); 1]>,
>,
derive_macros: FxHashMap<AstId<ast::Adt>, SmallVec<[DeriveMacroInvocation; 1]>>,
}

#[derive(Debug, PartialEq, Eq)]
struct DeriveMacroInvocation {
attr_id: AttrId,
attr_call_id: MacroCallId,
derive_call_ids: SmallVec<[Option<MacroCallId>; 1]>,
}

pub(crate) static BUILTIN_SCOPE: Lazy<FxHashMap<Name, PerNs>> = Lazy::new(|| {
Expand Down Expand Up @@ -210,12 +214,14 @@ impl ItemScope {
&mut self,
adt: AstId<ast::Adt>,
call: MacroCallId,
attr_id: AttrId,
id: AttrId,
idx: usize,
) {
if let Some(derives) = self.derive_macros.get_mut(&adt) {
if let Some((.., invocs)) = derives.iter_mut().find(|&&mut (id, ..)| id == attr_id) {
invocs[idx] = Some(call);
if let Some(DeriveMacroInvocation { derive_call_ids, .. }) =
derives.iter_mut().find(|&&mut DeriveMacroInvocation { attr_id, .. }| id == attr_id)
{
derive_call_ids[idx] = Some(call);
}
}
}
Expand All @@ -227,10 +233,14 @@ impl ItemScope {
&mut self,
adt: AstId<ast::Adt>,
attr_id: AttrId,
call_id: MacroCallId,
attr_call_id: MacroCallId,
len: usize,
) {
self.derive_macros.entry(adt).or_default().push((attr_id, call_id, smallvec![None; len]));
self.derive_macros.entry(adt).or_default().push(DeriveMacroInvocation {
attr_id,
attr_call_id,
derive_call_ids: smallvec![None; len],
});
}

pub(crate) fn derive_macro_invocs(
Expand All @@ -242,7 +252,12 @@ impl ItemScope {
),
> + '_ {
self.derive_macros.iter().map(|(k, v)| {
(*k, v.iter().map(|&(attr_id, call_id, ref invocs)| (attr_id, call_id, &**invocs)))
(
*k,
v.iter().map(|DeriveMacroInvocation { attr_id, attr_call_id, derive_call_ids }| {
(*attr_id, *attr_call_id, &**derive_call_ids)
}),
)
})
}

Expand Down
10 changes: 5 additions & 5 deletions src/tools/rust-analyzer/crates/hir-def/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,11 +934,11 @@ fn derive_macro_as_call_id(
derive_attr: AttrId,
derive_pos: u32,
krate: CrateId,
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
) -> Result<MacroCallId, UnresolvedMacro> {
let def: MacroDefId = resolver(item_attr.path.clone())
resolver: impl Fn(path::ModPath) -> Option<(MacroId, MacroDefId)>,
) -> Result<(MacroId, MacroDefId, MacroCallId), UnresolvedMacro> {
let (macro_id, def_id) = resolver(item_attr.path.clone())
.ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
let res = def.as_lazy_macro(
let call_id = def_id.as_lazy_macro(
db.upcast(),
krate,
MacroCallKind::Derive {
Expand All @@ -947,7 +947,7 @@ fn derive_macro_as_call_id(
derive_attr_index: derive_attr.ast_index,
},
);
Ok(res)
Ok((macro_id, def_id, call_id))
}

fn attr_macro_as_call_id(
Expand Down
Loading