Skip to content

Commit 6054bd5

Browse files
committed
Add optional ConstArg field to assoc consts in HIR
1 parent acd3ad9 commit 6054bd5

File tree

17 files changed

+57
-49
lines changed

17 files changed

+57
-49
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -814,8 +814,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
814814
let ty = this
815815
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
816816
let body = expr.as_ref().map(|x| this.lower_const_body(i.span, Some(x)));
817-
818-
hir::TraitItemKind::Const(ty, body)
817+
// TODO: make const arg instead of always using None
818+
hir::TraitItemKind::Const(ty, body, None)
819819
},
820820
);
821821

@@ -1008,7 +1008,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
10081008
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
10091009
let body = this.lower_const_body(i.span, expr.as_deref());
10101010
this.lower_define_opaque(hir_id, &define_opaque);
1011-
hir::ImplItemKind::Const(ty, body)
1011+
// TODO: make const arg instead of always using None
1012+
hir::ImplItemKind::Const(ty, body, None)
10121013
},
10131014
),
10141015
),

compiler/rustc_hir/src/hir.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2973,8 +2973,8 @@ impl<'hir> TraitItem<'hir> {
29732973
}
29742974

29752975
expect_methods_self_kind! {
2976-
expect_const, (&'hir Ty<'hir>, Option<BodyId>),
2977-
TraitItemKind::Const(ty, body), (ty, *body);
2976+
expect_const, (&'hir Ty<'hir>, Option<BodyId>, Option<&'hir ConstArg<'hir>>),
2977+
TraitItemKind::Const(ty, body, ct), (ty, *body, *ct);
29782978

29792979
expect_fn, (&FnSig<'hir>, &TraitFn<'hir>),
29802980
TraitItemKind::Fn(ty, trfn), (ty, trfn);
@@ -2998,7 +2998,7 @@ pub enum TraitFn<'hir> {
29982998
#[derive(Debug, Clone, Copy, HashStable_Generic)]
29992999
pub enum TraitItemKind<'hir> {
30003000
/// An associated constant with an optional value (otherwise `impl`s must contain a value).
3001-
Const(&'hir Ty<'hir>, Option<BodyId>),
3001+
Const(&'hir Ty<'hir>, Option<BodyId>, Option<&'hir ConstArg<'hir>>),
30023002
/// An associated function with an optional body.
30033003
Fn(FnSig<'hir>, TraitFn<'hir>),
30043004
/// An associated type with (possibly empty) bounds and optional concrete
@@ -3048,7 +3048,7 @@ impl<'hir> ImplItem<'hir> {
30483048
}
30493049

30503050
expect_methods_self_kind! {
3051-
expect_const, (&'hir Ty<'hir>, BodyId), ImplItemKind::Const(ty, body), (ty, *body);
3051+
expect_const, (&'hir Ty<'hir>, BodyId, Option<&'hir ConstArg<'hir>>), ImplItemKind::Const(ty, body, ct), (ty, *body, *ct);
30523052
expect_fn, (&FnSig<'hir>, BodyId), ImplItemKind::Fn(ty, body), (ty, *body);
30533053
expect_type, &'hir Ty<'hir>, ImplItemKind::Type(ty), ty;
30543054
}
@@ -3059,7 +3059,7 @@ impl<'hir> ImplItem<'hir> {
30593059
pub enum ImplItemKind<'hir> {
30603060
/// An associated constant of the given type, set to the constant result
30613061
/// of the expression.
3062-
Const(&'hir Ty<'hir>, BodyId),
3062+
Const(&'hir Ty<'hir>, BodyId, Option<&'hir ConstArg<'hir>>),
30633063
/// An associated function implementation with the given signature and body.
30643064
Fn(FnSig<'hir>, BodyId),
30653065
/// An associated type.
@@ -4461,11 +4461,12 @@ impl<'hir> OwnerNode<'hir> {
44614461
})
44624462
| OwnerNode::TraitItem(TraitItem {
44634463
kind:
4464-
TraitItemKind::Fn(_, TraitFn::Provided(body)) | TraitItemKind::Const(_, Some(body)),
4464+
TraitItemKind::Fn(_, TraitFn::Provided(body))
4465+
| TraitItemKind::Const(_, Some(body), _),
44654466
..
44664467
})
44674468
| OwnerNode::ImplItem(ImplItem {
4468-
kind: ImplItemKind::Fn(_, body) | ImplItemKind::Const(_, body),
4469+
kind: ImplItemKind::Fn(_, body) | ImplItemKind::Const(_, body, _),
44694470
..
44704471
}) => Some(*body),
44714472
_ => None,
@@ -4686,12 +4687,12 @@ impl<'hir> Node<'hir> {
46864687
_ => None,
46874688
},
46884689
Node::TraitItem(it) => match it.kind {
4689-
TraitItemKind::Const(ty, _) => Some(ty),
4690+
TraitItemKind::Const(ty, _, _) => Some(ty),
46904691
TraitItemKind::Type(_, ty) => ty,
46914692
_ => None,
46924693
},
46934694
Node::ImplItem(it) => match it.kind {
4694-
ImplItemKind::Const(ty, _) => Some(ty),
4695+
ImplItemKind::Const(ty, _, _) => Some(ty),
46954696
ImplItemKind::Type(ty) => Some(ty),
46964697
_ => None,
46974698
},
@@ -4720,12 +4721,13 @@ impl<'hir> Node<'hir> {
47204721
| Node::TraitItem(TraitItem {
47214722
owner_id,
47224723
kind:
4723-
TraitItemKind::Const(_, Some(body)) | TraitItemKind::Fn(_, TraitFn::Provided(body)),
4724+
TraitItemKind::Const(_, Some(body), _)
4725+
| TraitItemKind::Fn(_, TraitFn::Provided(body)),
47244726
..
47254727
})
47264728
| Node::ImplItem(ImplItem {
47274729
owner_id,
4728-
kind: ImplItemKind::Const(_, body) | ImplItemKind::Fn(_, body),
4730+
kind: ImplItemKind::Const(_, body, _) | ImplItemKind::Fn(_, body),
47294731
..
47304732
}) => Some((owner_id.def_id, *body)),
47314733

compiler/rustc_hir/src/intravisit.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,9 +1166,10 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(
11661166
try_visit!(visitor.visit_defaultness(&defaultness));
11671167
try_visit!(visitor.visit_id(hir_id));
11681168
match *kind {
1169-
TraitItemKind::Const(ref ty, default) => {
1169+
TraitItemKind::Const(ref ty, default, ct_arg) => {
11701170
try_visit!(visitor.visit_ty_unambig(ty));
11711171
visit_opt!(visitor, visit_nested_body, default);
1172+
visit_opt!(visitor, visit_const_arg_unambig, ct_arg);
11721173
}
11731174
TraitItemKind::Fn(ref sig, TraitFn::Required(param_names)) => {
11741175
try_visit!(visitor.visit_fn_decl(sig.decl));
@@ -1224,9 +1225,11 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(
12241225
try_visit!(visitor.visit_defaultness(defaultness));
12251226
try_visit!(visitor.visit_id(impl_item.hir_id()));
12261227
match *kind {
1227-
ImplItemKind::Const(ref ty, body) => {
1228+
ImplItemKind::Const(ref ty, body, ct_arg) => {
12281229
try_visit!(visitor.visit_ty_unambig(ty));
1229-
visitor.visit_nested_body(body)
1230+
try_visit!(visitor.visit_nested_body(body));
1231+
visit_opt!(visitor, visit_const_arg_unambig, ct_arg);
1232+
V::Result::output()
12301233
}
12311234
ImplItemKind::Fn(ref sig, body_id) => visitor.visit_fn(
12321235
FnKind::Method(impl_item.ident, sig),

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,7 +1860,7 @@ fn compare_const_predicate_entailment<'tcx>(
18601860
debug!(?impl_ty, ?trait_ty);
18611861

18621862
// Locate the Span containing just the type of the offending impl
1863-
let (ty, _) = tcx.hir_expect_impl_item(impl_ct_def_id).expect_const();
1863+
let (ty, ..) = tcx.hir_expect_impl_item(impl_ct_def_id).expect_const();
18641864
cause.span = ty.span;
18651865

18661866
let mut diag = struct_span_code_err!(
@@ -1873,7 +1873,7 @@ fn compare_const_predicate_entailment<'tcx>(
18731873

18741874
let trait_c_span = trait_ct.def_id.as_local().map(|trait_ct_def_id| {
18751875
// Add a label to the Span containing just the type of the const
1876-
let (ty, _) = tcx.hir_expect_trait_item(trait_ct_def_id).expect_const();
1876+
let (ty, ..) = tcx.hir_expect_trait_item(trait_ct_def_id).expect_const();
18771877
ty.span
18781878
});
18791879

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ fn check_dyn_incompatible_self_trait_by_name(tcx: TyCtxt<'_>, item: &hir::TraitI
834834
};
835835
let mut trait_should_be_self = vec![];
836836
match &item.kind {
837-
hir::TraitItemKind::Const(ty, _) | hir::TraitItemKind::Type(_, Some(ty))
837+
hir::TraitItemKind::Const(ty, ..) | hir::TraitItemKind::Type(_, Some(ty))
838838
if could_be_self(trait_def_id.def_id, ty) =>
839839
{
840840
trait_should_be_self.push(ty.span)

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ fn lower_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
799799
tcx.ensure_ok().fn_sig(def_id);
800800
}
801801

802-
hir::TraitItemKind::Const(ty, body_id) => {
802+
hir::TraitItemKind::Const(ty, body_id, _ct) => {
803803
tcx.ensure_ok().type_of(def_id);
804804
if !tcx.dcx().has_stashed_diagnostic(ty.span, StashKey::ItemNoType)
805805
&& !(ty.is_suggestable_infer_ty() && body_id.is_some())
@@ -883,7 +883,7 @@ fn lower_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::ImplItemId) {
883883
"associated type",
884884
);
885885
}
886-
hir::ImplItemKind::Const(ty, _) => {
886+
hir::ImplItemKind::Const(ty, ..) => {
887887
// Account for `const T: _ = ..;`
888888
if !ty.is_suggestable_infer_ty() {
889889
let mut visitor = HirPlaceholderCollector::default();

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
628628
intravisit::walk_item(self, item);
629629
}
630630
hir::ItemKind::TyAlias(_, _, generics)
631-
| hir::ItemKind::Const(_, _, generics,_, _)
631+
| hir::ItemKind::Const(_, _, generics, _, _)
632632
| hir::ItemKind::Enum(_, _, generics)
633633
| hir::ItemKind::Struct(_, _, generics)
634634
| hir::ItemKind::Union(_, _, generics)
@@ -848,7 +848,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
848848
}
849849
})
850850
}
851-
Const(_, _) => self.visit_early(trait_item.hir_id(), trait_item.generics, |this| {
851+
Const(_, _, _) => self.visit_early(trait_item.hir_id(), trait_item.generics, |this| {
852852
intravisit::walk_trait_item(this, trait_item)
853853
}),
854854
}
@@ -865,7 +865,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
865865
this.visit_generics(impl_item.generics);
866866
this.visit_ty_unambig(ty);
867867
}),
868-
Const(_, _) => self.visit_early(impl_item.hir_id(), impl_item.generics, |this| {
868+
Const(_, _, _) => self.visit_early(impl_item.hir_id(), impl_item.generics, |this| {
869869
intravisit::walk_impl_item(this, impl_item)
870870
}),
871871
}

compiler/rustc_hir_analysis/src/collect/type_of.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
153153
let args = ty::GenericArgs::identity_for_item(tcx, def_id);
154154
Ty::new_fn_def(tcx, def_id.to_def_id(), args)
155155
}
156-
TraitItemKind::Const(ty, body_id) => body_id
156+
TraitItemKind::Const(ty, body_id, _) => body_id
157157
.and_then(|body_id| {
158158
ty.is_suggestable_infer_ty().then(|| {
159159
infer_placeholder_type(
@@ -178,7 +178,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
178178
let args = ty::GenericArgs::identity_for_item(tcx, def_id);
179179
Ty::new_fn_def(tcx, def_id.to_def_id(), args)
180180
}
181-
ImplItemKind::Const(ty, body_id) => {
181+
ImplItemKind::Const(ty, body_id, _) => {
182182
if ty.is_suggestable_infer_ty() {
183183
infer_placeholder_type(
184184
icx.lowerer(),
@@ -216,7 +216,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
216216
icx.lower_ty(ty)
217217
}
218218
}
219-
ItemKind::Const(ident, ty, _, body_id,_) => {
219+
ItemKind::Const(ident, ty, _, body_id, _) => {
220220
if ty.is_suggestable_infer_ty() {
221221
infer_placeholder_type(
222222
icx.lowerer(),

compiler/rustc_hir_analysis/src/hir_wf_check.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,18 @@ fn diagnostic_hir_wf_check<'tcx>(
127127
WellFormedLoc::Ty(_) => match tcx.hir_node(hir_id) {
128128
hir::Node::ImplItem(item) => match item.kind {
129129
hir::ImplItemKind::Type(ty) => vec![ty],
130-
hir::ImplItemKind::Const(ty, _) => vec![ty],
130+
hir::ImplItemKind::Const(ty, _, _) => vec![ty],
131131
ref item => bug!("Unexpected ImplItem {:?}", item),
132132
},
133133
hir::Node::TraitItem(item) => match item.kind {
134134
hir::TraitItemKind::Type(_, ty) => ty.into_iter().collect(),
135-
hir::TraitItemKind::Const(ty, _) => vec![ty],
135+
hir::TraitItemKind::Const(ty, _, _) => vec![ty],
136136
ref item => bug!("Unexpected TraitItem {:?}", item),
137137
},
138138
hir::Node::Item(item) => match item.kind {
139139
hir::ItemKind::TyAlias(_, ty, _)
140140
| hir::ItemKind::Static(_, ty, _, _)
141-
| hir::ItemKind::Const(_, ty, _, _,_) => vec![ty],
141+
| hir::ItemKind::Const(_, ty, _, _, _) => vec![ty],
142142
hir::ItemKind::Impl(impl_) => match &impl_.of_trait {
143143
Some(t) => t
144144
.path

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ impl<'a> State<'a> {
911911
self.maybe_print_comment(ti.span.lo());
912912
self.print_attrs_as_outer(self.attrs(ti.hir_id()));
913913
match ti.kind {
914-
hir::TraitItemKind::Const(ty, default) => {
914+
hir::TraitItemKind::Const(ty, default, _ct) => {
915915
self.print_associated_const(ti.ident, ti.generics, ty, default);
916916
}
917917
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Required(arg_names)) => {
@@ -940,7 +940,7 @@ impl<'a> State<'a> {
940940
self.print_attrs_as_outer(self.attrs(ii.hir_id()));
941941

942942
match ii.kind {
943-
hir::ImplItemKind::Const(ty, expr) => {
943+
hir::ImplItemKind::Const(ty, expr, _ct) => {
944944
self.print_associated_const(ii.ident, ii.generics, ty, Some(expr));
945945
}
946946
hir::ImplItemKind::Fn(ref sig, body) => {

0 commit comments

Comments
 (0)