Skip to content

Commit

Permalink
Auto merge of #51570 - oli-obk:demotion, r=<try>
Browse files Browse the repository at this point in the history
Do not promote constants that contain unpromotable code

r? @eddyb

cc @nikomatsakis

I'll add more tests, just getting this out for a crater run
  • Loading branch information
bors committed Jun 15, 2018
2 parents 5205ae8 + db8dea2 commit 55b235a
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 29 deletions.
11 changes: 5 additions & 6 deletions src/librustc_mir/transform/qualify_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,12 +566,10 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {

ProjectionElem::Field(..) |
ProjectionElem::Index(_) => {
if this.mode == Mode::Fn {
let base_ty = proj.base.ty(this.mir, this.tcx).to_ty(this.tcx);
if let Some(def) = base_ty.ty_adt_def() {
if def.is_union() {
this.not_const();
}
let base_ty = proj.base.ty(this.mir, this.tcx).to_ty(this.tcx);
if let Some(def) = base_ty.ty_adt_def() {
if def.is_union() {
this.add(Qualif::NOT_PROMOTABLE);
}
} else if this.qualif.intersects(Qualif::STATIC) {
span_err!(this.tcx.sess, this.span, E0494,
Expand Down Expand Up @@ -615,6 +613,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
} = constant.literal {
// Don't peek inside trait associated constants.
if self.tcx.trait_of_item(def_id).is_some() {
self.add(Qualif::NOT_PROMOTABLE);
self.add_type(ty);
} else {
let (bits, _) = self.tcx.at(constant.span).mir_const_qualif(def_id);
Expand Down
20 changes: 11 additions & 9 deletions src/librustc_passes/rvalue_promotion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ impl<'a, 'tcx> Visitor<'tcx> for CheckCrateVisitor<'a, 'tcx> {

self.visit_body(body);

if self.promotable {
let body_hir_id = tcx.hir.node_to_hir_id(body_id.node_id);
assert!(!self.result.insert(body_hir_id.local_id));
}

self.in_fn = outer_in_fn;
self.tables = outer_tables;
self.param_env = outer_param_env;
Expand Down Expand Up @@ -361,17 +366,14 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, e: &hir::Expr, node

Def::Const(did) |
Def::AssociatedConst(did) => {
let promotable = if v.tcx.trait_of_item(did).is_some() {
if v.tcx.trait_of_item(did).is_some() {
// Don't peek inside trait associated constants.
false
v.promotable = false;
} else {
v.tcx.at(e.span).const_is_rvalue_promotable_to_static(did)
};

// Just in case the type is more specific than the definition,
// e.g. impl associated const with type parameters, check it.
// Also, trait associated consts are relaxed by this.
v.promotable &= promotable || v.type_has_only_promotable_values(node_ty);
// Just in case the type is more specific than the definition,
// e.g. impl associated const with type parameters, check it.
v.promotable &= v.tcx.at(e.span).const_is_rvalue_promotable_to_static(did);
}
}

_ => {
Expand Down
5 changes: 2 additions & 3 deletions src/test/ui/const-eval/issue-44578.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ enum Bar<A, B> {
}

impl<A: Foo, B: Foo> Foo for Bar<A, B> {
const AMT: usize = [A::AMT][(A::AMT > B::AMT) as usize];
const AMT: usize = [A::AMT][(A::AMT > B::AMT) as usize]; //~ ERROR E0080
//~^ ERROR E0080
}

impl Foo for u8 {
Expand All @@ -33,6 +34,4 @@ impl Foo for u16 {

fn main() {
println!("{}", <Bar<u16, u8> as Foo>::AMT);
//~^ ERROR erroneous constant used
//~| ERROR E0080
}
25 changes: 14 additions & 11 deletions src/test/ui/const-eval/issue-44578.stderr
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
error[E0080]: referenced constant
--> $DIR/issue-44578.rs:35:20
error[E0080]: constant evaluation error
--> $DIR/issue-44578.rs:23:24
|
LL | const AMT: usize = [A::AMT][(A::AMT > B::AMT) as usize];
| ------------------------------------ index out of bounds: the len is 1 but the index is 1
...
LL | println!("{}", <Bar<u16, u8> as Foo>::AMT);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | const AMT: usize = [A::AMT][(A::AMT > B::AMT) as usize]; //~ ERROR E0080
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ index out of bounds: the len is 1 but the index is 1

error[E0080]: erroneous constant used
--> $DIR/issue-44578.rs:35:20
error[E0080]: constant evaluation error
--> $DIR/issue-44578.rs:23:24
|
LL | println!("{}", <Bar<u16, u8> as Foo>::AMT);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors
LL | const AMT: usize = [A::AMT][(A::AMT > B::AMT) as usize]; //~ ERROR E0080
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ index out of bounds: the len is 1 but the index is 1
|
note: for constant here
--> $DIR/issue-44578.rs:14:5
|
LL | const AMT: usize;
| ^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

Expand Down
26 changes: 26 additions & 0 deletions src/test/ui/const-eval/unpromotable_constant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![allow(const_err)]

union Bad {
usize: usize,
ptr: &'static u32,
}

const FOO: usize = unsafe {
Bad { ptr: &1 }.usize
};


fn main() {
let x: &'static usize = &FOO; //~ ERROR does not live long enough
let y: &'static usize = &(FOO % 42); //~ ERROR does not live long enough
}
24 changes: 24 additions & 0 deletions src/test/ui/const-eval/unpromotable_constant.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
error[E0597]: borrowed value does not live long enough
--> $DIR/unpromotable_constant.rs:24:30
|
LL | let x: &'static usize = &FOO; //~ ERROR does not live long enough
| ^^^ temporary value does not live long enough
LL | let y: &'static usize = &(FOO % 42); //~ ERROR does not live long enough
LL | }
| - temporary value only lives until here
|
= note: borrowed value must be valid for the static lifetime...

error[E0597]: borrowed value does not live long enough
--> $DIR/unpromotable_constant.rs:25:30
|
LL | let y: &'static usize = &(FOO % 42); //~ ERROR does not live long enough
| ^^^^^^^^^^ temporary value does not live long enough
LL | }
| - temporary value only lives until here
|
= note: borrowed value must be valid for the static lifetime...

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0597`.

0 comments on commit 55b235a

Please sign in to comment.