Skip to content

Commit 70ec3b5

Browse files
skinnyBatmati865
authored andcommitted
Put lazy normalization behind a -Z flag
1 parent a479836 commit 70ec3b5

File tree

5 files changed

+36
-25
lines changed

5 files changed

+36
-25
lines changed

src/librustc_infer/infer/combine.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,11 @@ impl<'infcx, 'tcx> InferCtxt<'infcx, 'tcx> {
164164
(_, ty::ConstKind::Infer(InferConst::Var(vid))) => {
165165
return self.unify_const_variable(!a_is_expected, vid, a);
166166
}
167-
(ty::ConstKind::Unevaluated(..), _) => {
167+
(ty::ConstKind::Unevaluated(..), _) if self.tcx.sess.opts.debugging_opts.lazy_normalization => {
168168
relation.const_equate_obligation(a, b);
169169
return Ok(b);
170170
}
171-
(_, ty::ConstKind::Unevaluated(..)) => {
171+
(_, ty::ConstKind::Unevaluated(..)) if self.tcx.sess.opts.debugging_opts.lazy_normalization => {
172172
relation.const_equate_obligation(a, b);
173173
return Ok(a);
174174
}
@@ -656,7 +656,7 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
656656
}
657657
}
658658
}
659-
ty::ConstKind::Unevaluated(..) => Ok(c),
659+
ty::ConstKind::Unevaluated(..) if self.tcx().sess.opts.debugging_opts.lazy_normalization => Ok(c),
660660
_ => relate::super_relate_consts(self, c, c),
661661
}
662662
}

src/librustc_infer/infer/nll_relate/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ where
997997
}
998998
}
999999
}
1000-
ty::ConstKind::Unevaluated(..) => Ok(a),
1000+
ty::ConstKind::Unevaluated(..) if self.tcx().sess.opts.debugging_opts.lazy_normalization=> Ok(a),
10011001
_ => relate::super_relate_consts(self, a, a),
10021002
}
10031003
}

src/librustc_middle/ty/relate.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -431,18 +431,20 @@ pub fn super_relate_tys<R: TypeRelation<'tcx>>(
431431
let t = relation.relate(&a_t, &b_t)?;
432432
match relation.relate(&sz_a, &sz_b) {
433433
Ok(sz) => Ok(tcx.mk_ty(ty::Array(t, sz))),
434+
// FIXME(lazy-normalization) Implement improved diagnostics for mismatched array
435+
// length?
436+
Err(err) if relation.tcx().sess.opts.debugging_opts.lazy_normalization => Err(err),
434437
Err(err) => {
435-
// // Check whether the lengths are both concrete/known values,
436-
// // but are unequal, for better diagnostics.
437-
// let sz_a = sz_a.try_eval_usize(tcx, relation.param_env());
438-
// let sz_b = sz_b.try_eval_usize(tcx, relation.param_env());
439-
// match (sz_a, sz_b) {
440-
// (Some(sz_a_val), Some(sz_b_val)) => Err(TypeError::FixedArraySize(
441-
// expected_found(relation, &sz_a_val, &sz_b_val),
442-
// )),
443-
// _ => Err(err),
444-
// }
445-
Err(err)
438+
// Check whether the lengths are both concrete/known values,
439+
// but are unequal, for better diagnostics.
440+
let sz_a = sz_a.try_eval_usize(tcx, relation.param_env());
441+
let sz_b = sz_b.try_eval_usize(tcx, relation.param_env());
442+
match (sz_a, sz_b) {
443+
(Some(sz_a_val), Some(sz_b_val)) => Err(TypeError::FixedArraySize(
444+
expected_found(relation, &sz_a_val, &sz_b_val),
445+
)),
446+
_ => Err(err),
447+
}
446448
}
447449
}
448450
}
@@ -568,14 +570,14 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
568570
}
569571

570572
// FIXME(const_generics): this is wrong, as it is a projection
571-
// (
572-
// ty::ConstKind::Unevaluated(a_def_id, a_substs, a_promoted),
573-
// ty::ConstKind::Unevaluated(b_def_id, b_substs, b_promoted),
574-
// ) if a_def_id == b_def_id && a_promoted == b_promoted => {
575-
// let substs =
576-
// relation.relate_with_variance(ty::Variance::Invariant, &a_substs, &b_substs)?;
577-
// Ok(ty::ConstKind::Unevaluated(a_def_id, &substs, a_promoted))
578-
// }
573+
(
574+
ty::ConstKind::Unevaluated(a_def_id, a_substs, a_promoted),
575+
ty::ConstKind::Unevaluated(b_def_id, b_substs, b_promoted),
576+
) if a_def_id == b_def_id && a_promoted == b_promoted => {
577+
let substs =
578+
relation.relate_with_variance(ty::Variance::Invariant, &a_substs, &b_substs)?;
579+
Ok(ty::ConstKind::Unevaluated(a_def_id, &substs, a_promoted))
580+
}
579581
_ => Err(TypeError::ConstMismatch(expected_found(relation, &a, &b))),
580582
};
581583
new_const_val.map(|val| tcx.mk_const(ty::Const { val, ty: a.ty }))

src/librustc_session/options.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,4 +982,6 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
982982
"Link native libraries in the linker invocation."),
983983
src_hash_algorithm: Option<SourceFileHashAlgorithm> = (None, parse_src_file_hash, [TRACKED],
984984
"hash algorithm of source files in debug info (`md5`, or `sha1`)"),
985+
lazy_normalization: bool = (false, parse_bool, [UNTRACKED],
986+
"lazily evaluate constants (experimental)"),
985987
}

src/librustc_typeck/collect.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,15 +1172,22 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::Generics {
11721172

11731173
let node = tcx.hir().get(hir_id);
11741174
let parent_def_id = match node {
1175-
Node::AnonConst(_)
1176-
| Node::ImplItem(_)
1175+
Node::ImplItem(_)
11771176
| Node::TraitItem(_)
11781177
| Node::Variant(_)
11791178
| Node::Ctor(..)
11801179
| Node::Field(_) => {
11811180
let parent_id = tcx.hir().get_parent_item(hir_id);
11821181
Some(tcx.hir().local_def_id(parent_id))
11831182
}
1183+
Node::AnonConst(_) => {
1184+
if tcx.sess.opts.debugging_opts.lazy_normalization {
1185+
let parent_id = tcx.hir().get_parent_item(hir_id);
1186+
Some(tcx.hir().local_def_id(parent_id))
1187+
} else {
1188+
None
1189+
}
1190+
}
11841191
Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure(..), .. }) => {
11851192
Some(tcx.closure_base_def_id(def_id))
11861193
}

0 commit comments

Comments
 (0)