Skip to content

Commit 05e678c

Browse files
committed
Auto merge of rust-lang#99756 - fasterthanlime:ra-sync-and-pms-component, r=Mark-Simulacrum
Sync `rust-analyzer`, add `rust-analyzer-proc-macro-srv` binary to Rustc component As discussed earlier with `@jyn514` and `@pietroalbini,` I'm also going to use this PR to have `dist::Rustc` build the `rust-analyzer-proc-macro-srv` binary introduced in: * rust-lang/rust-analyzer#12871
2 parents 48316df + 6ea7d82 commit 05e678c

Some content is hidden

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

51 files changed

+681
-210
lines changed

Diff for: src/bootstrap/builder.rs

+1
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ impl<'a> Builder<'a> {
601601
tool::Cargo,
602602
tool::Rls,
603603
tool::RustAnalyzer,
604+
tool::RustAnalyzerProcMacroSrv,
604605
tool::RustDemangler,
605606
tool::Rustdoc,
606607
tool::Clippy,

Diff for: src/bootstrap/dist.rs

+12
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,18 @@ impl Step for Rustc {
362362

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

365+
let ra_proc_macro_srv = builder
366+
.ensure(tool::RustAnalyzerProcMacroSrv {
367+
compiler: builder.compiler_for(
368+
compiler.stage,
369+
builder.config.build,
370+
compiler.host,
371+
),
372+
target: compiler.host,
373+
})
374+
.expect("rust-analyzer-proc-macro-server always builds");
375+
builder.install(&ra_proc_macro_srv, &image.join("libexec"), 0o755);
376+
365377
let libdir_relative = builder.libdir_relative(compiler);
366378

367379
// Copy runtime DLLs needed by the compiler

Diff for: src/bootstrap/tool.rs

+44
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,50 @@ impl Step for RustAnalyzer {
727727
}
728728
}
729729

730+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
731+
pub struct RustAnalyzerProcMacroSrv {
732+
pub compiler: Compiler,
733+
pub target: TargetSelection,
734+
}
735+
736+
impl Step for RustAnalyzerProcMacroSrv {
737+
type Output = Option<PathBuf>;
738+
const DEFAULT: bool = true;
739+
const ONLY_HOSTS: bool = false;
740+
741+
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
742+
let builder = run.builder;
743+
run.path("src/tools/rust-analyzer").default_condition(
744+
builder.config.extended
745+
&& builder
746+
.config
747+
.tools
748+
.as_ref()
749+
.map_or(true, |tools| tools.iter().any(|tool| tool == "rust-analyzer")),
750+
)
751+
}
752+
753+
fn make_run(run: RunConfig<'_>) {
754+
run.builder.ensure(RustAnalyzerProcMacroSrv {
755+
compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
756+
target: run.target,
757+
});
758+
}
759+
760+
fn run(self, builder: &Builder<'_>) -> Option<PathBuf> {
761+
builder.ensure(ToolBuild {
762+
compiler: self.compiler,
763+
target: self.target,
764+
tool: "rust-analyzer-proc-macro-srv",
765+
mode: Mode::ToolStd,
766+
path: "src/tools/rust-analyzer/crates/proc-macro-srv-cli",
767+
extra_features: vec!["proc-macro-srv/sysroot-abi".to_owned()],
768+
is_optional_tool: false,
769+
source_type: SourceType::InTree,
770+
})
771+
}
772+
}
773+
730774
macro_rules! tool_extended {
731775
(($sel:ident, $builder:ident),
732776
$($name:ident,

Diff for: src/tools/rust-analyzer/Cargo.lock

+7
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,13 @@ dependencies = [
11981198
"tt",
11991199
]
12001200

1201+
[[package]]
1202+
name = "proc-macro-srv-cli"
1203+
version = "0.0.0"
1204+
dependencies = [
1205+
"proc-macro-srv",
1206+
]
1207+
12011208
[[package]]
12021209
name = "proc-macro-test"
12031210
version = "0.0.0"

Diff for: src/tools/rust-analyzer/crates/hir-def/src/body/lower.rs

+33-7
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ pub(super) fn lower(
9696
expander,
9797
name_to_pat_grouping: Default::default(),
9898
is_lowering_inside_or_pat: false,
99+
is_lowering_assignee_expr: false,
99100
}
100101
.collect(params, body)
101102
}
@@ -109,6 +110,7 @@ struct ExprCollector<'a> {
109110
// a poor-mans union-find?
110111
name_to_pat_grouping: FxHashMap<Name, Vec<PatId>>,
111112
is_lowering_inside_or_pat: bool,
113+
is_lowering_assignee_expr: bool,
112114
}
113115

114116
impl ExprCollector<'_> {
@@ -283,7 +285,10 @@ impl ExprCollector<'_> {
283285
} else {
284286
Box::default()
285287
};
286-
self.alloc_expr(Expr::Call { callee, args }, syntax_ptr)
288+
self.alloc_expr(
289+
Expr::Call { callee, args, is_assignee_expr: self.is_lowering_assignee_expr },
290+
syntax_ptr,
291+
)
287292
}
288293
ast::Expr::MethodCallExpr(e) => {
289294
let receiver = self.collect_expr_opt(e.receiver());
@@ -359,6 +364,7 @@ impl ExprCollector<'_> {
359364
ast::Expr::RecordExpr(e) => {
360365
let path =
361366
e.path().and_then(|path| self.expander.parse_path(self.db, path)).map(Box::new);
367+
let is_assignee_expr = self.is_lowering_assignee_expr;
362368
let record_lit = if let Some(nfl) = e.record_expr_field_list() {
363369
let fields = nfl
364370
.fields()
@@ -378,9 +384,16 @@ impl ExprCollector<'_> {
378384
})
379385
.collect();
380386
let spread = nfl.spread().map(|s| self.collect_expr(s));
381-
Expr::RecordLit { path, fields, spread }
387+
let ellipsis = nfl.dotdot_token().is_some();
388+
Expr::RecordLit { path, fields, spread, ellipsis, is_assignee_expr }
382389
} else {
383-
Expr::RecordLit { path, fields: Box::default(), spread: None }
390+
Expr::RecordLit {
391+
path,
392+
fields: Box::default(),
393+
spread: None,
394+
ellipsis: false,
395+
is_assignee_expr,
396+
}
384397
};
385398

