Skip to content

Commit e6f7f4a

Browse files
committed
Document error/lint cases in const eval
1 parent d736197 commit e6f7f4a

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/librustc_mir/const_eval.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,15 +649,19 @@ pub fn const_eval_raw_provider<'a, 'tcx>(
649649
}).map_err(|error| {
650650
let stacktrace = ecx.generate_stacktrace(None);
651651
let err = ConstEvalErr { error, stacktrace, span: ecx.tcx.span };
652+
// errors in statics are always emitted as fatal errors
652653
if tcx.is_static(def_id).is_some() {
653654
let err = err.report_as_error(ecx.tcx, "could not evaluate static initializer");
655+
// check that a static never produces `TooGeneric`
654656
if tcx.sess.err_count() == 0 {
655657
span_bug!(ecx.tcx.span, "static eval failure didn't emit an error: {:#?}", err);
656658
}
657659
err
658660
} else if def_id.is_local() {
659661
// constant defined in this crate, we can figure out a lint level!
660662
match tcx.describe_def(def_id) {
663+
// constants never produce a hard error at the definition site. Anything else is
664+
// a backwards compatibility hazard (and will break old versions of winapi for sure)
661665
Some(Def::Const(_)) | Some(Def::AssociatedConst(_)) => {
662666
let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
663667
err.report_as_lint(
@@ -666,6 +670,8 @@ pub fn const_eval_raw_provider<'a, 'tcx>(
666670
node_id,
667671
)
668672
},
673+
// promoting runtime code is only allowed to error if it references broken constants
674+
// any other kind of error will be reported to the user as a deny-by-default lint
669675
_ => if let Some(p) = cid.promoted {
670676
let span = tcx.optimized_mir(def_id).promoted[p].span;
671677
if let EvalErrorKind::ReferencedConstant = err.error.kind {
@@ -680,6 +686,8 @@ pub fn const_eval_raw_provider<'a, 'tcx>(
680686
tcx.hir.as_local_node_id(def_id).unwrap(),
681687
)
682688
}
689+
// anything else (array lengths, enum initializers, constant patterns) are reported
690+
// as hard errors
683691
} else {
684692
err.report_as_error(
685693
ecx.tcx,
@@ -688,7 +696,7 @@ pub fn const_eval_raw_provider<'a, 'tcx>(
688696
},
689697
}
690698
} else {
691-
// use of constant from other crate
699+
// use of broken constant from other crate
692700
err.report_as_error(ecx.tcx, "could not evaluate constant")
693701
}
694702
})

0 commit comments

Comments
 (0)