Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
2 changes: 2 additions & 0 deletions compiler/rustc_middle/src/ty/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::marker::{DiscriminantKind, PointeeSized};
use rustc_abi::FieldIdx;
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def_id::LocalDefId;
use rustc_middle::ty::Const;
use rustc_serialize::{Decodable, Encodable};
use rustc_span::source_map::Spanned;
use rustc_span::{Span, SpanDecoder, SpanEncoder};
Expand Down Expand Up @@ -498,6 +499,7 @@ impl_decodable_via_ref! {
&'tcx ty::List<ty::BoundVariableKind<'tcx>>,
&'tcx ty::List<ty::Pattern<'tcx>>,
&'tcx ty::ListWithCachedTypeInfo<ty::Clause<'tcx>>,
&'tcx ty::List<Const<'tcx>>,
}

#[macro_export]
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_middle/src/ty/consts/valtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl<'tcx> ValTree<'tcx> {
}

pub fn is_zst(self) -> bool {
matches!(*self, ty::ValTreeKind::Branch(box []))
matches!(*self, ty::ValTreeKind::Branch(consts) if consts.is_empty())
}

pub fn from_raw_bytes(tcx: TyCtxt<'tcx>, bytes: &[u8]) -> Self {
Expand All @@ -58,7 +58,9 @@ impl<'tcx> ValTree<'tcx> {
tcx: TyCtxt<'tcx>,
branches: impl IntoIterator<Item = ty::Const<'tcx>>,
) -> Self {
tcx.intern_valtree(ty::ValTreeKind::Branch(branches.into_iter().collect()))
tcx.intern_valtree(ty::ValTreeKind::Branch(
tcx.mk_const_list_from_iter(branches.into_iter()),
))
}

pub fn from_scalar_int(tcx: TyCtxt<'tcx>, i: ScalarInt) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ impl<'tcx> CommonConsts<'tcx> {
))
};

let valtree_zst = mk_valtree(ty::ValTreeKind::Branch(Box::default()));
let valtree_zst = mk_valtree(ty::ValTreeKind::Branch(List::empty()));
let valtree_true = mk_valtree(ty::ValTreeKind::Leaf(ty::ScalarInt::TRUE));
let valtree_false = mk_valtree(ty::ValTreeKind::Leaf(ty::ScalarInt::FALSE));

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/ty/context/impl_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
type Safety = hir::Safety;
type Abi = ExternAbi;
type Const = ty::Const<'tcx>;
type Consts = &'tcx List<Self::Const>;

