Skip to content

Commit 89e1009

Browse files
committed
implement ast->hir and astconv for defaulted const params
1 parent d79ca2d commit 89e1009

File tree

11 files changed

+36
-27
lines changed

11 files changed

+36
-27
lines changed

compiler/rustc_ast_lowering/src/index.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
168168
intravisit::walk_generic_param(self, param);
169169
}
170170

171-
fn visit_const_param_default(&mut self, param: HirId, ct: &'hir AnonConst) {
171+
fn visit_const_param_default(&mut self, param: HirId, ct: &'hir ConstArg<'hir>) {
172172
self.with_parent(param, |this| {
173173
intravisit::walk_const_param_default(this, ct);
174174
})

compiler/rustc_ast_lowering/src/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -2287,7 +2287,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22872287
&ty,
22882288
&ImplTraitContext::Disallowed(ImplTraitPosition::GenericDefault),
22892289
);
2290-
let default = default.as_ref().map(|def| self.lower_anon_const(def));
2290+
let default = default.as_ref().map(|def| {
2291+
self.lower_const_arg(
2292+
def,
2293+
&ImplTraitContext::Disallowed(ImplTraitPosition::GenericDefault),
2294+
)
2295+
});
22912296
(
22922297
hir::ParamName::Plain(self.lower_ident(param.ident)),
22932298
hir::GenericParamKind::Const { ty, default },

compiler/rustc_hir/src/hir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ pub enum GenericParamKind<'hir> {
522522
Const {
523523
ty: &'hir Ty<'hir>,
524524
/// Optional default value for the const generic param
525-
default: Option<AnonConst>,
525+
default: Option<&'hir ConstArg<'hir>>,
526526
},
527527
}
528528

compiler/rustc_hir/src/intravisit.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ pub trait Visitor<'v>: Sized {
350350
fn visit_generic_param(&mut self, p: &'v GenericParam<'v>) {
351351
walk_generic_param(self, p)
352352
}
353-
fn visit_const_param_default(&mut self, _param: HirId, ct: &'v AnonConst) {
353+
fn visit_const_param_default(&mut self, _param: HirId, ct: &'v ConstArg<'v>) {
354354
walk_const_param_default(self, ct)
355355
}
356356
fn visit_generics(&mut self, g: &'v Generics<'v>) {
@@ -877,8 +877,8 @@ pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v Generi
877877
}
878878
}
879879

880-
pub fn walk_const_param_default<'v, V: Visitor<'v>>(visitor: &mut V, ct: &'v AnonConst) {
881-
visitor.visit_anon_const(ct)
880+
pub fn walk_const_param_default<'v, V: Visitor<'v>>(visitor: &mut V, ct: &'v ConstArg<'v>) {
881+
visitor.visit_const_arg(ct)
882882
}
883883

884884
pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics<'v>) {

compiler/rustc_hir_analysis/src/collect.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ pub fn provide(providers: &mut Providers) {
7777
generator_kind,
7878
collect_mod_item_types,
7979
is_type_alias_impl_trait,
80+
const_param_default,
8081
..*providers
8182
};
8283
}
@@ -291,7 +292,9 @@ impl<'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
291292
self.tcx.ensure().type_of(param.def_id);
292293
if let Some(default) = default {
293294
// need to store default and type of default
294-
self.tcx.ensure().type_of(default.def_id);
295+
if let hir::ConstArgKind::AnonConst(_, anon_ct) = default.kind {
296+
self.tcx.ensure().type_of(anon_ct.def_id);
297+
}
295298
self.tcx.ensure().const_param_default(param.def_id);
296299
}
297300
}
@@ -1488,3 +1491,19 @@ fn is_type_alias_impl_trait<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> bool
14881491
_ => bug!("tried getting opaque_ty_origin for non-opaque: {:?}", def_id),
14891492
}
14901493
}
1494+
1495+
fn const_param_default(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<Const<'_>> {
1496+
let item_def_id = tcx.parent(def_id.to_def_id()).expect_local();
1497+
let item_ctxt = ItemCtxt::new(tcx, item_def_id);
1498+
1499+
match tcx.hir().get_by_def_id(def_id) {
1500+
hir::Node::GenericParam(hir::GenericParam {
1501+
kind: hir::GenericParamKind::Const { default: Some(ac), .. },
1502+
..
1503+
}) => ty::EarlyBinder(item_ctxt.astconv().ast_const_to_const(ac, def_id.to_def_id())),
1504+
_ => span_bug!(
1505+
tcx.def_span(def_id),
1506+
"`const_param_default` expected a generic parameter with a constant"
1507+
),
1508+
}
1509+
}

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ fn const_evaluatable_predicates_of(
357357
}
358358
}
359359

360-
fn visit_const_param_default(&mut self, _param: HirId, _ct: &'tcx hir::AnonConst) {
360+
fn visit_const_param_default(&mut self, _param: HirId, _ct: &'tcx hir::ConstArg<'tcx>) {
361361
// Do not look into const param defaults,
362362
// these get checked when they are actually instantiated.
363363
//

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -918,8 +918,8 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
918918
}
919919
GenericParamKind::Const { ty, default } => {
920920
this.visit_ty(ty);
921-
if let Some(default) = default {
922-
this.visit_body(this.tcx.hir().body(default.body));
921+
if let Some(arg) = default {
922+
this.visit_const_arg(arg);
923923
}
924924
}
925925
}

compiler/rustc_hir_analysis/src/collect/type_of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
6363
def_id: param_def_id,
6464
kind: GenericParamKind::Const { default: Some(ct), .. },
6565
..
66-
}) if ct.hir_id == hir_id => {
66+
}) if ct.hir_id() == hir_id => {
6767
return tcx.type_of(param_def_id)
6868
.no_bound_vars()
6969
.expect("const parameter types cannot be generic")

compiler/rustc_hir_pretty/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2168,7 +2168,7 @@ impl<'a> State<'a> {
21682168
if let Some(default) = default {
21692169
self.space();
21702170
self.word_space("=");
2171-
self.print_anon_const(default);
2171+
self.print_const_arg(default);
21722172
}
21732173
}
21742174
}

compiler/rustc_middle/src/ty/consts.rs

-14
Original file line numberDiff line numberDiff line change
@@ -218,17 +218,3 @@ impl<'tcx> Const<'tcx> {
218218
matches!(self.kind(), ty::ConstKind::Infer(_))
219219
}
220220
}
221-
222-
pub fn const_param_default(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<Const<'_>> {
223-
let default_def_id = match tcx.hir().get_by_def_id(def_id) {
224-
hir::Node::GenericParam(hir::GenericParam {
225-
kind: hir::GenericParamKind::Const { default: Some(ac), .. },
226-
..
227-
}) => ac.def_id,
228-
_ => span_bug!(
229-
tcx.def_span(def_id),
230-
"`const_param_default` expected a generic parameter with a constant"
231-
),
232-
};
233-
ty::EarlyBinder(Const::from_anon_const(tcx, default_def_id))
234-
}

compiler/rustc_middle/src/ty/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2590,7 +2590,6 @@ pub fn provide(providers: &mut ty::query::Providers) {
25902590
*providers = ty::query::Providers {
25912591
trait_impls_of: trait_def::trait_impls_of_provider,
25922592
incoherent_impls: trait_def::incoherent_impls_provider,
2593-
const_param_default: consts::const_param_default,
25942593
vtable_allocation: vtable::vtable_allocation_provider,
25952594
..*providers
25962595
};

0 commit comments

Comments
 (0)