Skip to content

Commit 57493e5

Browse files
committed
Introduce ConstnessArg::Infer and fix some tests
1 parent 4a58caa commit 57493e5

File tree

12 files changed

+78
-10
lines changed

12 files changed

+78
-10
lines changed

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,8 +1199,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
11991199
});
12001200
self.tcx.mk_const_var(const_var_id, self.tcx.type_of(param.def_id)).into()
12011201
}
1202-
// TODO: this should actually figure out a yes/no answer from the context
1203-
GenericParamDefKind::Constness => ty::ConstnessArg::Param.into(),
1202+
GenericParamDefKind::Constness => constness.unwrap_or(ty::ConstnessArg::Infer).into(),
12041203
}
12051204
}
12061205

compiler/rustc_middle/src/ty/fast_reject.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,8 @@ impl DeepRejectCtxt {
417417
TreatParams::AsPlaceholder => false,
418418
TreatParams::AsInfer => true,
419419
},
420+
(ty::ConstnessArg::Infer, _) => true,
421+
(ty::ConstnessArg::Not, ty::ConstnessArg::Required) => true,
420422
_ => obligation_ct == impl_ct,
421423
}
422424
}

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ pub enum ConstnessArg {
315315
Required,
316316
Not,
317317
Param,
318+
Infer,
318319
}
319320

320321
impl fmt::Display for ConstnessArg {
@@ -323,6 +324,7 @@ impl fmt::Display for ConstnessArg {
323324
Self::Required => "(constness: required)",
324325
Self::Not => "(constness: not)",
325326
Self::Param => "(constness: parameterized)",
327+
Self::Infer => "(constness: infer)"
326328
})
327329
}
328330
}

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2524,6 +2524,7 @@ define_print_and_forward_display! {
25242524
ty::ConstnessArg::Not => {},
25252525
ty::ConstnessArg::Required => p!(write("const ")),
25262526
ty::ConstnessArg::Param => p!(write("~const ")),
2527+
ty::ConstnessArg::Infer => p!(write("_const ")), // TODO wonky
25272528
}
25282529
}
25292530
p!(print_def_path(self.0.def_id, self.0.substs));

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,16 @@ impl<'tcx> TraitRef<'tcx> {
815815
ty::TraitRef { def_id: trait_id, substs: tcx.intern_substs(&substs[..defs.params.len()]) }
816816
}
817817

818+
// TODO remove this hack!
819+
pub fn normalize_constness_equate(&self, tcx: TyCtxt<'tcx>, actual: &mut Self) {
820+
if self.constness() == ty::ConstnessArg::Not {
821+
*actual = tcx.mk_substs(actual.iter().filter(|arg| match arg.unpack() {
822+
ty::subst::GenericArgKind::Constness(_) => false,
823+
_ => true,
824+
}));
825+
}
826+
}
827+
818828
pub fn constness(self) -> ty::ConstnessArg {
819829
for arg in self.substs.iter() {
820830
match arg.unpack() {

compiler/rustc_middle/src/ty/subst.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ impl<'tcx> GenericArgKind<'tcx> {
101101
ty::ConstnessArg::Not => 0,
102102
ty::ConstnessArg::Required => 0b100,
103103
ty::ConstnessArg::Param => 0b1000,
104+
ty::ConstnessArg::Infer => 0b10000,
104105
},
105106
),
106107
};
@@ -182,6 +183,7 @@ impl<'tcx> GenericArg<'tcx> {
182183
0 => ty::ConstnessArg::Not,
183184
0b100 => ty::ConstnessArg::Required,
184185
0b1000 => ty::ConstnessArg::Param,
186+
0b10000 => ty::ConstnessArg::Infer,
185187
_ => intrinsics::unreachable(),
186188
}),
187189
_ => intrinsics::unreachable(),
@@ -611,7 +613,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for SubstFolder<'a, 'tcx> {
611613
}
612614

613615
fn fold_constness(&mut self, c: ty::ConstnessArg) -> ty::ConstnessArg {
614-
if let ty::ConstnessArg::Param = c {
616+
if let ty::ConstnessArg::Param | ty::ConstnessArg::Infer = c {
615617
self.substs
616618
.iter()
617619
.find_map(|param| match param.unpack() {

compiler/rustc_trait_selection/src/traits/select/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2157,7 +2157,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
21572157

21582158
let impl_substs = self.infcx.fresh_substs_for_item(obligation.cause.span, impl_def_id);
21592159

2160-
let impl_trait_ref = impl_trait_ref.subst(self.tcx(), impl_substs);
2160+
let mut impl_trait_ref = impl_trait_ref.subst(self.tcx(), impl_substs);
2161+
placeholder_obligation_trait_ref.normalize_constness_equate(self.tcx(), &mut impl_trait_ref);
21612162

21622163
debug!(?impl_trait_ref);
21632164

compiler/rustc_typeck/src/astconv/generics.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,12 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
213213
// If we have already computed substitutions for parents, we can use those directly.
214214
while let Some(&param) = params.peek() {
215215
if let Some(&kind) = parent_substs.get(param.index as usize) {
216-
substs.push(kind);
216+
if let subst::GenericArgKind::Constness(ty::ConstnessArg::Infer) = kind.unpack()
217+
{
218+
substs.push(ctx.inferred_kind(None, param, false, None))
219+
} else {
220+
substs.push(kind);
221+
}
217222
params.next();
218223
} else {
219224
break;

compiler/rustc_typeck/src/astconv/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
574574
.unwrap_or_else(|| match self.astconv.item_def_id() {
575575
// no information available
576576
// TODO: fall back to `Not`?
577-
None => ty::ConstnessArg::Param,
577+
None => if infer_args { ty::ConstnessArg::Infer } else { ty::ConstnessArg::Param },
578578
Some(context) => {
579579
if tcx.generics_of(context).has_constness_param() {
580580
ty::ConstnessArg::Param
@@ -724,8 +724,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
724724
self_ty,
725725
trait_ref.path.segments.last().unwrap(),
726726
true,
727-
match constness {
728-
hir::Constness::Const => ty::ConstnessArg::Param,
727+
match constness { // TODO check this again
728+
hir::Constness::Const => ty::ConstnessArg::Required,
729729
hir::Constness::NotConst => ty::ConstnessArg::Not,
730730
},
731731
)

compiler/rustc_typeck/src/check/method/confirm.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,19 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
397397
_substs: Option<&[subst::GenericArg<'tcx>]>,
398398
param: &ty::GenericParamDef,
399399
_infer_args: bool,
400-
_constness: Option<ty::ConstnessArg>,
400+
constness: Option<ty::ConstnessArg>,
401401
) -> subst::GenericArg<'tcx> {
402-
self.cfcx.var_for_def(self.cfcx.span, param)
402+
if let ty::GenericParamDefKind::Constness = param.kind {
403+
let constness = if let Some(constness) = constness {
404+
constness
405+
} else {
406+
self.cfcx.fcx.constness()
407+
};
408+
debug!("inferred_kind constness={constness:?}");
409+
constness.into()
410+
} else {
411+
self.cfcx.var_for_def(self.cfcx.span, param)
412+
}
403413
}
404414
}
405415
<dyn AstConv<'_>>::create_substs_for_generic_args(

0 commit comments

Comments
 (0)