Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
7668154
triagebot: add autolabel for rustdoc JS files
AaryanAgrawal96 Jan 2, 2026
9c4ead6
Remove legacy homu `try` and `auto` branch mentions
Kobzol Jan 7, 2026
291d0a9
diagnostics: make implicit Sized bounds explicit in E0277
Daniel-B-Smith Jan 6, 2026
b57c249
fix: make `Type::of` supported unsized types
BD103 Jan 12, 2026
1bde2f4
chore: test `Type::of` on unsized types
BD103 Jan 12, 2026
feb44c3
Make `--print=check-cfg` output compatible `--check-cfg` arguments
Urgau Jan 8, 2026
dd2b0a8
Update compiletest for new `--print=check-cfg` output
Urgau Jan 8, 2026
d697b4d
Improve std::path::Path::join documentation
KaiTomotake Jan 4, 2026
e5dbb3b
armv7-unknown-linux-uclibceabihf.md: Fix build-toml syntax
3v1n0 Jan 13, 2026
1e69091
armv7-unknown-linux-uclibceabihf.md: Update toolchain download link
3v1n0 Jan 13, 2026
c263d36
Fix citool tests
Kobzol Jan 13, 2026
ef9cbad
ui: add regression test for macro resolution ICE
vsriramv Jan 13, 2026
62849e6
Reduce flakyness for `tests/rustdoc-gui/notable-trait.goml`
GuillaumeGomez Jan 13, 2026
1d96806
type params on eii
jdonszelmann Jan 10, 2026
d25f471
deduplicate error message when EII has generics
jdonszelmann Jan 10, 2026
b64a9be
bless the tests
jdonszelmann Jan 11, 2026
b454f76
ensure generics are still properly reported on EII *implementations*,…
jdonszelmann Jan 11, 2026
d616e6c
Emit error instead of delayed bug when meeting mismatch type for const
reddevilmidzy Jan 13, 2026
84d59d0
Port `#[rustc_dump_user_args]`
JonathanBrouwer Jan 12, 2026
cf4d480
Port `#[rustc_dump_def_parents]`
JonathanBrouwer Jan 12, 2026
2a45540
Port `#[rustc_dump_item_bounds]`
JonathanBrouwer Jan 12, 2026
a4c34b4
Port `#[rustc_dump_vtable]`
JonathanBrouwer Jan 12, 2026
abcbf72
Port `#[rustc_dump_predicates]`
JonathanBrouwer Jan 12, 2026
e027ecd
feat: support arrays in type reflection
BD103 Jan 12, 2026
71f8ea9
refactor: move tuples type info ui test to coretest
BD103 Jan 13, 2026
bf31616
Rollup merge of #150587 - add-rustdoc-js-autolabel, r=fmease
GuillaumeGomez Jan 13, 2026
7fc878c
Rollup merge of #150677 - improve-doc, r=Mark-Simulacrum
GuillaumeGomez Jan 13, 2026
6c700ec
Rollup merge of #150737 - smithdb3/fix-150576, r=chenyukang
GuillaumeGomez Jan 13, 2026
66f25e9
Rollup merge of #150771 - remove-homu-branches, r=marcoieni
GuillaumeGomez Jan 13, 2026
080c704
Rollup merge of #150840 - print-check-cfg-rework-output, r=nnethercote
GuillaumeGomez Jan 13, 2026
0d3692a
Rollup merge of #150915 - eii-regression-test-149983, r=kivooeo
GuillaumeGomez Jan 13, 2026
3bbce8f
Rollup merge of #151017 - rustc_dump, r=jdonszelmann
GuillaumeGomez Jan 13, 2026
93dc648
Rollup merge of #151019 - type-of-unsized, r=oli-obk
GuillaumeGomez Jan 13, 2026
a8e3ce5
Rollup merge of #151031 - reflect-arrays, r=oli-obk
GuillaumeGomez Jan 13, 2026
7709192
Rollup merge of #151043 - patch-1, r=ehuss
GuillaumeGomez Jan 13, 2026
5b7fc2f
Rollup merge of #151052 - add-test-ice-in-resolve, r=Kivooeo
GuillaumeGomez Jan 13, 2026
860e60a
Rollup merge of #151053 - reduce-gui-test-flakyness, r=jieyouxu
GuillaumeGomez Jan 13, 2026
3adbd3a
Rollup merge of #151055 - mgca-arr, r=BoxyUwU
GuillaumeGomez Jan 13, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: support arrays in type reflection
  • Loading branch information
BD103 committed Jan 13, 2026
commit e027ecdbb57822527ed69e3cc81ca9ce36735678
35 changes: 33 additions & 2 deletions compiler/rustc_const_eval/src/const_eval/type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustc_hir::LangItem;
use rustc_middle::mir::interpret::CtfeProvenance;
use rustc_middle::span_bug;
use rustc_middle::ty::layout::TyAndLayout;
use rustc_middle::ty::{self, ScalarInt, Ty};
use rustc_middle::ty::{self, Const, ScalarInt, Ty};
use rustc_span::{Symbol, sym};

