Skip to content

Commit 65f16da

Browse files
committed
Use ConstArg for assoc item constraints
1 parent dd1e585 commit 65f16da

File tree

10 files changed

+96
-53
lines changed

10 files changed

+96
-53
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10411041
AssocItemConstraintKind::Equality { term } => {
10421042
let term = match term {
10431043
Term::Ty(ty) => self.lower_ty(ty, itctx).into(),
1044-
Term::Const(c) => self.lower_anon_const(c).into(),
1044+
Term::Const(c) => self.lower_anon_const_as_const_arg(c).into(),
10451045
};
10461046
hir::AssocItemConstraintKind::Equality { term }
10471047
}
@@ -1155,11 +1155,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11551155
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
11561156
None,
11571157
);
1158-
return GenericArg::Const(ConstArg {
1158+
let const_arg = ConstArg {
11591159
hir_id: self.next_id(),
11601160
kind: ConstArgKind::Path(qpath),
11611161
is_desugared_from_effects: false,
1162-
});
1162+
};
1163+
return GenericArg::Const(self.arena.alloc(const_arg));
11631164
}
11641165
}
11651166
}
@@ -1171,7 +1172,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11711172
}
11721173
}
11731174

1174-
fn lower_anon_const_as_const_arg(&mut self, anon: &AnonConst) -> hir::ConstArg<'hir> {
1175+
fn lower_anon_const_as_const_arg(&mut self, anon: &AnonConst) -> &'hir hir::ConstArg<'hir> {
1176+
self.arena.alloc(self.lower_anon_const_as_const_arg_direct(anon))
1177+
}
1178+
1179+
fn lower_anon_const_as_const_arg_direct(&mut self, anon: &AnonConst) -> hir::ConstArg<'hir> {
11751180
if let ExprKind::Path(qself, path) = &anon.value.kind
11761181
&& let Some(res) = self
11771182
.resolver
@@ -2654,11 +2659,11 @@ impl<'hir> GenericArgsCtor<'hir> {
26542659
}
26552660
};
26562661

2657-
self.args.push(hir::GenericArg::Const(hir::ConstArg {
2662+
self.args.push(hir::GenericArg::Const(lcx.arena.alloc(hir::ConstArg {
26582663
hir_id: lcx.next_id(),
26592664
kind: const_arg_kind,
26602665
is_desugared_from_effects: true,
2661-
}))
2666+
})))
26622667
}
26632668

