Skip to content

Commit e7529d6

Browse files
committed
Update term for use in more places
Replace use of `ty()` on term and use it in more places. This will allow more flexibility in the future, but slightly worried it allows items which are consts which only accept types.
1 parent 67f5667 commit e7529d6

File tree

31 files changed

+284
-128
lines changed

31 files changed

+284
-128
lines changed

compiler/rustc_infer/src/infer/opaque_types.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_hir as hir;
77
use rustc_hir::def_id::LocalDefId;
88
use rustc_middle::ty::fold::BottomUpFolder;
99
use rustc_middle::ty::subst::{GenericArgKind, Subst};
10-
use rustc_middle::ty::{self, OpaqueTypeKey, Term, Ty, TyCtxt, TypeFoldable, TypeVisitor};
10+
use rustc_middle::ty::{self, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable, TypeVisitor};
1111
use rustc_span::Span;
1212

1313
use std::ops::ControlFlow;
@@ -584,13 +584,8 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> {
584584
debug!(?predicate);
585585

586586
if let ty::PredicateKind::Projection(projection) = predicate.kind().skip_binder() {
587-
if let Term::Ty(ty) = projection.term {
588-
if ty.references_error() {
589-
// No point on adding these obligations since there's a type error involved.
590-
return tcx.ty_error();
591-
}
592-
} else {
593-
todo!();
587+
if projection.term.references_error() {
588+
return tcx.ty_error();
594589
}
595590
}
596591

compiler/rustc_interface/src/util.rs

+2
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,8 @@ impl<'a, 'b> ReplaceBodyWithLoop<'a, 'b> {
742742
ast::AssocConstraintKind::Equality { ref term } => {
743743
match term {
744744
Term::Ty(ty) => involves_impl_trait(ty),
745+
// FIXME(...): This should check if the constant
746+
// involves a trait impl, but for now ignore.
745747
Term::Const(_) => false,
746748
}
747749
}

compiler/rustc_middle/src/ty/assoc.rs

+13
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,19 @@ impl<'tcx> AssocItems<'tcx> {
152152
.find(|item| tcx.hygienic_eq(ident, item.ident, parent_def_id))
153153
}
154154

155+
/// Returns the associated item with the given name and any of `AssocKind`, if one exists.
156+
pub fn find_by_name_and_kinds(
157+
&self,
158+
tcx: TyCtxt<'_>,
159+
ident: Ident,
160+
kinds: &[AssocKind],
161+
parent_def_id: DefId,
162+
) -> Option<&ty::AssocItem> {
163+
self.filter_by_name_unhygienic(ident.name)
164+
.filter(|item| kinds.contains(&item.kind))
165+
.find(|item| tcx.hygienic_eq(ident, item.ident, parent_def_id))
166+
}
167+
155168
/// Returns the associated item with the given name in the given `Namespace`, if one exists.
156169
pub fn find_by_name_and_namespace(
157170
&self,

compiler/rustc_middle/src/ty/consts.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,14 @@ impl<'tcx> Const<'tcx> {
8686
if let Some(lit_input) = lit_input {
8787
// If an error occurred, ignore that it's a literal and leave reporting the error up to
8888
// mir.
89-
if let Ok(c) = tcx.at(expr.span).lit_to_const(lit_input) {
90-
return Some(c);
91-
} else {
92-
tcx.sess.delay_span_bug(expr.span, "Const::from_anon_const: couldn't lit_to_const");
89+
match tcx.at(expr.span).lit_to_const(lit_input) {
90+
Ok(c) => return Some(c),
91+
Err(e) => {
92+
tcx.sess.delay_span_bug(
93+
expr.span,
94+
&format!("Const::from_anon_const: couldn't lit_to_const {:?}", e),
95+
);
96+
}
9397
}
9498
}
9599

compiler/rustc_middle/src/ty/flags.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ impl FlagComputation {
245245
self.add_projection_ty(projection_ty);
246246
match term {
247247
Term::Ty(ty) => self.add_ty(ty),
248-
Term::Const(_c) => todo!(),
248+
Term::Const(c) => self.add_const(c),
249249
}
250250
}
251251
ty::PredicateKind::WellFormed(arg) => {

compiler/rustc_middle/src/ty/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -815,8 +815,8 @@ impl<'tcx> From<&'tcx Const<'tcx>> for Term<'tcx> {
815815
}
816816

817817
impl<'tcx> Term<'tcx> {
818-
pub fn ty(&self) -> Ty<'tcx> {
819-
if let Term::Ty(ty) = self { ty } else { panic!("Expected type") }
818+
pub fn ty(&self) -> Option<Ty<'tcx>> {
819+
if let Term::Ty(ty) = self { Some(ty) } else { None }
820820
}
821821
}
822822

@@ -861,8 +861,8 @@ impl<'tcx> PolyProjectionPredicate<'tcx> {
861861
self.map_bound(|predicate| predicate.projection_ty.trait_ref(tcx))
862862
}
863863

864-
pub fn ty(&self) -> Binder<'tcx, Ty<'tcx>> {
865-
self.map_bound(|predicate| if let Term::Ty(ty) = predicate.term { ty } else { todo!() })
864+
pub fn term(&self) -> Binder<'tcx, Term<'tcx>> {
865+
self.map_bound(|predicate| predicate.term)
866866
}
867867

868868
/// The `DefId` of the `TraitItem` for the associated type.

compiler/rustc_middle/src/ty/print/pretty.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::mir::interpret::{AllocRange, ConstValue, GlobalAlloc, Pointer, Provenance, Scalar};
22
use crate::ty::subst::{GenericArg, GenericArgKind, Subst};
3-
use crate::ty::{self, ConstInt, DefIdTree, ParamConst, ScalarInt, Ty, TyCtxt, TypeFoldable};
3+
use crate::ty::{self, ConstInt, DefIdTree, ParamConst, ScalarInt, Term, Ty, TyCtxt, TypeFoldable};
44
use rustc_apfloat::ieee::{Double, Single};
55
use rustc_data_structures::fx::FxHashMap;
66
use rustc_data_structures::sso::SsoHashSet;
@@ -799,7 +799,7 @@ pub trait PrettyPrinter<'tcx>:
799799
let trait_ref = proj_ref.required_poly_trait_ref(self.tcx());
800800

801801
// Projection type entry -- the def-id for naming, and the ty.
802-
let proj_ty = (proj_ref.projection_def_id(), proj_ref.ty());
802+
let proj_ty = (proj_ref.projection_def_id(), proj_ref.term());
803803

804804
self.insert_trait_and_projection(
805805
trait_ref,
@@ -850,8 +850,10 @@ pub trait PrettyPrinter<'tcx>:
850850
}
851851

852852
p!(")");
853-
if !return_ty.skip_binder().is_unit() {
854-
p!("-> ", print(return_ty));
853+
if let Term::Ty(ty) = return_ty.skip_binder() {
854+
if !ty.is_unit() {
855+
p!("-> ", print(return_ty));
856+
}
855857
}
856858
p!(write("{}", if paren_needed { ")" } else { "" }));
857859

@@ -902,14 +904,15 @@ pub trait PrettyPrinter<'tcx>:
902904
first = false;
903905
}
904906

905-
for (assoc_item_def_id, ty) in assoc_items {
907+
for (assoc_item_def_id, term) in assoc_items {
908+
let ty = if let Term::Ty(ty) = term.skip_binder() { ty } else { continue };
906909
if !first {
907910
p!(", ");
908911
}
909912
p!(write("{} = ", self.tcx().associated_item(assoc_item_def_id).ident));
910913

911914
// Skip printing `<[generator@] as Generator<_>>::Return` from async blocks
912-
match ty.skip_binder().kind() {
915+
match ty.kind() {
913916
ty::Projection(ty::ProjectionTy { item_def_id, .. })
914917
if Some(*item_def_id) == self.tcx().lang_items().generator_return() =>
915918
{
@@ -943,8 +946,11 @@ pub trait PrettyPrinter<'tcx>:
943946
fn insert_trait_and_projection(
944947
&mut self,
945948
trait_ref: ty::PolyTraitRef<'tcx>,
946-
proj_ty: Option<(DefId, ty::Binder<'tcx, Ty<'tcx>>)>,
947-
traits: &mut BTreeMap<ty::PolyTraitRef<'tcx>, BTreeMap<DefId, ty::Binder<'tcx, Ty<'tcx>>>>,
949+
proj_ty: Option<(DefId, ty::Binder<'tcx, Term<'tcx>>)>,
950+
traits: &mut BTreeMap<
951+
ty::PolyTraitRef<'tcx>,
952+
BTreeMap<DefId, ty::Binder<'tcx, Term<'tcx>>>,
953+
>,
948954
fn_traits: &mut BTreeMap<ty::PolyTraitRef<'tcx>, OpaqueFnEntry<'tcx>>,
949955
) {
950956
let trait_def_id = trait_ref.def_id();
@@ -2716,5 +2722,5 @@ pub struct OpaqueFnEntry<'tcx> {
27162722
has_fn_once: bool,
27172723
fn_mut_trait_ref: Option<ty::PolyTraitRef<'tcx>>,
27182724
fn_trait_ref: Option<ty::PolyTraitRef<'tcx>>,
2719-
return_ty: Option<ty::Binder<'tcx, Ty<'tcx>>>,
2725+
return_ty: Option<ty::Binder<'tcx, Term<'tcx>>>,
27202726
}

compiler/rustc_middle/src/ty/relate.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -833,19 +833,30 @@ impl<'tcx> Relate<'tcx> for ty::TraitPredicate<'tcx> {
833833
}
834834
}
835835

836+
impl<'tcx> Relate<'tcx> for ty::Term<'tcx> {
837+
fn relate<R: TypeRelation<'tcx>>(
838+
relation: &mut R,
839+
a: Self,
840+
b: Self,
841+
) -> RelateResult<'tcx, Self> {
842+
Ok(match (a, b) {
843+
(Term::Ty(a), Term::Ty(b)) => relation.relate(a, b)?.into(),
844+
(Term::Const(a), Term::Const(b)) => relation.relate(a, b)?.into(),
845+
_ => return Err(TypeError::Mismatch),
846+
})
847+
}
848+
}
849+
836850
impl<'tcx> Relate<'tcx> for ty::ProjectionPredicate<'tcx> {
837851
fn relate<R: TypeRelation<'tcx>>(
838852
relation: &mut R,
839853
a: ty::ProjectionPredicate<'tcx>,
840854
b: ty::ProjectionPredicate<'tcx>,
841855
) -> RelateResult<'tcx, ty::ProjectionPredicate<'tcx>> {
842-
match (a.term, b.term) {
843-
(Term::Ty(a_ty), Term::Ty(b_ty)) => Ok(ty::ProjectionPredicate {
844-
projection_ty: relation.relate(a.projection_ty, b.projection_ty)?,
845-
term: relation.relate(a_ty, b_ty)?.into(),
846-
}),
847-
_ => todo!(),
848-
}
856+
Ok(ty::ProjectionPredicate {
857+
projection_ty: relation.relate(a.projection_ty, b.projection_ty)?,
858+
term: relation.relate(a.term, b.term)?.into(),
859+
})
849860
}
850861
}
851862

compiler/rustc_middle/src/ty/sty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1583,7 +1583,7 @@ impl<'tcx> ExistentialProjection<'tcx> {
15831583
let ty = if let Term::Ty(ty) = projection_predicate.term {
15841584
ty
15851585
} else {
1586-
todo!();
1586+
panic!("Only types are permitted here");
15871587
};
15881588

15891589
Self {

compiler/rustc_privacy/src/lib.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,10 @@ where
128128
polarity: _,
129129
}) => self.visit_trait(trait_ref),
130130
ty::PredicateKind::Projection(ty::ProjectionPredicate { projection_ty, term }) => {
131-
let ty = term.ty();
132-
ty.visit_with(self)?;
131+
match term {
132+
ty::Term::Ty(ty) => ty.visit_with(self)?,
133+
ty::Term::Const(ct) => ct.visit_with(self)?,
134+
}
133135
self.visit_projection_ty(projection_ty)
134136
}
135137
ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty, _region)) => {
@@ -1186,10 +1188,13 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> {
11861188
}
11871189

11881190
for (poly_predicate, _) in bounds.projection_bounds {
1189-
if self.visit(poly_predicate.skip_binder().term.ty()).is_break()
1190-
|| self
1191-
.visit_projection_ty(poly_predicate.skip_binder().projection_ty)
1192-
.is_break()
1191+
let pred = poly_predicate.skip_binder();
1192+
let poly_pred_term = match pred.term {
1193+
ty::Term::Ty(ty) => self.visit(ty),
1194+
ty::Term::Const(ct) => self.visit(ct),
1195+
};
1196+
if poly_pred_term.is_break()
1197+
|| self.visit_projection_ty(pred.projection_ty).is_break()
11931198
{
11941199
return;
11951200
}

compiler/rustc_trait_selection/src/traits/auto_trait.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use super::*;
66
use crate::infer::region_constraints::{Constraint, RegionConstraintData};
77
use crate::infer::InferCtxt;
88
use rustc_middle::ty::fold::TypeFolder;
9-
use rustc_middle::ty::{Region, RegionVid};
9+
use rustc_middle::ty::{Region, RegionVid, Term};
1010

1111
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1212

@@ -606,7 +606,11 @@ impl<'tcx> AutoTraitFinder<'tcx> {
606606
}
607607

608608
fn is_self_referential_projection(&self, p: ty::PolyProjectionPredicate<'_>) -> bool {
609-
matches!(*p.ty().skip_binder().kind(), ty::Projection(proj) if proj == p.skip_binder().projection_ty)
609+
if let Term::Ty(ty) = p.term().skip_binder() {
610+
matches!(ty.kind(), ty::Projection(proj) if proj == &p.skip_binder().projection_ty)
611+
} else {
612+
false
613+
}
610614
}
611615

612616
fn evaluate_nested_obligations(
@@ -663,7 +667,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
663667
// Additionally, we check if we've seen this predicate before,
664668
// to avoid rendering duplicate bounds to the user.
665669
if self.is_param_no_infer(p.skip_binder().projection_ty.substs)
666-
&& !p.ty().skip_binder().has_infer_types()
670+
&& !p.term().skip_binder().has_infer_types()
667671
&& is_new_pred
668672
{
669673
debug!(
@@ -752,7 +756,8 @@ impl<'tcx> AutoTraitFinder<'tcx> {
752756
// when we started out trying to unify
753757
// some inference variables. See the comment above
754758
// for more infomration
755-
if p.ty().skip_binder().has_infer_types() {
759+
if p.term().skip_binder().ty().map_or(false, |ty| ty.has_infer_types())
760+
{
756761
if !self.evaluate_nested_obligations(
757762
ty,
758763
v.into_iter(),
@@ -774,7 +779,8 @@ impl<'tcx> AutoTraitFinder<'tcx> {
774779
// However, we should always make progress (either by generating
775780
// subobligations or getting an error) when we started off with
776781
// inference variables
777-
if p.ty().skip_binder().has_infer_types() {
782+
if p.term().skip_binder().ty().map_or(false, |ty| ty.has_infer_types())
783+
{
778784
panic!("Unexpected result when selecting {:?} {:?}", ty, obligation)
779785
}
780786
}

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1304,8 +1304,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
13041304

13051305
debug!(
13061306
"report_projection_error normalized_ty={:?} data.ty={:?}",
1307-
normalized_ty,
1308-
data.term.ty()
1307+
normalized_ty, data.term,
13091308
);
13101309

13111310
let is_normalized_ty_expected = !matches!(
@@ -1315,16 +1314,17 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
13151314
| ObligationCauseCode::ObjectCastObligation(_)
13161315
| ObligationCauseCode::OpaqueType
13171316
);
1318-
1317+
// FIXME(...): Handle Consts here
1318+
let data_ty = data.term.ty().unwrap();
13191319
if let Err(error) = self.at(&obligation.cause, obligation.param_env).eq_exp(
13201320
is_normalized_ty_expected,
13211321
normalized_ty,
1322-
data.term.ty(),
1322+
data_ty,
13231323
) {
13241324
values = Some(infer::ValuePairs::Types(ExpectedFound::new(
13251325
is_normalized_ty_expected,
13261326
normalized_ty,
1327-
data.term.ty(),
1327+
data_ty,
13281328
)));
13291329

13301330
err_buf = error;

compiler/rustc_trait_selection/src/traits/project.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,9 @@ fn project_and_unify_type<'cx, 'tcx>(
212212
debug!(?normalized_ty, ?obligations, "project_and_unify_type result");
213213

214214
let infcx = selcx.infcx();
215-
match infcx
216-
.at(&obligation.cause, obligation.param_env)
217-
.eq(normalized_ty, obligation.predicate.term.ty())
218-
{
215+
// FIXME(...): Handle consts here as well as types.
216+
let obligation_pred_ty = obligation.predicate.term.ty().unwrap();
217+
match infcx.at(&obligation.cause, obligation.param_env).eq(normalized_ty, obligation_pred_ty) {
219218
Ok(InferOk { obligations: inferred_obligations, value: () }) => {
220219
obligations.extend(inferred_obligations);
221220
Ok(Ok(Some(obligations)))
@@ -1803,7 +1802,9 @@ fn confirm_param_env_candidate<'cx, 'tcx>(
18031802
Ok(InferOk { value: _, obligations }) => {
18041803
nested_obligations.extend(obligations);
18051804
assoc_ty_own_obligations(selcx, obligation, &mut nested_obligations);
1806-
Progress { ty: cache_entry.term.ty(), obligations: nested_obligations }
1805+
// FIXME(...): Handle consts here as well? Maybe this progress type should just take
1806+
// a term instead.
1807+
Progress { ty: cache_entry.term.ty().unwrap(), obligations: nested_obligations }
18071808
}
18081809
Err(e) => {
18091810
let msg = format!(

compiler/rustc_trait_selection/src/traits/relationships.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub(crate) fn update<'tcx, T>(
6262
if let ty::PredicateKind::Projection(predicate) = obligation.predicate.kind().skip_binder() {
6363
// If the projection predicate (Foo::Bar == X) has X as a non-TyVid,
6464
// we need to make it into one.
65-
if let Some(vid) = predicate.term.ty().ty_vid() {
65+
if let Some(vid) = predicate.term.ty().and_then(|ty| ty.ty_vid()) {
6666
debug!("relationship: {:?}.output = true", vid);
6767
engine.relationships().entry(vid).or_default().output = true;
6868
}

compiler/rustc_trait_selection/src/traits/wf.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ pub fn predicate_obligations<'a, 'tcx>(
116116
}
117117
ty::PredicateKind::Projection(t) => {
118118
wf.compute_projection(t.projection_ty);
119-
wf.compute(t.term.ty().into());
119+
wf.compute(match t.term {
120+
ty::Term::Ty(ty) => ty.into(),
121+
ty::Term::Const(c) => c.into(),
122+
})
120123
}
121124
ty::PredicateKind::WellFormed(arg) => {
122125
wf.compute(arg);
@@ -219,7 +222,7 @@ fn extend_cause_with_original_assoc_item_obligation<'tcx>(
219222
// projection coming from another associated type. See
220223
// `src/test/ui/associated-types/point-at-type-on-obligation-failure.rs` and
221224
// `traits-assoc-type-in-supertrait-bad.rs`.
222-
if let ty::Projection(projection_ty) = proj.term.ty().kind() {
225+
if let Some(ty::Projection(projection_ty)) = proj.term.ty().map(|ty| ty.kind()) {
223226
if let Some(&impl_item_id) =
224227
tcx.impl_item_implementor_ids(impl_def_id).get(&projection_ty.item_def_id)
225228
{

0 commit comments

Comments
 (0)