Skip to content

Commit 74e93bb

Browse files
authored
Rollup merge of #70913 - eddyb:rc-arc-diagnostic-items, r=matthewjasper
Replace "rc"/"arc" lang items with Rc/Arc diagnostic items. `Rc`/`Arc` should have no special semantics, so it seems appropriate for them to not be lang items. r? @matthewjasper
2 parents 81a360f + 9d13520 commit 74e93bb

File tree

17 files changed

+49
-78
lines changed

17 files changed

+49
-78
lines changed

src/liballoc/rc.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ struct RcBox<T: ?Sized> {
279279
/// type `T`.
280280
///
281281
/// [get_mut]: #method.get_mut
282-
#[cfg_attr(not(test), lang = "rc")]
282+
#[cfg_attr(all(bootstrap, not(test)), lang = "rc")]
283+
#[cfg_attr(not(test), rustc_diagnostic_item = "Rc")]
283284
#[stable(feature = "rust1", since = "1.0.0")]
284285
pub struct Rc<T: ?Sized> {
285286
ptr: NonNull<RcBox<T>>,

src/liballoc/sync.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ macro_rules! acquire {
207207
/// counting in general.
208208
///
209209
/// [rc_examples]: ../../std/rc/index.html#examples
210-
#[cfg_attr(not(test), lang = "arc")]
210+
#[cfg_attr(all(bootstrap, not(test)), lang = "arc")]
211+
#[cfg_attr(not(test), rustc_diagnostic_item = "Arc")]
211212
#[stable(feature = "rust1", since = "1.0.0")]
212213
pub struct Arc<T: ?Sized> {
213214
ptr: NonNull<ArcInner<T>>,

src/librustc_error_codes/error_codes/E0152.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Erroneous code example:
55
```compile_fail,E0152
66
#![feature(lang_items)]
77
8-
#[lang = "arc"]
9-
struct Foo; // error: duplicate lang item found: `arc`
8+
#[lang = "owned_box"]
9+
struct Foo; // error: duplicate lang item found: `owned_box`
1010
```
1111

1212
Lang items are already implemented in the standard library. Unless you are

src/librustc_error_codes/error_codes/E0718.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ Examples of erroneous code:
66
```compile_fail,E0718
77
#![feature(lang_items)]
88
9-
#[lang = "arc"]
9+
#[lang = "owned_box"]
1010
static X: u32 = 42;
1111
```

src/librustc_hir/lang_items.rs

-3
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,4 @@ language_item_table! {
254254
AlignOffsetLangItem, "align_offset", align_offset_fn, Target::Fn;
255255

256256
TerminationTraitLangItem, "termination", termination, Target::Trait;
257-
258-
Arc, "arc", arc, Target::Struct;
259-
Rc, "rc", rc, Target::Struct;
260257
}

src/librustc_middle/ty/context.rs

+6
Original file line numberDiff line numberDiff line change
@@ -2209,6 +2209,12 @@ impl<'tcx> TyCtxt<'tcx> {
22092209
Some(self.mk_generic_adt(def_id, ty))
22102210
}
22112211

2212+
#[inline]
2213+
pub fn mk_diagnostic_item(self, ty: Ty<'tcx>, name: Symbol) -> Option<Ty<'tcx>> {
2214+
let def_id = self.get_diagnostic_item(name)?;
2215+
Some(self.mk_generic_adt(def_id, ty))
2216+
}
2217+
22122218
#[inline]
22132219
pub fn mk_maybe_uninit(self, ty: Ty<'tcx>) -> Ty<'tcx> {
22142220
let def_id = self.require_lang_item(lang_items::MaybeUninitLangItem, None);

src/librustc_middle/ty/mod.rs

+1-22
Original file line numberDiff line numberDiff line change
@@ -1829,14 +1829,9 @@ bitflags! {
18291829
const IS_BOX = 1 << 6;
18301830
/// Indicates whether the type is `ManuallyDrop`.
18311831
const IS_MANUALLY_DROP = 1 << 7;
1832-
// FIXME(matthewjasper) replace these with diagnostic items
1833-
/// Indicates whether the type is an `Arc`.
1834-
const IS_ARC = 1 << 8;
1835-
/// Indicates whether the type is an `Rc`.
1836-
const IS_RC = 1 << 9;
18371832
/// Indicates whether the variant list of this ADT is `#[non_exhaustive]`.
18381833
/// (i.e., this flag is never set unless this ADT is an enum).
1839-
const IS_VARIANT_LIST_NON_EXHAUSTIVE = 1 << 10;
1834+
const IS_VARIANT_LIST_NON_EXHAUSTIVE = 1 << 8;
18401835
}
18411836
}
18421837

@@ -2221,12 +2216,6 @@ impl<'tcx> AdtDef {
22212216
if Some(did) == tcx.lang_items().manually_drop() {
22222217
flags |= AdtFlags::IS_MANUALLY_DROP;
22232218
}
2224-
if Some(did) == tcx.lang_items().arc() {
2225-
flags |= AdtFlags::IS_ARC;
2226-
}
2227-
if Some(did) == tcx.lang_items().rc() {
2228-
flags |= AdtFlags::IS_RC;
2229-
}
22302219

22312220
AdtDef { did, variants, flags, repr }
22322221
}
@@ -2305,16 +2294,6 @@ impl<'tcx> AdtDef {
23052294
self.flags.contains(AdtFlags::IS_PHANTOM_DATA)
23062295
}
23072296

2308-
/// Returns `true` if this is `Arc<T>`.
2309-
pub fn is_arc(&self) -> bool {
2310-
self.flags.contains(AdtFlags::IS_ARC)
2311-
}
2312-
2313-
/// Returns `true` if this is `Rc<T>`.
2314-
pub fn is_rc(&self) -> bool {
2315-
self.flags.contains(AdtFlags::IS_RC)
2316-
}
2317-
23182297
/// Returns `true` if this is Box<T>.
23192298
#[inline]
23202299
pub fn is_box(&self) -> bool {

src/librustc_middle/ty/sty.rs

-18
Original file line numberDiff line numberDiff line change
@@ -1864,24 +1864,6 @@ impl<'tcx> TyS<'tcx> {
18641864
self.is_region_ptr() || self.is_unsafe_ptr() || self.is_fn_ptr()
18651865
}
18661866

1867-
/// Returns `true` if this type is an `Arc<T>`.
1868-
#[inline]
1869-
pub fn is_arc(&self) -> bool {
1870-
match self.kind {
1871-
Adt(def, _) => def.is_arc(),
1872-
_ => false,
1873-
}
1874-
}
1875-
1876-
/// Returns `true` if this type is an `Rc<T>`.
1877-
#[inline]
1878-
pub fn is_rc(&self) -> bool {
1879-
match self.kind {
1880-
Adt(def, _) => def.is_rc(),
1881-
_ => false,
1882-
}
1883-
}
1884-
18851867
#[inline]
18861868
pub fn is_box(&self) -> bool {
18871869
match self.kind {

src/librustc_mir/borrow_check/diagnostics/mod.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_middle::mir::{
1111
};
1212
use rustc_middle::ty::print::Print;
1313
use rustc_middle::ty::{self, DefIdTree, Ty, TyCtxt};
14-
use rustc_span::Span;
14+
use rustc_span::{symbol::sym, Span};
1515
use rustc_target::abi::VariantIdx;
1616

1717
use super::borrow_set::BorrowData;
@@ -632,20 +632,20 @@ pub(super) enum BorrowedContentSource<'tcx> {
632632
}
633633

634634
impl BorrowedContentSource<'tcx> {
635-
pub(super) fn describe_for_unnamed_place(&self) -> String {
635+
pub(super) fn describe_for_unnamed_place(&self, tcx: TyCtxt<'_>) -> String {
636636
match *self {
637637
BorrowedContentSource::DerefRawPointer => "a raw pointer".to_string(),
638638
BorrowedContentSource::DerefSharedRef => "a shared reference".to_string(),
639639
BorrowedContentSource::DerefMutableRef => "a mutable reference".to_string(),
640-
BorrowedContentSource::OverloadedDeref(ty) => {
641-
if ty.is_rc() {
640+
BorrowedContentSource::OverloadedDeref(ty) => match ty.kind {
641+
ty::Adt(def, _) if tcx.is_diagnostic_item(sym::Rc, def.did) => {
642642
"an `Rc`".to_string()
643-
} else if ty.is_arc() {
643+
}
644+
ty::Adt(def, _) if tcx.is_diagnostic_item(sym::Arc, def.did) => {
644645
"an `Arc`".to_string()
645-
} else {
646-
format!("dereference of `{}`", ty)
647646
}
648-
}
647+
_ => format!("dereference of `{}`", ty),
648+
},
649649
BorrowedContentSource::OverloadedIndex(ty) => format!("index of `{}`", ty),
650650
}
651651
}
@@ -662,22 +662,22 @@ impl BorrowedContentSource<'tcx> {
662662
}
663663
}
664664

665-
pub(super) fn describe_for_immutable_place(&self) -> String {
665+
pub(super) fn describe_for_immutable_place(&self, tcx: TyCtxt<'_>) -> String {
666666
match *self {
667667
BorrowedContentSource::DerefRawPointer => "a `*const` pointer".to_string(),
668668
BorrowedContentSource::DerefSharedRef => "a `&` reference".to_string(),
669669
BorrowedContentSource::DerefMutableRef => {
670670
bug!("describe_for_immutable_place: DerefMutableRef isn't immutable")
671671
}
672-
BorrowedContentSource::OverloadedDeref(ty) => {
673-
if ty.is_rc() {
672+
BorrowedContentSource::OverloadedDeref(ty) => match ty.kind {
673+
ty::Adt(def, _) if tcx.is_diagnostic_item(sym::Rc, def.did) => {
674674
"an `Rc`".to_string()
675-
} else if ty.is_arc() {
675+
}
676+
ty::Adt(def, _) if tcx.is_diagnostic_item(sym::Arc, def.did) => {
676677
"an `Arc`".to_string()
677-
} else {
678-
format!("a dereference of `{}`", ty)
679678
}
680-
}
679+
_ => format!("a dereference of `{}`", ty),
680+
},
681681
BorrowedContentSource::OverloadedIndex(ty) => format!("an index of `{}`", ty),
682682
}
683683
}

src/librustc_mir/borrow_check/diagnostics/move_errors.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,10 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
377377
span,
378378
&format!("`{}` which is behind a {}", place_desc, source_desc),
379379
),
380-
(_, _) => self.cannot_move_out_of(span, &source.describe_for_unnamed_place()),
380+
(_, _) => self.cannot_move_out_of(
381+
span,
382+
&source.describe_for_unnamed_place(self.infcx.tcx),
383+
),
381384
}
382385
}
383386
};

src/librustc_mir/borrow_check/diagnostics/mutability_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
120120
local: the_place_err.local,
121121
projection: proj_base,
122122
});
123-
let pointer_type = source.describe_for_immutable_place();
123+
let pointer_type = source.describe_for_immutable_place(self.infcx.tcx);
124124
opt_source = Some(source);
125125
if let Some(desc) = access_place_desc {
126126
item_msg = format!("`{}`", desc);

src/librustc_span/symbol.rs

+2
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ symbols! {
144144
any,
145145
arbitrary_enum_discriminant,
146146
arbitrary_self_types,
147+
Arc,
147148
Arguments,
148149
ArgumentV1,
149150
arm_target_feature,
@@ -582,6 +583,7 @@ symbols! {
582583
raw_dylib,
583584
raw_identifiers,
584585
raw_ref_op,
586+
Rc,
585587
Ready,
586588
reason,
587589
recursion_limit,

src/librustc_typeck/check/expr.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -902,8 +902,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
902902
error: MethodError<'tcx>,
903903
) {
904904
let rcvr = &args[0];
905-
let try_alt_rcvr = |err: &mut DiagnosticBuilder<'_>, rcvr_t, lang_item| {
906-
if let Some(new_rcvr_t) = self.tcx.mk_lang_item(rcvr_t, lang_item) {
905+
let try_alt_rcvr = |err: &mut DiagnosticBuilder<'_>, new_rcvr_t| {
906+
if let Some(new_rcvr_t) = new_rcvr_t {
907907
if let Ok(pick) = self.lookup_probe(
908908
span,
909909
segment.ident,
@@ -931,10 +931,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
931931
// Try alternative arbitrary self types that could fulfill this call.
932932
// FIXME: probe for all types that *could* be arbitrary self-types, not
933933
// just this whitelist.
934-
try_alt_rcvr(&mut err, rcvr_t, lang_items::OwnedBoxLangItem);
935-
try_alt_rcvr(&mut err, rcvr_t, lang_items::PinTypeLangItem);
936-
try_alt_rcvr(&mut err, rcvr_t, lang_items::Arc);
937-
try_alt_rcvr(&mut err, rcvr_t, lang_items::Rc);
934+
try_alt_rcvr(&mut err, self.tcx.mk_lang_item(rcvr_t, lang_items::OwnedBoxLangItem));
935+
try_alt_rcvr(&mut err, self.tcx.mk_lang_item(rcvr_t, lang_items::PinTypeLangItem));
936+
try_alt_rcvr(&mut err, self.tcx.mk_diagnostic_item(rcvr_t, sym::Arc));
937+
try_alt_rcvr(&mut err, self.tcx.mk_diagnostic_item(rcvr_t, sym::Rc));
938938
}
939939
err.emit();
940940
}

src/test/ui/error-codes/E0152.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![feature(lang_items)]
22

3-
#[lang = "arc"]
3+
#[lang = "owned_box"]
44
struct Foo; //~ ERROR E0152
55

66
fn main() {

src/test/ui/error-codes/E0152.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0152]: found duplicate lang item `arc`
1+
error[E0152]: found duplicate lang item `owned_box`
22
--> $DIR/E0152.rs:4:1
33
|
44
LL | struct Foo;

src/test/ui/error-codes/E0718.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![feature(lang_items)]
22

3-
// Arc is expected to be a struct, so this will error.
4-
#[lang = "arc"] //~ ERROR language item must be applied to a struct
3+
// Box is expected to be a struct, so this will error.
4+
#[lang = "owned_box"] //~ ERROR language item must be applied to a struct
55
static X: u32 = 42;
66

77
fn main() {}

src/test/ui/error-codes/E0718.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0718]: `arc` language item must be applied to a struct
1+
error[E0718]: `owned_box` language item must be applied to a struct
22
--> $DIR/E0718.rs:4:1
33
|
4-
LL | #[lang = "arc"]
5-
| ^^^^^^^^^^^^^^^ attribute should be applied to a struct, not a static item
4+
LL | #[lang = "owned_box"]
5+
| ^^^^^^^^^^^^^^^^^^^^^ attribute should be applied to a struct, not a static item
66

77
error: aborting due to previous error
88

0 commit comments

Comments
 (0)