Skip to content

Commit b965844

Browse files
committed
resolve: Stable order for derive helper attributes
1 parent fbf1bec commit b965844

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

compiler/rustc_resolve/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ enum BuiltinMacroState {
853853

854854
struct DeriveData {
855855
resolutions: DeriveResolutions,
856-
helper_attrs: Vec<Ident>,
856+
helper_attrs: Vec<(usize, Ident)>,
857857
has_derive_copy: bool,
858858
}
859859

compiler/rustc_resolve/src/macros.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ impl<'a> ResolverExpand for Resolver<'a> {
376376
has_derive_copy: false,
377377
});
378378
let parent_scope = self.invocation_parent_scopes[&expn_id];
379-
for (path, opt_ext) in &mut entry.resolutions {
379+
for (i, (path, opt_ext)) in entry.resolutions.iter_mut().enumerate() {
380380
if opt_ext.is_none() {
381381
*opt_ext = Some(
382382
match self.resolve_macro_path(
@@ -391,7 +391,9 @@ impl<'a> ResolverExpand for Resolver<'a> {
391391
let last_seg = path.segments.last().unwrap();
392392
let span = last_seg.ident.span.normalize_to_macros_2_0();
393393
entry.helper_attrs.extend(
394-
ext.helper_attrs.iter().map(|name| Ident::new(*name, span)),
394+
ext.helper_attrs
395+
.iter()
396+
.map(|name| (i, Ident::new(*name, span))),
395397
);
396398
}
397399
entry.has_derive_copy |= ext.builtin_name == Some(sym::Copy);
@@ -407,9 +409,10 @@ impl<'a> ResolverExpand for Resolver<'a> {
407409
);
408410
}
409411
}
410-
// If we get to here, then `derive_data` for the given `expn_id` will only be accessed by
411-
// `take_derive_resolutions` later, so we can steal `helper_attrs` instead of cloning them.
412-
self.helper_attrs.insert(expn_id, mem::take(&mut entry.helper_attrs));
412+
// Sort helpers in a stable way independent from the derive resolution order.
413+
entry.helper_attrs.sort_by_key(|(i, _)| *i);
414+
self.helper_attrs
415+
.insert(expn_id, entry.helper_attrs.iter().map(|(_, ident)| *ident).collect());
413416
// Mark this derive as having `Copy` either if it has `Copy` itself or if its parent derive
414417
// has `Copy`, to support cases like `#[derive(Clone, Copy)] #[derive(Debug)]`.
415418
if entry.has_derive_copy || self.has_derive_copy(parent_scope.expansion) {

0 commit comments

Comments
 (0)