Skip to content

Commit 2d3bf55

Browse files
committed
codegen eii
1 parent 65c042e commit 2d3bf55

File tree

48 files changed

+570
-74
lines changed

Some content is hidden

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

48 files changed

+570
-74
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1968,6 +1968,7 @@ pub struct MacroDef {
19681968
pub struct EIIMacroFor {
19691969
pub extern_item_path: Path,
19701970
pub impl_unsafe: bool,
1971+
pub span: Span,
19711972
}
19721973

19731974
#[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
@@ -479,7 +479,9 @@ impl WalkItemKind for ItemKind {
479479
ItemKind::MacroDef(ident, ts) => {
480480
try_visit!(visitor.visit_ident(ident));
481481
try_visit!(visitor.visit_mac_def(ts, id));
482-
if let Some(EIIMacroFor { extern_item_path, impl_unsafe: _ }) = &ts.eii_macro_for {
482+
if let Some(EIIMacroFor { extern_item_path, impl_unsafe: _, span: _ }) =
483+
&ts.eii_macro_for
484+
{
483485
try_visit!(visitor.visit_path(extern_item_path, id));
484486
}
485487
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 6 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};
@@ -185,13 +185,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
185185
ItemKind::MacroDef(
186186
_,
187187
MacroDef {
188-
eii_macro_for: Some(EIIMacroFor { extern_item_path, impl_unsafe }), ..
188+
eii_macro_for: Some(EIIMacroFor { extern_item_path, impl_unsafe, span }),
189+
..
189190
},
190191
) => {
191-
vec![hir::Attribute::Parsed(AttributeKind::EiiMacroFor {
192+
vec![hir::Attribute::Parsed(AttributeKind::EiiMacroFor(EIIDecl {
192193
eii_extern_item: self.lower_path_simple_eii(id, extern_item_path),
193194
impl_unsafe: *impl_unsafe,
194-
})]
195+
span: self.lower_span(*span),
196+
}))]
195197
}
196198
ItemKind::ExternCrate(..)
197199
| 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: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,14 @@ fn eii_(
6363

6464
let item = item.into_inner();
6565

66-
let ast::Item { attrs, id: _, span: item_span, vis, kind: ItemKind::Fn(mut func), tokens: _ } =
67-
item
66+
let ast::Item {
67+
mut attrs,
68+
id: _,
69+
span: item_span,
70+
vis,
71+
kind: ItemKind::Fn(mut func),
72+
tokens: _,
73+
} = item
6874
else {
6975
ecx.dcx()
7076
.emit_err(EIIMacroExpectedFunction { span, name: path_to_string(&meta_item.path) });
@@ -105,8 +111,8 @@ fn eii_(
105111
id: ast::DUMMY_NODE_ID,
106112
span,
107113
vis: ast::Visibility { span, kind: ast::VisibilityKind::Inherited, tokens: None },
108-
ident: Ident { name: kw::Underscore, span },
109114
kind: ast::ItemKind::Const(Box::new(ast::ConstItem {
115+
ident: Ident { name: kw::Underscore, span },
110116
defaultness: ast::Defaultness::Final,
111117
generics: ast::Generics::default(),
112118
ty: P(ast::Ty {
@@ -130,7 +136,6 @@ fn eii_(
130136
kind: ast::VisibilityKind::Inherited,
131137
tokens: None
132138
},
133-
ident: item_name,
134139
kind: ItemKind::Fn(default_func),
135140
tokens: None,
136141
})),
@@ -140,14 +145,14 @@ fn eii_(
140145
rules: ast::BlockCheckMode::Default,
141146
span,
142147
tokens: None,
143-
could_be_bare_literal: false,
144148
}),
145149
None,
146150
),
147151
span,
148152
attrs: ThinVec::new(),
149153
tokens: None,
150154
})),
155+
define_opaque: None,
151156
})),
152157
tokens: None,
153158
})))
@@ -179,6 +184,21 @@ fn eii_(
179184
}
180185

