Skip to content

Commit 08865d9

Browse files
committed
begin using WithOptParam
1 parent 37a6103 commit 08865d9

File tree

5 files changed

+67
-10
lines changed

5 files changed

+67
-10
lines changed

src/librustc_middle/ty/mod.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,32 @@ pub type PlaceholderType = Placeholder<BoundVar>;
15711571

15721572
pub type PlaceholderConst = Placeholder<BoundVar>;
15731573

1574+
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
1575+
#[derive(PartialEq, Eq, PartialOrd, Ord)]
1576+
#[derive(Hash, HashStable)]
1577+
pub struct WithOptParam<T> {
1578+
pub did: T,
1579+
pub param_did: Option<DefId>,
1580+
}
1581+
1582+
impl<T> WithOptParam<T> {
1583+
pub fn dummy(did: T) -> WithOptParam<T> {
1584+
WithOptParam { did, param_did: None }
1585+
}
1586+
}
1587+
1588+
impl WithOptParam<LocalDefId> {
1589+
pub fn ty_def_id(self) -> DefId {
1590+
if let Some(did) = self.param_did { did } else { self.did.to_def_id() }
1591+
}
1592+
}
1593+
1594+
impl WithOptParam<DefId> {
1595+
pub fn ty_def_id(self) -> DefId {
1596+
self.param_did.unwrap_or(self.did)
1597+
}
1598+
}
1599+
15741600
/// When type checking, we use the `ParamEnv` to track
15751601
/// details about the set of where-clauses that are in scope at this
15761602
/// particular point.

src/librustc_middle/ty/sty.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,21 +2210,28 @@ impl<'tcx> Const<'tcx> {
22102210
/// Literals and const generic parameters are eagerly converted to a constant, everything else
22112211
/// becomes `Unevaluated`.
22122212
pub fn from_anon_const(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &'tcx Self {
2213-
debug!("Const::from_anon_const(id={:?})", def_id);
2213+
Self::const_arg_from_anon_const(tcx, ty::WithOptParam::dummy(def_id))
2214+
}
2215+
2216+
pub fn const_arg_from_anon_const(
2217+
tcx: TyCtxt<'tcx>,
2218+
def: ty::WithOptParam<LocalDefId>,
2219+
) -> &'tcx Self {
2220+
debug!("Const::from_anon_const(def={:?})", def);
22142221

2215-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
2222+
let hir_id = tcx.hir().local_def_id_to_hir_id(def.did);
22162223

22172224
let body_id = match tcx.hir().get(hir_id) {
22182225
hir::Node::AnonConst(ac) => ac.body,
22192226
_ => span_bug!(
2220-
tcx.def_span(def_id.to_def_id()),
2227+
tcx.def_span(def.did.to_def_id()),
22212228
"from_anon_const can only process anonymous constants"
22222229
),
22232230
};
22242231

22252232
let expr = &tcx.hir().body(body_id).value;
22262233

2227-
let ty = tcx.type_of(def_id.to_def_id());
2234+
let ty = tcx.type_of(def.ty_def_id());
22282235

22292236
let lit_input = match expr.kind {
22302237
hir::ExprKind::Lit(ref lit) => Some(LitToConstInput { lit: &lit.node, ty, neg: false }),
@@ -2271,8 +2278,8 @@ impl<'tcx> Const<'tcx> {
22712278
ty::ConstKind::Param(ty::ParamConst::new(index, name))
22722279
}
22732280
_ => ty::ConstKind::Unevaluated(
2274-
def_id.to_def_id(),
2275-
InternalSubsts::identity_for_item(tcx, def_id.to_def_id()),
2281+
def.did.to_def_id(),
2282+
InternalSubsts::identity_for_item(tcx, def.did.to_def_id()),
22762283
None,
22772284
),
22782285
};

src/librustc_typeck/astconv.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -886,8 +886,14 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
886886
}
887887
}
888888
(GenericParamDefKind::Const, GenericArg::Const(ct)) => {
889-
let ct_def_id = tcx.hir().local_def_id(ct.value.hir_id);
890-
ty::Const::from_anon_const(tcx, ct_def_id).into()
889+
ty::Const::const_arg_from_anon_const(
890+
tcx,
891+
ty::WithOptParam {
892+
did: tcx.hir().local_def_id(ct.value.hir_id),
893+
param_did: Some(param.def_id),
894+
},
895+
)
896+
.into()
891897
}
892898
_ => unreachable!(),
893899
},

src/librustc_typeck/check/method/confirm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
325325
}
326326
(GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => self.to_ty(ty).into(),
327327
(GenericParamDefKind::Const, GenericArg::Const(ct)) => {
328-
self.to_const(&ct.value).into()
328+
self.const_arg_to_const(&ct.value, param.def_id).into()
329329
}
330330
_ => unreachable!(),
331331
},

src/librustc_typeck/check/mod.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3542,6 +3542,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
35423542
c
35433543
}
35443544

3545+
pub fn const_arg_to_const(
3546+
&self,
3547+
ast_c: &hir::AnonConst,
3548+
param_def_id: DefId,
3549+
) -> &'tcx ty::Const<'tcx> {
3550+
let const_def = ty::WithOptParam {
3551+
did: self.tcx.hir().local_def_id(ast_c.hir_id),
3552+
param_did: Some(param_def_id),
3553+
};
3554+
let c = ty::Const::const_arg_from_anon_const(self.tcx, const_def);
3555+
self.register_wf_obligation(
3556+
c.into(),
3557+
self.tcx.hir().span(ast_c.hir_id),
3558+
ObligationCauseCode::MiscObligation,
3559+
);
3560+
c
3561+
}
3562+
35453563
// If the type given by the user has free regions, save it for later, since
35463564
// NLL would like to enforce those. Also pass in types that involve
35473565
// projections, since those can resolve to `'static` bounds (modulo #54940,
@@ -5655,7 +5673,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
56555673
self.to_ty(ty).into()
56565674
}
56575675
(GenericParamDefKind::Const, GenericArg::Const(ct)) => {
5658-
self.to_const(&ct.value).into()
5676+
self.const_arg_to_const(&ct.value, param.def_id).into()
56595677
}
56605678
_ => unreachable!(),
56615679
},

0 commit comments

Comments
 (0)