Skip to content

Commit 08ca134

Browse files
committed
make linkage be external more often
1 parent fefe20d commit 08ca134

File tree

19 files changed

+171
-112
lines changed

19 files changed

+171
-112
lines changed

compiler/rustc_error_codes/src/error_codes/E0264.md

Lines changed: 2 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,10 @@ language_item_table! {
285285
PanicMisalignedPointerDereference, sym::panic_misaligned_pointer_dereference, panic_misaligned_pointer_dereference_fn, Target::Fn, GenericRequirement::Exact(0);
286286
PanicInfo, sym::panic_info, panic_info, Target::Struct, GenericRequirement::None;
287287
PanicLocation, sym::panic_location, panic_location, Target::Struct, GenericRequirement::None;
288+
/// Note: used to mark an extern item but now marks an externally implementable item. This means
289+
/// that the PanicImpl used to be marked to be specially treated in the compiler, while it now
290+
/// is only marked so we can check if it exists. There's no other reason for this lang item
291+
/// anymore.
288292
PanicImpl, sym::panic_impl, panic_impl, Target::Fn, GenericRequirement::None;
289293
PanicCannotUnwind, sym::panic_cannot_unwind, panic_cannot_unwind, Target::Fn, GenericRequirement::Exact(0);
290294
PanicInCleanup, sym::panic_in_cleanup, panic_in_cleanup, Target::Fn, GenericRequirement::Exact(0);

compiler/rustc_middle/src/middle/eii.rs

Lines changed: 1 addition & 0 deletions
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/mono.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,12 @@ impl<O> AssertKind<O> {
225225
}
226226
}
227227

228+
/// Generally do not use this and use `panic_function` instead.
229+
/// Gives the lang item that is required to exist for this assertion
230+
/// to be emitted. This sometimes causes the assertion not to be emitted
231+
/// if a lang item isn't there.
232+
pub fn required_lang_item(&self) {}
233+
228234
/// Format the message arguments for the `assert(cond, msg..)` terminator in MIR printing.
229235
///
230236
/// Needs to be kept in sync with the run-time behavior (which is defined by

compiler/rustc_middle/src/mir/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ macro_rules! make_mir_visitor {
354354
receiver_by_ref: _,
355355
}
356356
| ty::InstanceKind::DropGlue(_def_id, None)
357-
| ty::InstanceKind::EiiShim { def_id: _def_id, extern_item: _, chosen_impl: _ } => {}
357+
| ty::InstanceKind::EiiShim { def_id: _def_id, extern_item: _, chosen_impl: _, weak_linkage: _ } => {}
358358

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

compiler/rustc_middle/src/ty/instance.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ pub enum InstanceKind<'tcx> {
187187
/// Generated by externally implementable items. This function adds indirection so we can choose
188188
/// in the final crate whether to call an explicit implementation or, if none are given, call the
189189
/// default.
190-
EiiShim { def_id: DefId, extern_item: DefId, chosen_impl: DefId },
190+
EiiShim { def_id: DefId, extern_item: DefId, chosen_impl: DefId, weak_linkage: bool },
191191
}
192192

193193
impl<'tcx> Instance<'tcx> {
@@ -436,7 +436,10 @@ pub fn fmt_instance(
436436
}
437437
InstanceKind::AsyncDropGlue(_, ty) => write!(f, " - shim({ty})"),
438438
InstanceKind::AsyncDropGlueCtorShim(_, ty) => write!(f, " - shim(Some({ty}))"),
439-
InstanceKind::EiiShim { def_id: _, extern_item, chosen_impl } => {
439+
InstanceKind::EiiShim { def_id: _, extern_item, chosen_impl, weak_linkage: true } => {
440+
write!(f, " - shim(eii: {extern_item:?} -> {chosen_impl:?} [weak]")
441+
}
442+
InstanceKind::EiiShim { def_id: _, extern_item, chosen_impl, weak_linkage: false } => {
440443
write!(f, " - shim(eii: {extern_item:?} -> {chosen_impl:?})")
441444
}
442445
}

compiler/rustc_mir_transform/src/shim.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceKind<'tcx>) -> Body<
114114
receiver_by_ref,
115115
} => build_construct_coroutine_by_move_shim(tcx, coroutine_closure_def_id, receiver_by_ref),
116116

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

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

compiler/rustc_monomorphize/src/collector.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,7 @@ impl<'v> RootCollector<'_, 'v> {
16141614
/// For each externally implementable item, we should generate an alias MonoItem that
16151615
/// determines what implementation is called. This could be a default implementation.
16161616
fn push_extra_eii_roots(&mut self) {
1617-
for (shim_did, &EiiMapping { extern_item, chosen_impl, .. }) in
1617+
for (shim_did, &EiiMapping { extern_item, chosen_impl, weak_linkage, .. }) in
16181618
self.tcx.get_externally_implementable_item_impls(())
16191619
{
16201620
self.output.push(create_fn_mono_item(
@@ -1624,6 +1624,7 @@ impl<'v> RootCollector<'_, 'v> {
16241624
def_id: (*shim_did).into(),
16251625
extern_item,
16261626
chosen_impl,
1627+
weak_linkage,
16271628
},
16281629
args: ty::GenericArgs::empty(),
16291630
},

compiler/rustc_monomorphize/src/partitioning.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,12 @@ fn characteristic_def_id_of_mono_item<'tcx>(
635635
ty::InstanceKind::Item(def) => def,
636636
// EII shims have a characteristic defid.
637637
// But it's not their own, its the one of the extern item it is implementing.
638-
ty::InstanceKind::EiiShim { def_id: _, extern_item, chosen_impl: _ } => extern_item,
638+
ty::InstanceKind::EiiShim {
639+
def_id: _,
640+
extern_item,
641+
chosen_impl: _,
642+
weak_linkage: _,
643+
} => extern_item,
639644
ty::InstanceKind::VTableShim(..)
640645
| ty::InstanceKind::ReifyShim(..)
641646
| ty::InstanceKind::FnPtrShim(..)
@@ -770,8 +775,10 @@ fn mono_item_linkage_and_visibility<'tcx>(
770775
// together anyway. LLVM ensures the last one is the one that's chosen
771776
// TODO: this isn't the problem if they both decided to choose either the default or the
772777
// same explicit impl but if their view on it differs it is a problem!
773-
if let MonoItem::Fn(Instance { def: InstanceKind::EiiShim { .. }, .. }) = mono_item {
774-
(Linkage::WeakAny, vis)
778+
if let MonoItem::Fn(Instance { def: InstanceKind::EiiShim { weak_linkage, .. }, .. }) =
779+
mono_item
780+
{
781+
if *weak_linkage { (Linkage::WeakAny, vis) } else { (Linkage::External, vis) }
775782
} else {
776783
(Linkage::External, vis)
777784
}

0 commit comments

Comments
 (0)