Skip to content

Commit d0247c8

Browse files
committed
Remove FeedConstTy
1 parent cfb12cc commit d0247c8

File tree

8 files changed

+45
-102
lines changed

8 files changed

+45
-102
lines changed

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ use rustc_trait_selection::traits::{
4747
use tracing::{debug, instrument};
4848

4949
use crate::errors;
50-
use crate::hir_ty_lowering::{
51-
FeedConstTy, HirTyLowerer, InherentAssocCandidate, RegionInferReason,
52-
};
50+
use crate::hir_ty_lowering::{HirTyLowerer, InherentAssocCandidate, RegionInferReason};
5351

5452
pub(crate) mod dump;
5553
mod generics_of;
@@ -1499,7 +1497,7 @@ fn const_param_default<'tcx>(
14991497

15001498
let ct = icx
15011499
.lowerer()
1502-
.lower_const_arg(default_ct, FeedConstTy::with_type_of(tcx, def_id, identity_args));
1500+
.lower_const_arg(default_ct, tcx.type_of(def_id).instantiate(tcx, identity_args));
15031501
ty::EarlyBinder::bind(ct)
15041502
}
15051503

@@ -1557,7 +1555,7 @@ fn const_of_item<'tcx>(
15571555
let identity_args = ty::GenericArgs::identity_for_item(tcx, def_id);
15581556
let ct = icx
15591557
.lowerer()
1560-
.lower_const_arg(ct_arg, FeedConstTy::with_type_of(tcx, def_id.to_def_id(), identity_args));
1558+
.lower_const_arg(ct_arg, tcx.type_of(def_id.to_def_id()).instantiate(tcx, identity_args));
15611559
if let Err(e) = icx.check_tainted_by_errors()
15621560
&& !ct.references_error()
15631561
{

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use tracing::{debug, instrument};
2020

2121
use crate::errors;
2222
use crate::hir_ty_lowering::{
23-
AssocItemQSelf, FeedConstTy, GenericsArgsErrExtend, HirTyLowerer, ImpliedBoundsContext,
23+
AssocItemQSelf, GenericsArgsErrExtend, HirTyLowerer, ImpliedBoundsContext,
2424
OverlappingAsssocItemConstraints, PredicateFilter, RegionInferReason,
2525
};
2626

@@ -543,7 +543,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
543543
let term = match term {
544544
hir::Term::Ty(ty) => self.lower_ty(ty).into(),
545545
hir::Term::Const(ct) => {
546-
// Provide the resolved type of the associated constant
547546
let ty = projection_term.map_bound(|alias| {
548547
tcx.type_of(alias.def_id).instantiate(tcx, alias.args)
549548
});
@@ -554,7 +553,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
554553
constraint.hir_id,
555554
);
556555

557-
self.lower_const_arg(ct, FeedConstTy::WithTy(ty)).into()
556+
self.lower_const_arg(ct, ty).into()
558557
}
559558
};
560559

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 18 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -253,35 +253,6 @@ impl AssocItemQSelf {
253253
}
254254
}
255255