386399
self.alloc_expr(record_lit, syntax_ptr)
@@ -458,14 +471,21 @@ impl ExprCollector<'_> {
458471
)
459472
}
460473
ast::Expr::BinExpr(e) => {
474+
let op = e.op_kind();
475+
if let Some(ast::BinaryOp::Assignment { op: None }) = op {
476+
self.is_lowering_assignee_expr = true;
477+
}
461478
let lhs = self.collect_expr_opt(e.lhs());
479+
self.is_lowering_assignee_expr = false;
462480
let rhs = self.collect_expr_opt(e.rhs());
463-
let op = e.op_kind();
464481
self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr)
465482
}
466483
ast::Expr::TupleExpr(e) => {
467484
let exprs = e.fields().map(|expr| self.collect_expr(expr)).collect();
468-
self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr)
485+
self.alloc_expr(
486+
Expr::Tuple { exprs, is_assignee_expr: self.is_lowering_assignee_expr },
487+
syntax_ptr,
488+
)
469489
}
470490
ast::Expr::BoxExpr(e) => {
471491
let expr = self.collect_expr_opt(e.expr());
@@ -477,8 +497,14 @@ impl ExprCollector<'_> {
477497

478498
match kind {
479499
ArrayExprKind::ElementList(e) => {
480-
let exprs = e.map(|expr| self.collect_expr(expr)).collect();
481-
self.alloc_expr(Expr::Array(Array::ElementList(exprs)), syntax_ptr)
500+
let elements = e.map(|expr| self.collect_expr(expr)).collect();
501+
self.alloc_expr(
502+
Expr::Array(Array::ElementList {
503+
elements,
504+
is_assignee_expr: self.is_lowering_assignee_expr,
505+
}),
506+
syntax_ptr,
507+
)
482508
}
483509
ArrayExprKind::Repeat { initializer, repeat } => {
484510
let initializer = self.collect_expr_opt(initializer);

Diff for: src/tools/rust-analyzer/crates/hir-def/src/data.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
db::DefDatabase,
1313
intern::Interned,
1414
item_tree::{self, AssocItem, FnFlags, ItemTree, ItemTreeId, ModItem, Param, TreeId},
15-
nameres::{attr_resolution::ResolvedAttr, DefMap},
15+
nameres::{attr_resolution::ResolvedAttr, proc_macro::ProcMacroKind, DefMap},
1616
type_ref::{TraitRef, TypeBound, TypeRef},
1717
visibility::RawVisibility,
1818
AssocItemId, AstIdWithPath, ConstId, ConstLoc, FunctionId, FunctionLoc, HasModule, ImplId,
@@ -348,7 +348,8 @@ impl MacroRulesData {
348348
#[derive(Debug, Clone, PartialEq, Eq)]
349349
pub struct ProcMacroData {
350350
pub name: Name,
351-
// FIXME: Record deriver helper here?
351+
/// Derive helpers, if this is a derive
352+
pub helpers: Option<Box<[Name]>>,
352353
}
353354

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

363-
let name = if let Some(def) = item_tree
364+
let (name, helpers) = if let Some(def) = item_tree
364365
.attrs(db, loc.container.krate(), ModItem::from(loc.id.value).into())
365366
.parse_proc_macro_decl(&makro.name)
366367
{
367-
def.name
368+
(
369+
def.name,
370+
match def.kind {
371+
ProcMacroKind::CustomDerive { helpers } => Some(helpers),
372+
ProcMacroKind::FnLike | ProcMacroKind::Attr => None,
373+
},
374+
)
368375
} else {
369376
// eeeh...
370377
stdx::never!("proc macro declaration is not a proc macro");
371-
makro.name.clone()
378+
(makro.name.clone(), None)
372379
};
373-
Arc::new(ProcMacroData { name })
380+
Arc::new(ProcMacroData { name, helpers })
374381
}
375382
}
376383

Diff for: src/tools/rust-analyzer/crates/hir-def/src/expr.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ pub enum Expr {
110110
Call {
111111
callee: ExprId,
112112
args: Box<[ExprId]>,
113+
is_assignee_expr: bool,
113114
},
114115
MethodCall {
115116
receiver: ExprId,
@@ -138,6 +139,8 @@ pub enum Expr {
138139
path: Option<Box<Path>>,
139140
fields: Box<[RecordLitField]>,
140141
spread: Option<ExprId>,
142+
ellipsis: bool,
143+
is_assignee_expr: bool,
141144
},
142145
Field {
143146
expr: ExprId,
@@ -196,6 +199,7 @@ pub enum Expr {
196199
},
197200
Tuple {
198201
exprs: Box<[ExprId]>,
202+
is_assignee_expr: bool,
199203
},
200204
Unsafe {
201205
body: ExprId,
@@ -211,7 +215,7 @@ pub enum Expr {
211215

212216
#[derive(Debug, Clone, Eq, PartialEq)]
213217
pub enum Array {
214-
ElementList(Box<[ExprId]>),
218+
ElementList { elements: Box<[ExprId]>, is_assignee_expr: bool },
215219
Repeat { initializer: ExprId, repeat: ExprId },
216220
}
217221

@@ -285,7 +289,7 @@ impl Expr {
285289
f(*iterable);
286290
f(*body);
287291
}
288-
Expr::Call { callee, args } => {
292+
Expr::Call { callee, args, .. } => {
289293
f(*callee);
290294
args.iter().copied().for_each(f);
291295
}
@@ -339,9 +343,9 @@ impl Expr {
339343
| Expr::Box { expr } => {
340344
f(*expr);
341345
}
342-
Expr::Tuple { exprs } => exprs.iter().copied().for_each(f),
346+
Expr::Tuple { exprs, .. } => exprs.iter().copied().for_each(f),
343347
Expr::Array(a) => match a {
344-
Array::ElementList(exprs) => exprs.iter().copied().for_each(f),
348+
Array::ElementList { elements, .. } => elements.iter().copied().for_each(f),
345349
Array::Repeat { initializer, repeat } => {
346350
f(*initializer);
347351
f(*repeat)

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

+25-10
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,14 @@ pub struct ItemScope {
6666
attr_macros: FxHashMap<AstId<ast::Item>, MacroCallId>,
6767
/// The derive macro invocations in this scope, keyed by the owner item over the actual derive attributes
6868
/// paired with the derive macro invocations for the specific attribute.
69-
derive_macros: FxHashMap<
70-
AstId<ast::Adt>,
71-
SmallVec<[(AttrId, MacroCallId, SmallVec<[Option<MacroCallId>; 1]>); 1]>,
72-
>,
69+
derive_macros: FxHashMap<AstId<ast::Adt>, SmallVec<[DeriveMacroInvocation; 1]>>,
70+
}
71+
72+
#[derive(Debug, PartialEq, Eq)]
73+
struct DeriveMacroInvocation {
74+
attr_id: AttrId,
75+
attr_call_id: MacroCallId,
76+
derive_call_ids: SmallVec<[Option<MacroCallId>; 1]>,
7377
}
7478

7579
pub(crate) static BUILTIN_SCOPE: Lazy<FxHashMap<Name, PerNs>> = Lazy::new(|| {
@@ -210,12 +214,14 @@ impl ItemScope {
210214
&mut self,
211215
adt: AstId<ast::Adt>,
212216
call: MacroCallId,
213-
attr_id: AttrId,
217+
id: AttrId,
214218
idx: usize,
215219
) {
216220
if let Some(derives) = self.derive_macros.get_mut(&adt) {
217-
if let Some((.., invocs)) = derives.iter_mut().find(|&&mut (id, ..)| id == attr_id) {
218-
invocs[idx] = Some(call);
221+
if let Some(DeriveMacroInvocation { derive_call_ids, .. }) =
222+
derives.iter_mut().find(|&&mut DeriveMacroInvocation { attr_id, .. }| id == attr_id)
223+
{
224+
derive_call_ids[idx] = Some(call);
219225
}
220226
}
221227
}
@@ -227,10 +233,14 @@ impl ItemScope {
227233
&mut self,
228234
adt: AstId<ast::Adt>,
229235
attr_id: AttrId,
230-
call_id: MacroCallId,
236+
attr_call_id: MacroCallId,
231237
len: usize,
232238
) {
233-
self.derive_macros.entry(adt).or_default().push((attr_id, call_id, smallvec![None; len]));
239+
self.derive_macros.entry(adt).or_default().push(DeriveMacroInvocation {
240+
attr_id,
241+
attr_call_id,
242+
derive_call_ids: smallvec![None; len],
243+
});
234244
}
235245

236246
pub(crate) fn derive_macro_invocs(
@@ -242,7 +252,12 @@ impl ItemScope {
242252
),
243253
> + '_ {
244254
self.derive_macros.iter().map(|(k, v)| {
245-
(*k, v.iter().map(|&(attr_id, call_id, ref invocs)| (attr_id, call_id, &**invocs)))
255+
(
256+
*k,
257+
v.iter().map(|DeriveMacroInvocation { attr_id, attr_call_id, derive_call_ids }| {
258+
(*attr_id, *attr_call_id, &**derive_call_ids)
259+
}),
260+
)
246261
})
247262
}
248263

Diff for: src/tools/rust-analyzer/crates/hir-def/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -934,11 +934,11 @@ fn derive_macro_as_call_id(
934934
derive_attr: AttrId,
935935
derive_pos: u32,
936936
krate: CrateId,
937-
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
938-
) -> Result<MacroCallId, UnresolvedMacro> {
939-
let def: MacroDefId = resolver(item_attr.path.clone())
937+
resolver: impl Fn(path::ModPath) -> Option<(MacroId, MacroDefId)>,
938+
) -> Result<(MacroId, MacroDefId, MacroCallId), UnresolvedMacro> {
939+
let (macro_id, def_id) = resolver(item_attr.path.clone())
940940
.ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
941-
let res = def.as_lazy_macro(
941+
let call_id = def_id.as_lazy_macro(
942942
db.upcast(),
943943
krate,
944944
MacroCallKind::Derive {
@@ -947,7 +947,7 @@ fn derive_macro_as_call_id(
947947
derive_attr_index: derive_attr.ast_index,
948948
},
949949
);
950-
Ok(res)
950+
Ok((macro_id, def_id, call_id))
951951
}
952952

953953
fn attr_macro_as_call_id(

0 commit comments

Comments
 (0)