Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl MultiItemModifier for Expander {
.map(|path| DeriveResolution {
path,
item: dummy_annotatable(),
exts: None,
has_been_handled: false,
is_const: self.is_const,
})
.collect()
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ pub struct Indeterminate;
pub struct DeriveResolution {
pub path: ast::Path,
pub item: Annotatable,
pub exts: Option<Arc<SyntaxExtension>>,
pub has_been_handled: bool,
pub is_const: bool,
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
derive_invocations.reserve(derives.len());
derives
.into_iter()
.map(|DeriveResolution { path, item, exts: _, is_const }| {
.map(|DeriveResolution { path, item, is_const, .. }| {
// FIXME: Consider using the derive resolutions (`_exts`)
Copy link
Contributor

@petrochenkov petrochenkov Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SyntaxExtension was preserved because of this FIXME.
Any fix will require preserving them again.

// instead of enqueuing the derives to be resolved again later.
// Note that this can result in duplicate diagnostics.
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_resolve/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,14 +458,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
let mut result = Err(Determinacy::Determined);
for derive in parent_scope.derives {
let parent_scope = &ParentScope { derives: &[], ..*parent_scope };
match this.reborrow().resolve_macro_path(
match this.reborrow().resolve_derive_macro_path(
derive,
MacroKind::Derive,
parent_scope,
true,
force,
ignore_import,
None,
) {
Ok((Some(ext), _)) => {
if ext.helper_attrs.contains(&ident.name) {
Expand Down
101 changes: 42 additions & 59 deletions compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,38 +393,31 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
});
let parent_scope = self.invocation_parent_scopes[&expn_id];
for (i, resolution) in entry.resolutions.iter_mut().enumerate() {
if resolution.exts.is_none() {
resolution.exts = Some(
match self.cm().resolve_macro_path(
&resolution.path,
MacroKind::Derive,
&parent_scope,
true,
force,
None,
None,
) {
Ok((Some(ext), _)) => {
if !ext.helper_attrs.is_empty() {
let last_seg = resolution.path.segments.last().unwrap();
let span = last_seg.ident.span.normalize_to_macros_2_0();
entry.helper_attrs.extend(
ext.helper_attrs
.iter()
.map(|name| (i, Ident::new(*name, span))),
);
}
entry.has_derive_copy |= ext.builtin_name == Some(sym::Copy);
ext
}
Ok(_) | Err(Determinacy::Determined) => self.dummy_ext(MacroKind::Derive),
Err(Determinacy::Undetermined) => {
assert!(self.derive_data.is_empty());
self.derive_data = derive_data;
return Err(Indeterminate);
if !resolution.has_been_handled {
match self.cm().resolve_derive_macro_path(
&resolution.path,
&parent_scope,
force,
None,
) {
Ok((Some(ext), _)) => {
if !ext.helper_attrs.is_empty() {
let last_seg = resolution.path.segments.last().unwrap();
let span = last_seg.ident.span.normalize_to_macros_2_0();
entry.helper_attrs.extend(
ext.helper_attrs.iter().map(|name| (i, Ident::new(*name, span))),
);
}
},
);
entry.has_derive_copy |= ext.builtin_name == Some(sym::Copy);
}
Ok(_) | Err(Determinacy::Determined) => (),
Err(Determinacy::Undetermined) => {
assert!(self.derive_data.is_empty());
self.derive_data = derive_data;
return Err(Indeterminate);
}
}
resolution.has_been_handled = true;
}
}
// Sort helpers in a stable way independent from the derive resolution order.
Expand Down Expand Up @@ -564,7 +557,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
path,
kind,
parent_scope,
true,
force,
deleg_impl,
invoc_in_mod_inert_attr.map(|def_id| (def_id, node_id)),
Expand Down Expand Up @@ -706,26 +698,22 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
Ok((ext, res))
}

pub(crate) fn resolve_macro_path<'r>(
pub(crate) fn resolve_derive_macro_path<'r>(
self: CmResolver<'r, 'ra, 'tcx>,
path: &ast::Path,
kind: MacroKind,
parent_scope: &ParentScope<'ra>,
trace: bool,
force: bool,
ignore_import: Option<Import<'ra>>,
suggestion_span: Option<Span>,
) -> Result<(Option<Arc<SyntaxExtension>>, Res), Determinacy> {
self.resolve_macro_or_delegation_path(
path,
kind,
MacroKind::Derive,
parent_scope,
trace,
force,
None,
None,
ignore_import,
suggestion_span,
None,
)
}

Expand All @@ -734,7 +722,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
ast_path: &ast::Path,
kind: MacroKind,
parent_scope: &ParentScope<'ra>,
trace: bool,
force: bool,
deleg_impl: Option<LocalDefId>,
invoc_in_mod_inert_attr: Option<(LocalDefId, NodeId)>,
Expand Down Expand Up @@ -773,16 +760,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
PathResult::Module(..) => unreachable!(),
};

if trace {
self.multi_segment_macro_resolutions.borrow_mut(&self).push((
path,
path_span,
kind,
*parent_scope,
res.ok(),
ns,
));
}
self.multi_segment_macro_resolutions.borrow_mut(&self).push((
path,
path_span,
kind,
*parent_scope,
res.ok(),
ns,
));

self.prohibit_imported_non_macro_attrs(None, res.ok(), path_span);
res
Expand All @@ -800,15 +785,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
return Err(Determinacy::Undetermined);
}

if trace {
self.single_segment_macro_resolutions.borrow_mut(&self).push((
path[0].ident,
kind,
*parent_scope,
binding.ok(),
suggestion_span,
));
}
self.single_segment_macro_resolutions.borrow_mut(&self).push((
path[0].ident,
kind,
*parent_scope,
binding.ok(),
suggestion_span,
));

let res = binding.map(|binding| binding.res());
self.prohibit_imported_non_macro_attrs(binding.ok(), res.ok(), path_span);
Expand Down
Loading