256-
/// In some cases, [`hir::ConstArg`]s that are being used in the type system
257-
/// through const generics need to have their type "fed" to them
258-
/// using the query system.
259-
///
260-
/// Use this enum with `<dyn HirTyLowerer>::lower_const_arg` to instruct it with the
261-
/// desired behavior.
262-
#[derive(Debug, Clone, Copy)]
263-
pub enum FeedConstTy<'tcx> {
264-
/// Feed the type to the (anno) const arg.
265-
WithTy(Ty<'tcx>),
266-
/// Don't feed the type.
267-
No,
268-
}
269-
270-
impl<'tcx> FeedConstTy<'tcx> {
271-
/// The `DefId` belongs to the const param that we are supplying
272-
/// this (anon) const arg to.
273-
///
274-
/// The list of generic args is used to instantiate the parameters
275-
/// used by the type of the const param specified by `DefId`.
276-
pub fn with_type_of(
277-
tcx: TyCtxt<'tcx>,
278-
def_id: DefId,
279-
generic_args: &[ty::GenericArg<'tcx>],
280-
) -> Self {
281-
Self::WithTy(tcx.type_of(def_id).instantiate(tcx, generic_args))
282-
}
283-
}
284-
285256
#[derive(Debug, Clone, Copy)]
286257
enum LowerTypeRelativePathMode {
287258
Type(PermitVariants),
@@ -733,7 +704,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
733704
// Ambig portions of `ConstArg` are handled in the match arm below
734705
.lower_const_arg(
735706
ct.as_unambig_ct(),
736-
FeedConstTy::with_type_of(tcx, param.def_id, preceding_args),
707+
tcx.type_of(param.def_id).instantiate(tcx, preceding_args),
737708
)
738709
.into(),
739710
(&GenericParamDefKind::Const { .. }, GenericArg::Infer(inf)) => {
@@ -1322,7 +1293,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
13221293
constraint.hir_id,
13231294
);
13241295

1325-
self.lower_const_arg(ct, FeedConstTy::WithTy(ty)).into()
1296+
self.lower_const_arg(ct, ty).into()
13261297
}
13271298
};
13281299
if term.references_error() {
@@ -2347,16 +2318,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23472318

23482319
/// Lower a [`hir::ConstArg`] to a (type-level) [`ty::Const`](Const).
23492320
#[instrument(skip(self), level = "debug")]
2350-
pub fn lower_const_arg(
2351-
&self,
2352-
const_arg: &hir::ConstArg<'tcx>,
2353-
feed: FeedConstTy<'tcx>,
2354-
) -> Const<'tcx> {
2321+
pub fn lower_const_arg(&self, const_arg: &hir::ConstArg<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> {
23552322
let tcx = self.tcx();
23562323

2357-
if let FeedConstTy::WithTy(anon_const_type) = feed
2358-
&& let hir::ConstArgKind::Anon(anon) = &const_arg.kind
2359-
{
2324+
if let hir::ConstArgKind::Anon(anon) = &const_arg.kind {
23602325
// FIXME(generic_const_parameter_types): Ideally we remove these errors below when
23612326
// we have the ability to intermix typeck of anon const const args with the parent
23622327
// bodies typeck.
@@ -2366,7 +2331,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23662331
// hir typeck was using equality but mir borrowck wound up using subtyping as that could
23672332
// result in a non-infer in hir typeck but a region variable in borrowck.
23682333
if tcx.features().generic_const_parameter_types()
2369-
&& (anon_const_type.has_free_regions() || anon_const_type.has_erased_regions())
2334+
&& (ty.has_free_regions() || ty.has_erased_regions())
23702335
{
23712336
let e = self.dcx().span_err(
23722337
const_arg.span,
@@ -2378,7 +2343,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23782343
// We must error if the instantiated type has any inference variables as we will
23792344
// use this type to feed the `type_of` and query results must not contain inference
23802345
// variables otherwise we will ICE.
2381-
if anon_const_type.has_non_region_infer() {
2346+
if ty.has_non_region_infer() {
23822347
let e = self.dcx().span_err(
23832348
const_arg.span,
23842349
"anonymous constants with inferred types are not yet supported",
@@ -2388,7 +2353,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23882353
}
23892354
// We error when the type contains unsubstituted generics since we do not currently
23902355
// give the anon const any of the generics from the parent.
2391-
if anon_const_type.has_non_region_param() {
2356+
if ty.has_non_region_param() {
23922357
let e = self.dcx().span_err(
23932358
const_arg.span,
23942359
"anonymous constants referencing generics are not yet supported",
@@ -2397,12 +2362,12 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23972362
return ty::Const::new_error(tcx, e);
23982363
}
23992364

2400-
tcx.feed_anon_const_type(anon.def_id, ty::EarlyBinder::bind(anon_const_type));
2365+
tcx.feed_anon_const_type(anon.def_id, ty::EarlyBinder::bind(ty));
24012366
}
24022367

24032368
let hir_id = const_arg.hir_id;
24042369
match const_arg.kind {
2405-
hir::ConstArgKind::Tup(exprs) => self.lower_const_arg_tup(exprs, feed, const_arg.span),
2370+
hir::ConstArgKind::Tup(exprs) => self.lower_const_arg_tup(exprs, ty, const_arg.span),
24062371
hir::ConstArgKind::Path(hir::QPath::Resolved(maybe_qself, path)) => {
24072372
debug!(?maybe_qself, ?path);
24082373
let opt_self_ty = maybe_qself.as_ref().map(|qself| self.lower_ty(qself));
@@ -2429,12 +2394,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
24292394
hir::ConstArgKind::Anon(anon) => self.lower_const_arg_anon(anon),
24302395
hir::ConstArgKind::Infer(()) => self.ct_infer(None, const_arg.span),
24312396
hir::ConstArgKind::Error(e) => ty::Const::new_error(tcx, e),
2432-
hir::ConstArgKind::Literal(kind) if let FeedConstTy::WithTy(anon_const_type) = feed => {
2433-
self.lower_const_arg_literal(&kind, anon_const_type, const_arg.span)
2434-
}
2435-
hir::ConstArgKind::Literal(..) => {
2436-
let e = self.dcx().span_err(const_arg.span, "literal of unknown type");
2437-
ty::Const::new_error(tcx, e)
2397+
hir::ConstArgKind::Literal(kind) => {
2398+
self.lower_const_arg_literal(&kind, ty, const_arg.span)
24382399
}
24392400
}
24402401
}
@@ -2514,7 +2475,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
25142475
.iter()
25152476
.zip(args)
25162477
.map(|(field_def, arg)| {
2517-
self.lower_const_arg(arg, FeedConstTy::with_type_of(tcx, field_def.did, adt_args))
2478+
self.lower_const_arg(arg, tcx.type_of(field_def.did).instantiate(tcx, adt_args))
25182479
})
25192480
.collect::<Vec<_>>();
25202481

@@ -2533,23 +2494,19 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
25332494
fn lower_const_arg_tup(
25342495
&self,
25352496
exprs: &'tcx [&'tcx hir::ConstArg<'tcx>],
2536-
feed: FeedConstTy<'tcx>,
2497+
ty: Ty<'tcx>,
25372498
span: Span,
25382499
) -> Const<'tcx> {
25392500
let tcx = self.tcx();
25402501

2541-
let FeedConstTy::WithTy(ty) = feed else {
2542-
return Const::new_error_with_message(tcx, span, "unsupported const tuple");
2543-
};
2544-
25452502
let ty::Tuple(tys) = ty.kind() else {
25462503
return Const::new_error_with_message(tcx, span, "const tuple must have a tuple type");
25472504
};
25482505

25492506
let exprs = exprs
25502507
.iter()
25512508
.zip(tys.iter())
2552-
.map(|(expr, ty)| self.lower_const_arg(expr, FeedConstTy::WithTy(ty)))
2509+
.map(|(expr, ty)| self.lower_const_arg(expr, ty))
25532510
.collect::<Vec<_>>();
25542511

25552512
let valtree = ty::ValTree::from_branches(tcx, exprs);
@@ -2636,7 +2593,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
26362593

26372594
self.lower_const_arg(
26382595
expr.expr,
2639-
FeedConstTy::with_type_of(tcx, field_def.did, adt_args),
2596+
tcx.type_of(field_def.did).instantiate(tcx, adt_args),
26402597
)
26412598
}
26422599
None => {
@@ -2998,7 +2955,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
29982955
.unwrap_or_else(|guar| Ty::new_error(tcx, guar))
29992956
}
30002957
hir::TyKind::Array(ty, length) => {
3001-
let length = self.lower_const_arg(length, FeedConstTy::WithTy(tcx.types.usize));
2958+
let length = self.lower_const_arg(length, tcx.types.usize);
30022959
Ty::new_array_with_const_len(tcx, self.lower_ty(ty), length)
30032960
}
30042961
hir::TyKind::Infer(()) => {
@@ -3038,8 +2995,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
30382995
// Keep this list of types in sync with the list of types that
30392996
// the `RangePattern` trait is implemented for.
30402997
ty::Int(_) | ty::Uint(_) | ty::Char => {
3041-
let start = self.lower_const_arg(start, FeedConstTy::WithTy(ty));
3042-
let end = self.lower_const_arg(end, FeedConstTy::WithTy(ty));
2998+
let start = self.lower_const_arg(start, ty);
2999+
let end = self.lower_const_arg(end, ty);
30433000
Ok(ty::PatternKind::Range { start, end })
30443001
}
30453002
_ => Err(self

compiler/rustc_hir_analysis/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ use rustc_span::{ErrorGuaranteed, Span};
101101
use rustc_trait_selection::traits;
102102

103103
pub use crate::collect::suggest_impl_trait;
104-
use crate::hir_ty_lowering::{FeedConstTy, HirTyLowerer};
104+
use crate::hir_ty_lowering::HirTyLowerer;
105105

106106
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
107107

@@ -301,8 +301,8 @@ pub fn lower_ty<'tcx>(tcx: TyCtxt<'tcx>, hir_ty: &hir::Ty<'tcx>) -> Ty<'tcx> {
301301
pub fn lower_const_arg_for_rustdoc<'tcx>(
302302
tcx: TyCtxt<'tcx>,
303303
hir_ct: &hir::ConstArg<'tcx>,
304-
feed: FeedConstTy<'tcx>,
304+
ty: Ty<'tcx>,
305305
) -> Const<'tcx> {
306306
let env_def_id = tcx.hir_get_parent_item(hir_ct.hir_id);
307-
collect::ItemCtxt::new(tcx, env_def_id.def_id).lowerer().lower_const_arg(hir_ct, feed)
307+
collect::ItemCtxt::new(tcx, env_def_id.def_id).lowerer().lower_const_arg(hir_ct, ty)
308308
}

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_hir::def_id::DefId;
2121
use rustc_hir::lang_items::LangItem;
2222
use rustc_hir::{ExprKind, HirId, QPath, find_attr, is_range_literal};
2323
use rustc_hir_analysis::NoVariantNamed;
24-
use rustc_hir_analysis::hir_ty_lowering::{FeedConstTy, HirTyLowerer as _};
24+
use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer as _;
2525
use rustc_infer::infer::{self, DefineOpaqueTypes, InferOk, RegionVariableOrigin};
2626
use rustc_infer::traits::query::NoSolution;
2727
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AllowTwoPhase};
@@ -1749,10 +1749,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17491749
let count_span = count.span;
17501750
let count = self.try_structurally_resolve_const(
17511751
count_span,
1752-
self.normalize(
1753-
count_span,
1754-
self.lower_const_arg(count, FeedConstTy::WithTy(tcx.types.usize)),
1755-
),
1752+
self.normalize(count_span, self.lower_const_arg(count, tcx.types.usize)),
17561753
);
17571754

17581755
if let Some(count) = count.try_to_target_usize(tcx) {

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use rustc_hir_analysis::hir_ty_lowering::generics::{
1414
check_generic_arg_count_for_call, lower_generic_args,
1515
};
1616
use rustc_hir_analysis::hir_ty_lowering::{
17-
ExplicitLateBound, FeedConstTy, GenericArgCountMismatch, GenericArgCountResult,
18-
GenericArgsLowerer, GenericPathSegment, HirTyLowerer, IsMethodCall, RegionInferReason,
17+
ExplicitLateBound, GenericArgCountMismatch, GenericArgCountResult, GenericArgsLowerer,
18+
GenericPathSegment, HirTyLowerer, IsMethodCall, RegionInferReason,
1919
};
2020
use rustc_infer::infer::canonical::{Canonical, OriginalQueryValues, QueryResponse};
2121
use rustc_infer::infer::{DefineOpaqueTypes, InferResult};
@@ -525,9 +525,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
525525
pub(crate) fn lower_const_arg(
526526
&self,
527527
const_arg: &'tcx hir::ConstArg<'tcx>,
528-
feed: FeedConstTy<'tcx>,
528+
ty: Ty<'tcx>,
529529
) -> ty::Const<'tcx> {
530-
let ct = self.lowerer().lower_const_arg(const_arg, feed);
530+
let ct = self.lowerer().lower_const_arg(const_arg, ty);
531531
self.register_wf_obligation(
532532
ct.into(),
533533
self.tcx.hir_span(const_arg.hir_id),
@@ -1228,7 +1228,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12281228
// Ambiguous parts of `ConstArg` are handled in the match arms below
12291229
.lower_const_arg(
12301230
ct.as_unambig_ct(),
1231-
FeedConstTy::with_type_of(self.fcx.tcx, param.def_id, preceding_args),
1231+
self.fcx
1232+
.tcx
1233+
.type_of(param.def_id)
1234+
.instantiate(self.fcx.tcx, preceding_args),
12321235
)
12331236
.into(),
12341237
(&GenericParamDefKind::Const { .. }, GenericArg::Infer(inf)) => {

compiler/rustc_hir_typeck/src/method/confirm.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_hir_analysis::hir_ty_lowering::generics::{
77
check_generic_arg_count_for_call, lower_generic_args,
88
};
99
use rustc_hir_analysis::hir_ty_lowering::{
10-
FeedConstTy, GenericArgsLowerer, HirTyLowerer, IsMethodCall, RegionInferReason,
10+
GenericArgsLowerer, HirTyLowerer, IsMethodCall, RegionInferReason,
1111
};
1212
use rustc_infer::infer::{
1313
BoundRegionConversionTime, DefineOpaqueTypes, InferOk, RegionVariableOrigin,
@@ -447,7 +447,10 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
447447
// We handle the ambig portions of `ConstArg` in the match arms below
448448
.lower_const_arg(
449449
ct.as_unambig_ct(),
450-
FeedConstTy::with_type_of(self.cfcx.tcx, param.def_id, preceding_args),
450+
self.cfcx
451+
.tcx
452+
.type_of(param.def_id)
453+
.instantiate(self.cfcx.tcx, preceding_args),
451454
)
452455
.into(),
453456
(GenericParamDefKind::Const { .. }, GenericArg::Infer(inf)) => {

0 commit comments

Comments
 (0)