Skip to content

Commit cfb12cc

Browse files
committed
Remove all usage of FeedConstTy::No
1 parent ad04f76 commit cfb12cc

4 files changed

Lines changed: 124 additions & 36 deletions

File tree

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
510510
// Create the generic arguments for the associated type or constant by joining the
511511
// parent arguments (the arguments of the trait) and the own arguments (the ones of
512512
// the associated item itself) and construct an alias type using them.
513-
let alias_term = candidate.map_bound(|trait_ref| {
513+
candidate.map_bound(|trait_ref| {
514514
let item_segment = hir::PathSegment {
515515
ident: constraint.ident,
516516
hir_id: constraint.hir_id,
@@ -528,20 +528,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
528528
debug!(?alias_args);
529529

530530
ty::AliasTerm::new_from_args(tcx, assoc_item.def_id, alias_args)
531-
});
532-
533-
// Provide the resolved type of the associated constant to `type_of(AnonConst)`.
534-
if let Some(const_arg) = constraint.ct()
535-
&& let hir::ConstArgKind::Anon(anon_const) = const_arg.kind
536-
{
537-
let ty = alias_term
538-
.map_bound(|alias| tcx.type_of(alias.def_id).instantiate(tcx, alias.args));
539-
let ty =
540-
check_assoc_const_binding_type(self, constraint.ident, ty, constraint.hir_id);
541-
tcx.feed_anon_const_type(anon_const.def_id, ty::EarlyBinder::bind(ty));
542-
}
543-
544-
alias_term
531+
})
545532
};
546533

