Use ConstArg for assoc item constraints

This commit is contained in:
Noah Lev 2024-06-04 17:21:54 -07:00
parent e7c85cb1e0
commit 8818708a31
9 changed files with 31 additions and 22 deletions

View File

@ -1062,7 +1062,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
AssocItemConstraintKind::Equality { term } => { AssocItemConstraintKind::Equality { term } => {
let term = match term { let term = match term {
Term::Ty(ty) => self.lower_ty(ty, itctx).into(), Term::Ty(ty) => self.lower_ty(ty, itctx).into(),
Term::Const(c) => self.lower_anon_const_to_anon_const(c).into(), Term::Const(c) => self.lower_anon_const_to_const_arg(c).into(),
}; };
hir::AssocItemConstraintKind::Equality { term } hir::AssocItemConstraintKind::Equality { term }
} }

View File

@ -242,7 +242,7 @@ impl<'hir> ConstArg<'hir> {
} }
} }
pub fn hir_id(&self) -> HirId { pub fn anon_const_hir_id(&self) -> HirId {
match self.kind { match self.kind {
ConstArgKind::Anon(anon) => anon.hir_id, ConstArgKind::Anon(anon) => anon.hir_id,
} }
@ -288,7 +288,7 @@ impl GenericArg<'_> {
match self { match self {
GenericArg::Lifetime(l) => l.hir_id, GenericArg::Lifetime(l) => l.hir_id,
GenericArg::Type(t) => t.hir_id, GenericArg::Type(t) => t.hir_id,
GenericArg::Const(c) => c.hir_id(), GenericArg::Const(c) => c.anon_const_hir_id(), // FIXME
GenericArg::Infer(i) => i.hir_id, GenericArg::Infer(i) => i.hir_id,
} }
} }
@ -2453,7 +2453,7 @@ impl<'hir> AssocItemConstraint<'hir> {
} }
/// Obtain the const on the RHS of an assoc const equality constraint if applicable. /// Obtain the const on the RHS of an assoc const equality constraint if applicable.
pub fn ct(self) -> Option<&'hir AnonConst> { pub fn ct(self) -> Option<&'hir ConstArg<'hir>> {
match self.kind { match self.kind {
AssocItemConstraintKind::Equality { term: Term::Const(ct) } => Some(ct), AssocItemConstraintKind::Equality { term: Term::Const(ct) } => Some(ct),
_ => None, _ => None,
@ -2464,7 +2464,7 @@ impl<'hir> AssocItemConstraint<'hir> {
#[derive(Debug, Clone, Copy, HashStable_Generic)] #[derive(Debug, Clone, Copy, HashStable_Generic)]
pub enum Term<'hir> { pub enum Term<'hir> {
Ty(&'hir Ty<'hir>), Ty(&'hir Ty<'hir>),
Const(&'hir AnonConst), Const(&'hir ConstArg<'hir>),
} }
impl<'hir> From<&'hir Ty<'hir>> for Term<'hir> { impl<'hir> From<&'hir Ty<'hir>> for Term<'hir> {
@ -2473,8 +2473,8 @@ impl<'hir> From<&'hir Ty<'hir>> for Term<'hir> {
} }
} }
impl<'hir> From<&'hir AnonConst> for Term<'hir> { impl<'hir> From<&'hir ConstArg<'hir>> for Term<'hir> {
fn from(c: &'hir AnonConst) -> Self { fn from(c: &'hir ConstArg<'hir>) -> Self {
Term::Const(c) Term::Const(c)
} }
} }

View File