type ParamConst = ty::ParamConst;
type ValueConst = ty::Value<'tcx>;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1920,7 +1920,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
}
// Otherwise, print the array separated by commas (or if it's a tuple)
(ty::ValTreeKind::Branch(fields), ty::Array(..) | ty::Tuple(..)) => {
let fields_iter = fields.iter().copied();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consts should implement SliceLike so we can just do .iter() here now

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consts implements SliceLike, but .iter() on fields returns &Const and the following code works only for iterator over Const:

error[E0277]: the trait bound `&ty::consts::Const<'_>: print::Print<'tcx, Self>` is not satisfied
    --> compiler/rustc_middle/src/ty/print/pretty.rs:1964:34
     |
1964 | ...                   self.comma_sep(fields)?;
     |                            ^^^^^^^^^ the nightly-only, unstable trait `print::Print<'tcx, Self>` is not implemented for `&ty::consts::Const<'_>`
     |
help: the trait `Print<'tcx, _>` is not implemented for `&ty::consts::Const<'_>`
      but trait `Print<'_, _>` is implemented for `ty::consts::Const<'_>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think the as_slice should not be needed 🤔

Though given that we're in rustc_middle i guess this is the inherent impl for List or via deref?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still relevant, can you try removing the as_slice?

@lcnr lcnr Mar 5, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ (get rid of as_slice)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry, I have overlooked your original reply. It is fixed now.

let fields_iter = fields.as_slice().iter().copied();

match *cv.ty.kind() {
ty::Array(..) => {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/ty/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,4 +805,5 @@ list_fold! {
&'tcx ty::List<PlaceElem<'tcx>> : mk_place_elems,
&'tcx ty::List<ty::Pattern<'tcx>> : mk_patterns,
&'tcx ty::List<ty::ArgOutlivesPredicate<'tcx>> : mk_outlives,
&'tcx ty::List<ty::Const<'tcx>> : mk_const_list,
}
6 changes: 4 additions & 2 deletions compiler/rustc_mir_build/src/builder/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2941,10 +2941,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {

match pat.ctor() {
Constructor::Variant(variant_index) => {
let ValTreeKind::Branch(box [actual_variant_idx]) = *valtree else {
let ValTreeKind::Branch(branch) = *valtree else {
bug!("malformed valtree for an enum")
};
let Some(actual_variant_idx) = branch.get(0) else {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we stop asserting that the len is 1 here

bug!("malformed valtree for an enum")
};

let ValTreeKind::Leaf(actual_variant_idx) = *actual_variant_idx.to_value().valtree
else {
bug!("malformed valtree for an enum")
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_type_ir/src/const_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use rustc_type_ir_macros::{
GenericTypeVisitable, Lift_Generic, TypeFoldable_Generic, TypeVisitable_Generic,
};

use crate::inherent::*;
use crate::{self as ty, BoundVarIndexKind, Interner};

/// Represents a constant in Rust.
Expand Down Expand Up @@ -159,8 +160,7 @@ pub enum ValTreeKind<I: Interner> {
/// the fields of the variant.
///
/// ZST types are represented as an empty slice.
// FIXME(mgca): Use a `List` here instead of a boxed slice
Branch(Box<[I::Const]>),
Branch(I::Consts),
}

impl<I: Interner> ValTreeKind<I> {
Expand All @@ -179,7 +179,7 @@ impl<I: Interner> ValTreeKind<I> {
#[inline]
pub fn to_branch(&self) -> &[I::Const] {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub fn to_branch(&self) -> &[I::Const] {
pub fn to_branch(&self) -> I::Consts {

match self {
ValTreeKind::Branch(branch) => &**branch,
ValTreeKind::Branch(branch) => branch.as_slice(),
ValTreeKind::Leaf(..) => panic!("expected branch, got {:?}", self),
}
}
Expand All @@ -195,7 +195,7 @@ impl<I: Interner> ValTreeKind<I> {
/// Attempts to convert to a `ValTreeKind::Branch` value.
pub fn try_to_branch(&self) -> Option<&[I::Const]> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

match self {
ValTreeKind::Branch(branch) => Some(&**branch),
ValTreeKind::Branch(branch) => Some(branch.as_slice()),
ValTreeKind::Leaf(_) => None,
}
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_type_ir/src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,8 @@ impl<I: Interner> FlagComputation<I> {
match cv.valtree().kind() {
ty::ValTreeKind::Leaf(_) => (),
ty::ValTreeKind::Branch(cts) => {
for ct in cts {
self.add_const(*ct);
for ct in cts.iter() {
self.add_const(ct);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_type_ir/src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ pub trait Interner:

// Kinds of consts
type Const: Const<Self>;
type Consts: Copy + Debug + Hash + Eq + SliceLike<Item = Self::Const> + Default;
type ParamConst: Copy + Debug + Hash + Eq + ParamLike;
type ValueConst: ValueConst<Self>;
type ExprConst: ExprConst<Self>;
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_type_ir/src/relate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,9 @@ pub fn structurally_relate_consts<I: Interner, R: TypeRelation<I>>(
if branches_a.len() == branches_b.len() =>
{
branches_a
.into_iter()
.zip(branches_b)
.as_slice()
.iter()
.zip(branches_b.as_slice().iter())
.all(|(a, b)| relation.relate(*a, *b).is_ok())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.as_slice()
.iter()
.zip(branches_b.as_slice().iter())
.all(|(a, b)| relation.relate(*a, *b).is_ok())
.iter()
.zip(branches_b.iter())
.all(|(a, b)| relation.relate(a, b).is_ok())

}
_ => false,
Expand Down
Loading