547534
match constraint.kind {
@@ -555,7 +542,20 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
555542
hir::AssocItemConstraintKind::Equality { term } => {
556543
let term = match term {
557544
hir::Term::Ty(ty) => self.lower_ty(ty).into(),
558-
hir::Term::Const(ct) => self.lower_const_arg(ct, FeedConstTy::No).into(),
545+
hir::Term::Const(ct) => {
546+
// Provide the resolved type of the associated constant
547+
let ty = projection_term.map_bound(|alias| {
548+
tcx.type_of(alias.def_id).instantiate(tcx, alias.args)
549+
});
550+
let ty = check_assoc_const_binding_type(
551+
self,
552+
constraint.ident,
553+
ty,
554+
constraint.hir_id,
555+
);
556+
557+
self.lower_const_arg(ct, FeedConstTy::WithTy(ty)).into()
558+
}
559559
};
560560

561561
// Find any late-bound regions declared in `ty` that are not
@@ -872,7 +872,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
872872
/// probably gate this behind another feature flag.
873873
///
874874
/// [^1]: <https://github.com/rust-lang/project-const-generics/issues/28>.
875-
fn check_assoc_const_binding_type<'tcx>(
875+
pub(crate) fn check_assoc_const_binding_type<'tcx>(
876876
cx: &dyn HirTyLowerer<'tcx>,
877877
assoc_const: Ident,
878878
ty: ty::Binder<'tcx, Ty<'tcx>>,

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,10 +1269,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
12691269
let mut where_bounds = vec![];
12701270
for bound in [bound, bound2].into_iter().chain(matching_candidates) {
12711271
let bound_id = bound.def_id();
1272-
let bound_span = tcx
1273-
.associated_items(bound_id)
1274-
.find_by_ident_and_kind(tcx, assoc_ident, assoc_tag, bound_id)
1275-
.and_then(|item| tcx.hir_span_if_local(item.def_id));
1272+
let assoc_item = tcx.associated_items(bound_id).find_by_ident_and_kind(
1273+
tcx,
1274+
assoc_ident,
1275+
assoc_tag,
1276+
bound_id,
1277+
);
1278+
let bound_span = assoc_item.and_then(|item| tcx.hir_span_if_local(item.def_id));
12761279

12771280
if let Some(bound_span) = bound_span {
12781281
err.span_label(
@@ -1285,7 +1288,41 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
12851288
let term: ty::Term<'_> = match term {
12861289
hir::Term::Ty(ty) => self.lower_ty(ty).into(),
12871290
hir::Term::Const(ct) => {
1288-
self.lower_const_arg(ct, FeedConstTy::No).into()
1291+
let assoc_item =
1292+
assoc_item.expect("assoc_item should be present");
1293+
let projection_term = bound.map_bound(|trait_ref| {
1294+
let item_segment = hir::PathSegment {
1295+
ident: constraint.ident,
1296+
hir_id: constraint.hir_id,
1297+
res: Res::Err,
1298+
args: Some(constraint.gen_args),
1299+
infer_args: false,
1300+
};
1301+
1302+
let alias_args = self.lower_generic_args_of_assoc_item(
1303+
constraint.ident.span,
1304+
assoc_item.def_id,
1305+
&item_segment,
1306+
trait_ref.args,
1307+
);
1308+
ty::AliasTerm::new_from_args(
1309+
tcx,
1310+
assoc_item.def_id,
1311+
alias_args,
1312+
)
1313+
});
1314+
1315+
let ty = projection_term.map_bound(|alias| {
1316+
tcx.type_of(alias.def_id).instantiate(tcx, alias.args)
1317+
});
1318+
let ty = bounds::check_assoc_const_binding_type(
1319+
self,
1320+
constraint.ident,
1321+
ty,
1322+
constraint.hir_id,
1323+
);
1324+
1325+
self.lower_const_arg(ct, FeedConstTy::WithTy(ty)).into()
12891326
}
12901327
};
12911328
if term.references_error() {
@@ -2961,7 +2998,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
29612998
.unwrap_or_else(|guar| Ty::new_error(tcx, guar))
29622999
}
29633000
hir::TyKind::Array(ty, length) => {
2964-
let length = self.lower_const_arg(length, FeedConstTy::No);
3001+
let length = self.lower_const_arg(length, FeedConstTy::WithTy(tcx.types.usize));
29653002
Ty::new_array_with_const_len(tcx, self.lower_ty(ty), length)
29663003
}
29673004
hir::TyKind::Infer(()) => {
@@ -3001,8 +3038,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
30013038
// Keep this list of types in sync with the list of types that
30023039
// the `RangePattern` trait is implemented for.
30033040
ty::Int(_) | ty::Uint(_) | ty::Char => {
3004-
let start = self.lower_const_arg(start, FeedConstTy::No);
3005-
let end = self.lower_const_arg(end, FeedConstTy::No);
3041+
let start = self.lower_const_arg(start, FeedConstTy::WithTy(ty));
3042+
let end = self.lower_const_arg(end, FeedConstTy::WithTy(ty));
30063043
Ok(ty::PatternKind::Range { start, end })
30073044
}
30083045
_ => Err(self

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1749,7 +1749,10 @@ 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(count_span, self.lower_const_arg(count, FeedConstTy::No)),
1752+
self.normalize(
1753+
count_span,
1754+
self.lower_const_arg(count, FeedConstTy::WithTy(tcx.types.usize)),
1755+
),
17531756
);
17541757

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

src/librustdoc/clean/mod.rs

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,23 @@ fn clean_middle_term<'tcx>(
466466
}
467467
}
468468

469-
fn clean_hir_term<'tcx>(term: &hir::Term<'tcx>, cx: &mut DocContext<'tcx>) -> Term {
469+
fn clean_hir_term<'tcx>(
470+
assoc_item: Option<DefId>,
471+
term: &hir::Term<'tcx>,
472+
span: rustc_span::Span,
473+
cx: &mut DocContext<'tcx>,
474+
) -> Term {
470475
match term {
471476
hir::Term::Ty(ty) => Term::Type(clean_ty(ty, cx)),
472477
hir::Term::Const(c) => {
473-
let ct = lower_const_arg_for_rustdoc(cx.tcx, c, FeedConstTy::No);
478+
let ty = if let Some(assoc_item) = assoc_item {
479+
// FIXME(generic_const_items): this should instantiate with the alias item's args
480+
cx.tcx.type_of(assoc_item).instantiate_identity()
481+
} else {
482+
Ty::new_error_with_message(cx.tcx, span, "cannot find the associated constant")
483+
};
484+
485+
let ct = lower_const_arg_for_rustdoc(cx.tcx, c, FeedConstTy::WithTy(ty));
474486
Term::Constant(clean_middle_const(ty::Binder::dummy(ct), cx))
475487
}
476488
}
@@ -647,7 +659,14 @@ fn clean_generic_param<'tcx>(
647659
GenericParamDefKind::Const {
648660
ty: Box::new(clean_ty(ty, cx)),
649661
default: default.map(|ct| {
650-
Box::new(lower_const_arg_for_rustdoc(cx.tcx, ct, FeedConstTy::No).to_string())
662+
Box::new(
663+
lower_const_arg_for_rustdoc(
664+
cx.tcx,
665+
ct,
666+
FeedConstTy::WithTy(lower_ty(cx.tcx, ty)),
667+
)
668+
.to_string(),
669+
)
651670
}),
652671
},
653672
),
@@ -1528,7 +1547,7 @@ fn first_non_private_clean_path<'tcx>(
15281547
&& path_last.args.is_some()
15291548
{
15301549
assert!(new_path_last.args.is_empty());
1531-
new_path_last.args = clean_generic_args(path_last_args, cx);
1550+
new_path_last.args = clean_generic_args(None, path_last_args, cx);
15321551
}
15331552
new_clean_path
15341553
}
@@ -1809,7 +1828,11 @@ pub(crate) fn clean_ty<'tcx>(ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> T
18091828
let length = match const_arg.kind {
18101829
hir::ConstArgKind::Infer(..) | hir::ConstArgKind::Error(..) => "_".to_string(),
18111830
hir::ConstArgKind::Anon(hir::AnonConst { def_id, .. }) => {
1812-
let ct = lower_const_arg_for_rustdoc(cx.tcx, const_arg, FeedConstTy::No);
1831+
let ct = lower_const_arg_for_rustdoc(
1832+
cx.tcx,
1833+
const_arg,
1834+
FeedConstTy::WithTy(cx.tcx.types.usize),
1835+
);
18131836
let typing_env = ty::TypingEnv::post_analysis(cx.tcx, *def_id);
18141837
let ct = cx.tcx.normalize_erasing_regions(typing_env, ct);
18151838
print_const(cx, ct)
@@ -1819,7 +1842,11 @@ pub(crate) fn clean_ty<'tcx>(ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> T
18191842
| hir::ConstArgKind::TupleCall(..)
18201843
| hir::ConstArgKind::Tup(..)
18211844
| hir::ConstArgKind::Literal(..) => {
1822-
let ct = lower_const_arg_for_rustdoc(cx.tcx, const_arg, FeedConstTy::No);
1845+
let ct = lower_const_arg_for_rustdoc(
1846+
cx.tcx,
1847+
const_arg,
1848+
FeedConstTy::WithTy(cx.tcx.types.usize),
1849+
);
18231850
print_const(cx, ct)
18241851
}
18251852
};
@@ -2512,6 +2539,7 @@ fn clean_path<'tcx>(path: &hir::Path<'tcx>, cx: &mut DocContext<'tcx>) -> Path {
25122539
}
25132540

25142541
fn clean_generic_args<'tcx>(
2542+
trait_did: Option<DefId>,
25152543
generic_args: &hir::GenericArgs<'tcx>,
25162544
cx: &mut DocContext<'tcx>,
25172545
) -> GenericArgs {
@@ -2535,7 +2563,13 @@ fn clean_generic_args<'tcx>(
25352563
let constraints = generic_args
25362564
.constraints
25372565
.iter()
2538-
.map(|c| clean_assoc_item_constraint(c, cx))
2566+
.map(|c| {
2567+
clean_assoc_item_constraint(
2568+
trait_did.expect("only trait ref has constraints"),
2569+
c,
2570+
cx,
2571+
)
2572+
})
25392573
.collect::<ThinVec<_>>();
25402574
GenericArgs::AngleBracketed { args, constraints }
25412575
}
@@ -2558,7 +2592,9 @@ fn clean_path_segment<'tcx>(
25582592
path: &hir::PathSegment<'tcx>,
25592593
cx: &mut DocContext<'tcx>,
25602594
) -> PathSegment {
2561-
PathSegment { name: path.ident.name, args: clean_generic_args(path.args(), cx) }
2595+
let trait_did =
2596+
if let hir::def::Res::Def(DefKind::Trait, did) = path.res { Some(did) } else { None };
2597+
PathSegment { name: path.ident.name, args: clean_generic_args(trait_did, path.args(), cx) }
25622598
}
25632599

