Skip to content

Commit b2f488f

Browse files
committed
codegen eii
1 parent 68c5571 commit b2f488f

File tree

61 files changed

+631
-103
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+631
-103
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,6 +1881,7 @@ pub struct MacroDef {
18811881
pub struct EIIMacroFor {
18821882
pub extern_item_path: Path,
18831883
pub impl_unsafe: bool,
1884+
pub span: Span,
18841885
}
18851886

18861887
#[derive(Clone, Encodable, Decodable, Debug, Copy, Hash, Eq, PartialEq)]

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ fn walk_mac<T: MutVisitor>(vis: &mut T, mac: &mut MacCall) {
751751

752752
fn walk_macro_def<T: MutVisitor>(vis: &mut T, macro_def: &mut MacroDef) {
753753
let MacroDef { body, macro_rules: _, eii_macro_for } = macro_def;
754-
if let Some(EIIMacroFor { extern_item_path, impl_unsafe: _ }) = eii_macro_for {
754+
if let Some(EIIMacroFor { extern_item_path, impl_unsafe: _, span: _ }) = eii_macro_for {
755755
vis.visit_path(extern_item_path);
756756
}
757757
visit_delim_args(vis, body);

compiler/rustc_ast/src/visit.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,9 @@ impl WalkItemKind for ItemKind {
460460
ItemKind::MacCall(mac) => try_visit!(visitor.visit_mac_call(mac)),
461461
ItemKind::MacroDef(ts) => {
462462
try_visit!(visitor.visit_mac_def(ts, id));
463-
if let Some(EIIMacroFor { extern_item_path, impl_unsafe: _ }) = &ts.eii_macro_for {
463+
if let Some(EIIMacroFor { extern_item_path, impl_unsafe: _, span: _ }) =
464+
&ts.eii_macro_for
465+
{
464466
try_visit!(visitor.visit_path(extern_item_path, id));
465467
}
466468
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_abi::ExternAbi;
22
use rustc_ast::ptr::P;
33
use rustc_ast::visit::AssocCtxt;
44
use rustc_ast::*;
5-
use rustc_attr_parsing::AttributeKind;
5+
use rustc_attr_parsing::{AttributeKind, EIIDecl};
66
use rustc_errors::ErrorGuaranteed;
77
use rustc_hir::def::{DefKind, Res};
88
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId};
@@ -184,13 +184,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
184184
))]
185185
}
186186
ItemKind::MacroDef(MacroDef {
187-
eii_macro_for: Some(EIIMacroFor { extern_item_path, impl_unsafe }),
187+
eii_macro_for: Some(EIIMacroFor { extern_item_path, impl_unsafe, span }),
188188
..
189189
}) => {
190-
vec![hir::Attribute::Parsed(AttributeKind::EiiMacroFor {
190+
vec![hir::Attribute::Parsed(AttributeKind::EiiMacroFor(EIIDecl {
191191
eii_extern_item: self.lower_path_simple_eii(id, extern_item_path),
192192
impl_unsafe: *impl_unsafe,
193-
})]
193+
span: self.lower_span(*span),
194+
}))]
194195
}
195196
ItemKind::ExternCrate(..)
196197
| ItemKind::Use(..)

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl Deprecation {
139139
}
140140
}
141141

142-
#[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute)]
142+
#[derive(Copy, Clone, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute)]
143143
pub struct EIIImpl {
144144
pub eii_macro: DefId,
145145
pub impl_marked_unsafe: bool,
@@ -148,6 +148,14 @@ pub struct EIIImpl {
148148
pub is_default: bool,
149149
}
150150

151+
#[derive(Copy, Clone, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute)]
152+
pub struct EIIDecl {
153+
pub eii_extern_item: DefId,
154+
/// whether or not it is unsafe to implement this EII
155+
pub impl_unsafe: bool,
156+
pub span: Span,
157+
}
158+
151159
/// Represent parsed, *built in*, inert attributes.
152160
///
153161
/// That means attributes that are not actually ever expanded.
@@ -200,11 +208,8 @@ pub enum AttributeKind {
200208
comment: Symbol,
201209
},
202210
EiiImpl(ThinVec<EIIImpl>),
203-
EiiMacroFor {
204-
eii_extern_item: DefId,
205-
/// whether or not it is unsafe to implement this EII
206-
impl_unsafe: bool,
207-
},
211+
EiiMacroFor(EIIDecl),
212+
EiiMangleExtern,
208213
MacroTransparency(Transparency),
209214
Repr(ThinVec<(ReprAttr, Span)>),
210215
RustcMacroEdition2021,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use rustc_attr_data_structures::AttributeKind;
2+
use rustc_span::{Span, Symbol, sym};
3+
4+
use super::{AcceptContext, SingleAttributeParser};
5+
use crate::parser::ArgParser;
6+
7+
pub(crate) struct EiiMangleExternParser;
8+
9+
impl SingleAttributeParser for EiiMangleExternParser {
10+
const PATH: &'static [Symbol] = &[sym::eii_mangle_extern];
11+
12+
fn on_duplicate(_cx: &AcceptContext<'_>, _first_span: Span) {}
13+
fn convert(_cx: &AcceptContext<'_>, args: &ArgParser<'_>) -> Option<AttributeKind> {
14+
assert!(args.no_args());
15+
Some(AttributeKind::EiiMangleExtern)
16+
}
17+
}

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 & 0 deletions
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::EiiMangleExternParser;
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<EiiMangleExternParser>,
8082
Single<RustcMacroEdition2021Parser>,
8183
Single<TransparencyParser>,
8284
// tidy-alphabetical-end

compiler/rustc_builtin_macros/src/eii.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ fn eii_(
7777
let item = item.into_inner();
7878

7979
let ast::Item {
80-
attrs,
80+
mut attrs,
8181
id: _,
8282
span: item_span,
8383
vis,
@@ -145,14 +145,14 @@ fn eii_(
145145
rules: ast::BlockCheckMode::Default,
146146
span,
147147
tokens: None,
148-
could_be_bare_literal: false,
149148
}),
150149
None,
151150
),
152151
span,
153152
attrs: ThinVec::new(),
154153
tokens: None,
155154
})),
155+
define_opaque: None,
156156
})),
157157
tokens: None,
158158
})))
@@ -184,6 +184,21 @@ fn eii_(
184184
}
185185

