Skip to content

Commit df67b90

Browse files
committed
add map for eii ids in resolver
1 parent 1f76d21 commit df67b90

File tree

7 files changed

+52
-4
lines changed

7 files changed

+52
-4
lines changed

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub(crate) mod allow_unstable;
2727
pub(crate) mod cfg;
2828
pub(crate) mod confusables;
2929
pub(crate) mod deprecation;
30+
pub(crate) mod eii;
3031
pub(crate) mod repr;
3132
pub(crate) mod rustc;
3233
pub(crate) mod stability;

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol, sym};
1414
use crate::attributes::allow_unstable::{AllowConstFnUnstableParser, AllowInternalUnstableParser};
1515
use crate::attributes::confusables::ConfusablesParser;
1616
use crate::attributes::deprecation::DeprecationParser;
17+
use crate::attributes::eii::EiiParser;
1718
use crate::attributes::repr::ReprParser;
1819
use crate::attributes::rustc::RustcMacroEdition2021Parser;
1920
use crate::attributes::stability::{
@@ -77,6 +78,7 @@ attribute_groups!(
7778
// tidy-alphabetical-start
7879
Single<ConstStabilityIndirectParser>,
7980
Single<DeprecationParser>,
81+
Single<EiiParser>,
8082
Single<RustcMacroEdition2021Parser>,
8183
Single<TransparencyParser>,
8284
// tidy-alphabetical-end
@@ -211,7 +213,6 @@ impl<'sess> AttributeParser<'sess> {
211213
attrs: &'a [ast::Attribute],
212214
target_span: Span,
213215
omit_doc: OmitDoc,
214-
215216
lower_span: impl Copy + Fn(Span) -> Span,
216217
) -> Vec<Attribute> {
217218
let mut attributes = Vec::new();

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use rustc_serialize::{Decodable, Encodable};
4848
use rustc_session::lint::LintBuffer;
4949
pub use rustc_session::lint::RegisteredTools;
5050
use rustc_span::hygiene::MacroKind;
51-
use rustc_span::{DUMMY_SP, ExpnId, ExpnKind, Ident, Span, Symbol, kw, sym};
51+
use rustc_span::{EiiId, DUMMY_SP, ExpnId, ExpnKind, Ident, Span, Symbol, kw, sym};
5252
pub use rustc_type_ir::relate::VarianceDiagInfo;
5353
pub use rustc_type_ir::*;
5454
use tracing::{debug, instrument};
@@ -215,6 +215,9 @@ pub struct ResolverAstLowering {
215215

216216
/// Information about functions signatures for delegation items expansion
217217
pub delegation_fn_sigs: LocalDefIdMap<DelegationFnSig>,
218+
219+
/// Map of defids for all items marked with #[eii(<eii id>)].
220+
pub eii: FxHashMap<EiiId, LocalDefId>,
218221
}
219222

220223
#[derive(Debug)]

compiler/rustc_passes/src/check_attr.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
124124
AttributeKind::Stability { span, .. }
125125
| AttributeKind::ConstStability { span, .. },
126126
) => self.check_stability_promotable(*span, target),
127+
Attribute::Parsed(AttributeKind::Eii(attr_span)) => {
128+
self.check_eii(hir_id, *attr_span, span, target)
129+
}
127130
Attribute::Parsed(AttributeKind::AllowInternalUnstable(syms)) => self
128131
.check_allow_internal_unstable(
129132
hir_id,
@@ -470,6 +473,17 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
470473
}
471474
}
472475

476+
/// Checks if an `#[inline]` is applied to a function or a closure.
477+
fn check_eii(&self, _hir_id: HirId, _attr_span: Span, _defn_span: Span, target: Target) {
478+
match target {
479+
Target::ForeignFn => {}
480+
target => {
481+
// TODO:
482+
bug!("wrong target for EII: {target:?}");
483+
}
484+
}
485+
}
486+
473487
/// Checks that `#[coverage(..)]` is applied to a function/closure/method,
474488
/// or to an impl block or module.
475489
fn check_coverage(&self, attr: &Attribute, target_span: Span, target: Target) {

compiler/rustc_resolve/src/def_collector.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ use rustc_expand::expand::AstFragment;
88
use rustc_hir as hir;
99
use rustc_hir::def::{CtorKind, CtorOf, DefKind};
1010
use rustc_hir::def_id::LocalDefId;
11+
use rustc_middle::bug;
1112
use rustc_span::hygiene::LocalExpnId;
12-
use rustc_span::{Span, Symbol, sym};
13+
use rustc_span::{EiiId, Span, Symbol, sym};
1314
use tracing::debug;
1415

1516
use crate::{ImplTraitContext, InvocationParent, Resolver};
@@ -137,6 +138,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
137138
&i.attrs,
138139
i.span,
139140
OmitDoc::Skip,
141+
None,
140142
std::convert::identity,
141143
);
142144

@@ -261,6 +263,22 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
261263

262264
let def = self.create_def(fi.id, Some(ident.name), def_kind, fi.span);
263265

266+
if let ForeignItemKind::Fn(_) = fi.kind {
267+
for attr in &fi.attrs {
268+
if attr.has_name(sym::eii)
269+
&& let Some([arg]) = attr.meta_item_list().as_deref()
270+
&& let Some(lit) = arg.lit()
271+
&& let LitKind::Int(i, _) = lit.kind
272+
&& let Ok(id) = u32::try_from(i.get())
273+
{
274+
let id = EiiId::from(id);
275+
if let Some(other) = self.resolver.eii.insert(id, def) {
276+
bug!("{def:?}: {id:?} used more than once (used first on {other:?})");
277+
}
278+
}
279+
}
280+
}
281+
264282
self.with_parent(def, |this| visit::walk_item(this, fi));
265283
}
266284