25642600
fn clean_bare_fn_ty<'tcx>(
@@ -3122,17 +3158,29 @@ fn clean_maybe_renamed_foreign_item<'tcx>(
31223158
}
31233159

31243160
fn clean_assoc_item_constraint<'tcx>(
3161+
trait_did: DefId,
31253162
constraint: &hir::AssocItemConstraint<'tcx>,
31263163
cx: &mut DocContext<'tcx>,
31273164
) -> AssocItemConstraint {
31283165
AssocItemConstraint {
31293166
assoc: PathSegment {
31303167
name: constraint.ident.name,
3131-
args: clean_generic_args(constraint.gen_args, cx),
3168+
args: clean_generic_args(None, constraint.gen_args, cx),
31323169
},
31333170
kind: match constraint.kind {
31343171
hir::AssocItemConstraintKind::Equality { ref term } => {
3135-
AssocItemConstraintKind::Equality { term: clean_hir_term(term, cx) }
3172+
let assoc_tag = match term {
3173+
hir::Term::Ty(_) => ty::AssocTag::Type,
3174+
hir::Term::Const(_) => ty::AssocTag::Const,
3175+
};
3176+
let assoc_item = cx
3177+
.tcx
3178+
.associated_items(trait_did)
3179+
.find_by_ident_and_kind(cx.tcx, constraint.ident, assoc_tag, trait_did)
3180+
.map(|item| item.def_id);
3181+
AssocItemConstraintKind::Equality {
3182+
term: clean_hir_term(assoc_item, term, constraint.span, cx),
3183+
}
31363184
}
31373185
hir::AssocItemConstraintKind::Bound { bounds } => AssocItemConstraintKind::Bound {
31383186
bounds: bounds.iter().filter_map(|b| clean_generic_bound(b, cx)).collect(),

0 commit comments

Comments
 (0)