Skip to content

Commit d76f399

Browse files
committed
Some improvements
1 parent 6e6d741 commit d76f399

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ struct AttributeAdditionInfo {
6868
pub kind: AttributeAdditionKind,
6969
}
7070

71-
struct AttributeAdditionStatus {
71+
struct AttributeAdditionState {
7272
pub info: &'static AttributeAdditionInfo,
7373
pub already_exists: bool,
7474
}
@@ -80,7 +80,7 @@ enum AttributeAdditionKind {
8080

8181
const PARENT_ID: hir::ItemLocalId = hir::ItemLocalId::ZERO;
8282

83-
static ATTRS_ADDITIONS: &'static [AttributeAdditionInfo; 2] = &[
83+
static ATTRIBUTES_ADDITIONS: &'static [AttributeAdditionInfo] = &[
8484
AttributeAdditionInfo {
8585
factory: |span| hir::Attribute::Parsed(AttributeKind::MustUse { span, reason: None }),
8686
equals: |a| matches!(a, hir::Attribute::Parsed(AttributeKind::MustUse { .. })),
@@ -135,12 +135,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
135135
}
136136

137137
fn add_attributes_if_needed(&mut self, span: Span, sig_id: DefId) {
138-
let mut candidate_additions = ATTRS_ADDITIONS
138+
let mut candidate_additions = ATTRIBUTES_ADDITIONS
139139
.iter()
140-
.map(|a| AttributeAdditionStatus { info: a, already_exists: false })
140+
.map(|info| AttributeAdditionState { info, already_exists: false })
141141
.collect::<Vec<_>>();
142142

143-
self.check_if_attributes_already_exist(&mut candidate_additions);
143+
self.mark_existing_attributes(&mut candidate_additions);
144144
let new_attributes = self.create_new_attributes(&candidate_additions, span, sig_id);
145145

146146
if new_attributes.is_empty() {
@@ -157,51 +157,50 @@ impl<'hir> LoweringContext<'_, 'hir> {
157157
self.attrs.insert(PARENT_ID, new_arena_allocated_attributes);
158158
}
159159

160-
fn check_if_attributes_already_exist(
161-
&mut self,
162-
candidate_additions: &mut Vec<AttributeAdditionStatus>,
163-
) {
164-
if let Some(existing_attrs) = self.attrs.get(&PARENT_ID) {
165-
for attr in existing_attrs.iter() {
166-
for addition in candidate_additions.iter_mut() {
167-
if addition.already_exists {
168-
continue;
169-
}
160+
fn mark_existing_attributes(&mut self, candidate_additions: &mut Vec<AttributeAdditionState>) {
161+
let Some(existing_attrs) = self.attrs.get(&PARENT_ID) else { return };
170162

171-
if (addition.info.equals)(attr) {
172-
addition.already_exists = true;
173-
}
163+
for attr in existing_attrs.iter() {
164+
for addition in candidate_additions.iter_mut() {
165+
if addition.already_exists {
166+
continue;
167+
}
168+
169+
if (addition.info.equals)(attr) {
170+
addition.already_exists = true;
174171
}
175172
}
176173
}
177174
}
178175

179176
fn create_new_attributes(
180177
&mut self,
181-
candidate_additions: &Vec<AttributeAdditionStatus>,
178+
candidate_additions: &Vec<AttributeAdditionState>,
182179
span: Span,
183180
sig_id: DefId,
184181
) -> Vec<hir::Attribute> {
185182
candidate_additions
186-
.into_iter()
183+
.iter()
187184
.filter_map(|a| match a.already_exists {
188185
true => None,
189186
false => match a.info.kind {
190187
AttributeAdditionKind::Default => Some((a.info.factory)(span)),
191188
AttributeAdditionKind::Inherit { flag } => {
192189
// Check if an attribute present on the target of delegation,
193-
// either in this crate or others
190+
// either in this crate or other
194191
let should_inherit = match sig_id.as_local() {
195-
None => self
196-
.tcx
197-
.get_all_attrs(sig_id)
198-
.iter()
199-
.any(|base_attr| (a.info.equals)(base_attr)),
200192
Some(local_id) => self
201193
.resolver
202194
.delegation_fn_sigs
203195
.get(&local_id)
204196
.is_some_and(|sig| sig.attrs_flags.contains(flag)),
197+
// We can not use `tcx.has_attr` and symbol here as attributes
198+
// from other crates have None in their identifiers
199+
None => self
200+
.tcx
201+
.get_all_attrs(sig_id)
202+
.iter()
203+
.any(|base_attr| (a.info.equals)(base_attr)),
205204
};
206205

207206
match should_inherit {

compiler/rustc_resolve/src/late.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5294,14 +5294,14 @@ impl ItemInfoCollector<'_, '_, '_> {
52945294
id: NodeId,
52955295
attrs: &[Attribute],
52965296
) {
5297-
static NAMES_TO_FLAGS: [(Symbol, DelegationFnSigAttrs); 2] = [
5297+
static NAMES_TO_FLAGS: &'static [(Symbol, DelegationFnSigAttrs)] = &[
52985298
(sym::target_feature, DelegationFnSigAttrs::TARGET_FEATURE),
52995299
(sym::must_use, DelegationFnSigAttrs::MUST_USE),
53005300
];
53015301

53025302
let mut attrs_flags = DelegationFnSigAttrs::empty();
53035303
for attr in attrs {
5304-
for (name, flag) in NAMES_TO_FLAGS {
5304+
for (name, flag) in NAMES_TO_FLAGS.iter().map(|(s, f)| (*s, *f)) {
53055305
if !attrs_flags.contains(flag) && attr.has_name(name) {
53065306
attrs_flags.set(flag, true);
53075307
continue;

0 commit comments

Comments
 (0)