Auto merge of #146348 - jdonszelmann:eiiv3, r=lcnr,oli-obk · rust-lang/rust@3f4dc1e (original) (raw)
`@@ -2,7 +2,7 @@ use rustc_abi::ExternAbi;
`
2
2
`use rustc_ast::visit::AssocCtxt;
`
3
3
`use rustc_ast::*;
`
4
4
`use rustc_errors::{E0570, ErrorGuaranteed, struct_span_code_err};
`
5
``
`-
use rustc_hir::attrs::AttributeKind;
`
``
5
`+
use rustc_hir::attrs::{AttributeKind, EiiDecl};
`
6
6
`use rustc_hir::def::{DefKind, PerNS, Res};
`
7
7
`use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId};
`
8
8
`use rustc_hir::{
`
`@@ -11,6 +11,7 @@ use rustc_hir::{
`
11
11
`use rustc_index::{IndexSlice, IndexVec};
`
12
12
`use rustc_middle::span_bug;
`
13
13
`use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
`
``
14
`+
use rustc_span::def_id::DefId;
`
14
15
`use rustc_span::edit_distance::find_best_match_for_name;
`
15
16
`use rustc_span::{DUMMY_SP, DesugaringKind, Ident, Span, Symbol, kw, sym};
`
16
17
`use smallvec::{SmallVec, smallvec};
`
`@@ -133,17 +134,103 @@ impl<'hir> LoweringContext<'_, 'hir> {
`
133
134
`}
`
134
135
`}
`
135
136
``
``
137
`+
fn generate_extra_attrs_for_item_kind(
`
``
138
`+
&mut self,
`
``
139
`+
id: NodeId,
`
``
140
`+
i: &ItemKind,
`
``
141
`+
) -> Vechir::Attribute {
`
``
142
`+
match i {
`
``
143
`+
ItemKind::Fn(box Fn { eii_impls, .. }) if eii_impls.is_empty() => Vec::new(),
`
``
144
`+
ItemKind::Fn(box Fn { eii_impls, .. }) => {
`
``
145
`+
vec![hir::Attribute::Parsed(AttributeKind::EiiImpls(
`
``
146
`+
eii_impls
`
``
147
`+
.iter()
`
``
148
`+
.flat_map(
`
``
149
`+
|EiiImpl {
`
``
150
`+
node_id,
`
``
151
`+
eii_macro_path,
`
``
152
`+
impl_safety,
`
``
153
`+
span,
`
``
154
`+
inner_span,
`
``
155
`+
is_default,
`
``
156
`+
}| {
`
``
157
`+
self.lower_path_simple_eii(*node_id, eii_macro_path).map(|did| {
`
``
158
`+
hir::attrs::EiiImpl {
`
``
159
`+
eii_macro: did,
`
``
160
`+
span: self.lower_span(*span),
`
``
161
`+
inner_span: self.lower_span(*inner_span),
`
``
162
`+
impl_marked_unsafe: self
`
``
163
`+
.lower_safety(*impl_safety, hir::Safety::Safe)
`
``
164
`+
.is_unsafe(),
`
``
165
`+
is_default: *is_default,
`
``
166
`+
}
`
``
167
`+
})
`
``
168
`+
},
`
``
169
`+
)
`
``
170
`+
.collect(),
`
``
171
`+
))]
`
``
172
`+
}
`
``
173
`+
ItemKind::MacroDef(
`
``
174
`+
_,
`
``
175
`+
MacroDef {
`
``
176
`+
eii_extern_target: Some(EiiExternTarget { extern_item_path, impl_unsafe, span }),
`
``
177
`+
..
`
``
178
`+
},
`
``
179
`+
) => self
`
``
180
`+
.lower_path_simple_eii(id, extern_item_path)
`
``
181
`+
.map(|did| {
`
``
182
`+
vec![hir::Attribute::Parsed(AttributeKind::EiiExternTarget(EiiDecl {
`
``
183
`+
eii_extern_target: did,
`
``
184
`+
impl_unsafe: *impl_unsafe,
`
``
185
`+
span: self.lower_span(*span),
`
``
186
`+
}))]
`
``
187
`+
})
`
``
188
`+
.unwrap_or_default(),
`
``
189
`+
ItemKind::ExternCrate(..)
`
``
190
`+
| ItemKind::Use(..)
`
``
191
`+
| ItemKind::Static(..)
`
``
192
`+
| ItemKind::Const(..)
`
``
193
`+
| ItemKind::Mod(..)
`
``
194
`+
| ItemKind::ForeignMod(..)
`
``
195
`+
| ItemKind::GlobalAsm(..)
`
``
196
`+
| ItemKind::TyAlias(..)
`
``
197
`+
| ItemKind::Enum(..)
`
``
198
`+
| ItemKind::Struct(..)
`
``
199
`+
| ItemKind::Union(..)
`
``
200
`+
| ItemKind::Trait(..)
`
``
201
`+
| ItemKind::TraitAlias(..)
`
``
202
`+
| ItemKind::Impl(..)
`
``
203
`+
| ItemKind::MacCall(..)
`
``
204
`+
| ItemKind::MacroDef(..)
`
``
205
`+
| ItemKind::Delegation(..)
`
``
206
`+
| ItemKind::DelegationMac(..) => Vec::new(),
`
``
207
`+
}
`
``
208
`+
}
`
``
209
+
136
210
`fn lower_item(&mut self, i: &Item) -> &'hir hir::Item<'hir> {
`
137
211
`let vis_span = self.lower_span(i.vis.span);
`
138
212
`let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
`
139
``
`-
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span, Target::from_ast_item(i));
`
``
213
+
``
214
`+
let extra_hir_attributes = self.generate_extra_attrs_for_item_kind(i.id, &i.kind);
`
``
215
`+
let attrs = self.lower_attrs_with_extra(
`
``
216
`+
hir_id,
`
``
217
`+
&i.attrs,
`
``
218
`+
i.span,
`
``
219
`+
Target::from_ast_item(i),
`
``
220
`+
&extra_hir_attributes,
`
``
221
`+
);
`
``
222
+
140
223
`let kind = self.lower_item_kind(i.span, i.id, hir_id, attrs, vis_span, &i.kind);
`
141
224
`let item = hir::Item {
`
142
225
`owner_id: hir_id.expect_owner(),
`
143
226
` kind,
`
144
227
` vis_span,
`
145
228
`span: self.lower_span(i.span),
`
146
229
`has_delayed_lints: !self.delayed_lints.is_empty(),
`
``
230
`+
eii: find_attr!(
`
``
231
`+
attrs,
`
``
232
`+
AttributeKind::EiiImpls(..) | AttributeKind::EiiExternTarget(..)
`
``
233
`+
),
`
147
234
`};
`
148
235
`self.arena.alloc(item)
`
149
236
`}
`
`@@ -435,7 +522,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
`
435
522
`);
`
436
523
` hir::ItemKind::TraitAlias(constness, ident, generics, bounds)
`
437
524
`}
`
438
``
`-
ItemKind::MacroDef(ident, MacroDef { body, macro_rules }) => {
`
``
525
`+
ItemKind::MacroDef(ident, MacroDef { body, macro_rules, eii_extern_target: _ }) => {
`
439
526
`let ident = self.lower_ident(*ident);
`
440
527
`let body = Box::new(self.lower_delim_args(body));
`
441
528
`let def_id = self.local_def_id(id);
`
`@@ -446,7 +533,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
`
446
533
` def_kind.descr(def_id.to_def_id())
`
447
534
`);
`
448
535
`};
`
449
``
`-
let macro_def = self.arena.alloc(ast::MacroDef { body, macro_rules: *macro_rules });
`
``
536
`+
let macro_def = self.arena.alloc(ast::MacroDef {
`
``
537
`+
body,
`
``
538
`+
macro_rules: *macro_rules,
`
``
539
`+
eii_extern_target: None,
`
``
540
`+
});
`
450
541
` hir::ItemKind::Macro(ident, macro_def, macro_kinds)
`
451
542
`}
`
452
543
`ItemKind::Delegation(box delegation) => {
`
`@@ -465,6 +556,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
`
465
556
`}
`
466
557
`}
`
467
558
``
``
559
`+
fn lower_path_simple_eii(&mut self, id: NodeId, path: &Path) -> Option {
`
``
560
`+
let res = self.resolver.get_partial_res(id)?;
`
``
561
`+
let Some(did) = res.expect_full_res().opt_def_id() else {
`
``
562
`+
self.dcx().span_delayed_bug(path.span, "should have errored in resolve");
`
``
563
`+
return None;
`
``
564
`+
};
`
``
565
+
``
566
`+
Some(did)
`
``
567
`+
}
`
``
568
+
468
569
`#[instrument(level = "debug", skip(self))]
`
469
570
`fn lower_use_tree(
`
470
571
`&mut self,
`
`@@ -573,6 +674,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
`
573
674
` vis_span,
`
574
675
`span: this.lower_span(use_tree.span),
`
575
676
`has_delayed_lints: !this.delayed_lints.is_empty(),
`
``
677
`+
eii: find_attr!(
`
``
678
`+
attrs,
`
``
679
`+
AttributeKind::EiiImpls(..) | AttributeKind::EiiExternTarget(..)
`
``
680
`+
),
`
576
681
`};
`
577
682
` hir::OwnerNode::Item(this.arena.alloc(item))
`
578
683
`});
`