Skip to content

Commit defdb83

Browse files
committed
Auto merge of #151234 - Zalathar:rollup-mtOIaDC, r=Zalathar
Rollup of 5 pull requests Successful merges: - #151092 (Generate macro expansion for rust compiler crates docs) - #151120 (Fix deprecated attribute intra-doc link not resolved in the right location on reexported item) - #151207 (Preliminary match/capture test cleanup for PR 150681) - #151221 (Reorganizing `tests/ui/issues` 5 tests [1/N]) - #151222 (feat: Support references in reflection type info) r? @ghost
2 parents 1a9f168 + 2956932 commit defdb83

28 files changed

+325
-140
lines changed

compiler/rustc_const_eval/src/const_eval/type_info.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_abi::FieldIdx;
2+
use rustc_ast::Mutability;
23
use rustc_hir::LangItem;
34
use rustc_middle::mir::interpret::{CtfeProvenance, Scalar};
45
use rustc_middle::span_bug;
@@ -103,12 +104,19 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
103104
let (variant, _variant_place) = downcast(sym::Str)?;
104105
variant
105106
}
107+
ty::Ref(_, ty, mutability) => {
108+
let (variant, variant_place) = downcast(sym::Reference)?;
109+
let reference_place =
110+
self.project_field(&variant_place, FieldIdx::ZERO)?;
111+
self.write_reference_type_info(reference_place, *ty, *mutability)?;
112+
113+
variant
114+
}
106115
ty::Adt(_, _)
107116
| ty::Foreign(_)
108117
| ty::Pat(_, _)
109118
| ty::Slice(_)
110119
| ty::RawPtr(..)
111-
| ty::Ref(..)
112120
| ty::FnDef(..)
113121
| ty::FnPtr(..)
114122
| ty::UnsafeBinder(..)
@@ -279,4 +287,29 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
279287
}
280288
interp_ok(())
281289
}
290+
291+
pub(crate) fn write_reference_type_info(
292+
&mut self,
293+
place: impl Writeable<'tcx, CtfeProvenance>,
294+
ty: Ty<'tcx>,
295+
mutability: Mutability,
296+
) -> InterpResult<'tcx> {
297+
// Iterate over all fields of `type_info::Reference`.
298+
for (field_idx, field) in
299+
place.layout().ty.ty_adt_def().unwrap().non_enum_variant().fields.iter_enumerated()
300+
{
301+
let field_place = self.project_field(&place, field_idx)?;
302+
303+
match field.name {
304+
// Write the `TypeId` of the reference's inner type to the `ty` field.
305+
sym::pointee => self.write_type_id(ty, &field_place)?,
306+
// Write the boolean representing the reference's mutability to the `mutable` field.
307+
sym::mutable => {
308+
self.write_scalar(Scalar::from_bool(mutability.is_mut()), &field_place)?
309+
}
310+
other => span_bug!(self.tcx.def_span(field.did), "unimplemented field {other}"),
311+
}
312+
}
313+
interp_ok(())
314+
}
282315
}