181186
// 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+
});
182202
let extern_block = Annotatable::Item(P(ast::Item {
183203
attrs: ast::AttrVec::default(),
184204
id: ast::DUMMY_NODE_ID,
@@ -258,6 +278,7 @@ fn eii_(
258278
eii_macro_for: Some(ast::EIIMacroFor {
259279
extern_item_path: ast::Path::from_ident(func.ident),
260280
impl_unsafe,
281+
span: decl_span,
261282
}),
262283
},
263284
),
@@ -317,7 +338,7 @@ pub(crate) fn eii_macro_for(
317338
false
318339
};
319340

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

322343
// Return the original item and the new methods.
323344
vec![item]

compiler/rustc_codegen_ssa/src/back/link.rs

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

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,8 @@ fn upstream_monomorphizations_provider(
436436
}
437437
ExportedSymbol::NonGeneric(..)
438438
| ExportedSymbol::ThreadLocalShim(..)
439-
| ExportedSymbol::NoDefId(..) => {
439+
| ExportedSymbol::NoDefId(..)
440+
| ExportedSymbol::Alias { .. } => {
440441
// These are no monomorphizations
441442
continue;
442443
}
@@ -582,6 +583,7 @@ pub(crate) fn symbol_name_for_instance_in_crate<'tcx>(
582583
instantiating_crate,
583584
)
584585
}
586+
ExportedSymbol::Alias { original: _, alternative_symbol } => alternative_symbol.to_string(),
585587
ExportedSymbol::NoDefId(symbol_name) => symbol_name.to_string(),
586588
}
587589
}
@@ -608,6 +610,10 @@ fn calling_convention_for_symbol<'tcx>(
608610
ExportedSymbol::NoDefId(..) => None,
609611
// ThreadLocalShim always follow the target's default symbol decoration scheme.
610612
ExportedSymbol::ThreadLocalShim(..) => None,
613+
// Aliases have the same calling convention as the thing they alias.
614+
ExportedSymbol::Alias { original, alternative_symbol: _ } => {
615+
Some(Instance::mono(tcx, original))
616+
}
611617
};
612618

613619
instance

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn linkage_by_name(tcx: TyCtxt<'_>, def_id: LocalDefId, name: &str) -> Linkage {
5151
}
5252
}
5353

