Skip to content

Commit 0ffc1ef

Browse files
committed
Auto merge of #16775 - Veykril:parse-macro-parse, r=Veykril
fix: Remove accidental dependency between `parse_macro_expansion` and `parse` Turns out my idea from #15251 causes all builtin derive expansions to obviously rely on the main parse, meaning the entire `macro_arg` layer becomes kind of pointless. So this reverts that PR again.
2 parents a1fda64 + bd0ffb0 commit 0ffc1ef

File tree

3 files changed

+46
-51
lines changed

3 files changed

+46
-51
lines changed

crates/hir-expand/src/builtin_derive_macro.rs

+37-36
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ use crate::{
1010
hygiene::span_with_def_site_ctxt,
1111
name::{AsName, Name},
1212
quote::dollar_crate,
13-
span_map::SpanMapRef,
13+
span_map::ExpansionSpanMap,
1414
tt,
1515
};
16-
use syntax::ast::{self, AstNode, FieldList, HasAttrs, HasGenericParams, HasName, HasTypeBounds};
16+
use syntax::ast::{
17+
self, AstNode, FieldList, HasAttrs, HasGenericParams, HasModuleItem, HasName, HasTypeBounds,
18+
};
1719

1820
use crate::{db::ExpandDatabase, name, quote, ExpandError, ExpandResult};
1921

@@ -25,7 +27,7 @@ macro_rules! register_builtin {
2527
}
2628

