Skip to content

Commit

Permalink
Do not promote constants that contain unpromotable code
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Jun 15, 2018
1 parent 5205ae8 commit e76172c
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 106 deletions.
16 changes: 9 additions & 7 deletions src/librustc_mir/transform/qualify_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,14 +566,15 @@ 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);
// accessing union fields is never allowed inside promoteds
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) {
}
// Only functions may refer to statics directly
if this.mode != Mode::Fn && this.qualif.intersects(Qualif::STATIC) {
span_err!(this.tcx.sess, this.span, E0494,
"cannot refer to the interior of another \
static, use a constant instead");
Expand Down Expand Up @@ -615,6 +616,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
17 changes: 7 additions & 10 deletions src/librustc_passes/rvalue_promotion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ impl<'a, 'tcx> Visitor<'tcx> for CheckCrateVisitor<'a, 'tcx> {
}

if self.promotable {
self.result.insert(ex.hir_id.local_id);
assert!(self.result.insert(ex.hir_id.local_id));
}
self.promotable &= outer;
}
Expand Down Expand Up @@ -361,17 +361,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
39 changes: 0 additions & 39 deletions src/test/ui/const-eval/issue-44578.nll.stderr

This file was deleted.

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 @@ -12,6 +12,7 @@

trait Foo {
const AMT: usize;
//~^ ERROR E0080
}

enum Bar<A, B> {
Expand All @@ -20,7 +21,7 @@ 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
}

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
}
22 changes: 11 additions & 11 deletions src/test/ui/const-eval/issue-44578.stderr
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
error[E0080]: referenced constant
--> $DIR/issue-44578.rs:35:20
error[E0080]: constant evaluation error
--> $DIR/issue-44578.rs:24: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:14:5
|
LL | println!("{}", <Bar<u16, u8> as Foo>::AMT);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors
LL | const AMT: usize;
| ^^^^^^^^^^^^^^^^^
...
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: aborting due to 2 previous errors

Expand Down
3 changes: 1 addition & 2 deletions src/test/ui/const-eval/issue-50814-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ impl<T: C> Foo<T> for A<T> {
}

fn foo<T: C>() -> &'static usize {
&<A<T> as Foo<T>>::BAR //~ ERROR erroneous constant used
//~| ERROR E0080
&<A<T> as Foo<T>>::BAR //~ ERROR borrowed value does not live long enough
}

impl C for () {
Expand Down
25 changes: 9 additions & 16 deletions src/test/ui/const-eval/issue-50814-2.stderr
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
error[E0080]: referenced constant
--> $DIR/issue-50814-2.rs:26:5
error[E0597]: borrowed value does not live long enough
--> $DIR/issue-50814-2.rs:26:6
|
LL | const BAR: usize = [5, 6, 7][T::BOO];
| ----------------- index out of bounds: the len is 3 but the index is 42
...
LL | &<A<T> as Foo<T>>::BAR //~ ERROR erroneous constant used
| ^^^^^^^^^^^^^^^^^^^^^^

error[E0080]: erroneous constant used
--> $DIR/issue-50814-2.rs:26:5
LL | &<A<T> as Foo<T>>::BAR //~ ERROR borrowed value does not live long enough
| ^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
LL | }
| - temporary value only lives until here
|
LL | &<A<T> as Foo<T>>::BAR //~ ERROR erroneous constant used
| ^---------------------
| |
| referenced constant has errors
= note: borrowed value must be valid for the static lifetime...

error: aborting due to 2 previous errors
error: aborting due to previous error

For more information about this error, try `rustc --explain E0080`.
For more information about this error, try `rustc --explain E0597`.
3 changes: 1 addition & 2 deletions src/test/ui/const-eval/issue-50814.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ impl<A: Unsigned, B: Unsigned> Unsigned for Sum<A,B> {
}

fn foo<T>(_: T) -> &'static u8 {
&Sum::<U8,U8>::MAX //~ ERROR erroneous constant used
//~| ERROR E0080
&Sum::<U8,U8>::MAX //~ ERROR borrowed value does not live long enough
}

fn main() {
Expand Down
25 changes: 9 additions & 16 deletions src/test/ui/const-eval/issue-50814.stderr
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
error[E0080]: referenced constant
--> $DIR/issue-50814.rs:27:5
error[E0597]: borrowed value does not live long enough
--> $DIR/issue-50814.rs:27:6
|
LL | const MAX: u8 = A::MAX + B::MAX;
| --------------- attempt to add with overflow
...
LL | &Sum::<U8,U8>::MAX //~ ERROR erroneous constant used
| ^^^^^^^^^^^^^^^^^^

error[E0080]: erroneous constant used
--> $DIR/issue-50814.rs:27:5
LL | &Sum::<U8,U8>::MAX //~ ERROR borrowed value does not live long enough
| ^^^^^^^^^^^^^^^^^ temporary value does not live long enough
LL | }
| - temporary value only lives until here
|
LL | &Sum::<U8,U8>::MAX //~ ERROR erroneous constant used
| ^-----------------
| |
| referenced constant has errors
= note: borrowed value must be valid for the static lifetime...

error: aborting due to 2 previous errors
error: aborting due to previous error

For more information about this error, try `rustc --explain E0080`.
For more information about this error, try `rustc --explain E0597`.
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 e76172c

Please sign in to comment.