186186
// extern "…" { safe fn item(); }
187+
// #[eii_mangle_extern]
188+
attrs.push(ast::Attribute {
189+
kind: ast::AttrKind::Normal(P(ast::NormalAttr {
190+
item: ast::AttrItem {
191+
unsafety: ast::Safety::Default,
192+
path: ast::Path::from_ident(Ident::new(sym::eii_mangle_extern, span)),
193+
args: ast::AttrArgs::Empty,
194+
tokens: None,
195+
},
196+
tokens: None,
197+
})),
198+
id: ecx.sess.psess.attr_id_generator.mk_attr_id(),
199+
style: ast::AttrStyle::Outer,
200+
span,
201+
});
187202
let extern_block = Annotatable::Item(P(ast::Item {
188203
attrs: ast::AttrVec::default(),
189204
id: ast::DUMMY_NODE_ID,
@@ -264,6 +279,7 @@ fn eii_(
264279
eii_macro_for: Some(ast::EIIMacroFor {
265280
extern_item_path: ast::Path::from_ident(item_name),
266281
impl_unsafe,
282+
span: decl_span,
267283
}),
268284
}),
269285
tokens: None,
@@ -322,7 +338,7 @@ pub(crate) fn eii_macro_for(
322338
false
323339
};
324340

325-
d.eii_macro_for = Some(EIIMacroFor { extern_item_path, impl_unsafe });
341+
d.eii_macro_for = Some(EIIMacroFor { extern_item_path, impl_unsafe, span });
326342

327343
// Return the original item and the new methods.
328344
vec![item]

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,6 +1981,7 @@ fn add_post_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor
19811981
/// used in any sections, so the linker will therefore pick relevant rlibs for linking, but
19821982
/// unused `#[no_mangle]` or `#[used]` can still be discard by GC sections.
19831983
///
1984+
// TODO: does EII solves this?
19841985
/// There's a few internal crates in the standard library (aka libcore and
19851986
/// libstd) which actually have a circular dependence upon one another. This
19861987
/// currently arises through "weak lang items" where libcore requires things

0 commit comments

Comments
 (0)