compiler/rustc_resolve/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ use rustc_query_system::ich::StableHashingContext;
6969
use rustc_session::lint::builtin::PRIVATE_MACRO_USE;
7070
use rustc_session::lint::{BuiltinLintDiag, LintBuffer};
7171
use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind, SyntaxContext, Transparency};
72-
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
72+
use rustc_span::{DUMMY_SP, EiiId, Ident, Span, Symbol, kw, sym};
7373
use smallvec::{SmallVec, smallvec};
7474
use tracing::debug;
7575

@@ -1227,6 +1227,9 @@ pub struct Resolver<'ra, 'tcx> {
12271227
current_crate_outer_attr_insert_span: Span,
12281228

12291229
mods_with_parse_errors: FxHashSet<DefId>,
1230+
1231+
/// Map of defids for all items marked with #[eii(<eii id>)].
1232+
eii: FxHashMap<EiiId, LocalDefId>,
12301233
}
12311234

12321235
/// This provides memory for the rest of the crate. The `'ra` lifetime that is
@@ -1581,6 +1584,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15811584
impl_binding_keys: Default::default(),
15821585
current_crate_outer_attr_insert_span,
15831586
mods_with_parse_errors: Default::default(),
1587+
eii: Default::default(),
15841588
};
15851589

15861590
let root_parent_scope = ParentScope::module(graph_root, &resolver);
@@ -1694,6 +1698,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16941698
lifetime_elision_allowed: self.lifetime_elision_allowed,
16951699
lint_buffer: Steal::new(self.lint_buffer),
16961700
delegation_fn_sigs: self.delegation_fn_sigs,
1701+
eii: self.eii,
16971702
};
16981703
ResolverOutputs { global_ctxt, ast_lowering }
16991704
}

compiler/rustc_span/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,12 @@ rustc_index::newtype_index! {
12091209
pub struct AttrId {}
12101210
}
12111211

1212+
rustc_index::newtype_index! {
1213+
#[orderable]
1214+
#[debug_format = "EiiId({})"]
1215+
pub struct EiiId {}
1216+
}
1217+
12121218
/// This trait is used to allow encoder specific encodings of certain types.
12131219
/// It is similar to rustc_type_ir's TyEncoder.
12141220
pub trait SpanEncoder: Encoder {

0 commit comments

Comments
 (0)