Skip to content

Commit

Permalink
Auto merge of #28845 - oli-obk:rfc1229, r=pnkfelix
Browse files Browse the repository at this point in the history
This PR turns statically known erroneous code (e.g. numeric overflow) into a warning and continues normal code-generation to emit the same code that would have been generated without `check_const` detecting that the result can be computed at compile-time.

<del>It's not done yet, as I don't know how to properly emit a lint from trans. I can't seem to extract the real lint level of the item the erroneous expression is in.</del> It's an unconditional warning now.

r? @pnkfelix 

cc @nikomatsakis 

* [RFC 1229 text](/~https://github.com/rust-lang/rfcs/blob/master/text/1229-compile-time-asserts.md)
* RFC PR: rust-lang/rfcs#1229
* tracking issue: #28238
  • Loading branch information
bors committed Oct 18, 2015
2 parents 9d7c5e5 + 09d3adf commit 3f2ad61
Show file tree
Hide file tree
Showing 23 changed files with 376 additions and 194 deletions.
9 changes: 8 additions & 1 deletion src/librustc/lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
use lint::{LintPass, LateLintPass, LintArray};

declare_lint! {
pub CONST_ERR,
Warn,
"constant evaluation detected erroneous expression"
}

declare_lint! {
pub UNUSED_IMPORTS,
Warn,
Expand Down Expand Up @@ -134,7 +140,8 @@ impl LintPass for HardwiredLints {
VARIANT_SIZE_DIFFERENCES,
FAT_PTR_TRANSMUTES,
TRIVIAL_CASTS,
TRIVIAL_NUMERIC_CASTS
TRIVIAL_NUMERIC_CASTS,
CONST_ERR
)
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/middle/check_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ impl<'a, 'tcx> CheckCrateVisitor<'a, 'tcx> {
if mode == Mode::ConstFn {
for arg in &fd.inputs {
match arg.pat.node {
hir::PatIdent(hir::BindByValue(hir::MutImmutable), _, None) => {}
hir::PatWild(_) => {}
hir::PatIdent(hir::BindByValue(hir::MutImmutable), _, None) => {}
_ => {
span_err!(self.tcx.sess, arg.pat.span, E0022,
"arguments of constant functions can only \
Expand Down Expand Up @@ -476,9 +476,9 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> {
self.tcx, ex, ExprTypeChecked) {
Ok(_) => {}
Err(msg) => {
span_err!(self.tcx.sess, msg.span, E0020,
"{} in a constant expression",
msg.description())
self.tcx.sess.add_lint(::lint::builtin::CONST_ERR, ex.id,
msg.span,
msg.description().into_owned())
}
}
}
Expand Down
17 changes: 14 additions & 3 deletions src/librustc_trans/trans/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,19 +275,30 @@ impl<'a, 'tcx> Opt<'a, 'tcx> {
}

fn trans<'blk>(&self, mut bcx: Block<'blk, 'tcx>) -> OptResult<'blk, 'tcx> {
use trans::consts::TrueConst::Yes;
let _icx = push_ctxt("match::trans_opt");
let ccx = bcx.ccx();
match *self {
ConstantValue(ConstantExpr(lit_expr), _) => {
let lit_ty = bcx.tcx().node_id_to_type(lit_expr.id);
let (llval, _) = consts::const_expr(ccx, &*lit_expr, bcx.fcx.param_substs, None);
let expr = consts::const_expr(ccx, &*lit_expr, bcx.fcx.param_substs, None, Yes);
let llval = match expr {
Ok((llval, _)) => llval,
Err(err) => bcx.ccx().sess().span_fatal(lit_expr.span, &err.description()),
};
let lit_datum = immediate_rvalue(llval, lit_ty);
let lit_datum = unpack_datum!(bcx, lit_datum.to_appropriate_datum(bcx));
SingleResult(Result::new(bcx, lit_datum.val))
}
ConstantRange(ConstantExpr(ref l1), ConstantExpr(ref l2), _) => {
let (l1, _) = consts::const_expr(ccx, &**l1, bcx.fcx.param_substs, None);
let (l2, _) = consts::const_expr(ccx, &**l2, bcx.fcx.param_substs, None);
let l1 = match consts::const_expr(ccx, &**l1, bcx.fcx.param_substs, None, Yes) {
Ok((l1, _)) => l1,
Err(err) => bcx.ccx().sess().span_fatal(l1.span, &err.description()),
};
let l2 = match consts::const_expr(ccx, &**l2, bcx.fcx.param_substs, None, Yes) {
Ok((l2, _)) => l2,
Err(err) => bcx.ccx().sess().span_fatal(l2.span, &err.description()),
};
RangeResult(Result::new(bcx, l1), Result::new(bcx, l2))
}
Variant(disr_val, ref repr, _, _) => {
Expand Down
5 changes: 4 additions & 1 deletion src/librustc_trans/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2125,7 +2125,10 @@ pub fn trans_item(ccx: &CrateContext, item: &hir::Item) {
let mut v = TransItemVisitor{ ccx: ccx };
v.visit_expr(&**expr);

let g = consts::trans_static(ccx, m, expr, item.id, &item.attrs);
let g = match consts::trans_static(ccx, m, expr, item.id, &item.attrs) {
Ok(g) => g,
Err(err) => ccx.tcx().sess.span_fatal(expr.span, &err.description()),
};
set_global_section(ccx, g, item);
update_linkage(ccx, g, Some(item.id), OriginalTranslation);
},
Expand Down
Loading

0 comments on commit 3f2ad61

Please sign in to comment.