@ -1290,7 +1290,7 @@ pub fn walk_assoc_item_constraint<'v, V: Visitor<'v>>(
match constraint.kind { match constraint.kind {
AssocItemConstraintKind::Equality { ref term } => match term { AssocItemConstraintKind::Equality { ref term } => match term {
Term::Ty(ref ty) => try_visit!(visitor.visit_ty(ty)), Term::Ty(ref ty) => try_visit!(visitor.visit_ty(ty)),
Term::Const(ref c) => try_visit!(visitor.visit_anon_const(c)), Term::Const(ref c) => try_visit!(visitor.visit_const_arg(c)),
}, },
AssocItemConstraintKind::Bound { bounds } => { AssocItemConstraintKind::Bound { bounds } => {
walk_list!(visitor, visit_param_bound, bounds) walk_list!(visitor, visit_param_bound, bounds)

View File

@ -224,7 +224,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
.iter() .iter()
.copied() .copied()
.filter_map(AssocItemConstraint::ct) .filter_map(AssocItemConstraint::ct)
.position(|ct| ct.hir_id == hir_id) .position(|ct| ct.anon_const_hir_id() == hir_id)
.map(|idx| (idx, seg)) .map(|idx| (idx, seg))
}) })
}) else { }) else {

View File

@ -413,12 +413,19 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
}); });
// Provide the resolved type of the associated constant to `type_of(AnonConst)`. // Provide the resolved type of the associated constant to `type_of(AnonConst)`.
if let Some(anon_const) = constraint.ct() { if let Some(const_arg) = constraint.ct() {
let ty = alias_term #[allow(irrefutable_let_patterns)] // FIXME
.map_bound(|alias| tcx.type_of(alias.def_id).instantiate(tcx, alias.args)); if let hir::ConstArgKind::Anon(anon_const) = const_arg.kind {
let ty = let ty = alias_term
check_assoc_const_binding_type(self, constraint.ident, ty, constraint.hir_id); .map_bound(|alias| tcx.type_of(alias.def_id).instantiate(tcx, alias.args));
tcx.feed_anon_const_type(anon_const.def_id, ty::EarlyBinder::bind(ty)); let ty = check_assoc_const_binding_type(
self,
constraint.ident,
ty,
constraint.hir_id,
);
tcx.feed_anon_const_type(anon_const.def_id, ty::EarlyBinder::bind(ty));
}
} }
alias_term alias_term
@ -435,7 +442,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
hir::AssocItemConstraintKind::Equality { term } => { hir::AssocItemConstraintKind::Equality { term } => {
let term = match term { let term = match term {
hir::Term::Ty(ty) => self.lower_ty(ty).into(), hir::Term::Ty(ty) => self.lower_ty(ty).into(),
hir::Term::Const(ct) => ty::Const::from_anon_const(tcx, ct.def_id).into(), hir::Term::Const(ct) => {
ty::Const::from_const_arg(tcx, ct, ty::FeedConstTy::No).into()
}
}; };
// Find any late-bound regions declared in `ty` that are not // Find any late-bound regions declared in `ty` that are not

View File

@ -340,7 +340,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
{ {
let span = match term { let span = match term {
hir::Term::Ty(ty) => ty.span, hir::Term::Ty(ty) => ty.span,
hir::Term::Const(ct) => tcx.def_span(ct.def_id), hir::Term::Const(ct) => ct.span(),
}; };
(span, Some(ident.span), assoc_item.kind, assoc_kind) (span, Some(ident.span), assoc_item.kind, assoc_kind)
} else { } else {
@ -1296,8 +1296,7 @@ pub fn prohibit_assoc_item_constraint(
hir::AssocItemConstraintKind::Equality { term: hir::Term::Const(c) }, hir::AssocItemConstraintKind::Equality { term: hir::Term::Const(c) },
GenericParamDefKind::Const { .. }, GenericParamDefKind::Const { .. },
) => { ) => {
let span = tcx.hir().span(c.hir_id); suggest_direct_use(&mut err, c.span());
suggest_direct_use(&mut err, span);
} }
(hir::AssocItemConstraintKind::Bound { bounds }, _) => { (hir::AssocItemConstraintKind::Bound { bounds }, _) => {
// Suggest `impl<T: Bound> Trait<T> for Foo` when finding // Suggest `impl<T: Bound> Trait<T> for Foo` when finding

View File

@ -911,7 +911,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
let term: ty::Term<'_> = match term { let term: ty::Term<'_> = match term {
hir::Term::Ty(ty) => self.lower_ty(ty).into(), hir::Term::Ty(ty) => self.lower_ty(ty).into(),
hir::Term::Const(ct) => { hir::Term::Const(ct) => {
ty::Const::from_anon_const(tcx, ct.def_id).into() ty::Const::from_const_arg(tcx, ct, ty::FeedConstTy::No)
.into()
} }
}; };
// FIXME(#97583): This isn't syntactically well-formed! // FIXME(#97583): This isn't syntactically well-formed!

View File

@ -1726,7 +1726,7 @@ impl<'a> State<'a> {
self.word_space("="); self.word_space("=");
match term { match term {
Term::Ty(ty) => self.print_type(ty), Term::Ty(ty) => self.print_type(ty),
Term::Const(ref c) => self.print_anon_const(c), Term::Const(ref c) => self.print_const_arg(c),
} }
} }
hir::AssocItemConstraintKind::Bound { bounds } => { hir::AssocItemConstraintKind::Bound { bounds } => {

View File

@ -433,7 +433,7 @@ fn clean_hir_term<'tcx>(term: &hir::Term<'tcx>, cx: &mut DocContext<'tcx>) -> Te
match term { match term {
hir::Term::Ty(ty) => Term::Type(clean_ty(ty, cx)), hir::Term::Ty(ty) => Term::Type(clean_ty(ty, cx)),
hir::Term::Const(c) => Term::Constant(clean_middle_const( hir::Term::Const(c) => Term::Constant(clean_middle_const(
ty::Binder::dummy(ty::Const::from_anon_const(cx.tcx, c.def_id)), ty::Binder::dummy(ty::Const::from_const_arg(cx.tcx, c, ty::FeedConstTy::No)),
cx, cx,
)), )),
} }