2729
impl BuiltinDeriveExpander {
28-
pub fn expander(&self) -> fn(Span, &ast::Adt, SpanMapRef<'_>) -> ExpandResult<tt::Subtree> {
30+
pub fn expander(&self) -> fn(Span, &tt::Subtree) -> ExpandResult<tt::Subtree> {
2931
match *self {
3032
$( BuiltinDeriveExpander::$trait => $expand, )*
3133
}
@@ -47,12 +49,11 @@ impl BuiltinDeriveExpander {
4749
&self,
4850
db: &dyn ExpandDatabase,
4951
id: MacroCallId,
50-
tt: &ast::Adt,
51-
token_map: SpanMapRef<'_>,
52+
tt: &tt::Subtree,
5253
) -> ExpandResult<tt::Subtree> {
5354
let span = db.lookup_intern_macro_call(id).call_site;
5455
let span = span_with_def_site_ctxt(db, span, id);
55-
self.expander()(span, tt, token_map)
56+
self.expander()(span, tt)
5657
}
5758
}
5859

@@ -126,7 +127,7 @@ impl VariantShape {
126127
}
127128
}
128129

129-
fn from(tm: SpanMapRef<'_>, value: Option<FieldList>) -> Result<Self, ExpandError> {
130+
fn from(tm: &ExpansionSpanMap, value: Option<FieldList>) -> Result<Self, ExpandError> {
130131
let r = match value {
131132
None => VariantShape::Unit,
132133
Some(FieldList::RecordFieldList(it)) => VariantShape::Struct(
@@ -202,11 +203,13 @@ struct BasicAdtInfo {
202203
associated_types: Vec<tt::Subtree>,
203204
}
204205

205-
fn parse_adt(
206-
tm: SpanMapRef<'_>,
207-
adt: &ast::Adt,
208-
call_site: Span,
209-
) -> Result<BasicAdtInfo, ExpandError> {
206+
fn parse_adt(tt: &tt::Subtree, call_site: Span) -> Result<BasicAdtInfo, ExpandError> {
207+
let (parsed, tm) = &mbe::token_tree_to_syntax_node(tt, mbe::TopEntryPoint::MacroItems);
208+
let macro_items = ast::MacroItems::cast(parsed.syntax_node())
209+
.ok_or_else(|| ExpandError::other("invalid item definition"))?;
210+
let item = macro_items.items().next().ok_or_else(|| ExpandError::other("no item found"))?;
211+
let adt = &ast::Adt::cast(item.syntax().clone())
212+
.ok_or_else(|| ExpandError::other("expected struct, enum or union"))?;
210213
let (name, generic_param_list, where_clause, shape) = match adt {
211214
ast::Adt::Struct(it) => (
212215
it.name(),
@@ -322,14 +325,14 @@ fn parse_adt(
322325
}
323326

324327
fn name_to_token(
325-
token_map: SpanMapRef<'_>,
328+
token_map: &ExpansionSpanMap,
326329
name: Option<ast::Name>,
327330
) -> Result<tt::Ident, ExpandError> {
328331
let name = name.ok_or_else(|| {
329332
debug!("parsed item has no name");
330333
ExpandError::other("missing name")
331334
})?;
332-
let span = token_map.span_for_range(name.syntax().text_range());
335+
let span = token_map.span_at(name.syntax().text_range().start());
333336
let name_token = tt::Ident { span, text: name.text().into() };
334337
Ok(name_token)
335338
}
@@ -366,14 +369,12 @@ fn name_to_token(
366369
/// where B1, ..., BN are the bounds given by `bounds_paths`. Z is a phantom type, and
367370
/// therefore does not get bound by the derived trait.
368371
fn expand_simple_derive(
369-
// FIXME: use
370372
invoc_span: Span,
371-
tt: &ast::Adt,
372-
tm: SpanMapRef<'_>,
373+
tt: &tt::Subtree,
373374
trait_path: tt::Subtree,
374375
make_trait_body: impl FnOnce(&BasicAdtInfo) -> tt::Subtree,
375376
) -> ExpandResult<tt::Subtree> {
376-
let info = match parse_adt(tm, tt, invoc_span) {
377+
let info = match parse_adt(tt, invoc_span) {
377378
Ok(info) => info,
378379
Err(e) => {
379380
return ExpandResult::new(
@@ -416,14 +417,14 @@ fn expand_simple_derive(
416417
ExpandResult::ok(expanded)
417418
}
418419

419-
fn copy_expand(span: Span, tt: &ast::Adt, tm: SpanMapRef<'_>) -> ExpandResult<tt::Subtree> {
420+
fn copy_expand(span: Span, tt: &tt::Subtree) -> ExpandResult<tt::Subtree> {
420421
let krate = dollar_crate(span);
421-
expand_simple_derive(span, tt, tm, quote! {span => #krate::marker::Copy }, |_| quote! {span =>})
422+
expand_simple_derive(span, tt, quote! {span => #krate::marker::Copy }, |_| quote! {span =>})
422423
}
423424

424-
fn clone_expand(span: Span, tt: &ast::Adt, tm: SpanMapRef<'_>) -> ExpandResult<tt::Subtree> {
425+
fn clone_expand(span: Span, tt: &tt::Subtree) -> ExpandResult<tt::Subtree> {
425426
let krate = dollar_crate(span);
426-
expand_simple_derive(span, tt, tm, quote! {span => #krate::clone::Clone }, |adt| {
427+
expand_simple_derive(span, tt, quote! {span => #krate::clone::Clone }, |adt| {
427428
if matches!(adt.shape, AdtShape::Union) {
428429
let star = tt::Punct { char: '*', spacing: ::tt::Spacing::Alone, span };
429430
return quote! {span =>
@@ -472,9 +473,9 @@ fn and_and(span: Span) -> tt::Subtree {
472473
quote! {span => #and& }
473474
}
474475

475-
fn default_expand(span: Span, tt: &ast::Adt, tm: SpanMapRef<'_>) -> ExpandResult<tt::Subtree> {
476+
fn default_expand(span: Span, tt: &tt::Subtree) -> ExpandResult<tt::Subtree> {
476477
let krate = &dollar_crate(span);
477-
expand_simple_derive(span, tt, tm, quote! {span => #krate::default::Default }, |adt| {
478+
expand_simple_derive(span, tt, quote! {span => #krate::default::Default }, |adt| {
478479
let body = match &adt.shape {
479480
AdtShape::Struct(fields) => {
480481
let name = &adt.name;
@@ -511,9 +512,9 @@ fn default_expand(span: Span, tt: &ast::Adt, tm: SpanMapRef<'_>) -> ExpandResult
511512
})
512513
}
513514

514-
fn debug_expand(span: Span, tt: &ast::Adt, tm: SpanMapRef<'_>) -> ExpandResult<tt::Subtree> {
515+
fn debug_expand(span: Span, tt: &tt::Subtree) -> ExpandResult<tt::Subtree> {
515516
let krate = &dollar_crate(span);
516-
expand_simple_derive(span, tt, tm, quote! {span => #krate::fmt::Debug }, |adt| {
517+
expand_simple_derive(span, tt, quote! {span => #krate::fmt::Debug }, |adt| {
517518
let for_variant = |name: String, v: &VariantShape| match v {
518519
VariantShape::Struct(fields) => {
519520
let for_fields = fields.iter().map(|it| {
@@ -583,9 +584,9 @@ fn debug_expand(span: Span, tt: &ast::Adt, tm: SpanMapRef<'_>) -> ExpandResult<t
583584
})
584585
}
585586

586-
fn hash_expand(span: Span, tt: &ast::Adt, tm: SpanMapRef<'_>) -> ExpandResult<tt::Subtree> {
587+
fn hash_expand(span: Span, tt: &tt::Subtree) -> ExpandResult<tt::Subtree> {
587588
let krate = &dollar_crate(span);
588-
expand_simple_derive(span, tt, tm, quote! {span => #krate::hash::Hash }, |adt| {
589+
expand_simple_derive(span, tt, quote! {span => #krate::hash::Hash }, |adt| {
589590
if matches!(adt.shape, AdtShape::Union) {
590591
// FIXME: Return expand error here
591592
return quote! {span =>};
@@ -630,14 +631,14 @@ fn hash_expand(span: Span, tt: &ast::Adt, tm: SpanMapRef<'_>) -> ExpandResult<tt
630631
})
631632
}
632633

633-
fn eq_expand(span: Span, tt: &ast::Adt, tm: SpanMapRef<'_>) -> ExpandResult<tt::Subtree> {
634+
fn eq_expand(span: Span, tt: &tt::Subtree) -> ExpandResult<tt::Subtree> {
634635
let krate = dollar_crate(span);
635-
expand_simple_derive(span, tt, tm, quote! {span => #krate::cmp::Eq }, |_| quote! {span =>})
636+
expand_simple_derive(span, tt, quote! {span => #krate::cmp::Eq }, |_| quote! {span =>})
636637
}
637638

638-
fn partial_eq_expand(span: Span, tt: &ast::Adt, tm: SpanMapRef<'_>) -> ExpandResult<tt::Subtree> {
639+
fn partial_eq_expand(span: Span, tt: &tt::Subtree) -> ExpandResult<tt::Subtree> {
639640
let krate = dollar_crate(span);
640-
expand_simple_derive(span, tt, tm, quote! {span => #krate::cmp::PartialEq }, |adt| {
641+
expand_simple_derive(span, tt, quote! {span => #krate::cmp::PartialEq }, |adt| {
641642
if matches!(adt.shape, AdtShape::Union) {
642643
// FIXME: Return expand error here
643644
return quote! {span =>};
@@ -707,9 +708,9 @@ fn self_and_other_patterns(
707708
(self_patterns, other_patterns)
708709
}
709710

710-
fn ord_expand(span: Span, tt: &ast::Adt, tm: SpanMapRef<'_>) -> ExpandResult<tt::Subtree> {
711+
fn ord_expand(span: Span, tt: &tt::Subtree) -> ExpandResult<tt::Subtree> {
711712
let krate = &dollar_crate(span);
712-
expand_simple_derive(span, tt, tm, quote! {span => #krate::cmp::Ord }, |adt| {
713+
expand_simple_derive(span, tt, quote! {span => #krate::cmp::Ord }, |adt| {
713714
fn compare(
714715
krate: &tt::Ident,
715716
left: tt::Subtree,
@@ -765,9 +766,9 @@ fn ord_expand(span: Span, tt: &ast::Adt, tm: SpanMapRef<'_>) -> ExpandResult<tt:
765766
})
766767
}
767768

768-
fn partial_ord_expand(span: Span, tt: &ast::Adt, tm: SpanMapRef<'_>) -> ExpandResult<tt::Subtree> {
769+
fn partial_ord_expand(span: Span, tt: &tt::Subtree) -> ExpandResult<tt::Subtree> {
769770
let krate = &dollar_crate(span);
770-
expand_simple_derive(span, tt, tm, quote! {span => #krate::cmp::PartialOrd }, |adt| {
771+
expand_simple_derive(span, tt, quote! {span => #krate::cmp::PartialOrd }, |adt| {
771772
fn compare(
772773
krate: &tt::Ident,
773774
left: tt::Subtree,

crates/hir-expand/src/db.rs

+7-15
Original file line numberDiff line numberDiff line change
@@ -215,18 +215,16 @@ pub fn expand_speculative(
215215
MacroDefKind::BuiltInAttr(BuiltinAttrExpander::Derive, _) => {
216216
pseudo_derive_attr_expansion(&tt, attr_arg.as_ref()?, loc.call_site)
217217
}
218-
MacroDefKind::BuiltInDerive(expander, ..) => {
219-
// this cast is a bit sus, can we avoid losing the typedness here?
220-
let adt = ast::Adt::cast(speculative_args.clone()).unwrap();
221-
expander.expand(db, actual_macro_call, &adt, span_map)
222-
}
223218
MacroDefKind::Declarative(it) => db.decl_macro_expander(loc.krate, it).expand_unhygienic(
224219
db,
225220
tt,
226221
loc.def.krate,
227222
loc.call_site,
228223
),
229224
MacroDefKind::BuiltIn(it, _) => it.expand(db, actual_macro_call, &tt).map_err(Into::into),
225+
MacroDefKind::BuiltInDerive(it, ..) => {
226+
it.expand(db, actual_macro_call, &tt).map_err(Into::into)
227+
}
230228
MacroDefKind::BuiltInEager(it, _) => {
231229
it.expand(db, actual_macro_call, &tt).map_err(Into::into)
232230
}
@@ -321,6 +319,7 @@ pub(crate) fn parse_with_map(
321319
}
322320
}
323321

322+
// FIXME: for derive attributes, this will return separate copies of the same structures!
324323
fn macro_arg(
325324
db: &dyn ExpandDatabase,
326325
id: MacroCallId,
@@ -526,16 +525,6 @@ fn macro_expand(
526525

527526
let ExpandResult { value: tt, mut err } = match loc.def.kind {
528527
MacroDefKind::ProcMacro(..) => return db.expand_proc_macro(macro_call_id).map(CowArc::Arc),
529-
MacroDefKind::BuiltInDerive(expander, ..) => {
530-
let (root, map) = parse_with_map(db, loc.kind.file_id());
531-
let root = root.syntax_node();
532-
let MacroCallKind::Derive { ast_id, .. } = loc.kind else { unreachable!() };
533-
let node = ast_id.to_ptr(db).to_node(&root);
534-
535-
// FIXME: Use censoring
536-
let _censor = censor_for_macro_input(&loc, node.syntax());
537-
expander.expand(db, macro_call_id, &node, map.as_ref())
538-
}
539528
_ => {
540529
let ValueResult { value: (macro_arg, undo_info), err } = db.macro_arg(macro_call_id);
541530
let format_parse_err = |err: Arc<Box<[SyntaxError]>>| {
@@ -569,6 +558,9 @@ fn macro_expand(
569558
err: err.map(format_parse_err),
570559
};
571560
}
561+
MacroDefKind::BuiltInDerive(it, _) => {
562+
it.expand(db, macro_call_id, arg).map_err(Into::into)
563+
}
572564
MacroDefKind::BuiltInEager(it, _) => {
573565
it.expand(db, macro_call_id, arg).map_err(Into::into)
574566
}

crates/hir-expand/src/span_map.rs

+2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ impl mbe::SpanMapper<Span> for SpanMap {
3131
self.span_for_range(range)
3232
}
3333
}
34+
3435
impl mbe::SpanMapper<Span> for SpanMapRef<'_> {
3536
fn span_for(&self, range: TextRange) -> Span {
3637
self.span_for_range(range)
3738
}
3839
}
40+
3941
impl SpanMap {
4042
pub fn span_for_range(&self, range: TextRange) -> Span {
4143
match self {

0 commit comments

Comments
 (0)