Skip to content

Commit e2d2b1c

Browse files
committed
Introduce DeriveResolution.
Making this a proper struct, and giving its fields names, makes things easier to understand.
1 parent 11e95d4 commit e2d2b1c

File tree

5 files changed

+32
-20
lines changed

5 files changed

+32
-20
lines changed

compiler/rustc_builtin_macros/src/derive.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use crate::errors;
33

44
use rustc_ast as ast;
55
use rustc_ast::{GenericParamKind, ItemKind, MetaItemKind, NestedMetaItem, StmtKind};
6-
use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, Indeterminate, MultiItemModifier};
6+
use rustc_expand::base::{
7+
Annotatable, DeriveResolution, ExpandResult, ExtCtxt, Indeterminate, MultiItemModifier,
8+
};
79
use rustc_feature::AttributeTemplate;
810
use rustc_parse::validate_attr;
911
use rustc_session::Session;
@@ -60,7 +62,12 @@ impl MultiItemModifier for Expander {
6062
report_path_args(sess, meta);
6163
meta.path.clone()
6264
})
63-
.map(|path| (path, dummy_annotatable(), None, self.is_const))
65+
.map(|path| DeriveResolution {
66+
path,
67+
item: dummy_annotatable(),
68+
exts: None,
69+
is_const: self.is_const,
70+
})
6471
.collect()
6572
}
6673
_ => vec![],
@@ -69,15 +76,15 @@ impl MultiItemModifier for Expander {
6976
// Do not configure or clone items unless necessary.
7077
match &mut resolutions[..] {
7178
[] => {}
72-
[(_, first_item, ..), others @ ..] => {
73-
*first_item = cfg_eval(
79+
[first, others @ ..] => {
80+
first.item = cfg_eval(
7481
sess,
7582
features,
7683
item.clone(),
7784
ecx.current_expansion.lint_node_id,
7885
);
79-
for (_, item, _, _) in others {
80-
*item = first_item.clone();
86+
for other in others {
87+
other.item = first.item.clone();
8188
}
8289
}
8390
}

compiler/rustc_expand/src/base.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,12 @@ impl SyntaxExtension {
968968
/// Error type that denotes indeterminacy.
969969
pub struct Indeterminate;
970970

971-
pub type DeriveResolutions = Vec<(ast::Path, Annotatable, Option<Lrc<SyntaxExtension>>, bool)>;
971+
pub struct DeriveResolution {
972+
pub path: ast::Path,
973+
pub item: Annotatable,
974+
pub exts: Option<Lrc<SyntaxExtension>>,
975+
pub is_const: bool,
976+
}
972977

973978
pub trait ResolverExpand {
974979
fn next_node_id(&mut self) -> NodeId;
@@ -1011,11 +1016,11 @@ pub trait ResolverExpand {
10111016
&mut self,
10121017
expn_id: LocalExpnId,
10131018
force: bool,
1014-
derive_paths: &dyn Fn() -> DeriveResolutions,
1019+
derive_paths: &dyn Fn() -> Vec<DeriveResolution>,
10151020
) -> Result<(), Indeterminate>;
10161021
/// Take resolutions for paths inside the `#[derive(...)]` attribute with the given `ExpnId`
10171022
/// back from resolver.
1018-
fn take_derive_resolutions(&mut self, expn_id: LocalExpnId) -> Option<DeriveResolutions>;
1023+
fn take_derive_resolutions(&mut self, expn_id: LocalExpnId) -> Option<Vec<DeriveResolution>>;
10191024
/// Path resolution logic for `#[cfg_accessible(path)]`.
10201025
fn cfg_accessible(
10211026
&mut self,

compiler/rustc_expand/src/expand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
482482
derive_invocations.reserve(derives.len());
483483
derives
484484
.into_iter()
485-
.map(|(path, item, _exts, is_const)| {
485+
.map(|DeriveResolution { path, item, exts: _, is_const }| {
486486
// FIXME: Consider using the derive resolutions (`_exts`)
487487
// instead of enqueuing the derives to be resolved again later.
488488
let expn_id = LocalExpnId::fresh_empty();

compiler/rustc_resolve/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use rustc_data_structures::intern::Interned;
3838
use rustc_data_structures::steal::Steal;
3939
use rustc_data_structures::sync::{FreezeReadGuard, Lrc};
4040
use rustc_errors::{Applicability, Diag, ErrCode};
41-
use rustc_expand::base::{DeriveResolutions, SyntaxExtension, SyntaxExtensionKind};
41+
use rustc_expand::base::{DeriveResolution, SyntaxExtension, SyntaxExtensionKind};
4242
use rustc_feature::BUILTIN_ATTRIBUTES;
4343
use rustc_hir::def::Namespace::{self, *};
4444
use rustc_hir::def::NonMacroAttrKind;
@@ -959,7 +959,7 @@ enum BuiltinMacroState {
959959
}
960960

961961
struct DeriveData {
962-
resolutions: DeriveResolutions,
962+
resolutions: Vec<DeriveResolution>,
963963
helper_attrs: Vec<(usize, Ident)>,
964964
has_derive_copy: bool,
965965
}

compiler/rustc_resolve/src/macros.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_attr::StabilityLevel;
1515
use rustc_data_structures::intern::Interned;
1616
use rustc_data_structures::sync::Lrc;
1717
use rustc_errors::{Applicability, StashKey};
18-
use rustc_expand::base::{Annotatable, DeriveResolutions, Indeterminate, ResolverExpand};
18+
use rustc_expand::base::{Annotatable, DeriveResolution, Indeterminate, ResolverExpand};
1919
use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind};
2020
use rustc_expand::compile_declarative_macro;
2121
use rustc_expand::expand::{AstFragment, Invocation, InvocationKind, SupportsMacroExpansion};
@@ -344,7 +344,7 @@ impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> {
344344
&mut self,
345345
expn_id: LocalExpnId,
346346
force: bool,
347-
derive_paths: &dyn Fn() -> DeriveResolutions,
347+
derive_paths: &dyn Fn() -> Vec<DeriveResolution>,
348348
) -> Result<(), Indeterminate> {
349349
// Block expansion of the container until we resolve all derives in it.
350350
// This is required for two reasons:
@@ -360,19 +360,19 @@ impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> {
360360
has_derive_copy: false,
361361
});
362362
let parent_scope = self.invocation_parent_scopes[&expn_id];
363-
for (i, (path, _, opt_ext, _)) in entry.resolutions.iter_mut().enumerate() {
364-
if opt_ext.is_none() {
365-
*opt_ext = Some(
363+
for (i, resolution) in entry.resolutions.iter_mut().enumerate() {
364+
if resolution.exts.is_none() {
365+
resolution.exts = Some(
366366
match self.resolve_macro_path(
367-
path,
367+
&resolution.path,
368368
Some(MacroKind::Derive),
369369
&parent_scope,
370370
true,
371371
force,
372372
) {
373373
Ok((Some(ext), _)) => {
374374
if !ext.helper_attrs.is_empty() {
375-
let last_seg = path.segments.last().unwrap();
375+
let last_seg = resolution.path.segments.last().unwrap();
376376
let span = last_seg.ident.span.normalize_to_macros_2_0();
377377
entry.helper_attrs.extend(
378378
ext.helper_attrs
@@ -416,7 +416,7 @@ impl<'a, 'tcx> ResolverExpand for Resolver<'a, 'tcx> {
416416
Ok(())
417417
}
418418

419-
fn take_derive_resolutions(&mut self, expn_id: LocalExpnId) -> Option<DeriveResolutions> {
419+
fn take_derive_resolutions(&mut self, expn_id: LocalExpnId) -> Option<Vec<DeriveResolution>> {
420420
self.derive_data.remove(&expn_id).map(|data| data.resolutions)
421421
}
422422

0 commit comments

Comments
 (0)