26642669
fn is_empty(&self) -> bool {

compiler/rustc_hir/src/hir.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ impl InferArg {
268268
pub enum GenericArg<'hir> {
269269
Lifetime(&'hir Lifetime),
270270
Type(&'hir Ty<'hir>),
271-
Const(ConstArg<'hir>),
271+
Const(&'hir ConstArg<'hir>),
272272
Infer(InferArg),
273273
}
274274

@@ -2440,7 +2440,7 @@ impl<'hir> AssocItemConstraint<'hir> {
24402440
}
24412441

24422442
/// Obtain the const on the RHS of an assoc const equality constraint if applicable.
2443-
pub fn ct(self) -> Option<&'hir AnonConst> {
2443+
pub fn ct(self) -> Option<&'hir ConstArg<'hir>> {
24442444
match self.kind {
24452445
AssocItemConstraintKind::Equality { term: Term::Const(ct) } => Some(ct),
24462446
_ => None,
@@ -2451,7 +2451,7 @@ impl<'hir> AssocItemConstraint<'hir> {
24512451
#[derive(Debug, Clone, Copy, HashStable_Generic)]
24522452
pub enum Term<'hir> {
24532453
Ty(&'hir Ty<'hir>),
2454-
Const(&'hir AnonConst),
2454+
Const(&'hir ConstArg<'hir>),
24552455
}
24562456

24572457
impl<'hir> From<&'hir Ty<'hir>> for Term<'hir> {
@@ -2460,8 +2460,8 @@ impl<'hir> From<&'hir Ty<'hir>> for Term<'hir> {
24602460
}
24612461
}
24622462

2463-
impl<'hir> From<&'hir AnonConst> for Term<'hir> {
2464-
fn from(c: &'hir AnonConst) -> Self {
2463+
impl<'hir> From<&'hir ConstArg<'hir>> for Term<'hir> {
2464+
fn from(c: &'hir ConstArg<'hir>) -> Self {
24652465
Term::Const(c)
24662466
}
24672467
}
@@ -3937,7 +3937,7 @@ mod size_asserts {
39373937
static_assert_size!(FnDecl<'_>, 40);
39383938
static_assert_size!(ForeignItem<'_>, 72);
39393939
static_assert_size!(ForeignItemKind<'_>, 40);
3940-
static_assert_size!(GenericArg<'_>, 40);
3940+
static_assert_size!(GenericArg<'_>, 16);
39413941
static_assert_size!(GenericBound<'_>, 48);
39423942
static_assert_size!(Generics<'_>, 56);
39433943
static_assert_size!(Impl<'_>, 80);

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1292,7 +1292,7 @@ pub fn walk_assoc_item_constraint<'v, V: Visitor<'v>>(
12921292
match constraint.kind {
12931293
AssocItemConstraintKind::Equality { ref term } => match term {
12941294
Term::Ty(ref ty) => try_visit!(visitor.visit_ty(ty)),
1295-
Term::Const(ref c) => try_visit!(visitor.visit_anon_const(c)),
1295+
Term::Const(ref c) => try_visit!(visitor.visit_const_arg(c)),
12961296
},
12971297
AssocItemConstraintKind::Bound { bounds } => {
12981298
walk_list!(visitor, visit_param_bound, bounds)

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -426,12 +426,16 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
426426
});
427427

428428
// Provide the resolved type of the associated constant to `type_of(AnonConst)`.
429-
if let hir::AssocItemConstraintKind::Equality { term: hir::Term::Const(anon_const) } =
429+
if let hir::AssocItemConstraintKind::Equality { term: hir::Term::Const(const_arg) } =
430430
constraint.kind
431431
{
432-
let ty = alias_ty.map_bound(|ty| tcx.type_of(ty.def_id).instantiate(tcx, ty.args));
433-
let ty = check_assoc_const_binding_type(tcx, assoc_ident, ty, constraint.hir_id);
434-
tcx.feed_anon_const_type(anon_const.def_id, ty::EarlyBinder::bind(ty));
432+
if let hir::ConstArgKind::Anon(anon_const) = const_arg.kind {
433+
let ty =
434+
alias_ty.map_bound(|ty| tcx.type_of(ty.def_id).instantiate(tcx, ty.args));
435+
let ty =
436+
check_assoc_const_binding_type(tcx, assoc_ident, ty, constraint.hir_id);
437+
tcx.feed_anon_const_type(anon_const.def_id, ty::EarlyBinder::bind(ty));
438+
}
435439
}
436440

437441
alias_ty
@@ -448,7 +452,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
448452
hir::AssocItemConstraintKind::Equality { term } => {
449453
let term = match term {
450454
hir::Term::Ty(ty) => self.lower_ty(ty).into(),
451-
hir::Term::Const(ct) => ty::Const::from_anon_const(tcx, ct.def_id).into(),
455+
hir::Term::Const(ct) => {
456+
ty::Const::from_const_arg(tcx, ct, assoc_item.def_id).into()
457+
}
452458
};
453459

454460
// Find any late-bound regions declared in `ty` that are not

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
340340
{
341341
let span = match term {
342342
hir::Term::Ty(ty) => ty.span,
343-
hir::Term::Const(ct) => tcx.def_span(ct.def_id),
343+
hir::Term::Const(ct) => ct.span(),
344344
};
345345
(span, Some(ident.span), assoc_item.kind, assoc_kind)
346346
} else {

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -476,18 +476,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
476476
(&GenericParamDefKind::Type { has_default, .. }, GenericArg::Infer(inf)) => {
477477
handle_ty_args(has_default, &inf.to_ty())
478478
}
479-
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => match ct.kind {
480-
hir::ConstArgKind::Path(qpath) => {
481-
// FIXME(min_generic_const_exprs): for now only params are lowered to ConstArgKind::Path
482-
ty::Const::from_param(tcx, qpath, ct.hir_id).into()
483-
}
484-
hir::ConstArgKind::Anon(anon) => {
485-
let did = anon.def_id;
486-
tcx.feed_anon_const_type(did, tcx.type_of(param.def_id));
487-
ty::Const::from_anon_const(tcx, did).into()
488-
}
489-
},
490-
(&GenericParamDefKind::Const { .. }, hir::GenericArg::Infer(inf)) => {
479+
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => {
480+
ty::Const::from_const_arg(tcx, ct, param.def_id).into()
481+
}
482+
(&GenericParamDefKind::Const { .. }, GenericArg::Infer(inf)) => {
491483
self.lowerer.ct_infer(Some(param), inf.span).into()
492484
}
493485
(kind, arg) => span_bug!(
@@ -907,12 +899,14 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
907899
let mut where_bounds = vec![];
908900
for bound in [bound, bound2].into_iter().chain(matching_candidates) {
909901
let bound_id = bound.def_id();
910-
let bound_span = tcx
902+
let assoc_item = tcx
911903
.associated_items(bound_id)
912-
.find_by_name_and_kind(tcx, assoc_name, assoc_kind, bound_id)
913-
.and_then(|item| tcx.hir().span_if_local(item.def_id));
904+
.find_by_name_and_kind(tcx, assoc_name, assoc_kind, bound_id);
905+
let bound_span = assoc_item.and_then(|item| tcx.hir().span_if_local(item.def_id));
914906

915-
if let Some(bound_span) = bound_span {
907+
if let Some(assoc_item) = assoc_item
908+
&& let Some(bound_span) = bound_span
909+
{
916910
err.span_label(
917911
bound_span,
918912
format!("ambiguous `{assoc_name}` from `{}`", bound.print_trait_sugared(),),
@@ -923,7 +917,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
923917
let term: ty::Term<'_> = match term {
924918
hir::Term::Ty(ty) => self.lower_ty(ty).into(),
925919
hir::Term::Const(ct) => {
926-
ty::Const::from_anon_const(tcx, ct.def_id).into()
920+
ty::Const::from_const_arg(tcx, ct, assoc_item.def_id).into()
927921
}
928922
};
929923
// FIXME(#97583): This isn't syntactically well-formed!

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1721,7 +1721,7 @@ impl<'a> State<'a> {
17211721
self.word_space("=");
17221722
match term {
17231723
Term::Ty(ty) => self.print_type(ty),
1724-
Term::Const(ref c) => self.print_anon_const(c),
1724+
Term::Const(ref c) => self.print_const_arg(c),
17251725
}
17261726
}
17271727
hir::AssocItemConstraintKind::Bound { bounds } => {

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_errors::{Applicability, Diag, ErrorGuaranteed, MultiSpan, StashKey};
88
use rustc_hir::def::{CtorOf, DefKind, Res};
99
use rustc_hir::def_id::DefId;
1010
use rustc_hir::lang_items::LangItem;
11-
use rustc_hir::{self as hir, ConstArg, ConstArgKind};
11+
use rustc_hir::{self as hir};
1212
use rustc_hir::{ExprKind, GenericArg, HirId, Node, QPath};
1313
use rustc_hir_analysis::hir_ty_lowering::errors::GenericsArgsErrExtend;
1414
use rustc_hir_analysis::hir_ty_lowering::generics::{
@@ -468,20 +468,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
468468

469469
pub fn lower_const_arg(
470470
&self,
471-
const_arg: &ConstArg<'tcx>,
471+
const_arg: &'tcx hir::ConstArg<'tcx>,
472472
param_def_id: DefId,
473473
) -> ty::Const<'tcx> {
474-
let ct = match const_arg.kind {
475-
ConstArgKind::Path(qpath) => {
476-
// FIXME(min_generic_const_exprs): for now only params are lowered to ConstArgKind::Path
477-
ty::Const::from_param(self.tcx, qpath, const_arg.hir_id)
478-
}
479-
ConstArgKind::Anon(anon) => {
480-
let did = anon.def_id;
481-
self.tcx.feed_anon_const_type(did, self.tcx.type_of(param_def_id));
482-
ty::Const::from_anon_const(self.tcx, did)
483-
}
484-
};
474+
let ct = ty::Const::from_const_arg(self.tcx, const_arg, param_def_id);
485475
self.register_wf_obligation(
486476
ct.into(),
487477
self.tcx.hir().span(const_arg.hir_id),

compiler/rustc_middle/src/ty/consts.rs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::ty::{self, GenericArgs, ParamEnv, ParamEnvAnd, Ty, TyCtxt, TypeVisita
44
use rustc_data_structures::intern::Interned;
55
use rustc_error_messages::MultiSpan;
66
use rustc_hir::def::{DefKind, Res};
7-
use rustc_hir::def_id::LocalDefId;
7+
use rustc_hir::def_id::{DefId, LocalDefId};
88
use rustc_hir::{self as hir, HirId};
99
use rustc_macros::HashStable;
1010
use rustc_type_ir::{self as ir, TypeFlags, WithCachedTypeInfo};
@@ -179,6 +179,39 @@ impl<'tcx> rustc_type_ir::inherent::Const<TyCtxt<'tcx>> for Const<'tcx> {
179179
}
180180

181181
impl<'tcx> Const<'tcx> {
182+
/// Convert a [`hir::ConstArg`] to a [`ty::Const`](Self).
183+
///
184+
/// `param_def_id` is the [`DefId`] of the declared param that this [`ConstArg`] is being
185+
/// supplied to. We need this in case this `ConstArg` is a [`ConstArgKind::Anon`]
186+
/// so we can tell the [`AnonConst`] what type it should be.
187+
pub fn from_const_arg(
188+
tcx: TyCtxt<'tcx>,
189+
const_arg: &'tcx hir::ConstArg<'tcx>,
190+
param_def_id: DefId,
191+
) -> Self {
192+
if let hir::ConstArgKind::Anon(anon) = &const_arg.kind {
193+
tcx.feed_anon_const_type(anon.def_id, tcx.type_of(param_def_id));
194+
}
195+
Self::from_const_arg_without_feeding(tcx, const_arg)
196+
}
197+
198+
/// Convert a [`hir::ConstArg`] to a [`ty::Const`](Self), without feeding it its expected type.
199+
///
200+
/// This distinction is only relevant for [`hir::ConstArgKind::Anon`];
201+
/// see [`Self::from_const_arg_without_feeding`].
202+
pub fn from_const_arg_without_feeding(
203+
tcx: TyCtxt<'tcx>,
204+
const_arg: &'tcx hir::ConstArg<'tcx>,
205+
) -> Self {
206+
match const_arg.kind {
207+
hir::ConstArgKind::Path(qpath) => {
208+
// FIXME(min_generic_const_exprs): for now only params are lowered to ConstArgKind::Path
209+
Self::from_param(tcx, qpath, const_arg.hir_id)
210+
}
211+
hir::ConstArgKind::Anon(anon) => Self::from_anon_const(tcx, anon.def_id),
212+
}
213+
}
214+
182215
/// Literals and const generic parameters are eagerly converted to a constant, everything else
183216
/// becomes `Unevaluated`.
184217
#[instrument(skip(tcx), level = "debug")]
@@ -212,7 +245,7 @@ impl<'tcx> Const<'tcx> {
212245
/// Lower a const param to a [`Const`].
213246
///
214247
/// IMPORTANT: `qpath` must be a const param, otherwise this will panic
215-
pub fn from_param(tcx: TyCtxt<'tcx>, qpath: hir::QPath<'tcx>, hir_id: HirId) -> Self {
248+
fn from_param(tcx: TyCtxt<'tcx>, qpath: hir::QPath<'tcx>, hir_id: HirId) -> Self {
216249
// FIXME(const_generics): We currently have to special case parameters because `min_const_generics`
217250
// does not provide the parents generics to anonymous constants. We still allow generic const
218251
// parameters by themselves however, e.g. `N`. These constants would cause an ICE if we were to
@@ -274,6 +307,21 @@ impl<'tcx> Const<'tcx> {
274307
}
275308
}
276309
}
310+
311+
// FIXME: this shouldn't panic, it's just temporary code
312+
match expr.kind {
313+
hir::ExprKind::Path(hir::QPath::Resolved(
314+
_,
315+
&hir::Path { res: Res::Def(DefKind::ConstParam, _), .. },
316+
)) => {
317+
span_bug!(
318+
expr.span,
319+
"try_from_lit: received const param which shouldn't be possible"
320+
)
321+
}
322+
_ => {}
323+
}
324+
277325
None
278326
}
279327

src/librustdoc/clean/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ fn clean_hir_term<'tcx>(term: &hir::Term<'tcx>, cx: &mut DocContext<'tcx>) -> Te
433433
match term {
434434
hir::Term::Ty(ty) => Term::Type(clean_ty(ty, cx)),
435435
hir::Term::Const(c) => Term::Constant(clean_middle_const(
436-
ty::Binder::dummy(ty::Const::from_anon_const(cx.tcx, c.def_id)),
436+
ty::Binder::dummy(ty::Const::from_const_arg_without_feeding(cx.tcx, c)),
437437
cx,
438438
)),
439439
}

0 commit comments

Comments
 (0)