compiler/rustc_span/src/symbol.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ symbols! {
343343
RefCell,
344344
RefCellRef,
345345
RefCellRefMut,
346+
Reference,
346347
Relaxed,
347348
Release,
348349
Result,
@@ -1521,6 +1522,7 @@ symbols! {
15211522
must_use,
15221523
mut_preserve_binding_mode_2024,
15231524
mut_ref,
1525+
mutable,
15241526
naked,
15251527
naked_asm,
15261528
naked_functions,

library/core/src/mem/type_info.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ pub enum TypeKind {
5555
Float(Float),
5656
/// String slice type.
5757
Str(Str),
58+
/// References.
59+
Reference(Reference),
5860
/// FIXME(#146922): add all the common types
5961
Other,
6062
}
@@ -133,3 +135,14 @@ pub struct Float {
133135
pub struct Str {
134136
// No additional information to provide for now.
135137
}
138+
139+
/// Compile-time type information about references.
140+
#[derive(Debug)]
141+
#[non_exhaustive]
142+
#[unstable(feature = "type_info", issue = "146922")]
143+
pub struct Reference {
144+
/// The type of the value being referred to.
145+
pub pointee: TypeId,
146+
/// Whether this reference is mutable or not.
147+
pub mutable: bool,
148+
}

library/coretests/tests/mem/type_info.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::any::TypeId;
1+
use std::any::{Any, TypeId};
22
use std::mem::type_info::{Type, TypeKind};
33

44
#[test]
@@ -95,3 +95,33 @@ fn test_primitives() {
9595
let Type { kind: Str(_ty), size, .. } = (const { Type::of::<str>() }) else { panic!() };
9696
assert_eq!(size, None);
9797
}
98+
99+
#[test]
100+
fn test_references() {
101+
// Immutable reference.
102+
match const { Type::of::<&u8>() }.kind {
103+
TypeKind::Reference(reference) => {
104+
assert_eq!(reference.pointee, TypeId::of::<u8>());
105+
assert!(!reference.mutable);
106+
}
107+
_ => unreachable!(),
108+
}
109+
110+
// Mutable pointer.
111+
match const { Type::of::<&mut u64>() }.kind {
112+
TypeKind::Reference(reference) => {
113+
assert_eq!(reference.pointee, TypeId::of::<u64>());
114+
assert!(reference.mutable);
115+
}
116+
_ => unreachable!(),
117+
}
118+
119+
// Wide pointer.
120+
match const { Type::of::<&dyn Any>() }.kind {
121+
TypeKind::Reference(reference) => {
122+
assert_eq!(reference.pointee, TypeId::of::<dyn Any>());
123+
assert!(!reference.mutable);
124+
}
125+
_ => unreachable!(),
126+
}
127+
}

src/bootstrap/src/core/build_steps/doc.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,13 @@ impl Step for Rustc {
932932
// see https://github.com/rust-lang/rust/pull/122066#issuecomment-1983049222
933933
// If there is any bug, please comment out the next line.
934934
cargo.rustdocflag("--generate-link-to-definition");
935+
// FIXME: Currently, `--generate-macro-expansion` option is buggy in `beta` rustdoc. To
936+
// allow CI to pass, we only enable the option in stage 2 and higher.
937+
// cfg(bootstrap)
938+
// ^ Adding this so it's not forgotten when the new release is done.
939+
if builder.top_stage > 1 {
940+
cargo.rustdocflag("--generate-macro-expansion");
941+
}
935942

936943
compile::rustc_cargo(builder, &mut cargo, target, &build_compiler, &self.crates);
937944
cargo.arg("-Zskip-rustdoc-fingerprint");

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::fmt::Display;
77
use std::mem;
88
use std::ops::Range;
99

10-
use rustc_ast::attr::AttributeExt;
1110
use rustc_ast::util::comments::may_have_doc_links;
1211
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
1312
use rustc_data_structures::intern::Interned;
@@ -1062,20 +1061,21 @@ fn preprocessed_markdown_links(s: &str) -> Vec<PreprocessedMarkdownLink> {
10621061
impl LinkCollector<'_, '_> {
10631062
#[instrument(level = "debug", skip_all)]
10641063
fn resolve_links(&mut self, item: &Item) {
1064+
let tcx = self.cx.tcx;
10651065
if !self.cx.document_private()
10661066
&& let Some(def_id) = item.item_id.as_def_id()
10671067
&& let Some(def_id) = def_id.as_local()
1068-
&& !self.cx.tcx.effective_visibilities(()).is_exported(def_id)
1068+
&& !tcx.effective_visibilities(()).is_exported(def_id)
10691069
&& !has_primitive_or_keyword_or_attribute_docs(&item.attrs.other_attrs)
10701070
{
10711071
// Skip link resolution for non-exported items.
10721072
return;
10731073
}
10741074

10751075
let mut insert_links = |item_id, doc: &str| {
1076-
let module_id = match self.cx.tcx.def_kind(item_id) {
1077-
DefKind::Mod if item.inner_docs(self.cx.tcx) => item_id,
1078-
_ => find_nearest_parent_module(self.cx.tcx, item_id).unwrap(),
1076+
let module_id = match tcx.def_kind(item_id) {
1077+
DefKind::Mod if item.inner_docs(tcx) => item_id,
1078+
_ => find_nearest_parent_module(tcx, item_id).unwrap(),
10791079
};
10801080
for md_link in preprocessed_markdown_links(&doc) {
10811081
let link = self.resolve_link(&doc, item, item_id, module_id, &md_link);
@@ -1108,15 +1108,33 @@ impl LinkCollector<'_, '_> {
11081108

11091109
// Also resolve links in the note text of `#[deprecated]`.
11101110
for attr in &item.attrs.other_attrs {
1111-
let Some(note_sym) = attr.deprecation_note() else { continue };
1111+
let rustc_hir::Attribute::Parsed(rustc_hir::attrs::AttributeKind::Deprecation {
1112+
span,
1113+
deprecation,
1114+
}) = attr
1115+
else {
1116+
continue;
1117+
};
1118+
let Some(note_sym) = deprecation.note else { continue };
11121119
let note = note_sym.as_str();
11131120

11141121
if !may_have_doc_links(note) {
11151122
continue;
11161123
}
11171124

11181125
debug!("deprecated_note={note}");
1119-
insert_links(item.item_id.expect_def_id(), note)
1126+
// When resolving an intra-doc link inside a deprecation note that is on an inlined
1127+
// `use` statement, we need to use the `def_id` of the `use` statement, not the
1128+
// inlined item.
1129+
// <https://github.com/rust-lang/rust/pull/151120>
1130+
let item_id = if let Some(inline_stmt_id) = item.inline_stmt_id
1131+
&& item.span(tcx).is_none_or(|item_span| !item_span.inner().contains(*span))
1132+
{
1133+
inline_stmt_id.to_def_id()
1134+
} else {
1135+
item.item_id.expect_def_id()
1136+
};
1137+
insert_links(item_id, note)
11201138
}
11211139
}
11221140

tests/mir-opt/unreachable.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ fn as_match() {
4747
// CHECK: bb1: {
4848
// CHECK: [[eq:_.*]] = Ne({{.*}}, const 1_isize);
4949
// CHECK-NEXT: assume(move [[eq]]);
50-
// CHECK-NEXT: goto -> bb2;
51-
// CHECK: bb2: {
50+
// CHECK-NEXT: goto -> [[return:bb.*]];
51+
// CHECK: [[return]]: {
52+
// CHECK-NOT: {{bb.*}}: {
5253
// CHECK: return;
53-
// CHECK: bb3: {
54+
// CHECK: {{bb.*}}: {
5455
// CHECK-NEXT: unreachable;
5556
match empty() {
5657
Some(_x) => match _x {},

tests/mir-opt/unreachable_enum_branching.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct Plop {
4949
fn simple() {
5050
// CHECK-LABEL: fn simple(
5151
// CHECK: [[discr:_.*]] = discriminant(
52-
// CHECK: switchInt(move [[discr]]) -> [0: [[unreachable:bb.*]], 1: [[unreachable]], 2: bb1, otherwise: [[unreachable]]];
52+
// CHECK: switchInt(move [[discr]]) -> [0: [[unreachable:bb.*]], 1: [[unreachable]], 2: {{bb.*}}, otherwise: [[unreachable]]];
5353
// CHECK: [[unreachable]]: {
5454
// CHECK-NEXT: unreachable;
5555
match Test1::C {
@@ -63,7 +63,7 @@ fn simple() {
6363
fn custom_discriminant() {
6464
// CHECK-LABEL: fn custom_discriminant(
6565
// CHECK: [[discr:_.*]] = discriminant(
66-
// CHECK: switchInt(move [[discr]]) -> [4: bb3, 5: bb2, otherwise: [[unreachable:bb.*]]];
66+
// CHECK: switchInt(move [[discr]]) -> [4: {{bb.*}}, 5: {{bb.*}}, otherwise: [[unreachable:bb.*]]];
6767
// CHECK: [[unreachable]]: {
6868
// CHECK-NEXT: unreachable;
6969
match Test2::D {
@@ -76,7 +76,7 @@ fn custom_discriminant() {
7676
fn otherwise_t1() {
7777
// CHECK-LABEL: fn otherwise_t1(
7878
// CHECK: [[discr:_.*]] = discriminant(
79-
// CHECK: switchInt(move [[discr]]) -> [0: bb5, 1: bb5, 2: bb1, otherwise: [[unreachable:bb.*]]];
79+
// CHECK: switchInt(move [[discr]]) -> [0: {{bb.*}}, 1: {{bb.*}}, 2: {{bb.*}}, otherwise: [[unreachable:bb.*]]];
8080
// CHECK: [[unreachable]]: {
8181
// CHECK-NEXT: unreachable;
8282
match Test1::C {
@@ -90,7 +90,7 @@ fn otherwise_t1() {
9090
fn otherwise_t2() {
9191
// CHECK-LABEL: fn otherwise_t2(
9292
// CHECK: [[discr:_.*]] = discriminant(
93-
// CHECK: switchInt(move [[discr]]) -> [4: bb2, 5: bb1, otherwise: [[unreachable:bb.*]]];
93+
// CHECK: switchInt(move [[discr]]) -> [4: {{bb.*}}, 5: {{bb.*}}, otherwise: [[unreachable:bb.*]]];
9494
// CHECK: [[unreachable]]: {
9595
// CHECK-NEXT: unreachable;
9696
match Test2::D {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// This test ensures that the intra-doc link in `deprecated` note is resolved at the correct
2+
// location (ie in the current crate and not in the reexported item's location/crate) and
3+
// therefore doesn't crash.
4+
//
5+
// This is a regression test for <https://github.com/rust-lang/rust/issues/151028>.
6+
7+
#![crate_name = "foo"]
8+
9+
#[deprecated(note = "use [`std::mem::forget`]")]
10+
#[doc(inline)]
11+
pub use std::mem::drop;

tests/ui/closures/2229_closure_analysis/capture-enums.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@ edition:2021
22

33
#![feature(rustc_attrs)]
4+
#![feature(stmt_expr_attributes)]
45

56
enum Info {
67
Point(i32, i32, String),
@@ -14,9 +15,6 @@ fn multi_variant_enum() {
1415
let meta = Info::Meta("meta".into(), vec);
1516

1617
let c = #[rustc_capture_analysis]
17-
//~^ ERROR: attributes on expressions are experimental
18-
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
19-
//~| NOTE: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
2018
|| {
2119
//~^ ERROR First Pass analysis includes:
2220
//~| ERROR Min Capture analysis includes:
@@ -48,9 +46,6 @@ fn single_variant_enum() {
4846
let point = SingleVariant::Point(10, -10, "1".into());
4947

5048
let c = #[rustc_capture_analysis]
51-
//~^ ERROR: attributes on expressions are experimental
52-
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
53-
//~| NOTE: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
5449
|| {
5550
//~^ ERROR First Pass analysis includes:
5651
//~| ERROR Min Capture analysis includes:

0 commit comments

Comments
 (0)