use crate::const_eval::CompileTimeMachine;
Expand Down Expand Up @@ -56,14 +56,21 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
self.write_tuple_fields(tuple_place, fields, ty)?;
variant
}
ty::Array(ty, len) => {
let (variant, variant_place) = downcast(sym::Array)?;
let array_place = self.project_field(&variant_place, FieldIdx::ZERO)?;

self.write_array_type_info(array_place, *ty, *len)?;

variant
}
// For now just merge all primitives into one `Leaf` variant with no data
ty::Uint(_) | ty::Int(_) | ty::Float(_) | ty::Char | ty::Bool => {
downcast(sym::Leaf)?.0
}
ty::Adt(_, _)
| ty::Foreign(_)
| ty::Str
| ty::Array(_, _)
| ty::Pat(_, _)
| ty::Slice(_)
| ty::RawPtr(..)
Expand Down Expand Up @@ -172,4 +179,28 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
}
interp_ok(())
}

pub(crate) fn write_array_type_info(
&mut self,
place: impl Writeable<'tcx, CtfeProvenance>,
ty: Ty<'tcx>,
len: Const<'tcx>,
) -> InterpResult<'tcx> {
// Iterate over all fields of `type_info::Array`.
for (field_idx, field) in
place.layout().ty.ty_adt_def().unwrap().non_enum_variant().fields.iter_enumerated()
{
let field_place = self.project_field(&place, field_idx)?;

match field.name {
// Write the `TypeId` of the array's elements to the `element_ty` field.
sym::element_ty => self.write_type_id(ty, &field_place)?,
// Write the length of the array to the `len` field.
sym::len => self.write_scalar(len.to_leaf(), &field_place)?,
other => span_bug!(self.tcx.def_span(field.did), "unimplemented field {other}"),
}
}

interp_ok(())
}
}
2 changes: 2 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ symbols! {
Arc,
ArcWeak,
Argument,
Array,
ArrayIntoIter,
AsMut,
AsRef,
Expand Down Expand Up @@ -939,6 +940,7 @@ symbols! {
eii_impl,
eii_internals,
eii_shared_macro,
element_ty,
emit,
emit_enum,
emit_enum_variant,
Expand Down
13 changes: 13 additions & 0 deletions library/core/src/mem/type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ impl Type {
pub enum TypeKind {
/// Tuples.
Tuple(Tuple),
/// Arrays.
Array(Array),
/// Primitives
/// FIXME(#146922): disambiguate further
Leaf,
Expand All @@ -69,3 +71,14 @@ pub struct Field {
/// Offset in bytes from the parent type
pub offset: usize,
}

/// Compile-time type information about arrays.
#[derive(Debug)]
#[non_exhaustive]
#[unstable(feature = "type_info", issue = "146922")]
pub struct Array {
/// The type of each element in the array.
pub element_ty: TypeId,
/// The length of the array.
pub len: usize,
}
1 change: 1 addition & 0 deletions library/coretests/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
#![feature(try_blocks)]
#![feature(try_find)]
#![feature(try_trait_v2)]
#![feature(type_info)]
#![feature(uint_bit_width)]
#![feature(uint_gather_scatter_bits)]
#![feature(unsize)]
Expand Down
2 changes: 2 additions & 0 deletions library/coretests/tests/mem.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod type_info;

use core::mem::*;
use core::{array, ptr};
use std::cell::Cell;
Expand Down
23 changes: 23 additions & 0 deletions library/coretests/tests/mem/type_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::any::TypeId;
use std::mem::type_info::{Type, TypeKind};

#[test]
fn test_arrays() {
// Normal array.
match const { Type::of::<[u16; 4]>() }.kind {
TypeKind::Array(array) => {
assert_eq!(array.element_ty, TypeId::of::<u16>());
assert_eq!(array.len, 4);
}
_ => unreachable!(),
}

// Zero-length array.
match const { Type::of::<[bool; 0]>() }.kind {
TypeKind::Array(array) => {
assert_eq!(array.element_ty, TypeId::of::<bool>());
assert_eq!(array.len, 0);
}
_ => unreachable!(),
}
}
1 change: 1 addition & 0 deletions tests/ui/reflection/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct Unsized {

fn main() {
println!("{:#?}", const { Type::of::<(u8, u8, ())>() }.kind);
println!("{:#?}", const { Type::of::<[u8; 2]>() }.kind);
println!("{:#?}", const { Type::of::<Foo>() }.kind);
println!("{:#?}", const { Type::of::<Bar>() }.kind);
println!("{:#?}", const { Type::of::<&Unsized>() }.kind);
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/reflection/dump.run.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ Tuple(
],
},
)
Array(
Array {
element_ty: TypeId(0x0596b48cc04376e64d5c788c2aa46bdb),
len: 2,
},
)
Other
Other
Other
Expand Down