Skip to content

Commit 12eb261

Browse files
committed
Auto merge of #148808 - nnethercote:resolve-cleanups, r=chenyukang
Some resolve cleanups Minor improvements I found while looking over this code. r? `@petrochenkov`
2 parents 2fcbda6 + 6217cad commit 12eb261

File tree

5 files changed

+46
-66
lines changed

5 files changed

+46
-66
lines changed

compiler/rustc_builtin_macros/src/derive.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl MultiItemModifier for Expander {
6767
.map(|path| DeriveResolution {
6868
path,
6969
item: dummy_annotatable(),
70-
exts: None,
70+
has_been_handled: false,
7171
is_const: self.is_const,
7272
})
7373
.collect()

compiler/rustc_expand/src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ pub struct Indeterminate;
10991099
pub struct DeriveResolution {
11001100
pub path: ast::Path,
11011101
pub item: Annotatable,
1102-
pub exts: Option<Arc<SyntaxExtension>>,
1102+
pub has_been_handled: bool,
11031103
pub is_const: bool,
11041104
}
11051105

compiler/rustc_expand/src/expand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
564564
derive_invocations.reserve(derives.len());
565565
derives
566566
.into_iter()
567-
.map(|DeriveResolution { path, item, exts: _, is_const }| {
567+
.map(|DeriveResolution { path, item, is_const, .. }| {
568568
// FIXME: Consider using the derive resolutions (`_exts`)
569569
// instead of enqueuing the derives to be resolved again later.
570570
// Note that this can result in duplicate diagnostics.

compiler/rustc_resolve/src/ident.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -458,14 +458,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
458458
let mut result = Err(Determinacy::Determined);
459459
for derive in parent_scope.derives {
460460
let parent_scope = &ParentScope { derives: &[], ..*parent_scope };
461-
match this.reborrow().resolve_macro_path(
461+
match this.reborrow().resolve_derive_macro_path(
462462
derive,
463-
MacroKind::Derive,
464463
parent_scope,
465-
true,
466464
force,
467465
ignore_import,
468-
None,
469466
) {
470467
Ok((Some(ext), _)) => {
471468
if ext.helper_attrs.contains(&ident.name) {

compiler/rustc_resolve/src/macros.rs

Lines changed: 42 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -394,38 +394,31 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
394394
});
395395
let parent_scope = self.invocation_parent_scopes[&expn_id];
396396
for (i, resolution) in entry.resolutions.iter_mut().enumerate() {
397-
if resolution.exts.is_none() {
398-
resolution.exts = Some(
399-
match self.cm().resolve_macro_path(
400-
&resolution.path,
401-
MacroKind::Derive,
402-
&parent_scope,
403-
true,
404-
force,
405-
None,
406-
None,
407-
) {
408-
Ok((Some(ext), _)) => {
409-
if !ext.helper_attrs.is_empty() {
410-
let last_seg = resolution.path.segments.last().unwrap();
411-
let span = last_seg.ident.span.normalize_to_macros_2_0();
412-
entry.helper_attrs.extend(
413-
ext.helper_attrs
414-
.iter()
415-
.map(|name| (i, Ident::new(*name, span))),
416-
);
417-
}
418-
entry.has_derive_copy |= ext.builtin_name == Some(sym::Copy);
419-
ext
420-
}
421-
Ok(_) | Err(Determinacy::Determined) => self.dummy_ext(MacroKind::Derive),
422-
Err(Determinacy::Undetermined) => {
423-
assert!(self.derive_data.is_empty());
424-
self.derive_data = derive_data;
425-
return Err(Indeterminate);
397+
if !resolution.has_been_handled {
398+
match self.cm().resolve_derive_macro_path(
399+
&resolution.path,
400+
&parent_scope,
401+
force,
402+
None,
403+
) {
404+
Ok((Some(ext), _)) => {
405+
if !ext.helper_attrs.is_empty() {
406+
let last_seg = resolution.path.segments.last().unwrap();
407+
let span = last_seg.ident.span.normalize_to_macros_2_0();
408+
entry.helper_attrs.extend(
409+
ext.helper_attrs.iter().map(|name| (i, Ident::new(*name, span))),
410+
);
426411
}
427-
},
428-
);
412+
entry.has_derive_copy |= ext.builtin_name == Some(sym::Copy);
413+
}
414+
Ok(_) | Err(Determinacy::Determined) => (),
415+
Err(Determinacy::Undetermined) => {
416+
assert!(self.derive_data.is_empty());
417+
self.derive_data = derive_data;
418+
return Err(Indeterminate);
419+
}
420+
}
421+
resolution.has_been_handled = true;
429422
}
430423
}
431424
// Sort helpers in a stable way independent from the derive resolution order.
@@ -571,7 +564,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
571564
path,
572565
kind,
573566
parent_scope,
574-
true,
575567
force,
576568
deleg_impl,
577569
invoc_in_mod_inert_attr.map(|def_id| (def_id, node_id)),
@@ -713,26 +705,22 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
713705
Ok((ext, res))
714706
}
715707

716-
pub(crate) fn resolve_macro_path<'r>(
708+
pub(crate) fn resolve_derive_macro_path<'r>(
717709
self: CmResolver<'r, 'ra, 'tcx>,
718710
path: &ast::Path,
719-
kind: MacroKind,
720711
parent_scope: &ParentScope<'ra>,
721-
trace: bool,
722712
force: bool,
723713
ignore_import: Option<Import<'ra>>,
724-
suggestion_span: Option<Span>,
725714
) -> Result<(Option<Arc<SyntaxExtension>>, Res), Determinacy> {
726715
self.resolve_macro_or_delegation_path(
727716
path,
728-
kind,
717+
MacroKind::Derive,
729718
parent_scope,
730-
trace,
731719
force,
732720
None,
733721
None,
734722
ignore_import,
735-
suggestion_span,
723+
None,
736724
)
737725
}
738726

@@ -741,7 +729,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
741729
ast_path: &ast::Path,
742730
kind: MacroKind,
743731
parent_scope: &ParentScope<'ra>,
744-
trace: bool,
745732
force: bool,
746733
deleg_impl: Option<LocalDefId>,
747734
invoc_in_mod_inert_attr: Option<(LocalDefId, NodeId)>,
@@ -780,16 +767,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
780767
PathResult::Module(..) => unreachable!(),
781768
};
782769

783-
if trace {
784-
self.multi_segment_macro_resolutions.borrow_mut(&self).push((
785-
path,
786-
path_span,
787-
kind,
788-
*parent_scope,
789-
res.ok(),
790-
ns,
791-
));
792-
}
770+
self.multi_segment_macro_resolutions.borrow_mut(&self).push((
771+
path,
772+
path_span,
773+
kind,
774+
*parent_scope,
775+
res.ok(),
776+
ns,
777+
));
793778

794779
self.prohibit_imported_non_macro_attrs(None, res.ok(), path_span);
795780
res
@@ -807,15 +792,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
807792
return Err(Determinacy::Undetermined);
808793
}
809794

810-
if trace {
811-
self.single_segment_macro_resolutions.borrow_mut(&self).push((
812-
path[0].ident,
813-
kind,
814-
*parent_scope,
815-
binding.ok(),
816-
suggestion_span,
817-
));
818-
}
795+
self.single_segment_macro_resolutions.borrow_mut(&self).push((
796+
path[0].ident,
797+
kind,
798+
*parent_scope,
799+
binding.ok(),
800+
suggestion_span,
801+
));
819802

820803
let res = binding.map(|binding| binding.res());
821804
self.prohibit_imported_non_macro_attrs(binding.ok(), res.ok(), path_span);

0 commit comments

Comments
 (0)