Skip to content

Commit 09d3adf

Browse files
committedOct 13, 2015
implement RFC 1229
const eval errors outside of true constant enviroments are not reported anymore, but instead forwarded to a lint.
1 parent 81b3b27 commit 09d3adf

23 files changed

+376
-194
lines changed
 

‎src/librustc/lint/builtin.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
1717
use lint::{LintPass, LateLintPass, LintArray};
1818

19+
declare_lint! {
20+
pub CONST_ERR,
21+
Warn,
22+
"constant evaluation detected erroneous expression"
23+
}
24+
1925
declare_lint! {
2026
pub UNUSED_IMPORTS,
2127
Warn,
@@ -134,7 +140,8 @@ impl LintPass for HardwiredLints {
134140
VARIANT_SIZE_DIFFERENCES,
135141
FAT_PTR_TRANSMUTES,
136142
TRIVIAL_CASTS,
137-
TRIVIAL_NUMERIC_CASTS
143+
TRIVIAL_NUMERIC_CASTS,
144+
CONST_ERR
138145
)
139146
}
140147
}

‎src/librustc/middle/check_const.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ impl<'a, 'tcx> CheckCrateVisitor<'a, 'tcx> {
176176
if mode == Mode::ConstFn {
177177
for arg in &fd.inputs {
178178
match arg.pat.node {
179-
hir::PatIdent(hir::BindByValue(hir::MutImmutable), _, None) => {}
180179
hir::PatWild(_) => {}
180+
hir::PatIdent(hir::BindByValue(hir::MutImmutable), _, None) => {}
181181
_ => {
182182
span_err!(self.tcx.sess, arg.pat.span, E0022,
183183
"arguments of constant functions can only \
@@ -476,9 +476,9 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> {
476476
self.tcx, ex, ExprTypeChecked) {
477477
Ok(_) => {}
478478
Err(msg) => {
479-
span_err!(self.tcx.sess, msg.span, E0020,
480-
"{} in a constant expression",
481-
msg.description())
479+
self.tcx.sess.add_lint(::lint::builtin::CONST_ERR, ex.id,
480+
msg.span,
481+
msg.description().into_owned())
482482
}
483483
}
484484
}

‎src/librustc_trans/trans/_match.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -275,19 +275,30 @@ impl<'a, 'tcx> Opt<'a, 'tcx> {
275275
}
276276

277277
fn trans<'blk>(&self, mut bcx: Block<'blk, 'tcx>) -> OptResult<'blk, 'tcx> {
278+
use trans::consts::TrueConst::Yes;
278279
let _icx = push_ctxt("match::trans_opt");
279280
let ccx = bcx.ccx();
280281
match *self {
281282
ConstantValue(ConstantExpr(lit_expr), _) => {
282283
let lit_ty = bcx.tcx().node_id_to_type(lit_expr.id);
283-
let (llval, _) = consts::const_expr(ccx, &*lit_expr, bcx.fcx.param_substs, None);
284+
let expr = consts::const_expr(ccx, &*lit_expr, bcx.fcx.param_substs, None, Yes);
285+
let llval = match expr {
286+
Ok((llval, _)) => llval,
287+
Err(err) => bcx.ccx().sess().span_fatal(lit_expr.span, &err.description()),
288+
};
284289
let lit_datum = immediate_rvalue(llval, lit_ty);
285290
let lit_datum = unpack_datum!(bcx, lit_datum.to_appropriate_datum(bcx));
286291
SingleResult(Result::new(bcx, lit_datum.val))
287292
}
288293
ConstantRange(ConstantExpr(ref l1), ConstantExpr(ref l2), _) => {
289-
let (l1, _) = consts::const_expr(ccx, &**l1, bcx.fcx.param_substs, None);
290-
let (l2, _) = consts::const_expr(ccx, &**l2, bcx.fcx.param_substs, None);
294+
let l1 = match consts::const_expr(ccx, &**l1, bcx.fcx.param_substs, None, Yes) {
295+
Ok((l1, _)) => l1,
296+
Err(err) => bcx.ccx().sess().span_fatal(l1.span, &err.description()),
297+
};
298+
let l2 = match consts::const_expr(ccx, &**l2, bcx.fcx.param_substs, None, Yes) {
299+
Ok((l2, _)) => l2,
300+
Err(err) => bcx.ccx().sess().span_fatal(l2.span, &err.description()),
301+
};
291302
RangeResult(Result::new(bcx, l1), Result::new(bcx, l2))
292303
}
293304
Variant(disr_val, ref repr, _, _) => {

‎src/librustc_trans/trans/base.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -2125,7 +2125,10 @@ pub fn trans_item(ccx: &CrateContext, item: &hir::Item) {
21252125
let mut v = TransItemVisitor{ ccx: ccx };
21262126
v.visit_expr(&**expr);
21272127

2128-
let g = consts::trans_static(ccx, m, expr, item.id, &item.attrs);
2128+
let g = match consts::trans_static(ccx, m, expr, item.id, &item.attrs) {
2129+
Ok(g) => g,
2130+
Err(err) => ccx.tcx().sess.span_fatal(expr.span, &err.description()),
2131+
};
21292132
set_global_section(ccx, g, item);
21302133
update_linkage(ccx, g, Some(item.id), OriginalTranslation);
21312134
},

0 commit comments

Comments
 (0)
Please sign in to comment.