Skip to content

Commit 6f0acbc

Browse files
authored
Rollup merge of #88644 - eopb:abstractconst_leaf_subst, r=lcnr
`AbstractConst` private fields Calls `subst` in `AbstractConst::root` when `Node` is `Leaf`. r? ``@lcnr``
2 parents fb9232b + be30e60 commit 6f0acbc

File tree

3 files changed

+25
-29
lines changed

3 files changed

+25
-29
lines changed

compiler/rustc_privacy/src/lib.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rustc_middle::span_bug;
2323
use rustc_middle::thir::abstract_const::Node as ACNode;
2424
use rustc_middle::ty::fold::TypeVisitor;
2525
use rustc_middle::ty::query::Providers;
26-
use rustc_middle::ty::subst::{InternalSubsts, Subst};
26+
use rustc_middle::ty::subst::InternalSubsts;
2727
use rustc_middle::ty::{self, Const, GenericParamDefKind, TraitRef, Ty, TyCtxt, TypeFoldable};
2828
use rustc_session::lint;
2929
use rustc_span::hygiene::Transparency;
@@ -153,11 +153,8 @@ where
153153
tcx: TyCtxt<'tcx>,
154154
ct: AbstractConst<'tcx>,
155155
) -> ControlFlow<V::BreakTy> {
156-
const_evaluatable::walk_abstract_const(tcx, ct, |node| match node.root() {
157-
ACNode::Leaf(leaf) => {
158-
let leaf = leaf.subst(tcx, ct.substs);
159-
self.visit_const(leaf)
160-
}
156+
const_evaluatable::walk_abstract_const(tcx, ct, |node| match node.root(tcx) {
157+
ACNode::Leaf(leaf) => self.visit_const(leaf),
161158
ACNode::Cast(_, _, ty) => self.visit_ty(ty),
162159
ACNode::Binop(..) | ACNode::UnaryOp(..) | ACNode::FunctionCall(_, _) => {
163160
ControlFlow::CONTINUE

compiler/rustc_trait_selection/src/traits/const_evaluatable.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@ pub fn is_const_evaluatable<'cx, 'tcx>(
8080
Concrete,
8181
}
8282
let mut failure_kind = FailureKind::Concrete;
83-
walk_abstract_const::<!, _>(tcx, ct, |node| match node.root() {
83+
walk_abstract_const::<!, _>(tcx, ct, |node| match node.root(tcx) {
8484
Node::Leaf(leaf) => {
85-
let leaf = leaf.subst(tcx, ct.substs);
8685
if leaf.has_infer_types_or_consts() {
8786
failure_kind = FailureKind::MentionsInfer;
8887
} else if leaf.definitely_has_param_types_or_consts(tcx) {
@@ -92,7 +91,6 @@ pub fn is_const_evaluatable<'cx, 'tcx>(
9291
ControlFlow::CONTINUE
9392
}
9493
Node::Cast(_, _, ty) => {
95-
let ty = ty.subst(tcx, ct.substs);
9694
if ty.has_infer_types_or_consts() {
9795
failure_kind = FailureKind::MentionsInfer;
9896
} else if ty.definitely_has_param_types_or_consts(tcx) {
@@ -187,8 +185,8 @@ pub fn is_const_evaluatable<'cx, 'tcx>(
187185
pub struct AbstractConst<'tcx> {
188186
// FIXME: Consider adding something like `IndexSlice`
189187
// and use this here.
190-
pub inner: &'tcx [Node<'tcx>],
191-
pub substs: SubstsRef<'tcx>,
188+
inner: &'tcx [Node<'tcx>],
189+
substs: SubstsRef<'tcx>,
192190
}
193191

194192
impl<'tcx> AbstractConst<'tcx> {
@@ -218,8 +216,14 @@ impl<'tcx> AbstractConst<'tcx> {
218216
}
219217

220218
#[inline]
221-
pub fn root(self) -> Node<'tcx> {
222-
self.inner.last().copied().unwrap()
219+
pub fn root(self, tcx: TyCtxt<'tcx>) -> Node<'tcx> {
220+
let node = self.inner.last().copied().unwrap();
221+
match node {
222+
Node::Leaf(leaf) => Node::Leaf(leaf.subst(tcx, self.substs)),
223+
Node::Cast(kind, operand, ty) => Node::Cast(kind, operand, ty.subst(tcx, self.substs)),
224+
// Don't perform substitution on the following as they can't directly contain generic params
225+
Node::Binop(_, _, _) | Node::UnaryOp(_, _) | Node::FunctionCall(_, _) => node,
226+
}
223227
}
224228
}
225229

@@ -542,7 +546,7 @@ where
542546
f: &mut dyn FnMut(AbstractConst<'tcx>) -> ControlFlow<R>,
543547
) -> ControlFlow<R> {
544548
f(ct)?;
545-
let root = ct.root();
549+
let root = ct.root(tcx);
546550
match root {
547551
Node::Leaf(_) => ControlFlow::CONTINUE,
548552
Node::Binop(_, l, r) => {
@@ -570,27 +574,23 @@ pub(super) fn try_unify<'tcx>(
570574
// We substitute generics repeatedly to allow AbstractConsts to unify where a
571575
// ConstKind::Unevalated could be turned into an AbstractConst that would unify e.g.
572576
// Param(N) should unify with Param(T), substs: [Unevaluated("T2", [Unevaluated("T3", [Param(N)])])]
573-
while let Node::Leaf(a_ct) = a.root() {
574-
let a_ct = a_ct.subst(tcx, a.substs);
577+
while let Node::Leaf(a_ct) = a.root(tcx) {
575578
match AbstractConst::from_const(tcx, a_ct) {
576579
Ok(Some(a_act)) => a = a_act,
577580
Ok(None) => break,
578581
Err(_) => return true,
579582
}
580583
}
581-
while let Node::Leaf(b_ct) = b.root() {
582-
let b_ct = b_ct.subst(tcx, b.substs);
584+
while let Node::Leaf(b_ct) = b.root(tcx) {
583585
match AbstractConst::from_const(tcx, b_ct) {
584586
Ok(Some(b_act)) => b = b_act,
585587
Ok(None) => break,
586588
Err(_) => return true,
587589
}
588590
}
589591

590-
match (a.root(), b.root()) {
592+
match (a.root(tcx), b.root(tcx)) {
591593
(Node::Leaf(a_ct), Node::Leaf(b_ct)) => {
592-
let a_ct = a_ct.subst(tcx, a.substs);
593-
let b_ct = b_ct.subst(tcx, b.substs);
594594
if a_ct.ty != b_ct.ty {
595595
return false;
596596
}

compiler/rustc_trait_selection/src/traits/object_safety.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -837,14 +837,13 @@ fn contains_illegal_self_type_reference<'tcx, T: TypeFoldable<'tcx>>(
837837
// constants which are not considered const evaluatable.
838838
use rustc_middle::thir::abstract_const::Node;
839839
if let Ok(Some(ct)) = AbstractConst::new(self.tcx, uv.shrink()) {
840-
const_evaluatable::walk_abstract_const(self.tcx, ct, |node| match node.root() {
841-
Node::Leaf(leaf) => {
842-
let leaf = leaf.subst(self.tcx, ct.substs);
843-
self.visit_const(leaf)
844-
}
845-
Node::Cast(_, _, ty) => self.visit_ty(ty),
846-
Node::Binop(..) | Node::UnaryOp(..) | Node::FunctionCall(_, _) => {
847-
ControlFlow::CONTINUE
840+
const_evaluatable::walk_abstract_const(self.tcx, ct, |node| {
841+
match node.root(self.tcx) {
842+
Node::Leaf(leaf) => self.visit_const(leaf),
843+
Node::Cast(_, _, ty) => self.visit_ty(ty),
844+
Node::Binop(..) | Node::UnaryOp(..) | Node::FunctionCall(_, _) => {
845+
ControlFlow::CONTINUE
846+
}
848847
}
849848
})
850849
} else {

0 commit comments

Comments
 (0)