54-
fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
54+
fn codegen_fn_attrs<'tcx>(tcx: TyCtxt<'tcx>, did: LocalDefId) -> CodegenFnAttrs {
5555
if cfg!(debug_assertions) {
5656
let def_kind = tcx.def_kind(did);
5757
assert!(
@@ -111,17 +111,9 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
111111

112112
if let hir::Attribute::Parsed(p) = attr {
113113
match p {
114-
/*
115-
AttributeKind::EiiImpl(eii_macro) => {
116-
let Some(eii_extern_item) = find_attr!(
117-
tcx.get_all_attrs(eii_macro),
118-
AttributeKind::EiiMacroFor { eii_extern_item, .. } => *eii_extern_item
119-
) else {
120-
tcx.dcx().span_delayed_bug(attr.span(), "missing attr on EII macro");
121-
continue;
122-
};
123-
let _ = eii_extern_item; // XXX mangle as this item or something.
124-
}*/
114+
AttributeKind::EiiMangleExtern => {
115+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::EII_MANGLE_EXTERN;
116+
}
125117
AttributeKind::Repr(reprs) => {
126118
codegen_fn_attrs.alignment = reprs
127119
.iter()
@@ -729,8 +721,8 @@ fn check_link_ordinal(tcx: TyCtxt<'_>, attr: &hir::Attribute) -> Option<u16> {
729721
}
730722
}
731723

732-
fn check_link_name_xor_ordinal(
733-
tcx: TyCtxt<'_>,
724+
fn check_link_name_xor_ordinal<'tcx>(
725+
tcx: TyCtxt<'tcx>,
734726
codegen_fn_attrs: &CodegenFnAttrs,
735727
inline_span: Option<Span>,
736728
) {

compiler/rustc_const_eval/src/interpret/call.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
570570
| ty::InstanceKind::FnPtrAddrShim(..)
571571
| ty::InstanceKind::ThreadLocalShim(..)
572572
| ty::InstanceKind::AsyncDropGlueCtorShim(..)
573+
| ty::InstanceKind::EiiShim { .. }
573574
| ty::InstanceKind::Item(_) => {
574575
// We need MIR for this fn.
575576
// Note that this can be an intrinsic, if we are executing its fallback body.

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,11 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
10551055
rustc_force_inline, Normal, template!(Word, NameValueStr: "reason"), WarnFollowing, EncodeCrossCrate::Yes,
10561056
"#[rustc_force_inline] forces a free function to be inlined"
10571057
),
1058+
gated!(
1059+
// Used in resolve:
1060+
eii_mangle_extern, Normal, template!(Word), ErrorFollowing,
1061+
EncodeCrossCrate::Yes, eii_internals, "`#[eii_mangle_extern]` is for use by rustc only",
1062+
),
10581063

10591064
// ==========================================================================
10601065
// Internal attributes, Testing:

compiler/rustc_hir/src/def.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ pub enum Res<Id = hir::HirId> {
456456
// FIXME(generic_const_exprs): Remove this bodge once that feature is stable.
457457
forbid_generic: bool,
458458

459-
/// Is this within an `impl Foo for bar`?
459+
/// Is this within an `impl Foo for bar`?:
460460
is_trait_impl: bool,
461461
},
462462

compiler/rustc_hir_analysis/src/check/compare_eii.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ use rustc_data_structures::fx::FxIndexSet;
55
use rustc_errors::{Applicability, E0050, E0053, struct_span_code_err};
66
use rustc_hir::def_id::{DefId, LocalDefId};
77
use rustc_hir::{self as hir, FnSig, HirId, ItemKind};
8-
use rustc_infer::infer::canonical::ir::TypingMode;
98
use rustc_infer::infer::{self, InferCtxt, TyCtxtInferExt};
109
use rustc_infer::traits::{ObligationCause, ObligationCauseCode};
11-
use rustc_middle::ty;
12-
use rustc_middle::ty::TyCtxt;
1310
use rustc_middle::ty::error::{ExpectedFound, TypeError};
11+
use rustc_middle::ty::{self, TyCtxt, TypingMode};
1412
use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol};
1513
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
1614
use rustc_trait_selection::regions::InferCtxtRegionExt;

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::ops::{ControlFlow, Deref};
33

44
use hir::intravisit::{self, Visitor};
55
use rustc_abi::ExternAbi;
6-
use rustc_attr_parsing::{AttributeKind, EIIImpl, find_attr};
6+
use rustc_attr_parsing::{AttributeKind, EIIDecl, EIIImpl, find_attr};
77
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
88
use rustc_errors::codes::*;
99
use rustc_errors::{Applicability, ErrorGuaranteed, pluralize, struct_span_code_err};
@@ -1305,7 +1305,7 @@ fn check_item_fn(
13051305
{
13061306
// we expect this macro to have the `EiiMacroFor` attribute, that points to a function
13071307
// signature that we'd like to compare the function we're currently checking with
1308-
if let Some(eii_extern_item) = find_attr!(tcx.get_all_attrs(*eii_macro), AttributeKind::EiiMacroFor {eii_extern_item, ..} => *eii_extern_item)
1308+
if let Some(eii_extern_item) = find_attr!(tcx.get_all_attrs(*eii_macro), AttributeKind::EiiMacroFor(EIIDecl {eii_extern_item, ..}) => *eii_extern_item)
13091309
{
13101310
let _ = compare_eii_function_types(
13111311
tcx,

compiler/rustc_interface/src/passes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,9 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
905905
parallel!(
906906
{
907907
sess.time("looking_for_entry_point", || tcx.ensure_ok().entry_fn(()));
908+
sess.time("check_externally_implementable_items", || {
909+
tcx.ensure_ok().get_externally_implementable_item_impls(())
910+
});
908911

909912
sess.time("looking_for_derive_registrar", || {
910913
tcx.ensure_ok().proc_macro_decls_static(())

0 commit comments

Comments
 (0)