Skip to content

Commit 207308f

Browse files
committed
make linkage be external more often
1 parent b5a0367 commit 207308f

File tree

20 files changed

+179
-119
lines changed

20 files changed

+179
-119
lines changed

compiler/rustc_error_codes/src/error_codes/E0264.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#### this error code is no longer emitted by the compiler.
2+
13
An unknown external lang item was used.
24

35
Erroneous code example:

compiler/rustc_hir/src/lang_items.rs

+4
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ language_item_table! {
296296
PanicMisalignedPointerDereference, sym::panic_misaligned_pointer_dereference, panic_misaligned_pointer_dereference_fn, Target::Fn, GenericRequirement::Exact(0);
297297
PanicInfo, sym::panic_info, panic_info, Target::Struct, GenericRequirement::None;
298298
PanicLocation, sym::panic_location, panic_location, Target::Struct, GenericRequirement::None;
299+
/// Note: used to mark an extern item but now marks an externally implementable item. This means
300+
/// that the PanicImpl used to be marked to be specially treated in the compiler, while it now
301+
/// is only marked so we can check if it exists. There's no other reason for this lang item
302+
/// anymore.
299303
PanicImpl, sym::panic_impl, panic_impl, Target::Fn, GenericRequirement::None;
300304
PanicCannotUnwind, sym::panic_cannot_unwind, panic_cannot_unwind, Target::Fn, GenericRequirement::Exact(0);
301305
PanicInCleanup, sym::panic_in_cleanup, panic_in_cleanup, Target::Fn, GenericRequirement::Exact(0);

compiler/rustc_middle/src/middle/eii.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_macros::{HashStable, TyDecodable, TyEncodable};
77
pub struct EiiMapping {
88
pub extern_item: DefId,
99
pub chosen_impl: DefId,
10+
pub weak_linkage: bool,
1011
}
1112

1213
pub type EiiMap = FxIndexMap<LocalDefId, EiiMapping>;

compiler/rustc_middle/src/mir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1672,7 +1672,7 @@ mod size_asserts {
16721672
// tidy-alphabetical-start
16731673
static_assert_size!(BasicBlockData<'_>, 128);
16741674
static_assert_size!(LocalDecl<'_>, 40);
1675-
static_assert_size!(SourceScopeData<'_>, 64);
1675+
static_assert_size!(SourceScopeData<'_>, 72);
16761676
static_assert_size!(Statement<'_>, 32);
16771677
static_assert_size!(Terminator<'_>, 96);
16781678
static_assert_size!(VarDebugInfo<'_>, 88);

compiler/rustc_middle/src/mir/mono.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ pub struct MonoItemData {
383383
/// Specifies the linkage type for a `MonoItem`.
384384
///
385385
/// See <https://llvm.org/docs/LangRef.html#linkage-types> for more details about these variants.
386-
#[derive(Copy, Clone, PartialEq, Debug, TyEncodable, TyDecodable, HashStable)]
386+
#[derive(Copy, Clone, PartialEq, Debug, TyEncodable, TyDecodable, HashStable, Eq, Hash)]
387387
pub enum Linkage {
388388
External,
389389
AvailableExternally,

compiler/rustc_middle/src/mir/terminator.rs

+6
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,12 @@ impl<O> AssertKind<O> {
215215
}
216216
}
217217

218+
/// Generally do not use this and use `panic_function` instead.
219+
/// Gives the lang item that is required to exist for this assertion
220+
/// to be emitted. This sometimes causes the assertion not to be emitted
221+
/// if a lang item isn't there.
222+
pub fn required_lang_item(&self) {}
223+
218224
/// Format the message arguments for the `assert(cond, msg..)` terminator in MIR printing.
219225
///
220226
/// Needs to be kept in sync with the run-time behavior (which is defined by

compiler/rustc_middle/src/mir/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ macro_rules! make_mir_visitor {
355355
}
356356
| ty::InstanceKind::AsyncDropGlueCtorShim(_def_id, None)
357357
| ty::InstanceKind::DropGlue(_def_id, None)
358-
| ty::InstanceKind::EiiShim { def_id: _def_id, extern_item: _, chosen_impl: _ } => {}
358+
| ty::InstanceKind::EiiShim { def_id: _def_id, extern_item: _, chosen_impl: _, weak_linkage: _ } => {}
359359

360360
ty::InstanceKind::FnPtrShim(_def_id, ty)
361361
| ty::InstanceKind::DropGlue(_def_id, Some(ty))

compiler/rustc_middle/src/ty/instance.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ pub enum InstanceKind<'tcx> {
178178
/// Generated by externally implementable items. This function adds indirection so we can choose
179179
/// in the final crate whether to call an explicit implementation or, if none are given, call the
180180
/// default.
181-
EiiShim { def_id: DefId, extern_item: DefId, chosen_impl: DefId },
181+
EiiShim { def_id: DefId, extern_item: DefId, chosen_impl: DefId, weak_linkage: bool },
182182
}
183183

184184
impl<'tcx> Instance<'tcx> {
@@ -416,7 +416,10 @@ pub fn fmt_instance(
416416
InstanceKind::FnPtrAddrShim(_, ty) => write!(f, " - shim({ty})"),
417417
InstanceKind::AsyncDropGlueCtorShim(_, None) => write!(f, " - shim(None)"),
418418
InstanceKind::AsyncDropGlueCtorShim(_, Some(ty)) => write!(f, " - shim(Some({ty}))"),
419-
InstanceKind::EiiShim { def_id: _, extern_item, chosen_impl } => {
419+
InstanceKind::EiiShim { def_id: _, extern_item, chosen_impl, weak_linkage: true } => {
420+
write!(f, " - shim(eii: {extern_item:?} -> {chosen_impl:?} [weak]")
421+
}
422+
InstanceKind::EiiShim { def_id: _, extern_item, chosen_impl, weak_linkage: false } => {
420423
write!(f, " - shim(eii: {extern_item:?} -> {chosen_impl:?})")
421424
}
422425
}

compiler/rustc_mir_transform/src/shim.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceKind<'tcx>) -> Body<
7878
receiver_by_ref,
7979
} => build_construct_coroutine_by_move_shim(tcx, coroutine_closure_def_id, receiver_by_ref),
8080

81-
e @ ty::InstanceKind::EiiShim { def_id: _, extern_item, chosen_impl } => {
81+
e @ ty::InstanceKind::EiiShim { def_id: _, extern_item, chosen_impl, weak_linkage: _ } => {
8282
let source = MirSource::from_instance(e);
8383

8484
// get the signature for the new function this shim is creating

compiler/rustc_monomorphize/src/collector.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1594,7 +1594,7 @@ impl<'v> RootCollector<'_, 'v> {
15941594
/// For each externally implementable item, we should generate an alias MonoItem that
15951595
/// determines what implementation is called. This could be a default implementation.
15961596
fn push_extra_eii_roots(&mut self) {
1597-
for (shim_did, &EiiMapping { extern_item, chosen_impl, .. }) in
1597+
for (shim_did, &EiiMapping { extern_item, chosen_impl, weak_linkage, .. }) in
15981598
self.tcx.get_externally_implementable_item_impls(())
15991599
{
16001600
self.output.push(create_fn_mono_item(
@@ -1604,6 +1604,7 @@ impl<'v> RootCollector<'_, 'v> {
16041604
def_id: (*shim_did).into(),
16051605
extern_item,
16061606
chosen_impl,
1607+
weak_linkage,
16071608
},
16081609
args: ty::GenericArgs::empty(),
16091610
},

compiler/rustc_monomorphize/src/partitioning.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,12 @@ fn characteristic_def_id_of_mono_item<'tcx>(
634634
ty::InstanceKind::Item(def) => def,
635635
// EII shims have a characteristic defid.
636636
// But it's not their own, its the one of the extern item it is implementing.
637-
ty::InstanceKind::EiiShim { def_id: _, extern_item, chosen_impl: _ } => extern_item,
637+
ty::InstanceKind::EiiShim {
638+
def_id: _,
639+
extern_item,
640+
chosen_impl: _,
641+
weak_linkage: _,
642+
} => extern_item,
638643
ty::InstanceKind::VTableShim(..)
639644
| ty::InstanceKind::ReifyShim(..)
640645
| ty::InstanceKind::FnPtrShim(..)
@@ -767,8 +772,10 @@ fn mono_item_linkage_and_visibility<'tcx>(
767772
// together anyway. LLVM ensures the last one is the one that's chosen
768773
// TODO: this isn't the problem if they both decided to choose either the default or the
769774
// same explicit impl but if their view on it differs it is a problem!
770-
if let MonoItem::Fn(Instance { def: InstanceKind::EiiShim { .. }, .. }) = mono_item {
771-
(Linkage::WeakAny, vis)
775+
if let MonoItem::Fn(Instance { def: InstanceKind::EiiShim { weak_linkage, .. }, .. }) =
776+
mono_item
777+
{
778+
if *weak_linkage { (Linkage::WeakAny, vis) } else { (Linkage::External, vis) }
772779
} else {
773780
(Linkage::External, vis)
774781
}

compiler/rustc_passes/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,9 @@ passes_trait_impl_const_stable =
761761
passes_transparent_incompatible =
762762
transparent {$target} cannot have other repr hints
763763
764+
passes_undefined_naked_function_abi =
765+
Rust ABI is unsupported in naked functions
766+
764767
passes_unknown_external_lang_item =
765768
unknown external lang item: `{$lang_item}`
766769

0 commit comments

Comments
 (0)