Skip to content

Commit e76172c

Browse files
committed
Do not promote constants that contain unpromotable code
1 parent 5205ae8 commit e76172c

11 files changed

+99
-106
lines changed

src/librustc_mir/transform/qualify_consts.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -566,14 +566,15 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
566566

567567
ProjectionElem::Field(..) |
568568
ProjectionElem::Index(_) => {
569-
if this.mode == Mode::Fn {
570-
let base_ty = proj.base.ty(this.mir, this.tcx).to_ty(this.tcx);
571-
if let Some(def) = base_ty.ty_adt_def() {
572-
if def.is_union() {
573-
this.not_const();
574-
}
569+
let base_ty = proj.base.ty(this.mir, this.tcx).to_ty(this.tcx);
570+
// accessing union fields is never allowed inside promoteds
571+
if let Some(def) = base_ty.ty_adt_def() {
572+
if def.is_union() {
573+
this.add(Qualif::NOT_PROMOTABLE);
575574
}
576-
} else if this.qualif.intersects(Qualif::STATIC) {
575+
}
576+
// Only functions may refer to statics directly
577+
if this.mode != Mode::Fn && this.qualif.intersects(Qualif::STATIC) {
577578
span_err!(this.tcx.sess, this.span, E0494,
578579
"cannot refer to the interior of another \
579580
static, use a constant instead");
@@ -615,6 +616,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
615616
} = constant.literal {
616617
// Don't peek inside trait associated constants.
617618
if self.tcx.trait_of_item(def_id).is_some() {
619+
self.add(Qualif::NOT_PROMOTABLE);
618620
self.add_type(ty);
619621
} else {
620622
let (bits, _) = self.tcx.at(constant.span).mir_const_qualif(def_id);

src/librustc_passes/rvalue_promotion.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ impl<'a, 'tcx> Visitor<'tcx> for CheckCrateVisitor<'a, 'tcx> {
267267
}
268268

269269
if self.promotable {
270-
self.result.insert(ex.hir_id.local_id);
270+
assert!(self.result.insert(ex.hir_id.local_id));
271271
}
272272
self.promotable &= outer;
273273
}
@@ -361,17 +361,14 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, e: &hir::Expr, node
361361

362362
Def::Const(did) |
363363
Def::AssociatedConst(did) => {
364-
let promotable = if v.tcx.trait_of_item(did).is_some() {
364+
if v.tcx.trait_of_item(did).is_some() {
365365
// Don't peek inside trait associated constants.
366-
false
366+
v.promotable = false;
367367
} else {
368-
v.tcx.at(e.span).const_is_rvalue_promotable_to_static(did)
369-
};
370-
371-
// Just in case the type is more specific than the definition,
372-
// e.g. impl associated const with type parameters, check it.
373-
// Also, trait associated consts are relaxed by this.
374-
v.promotable &= promotable || v.type_has_only_promotable_values(node_ty);
368+
// Just in case the type is more specific than the definition,
369+
// e.g. impl associated const with type parameters, check it.
370+
v.promotable &= v.tcx.at(e.span).const_is_rvalue_promotable_to_static(did);
371+
}
375372
}
376373

377374
_ => {

src/test/ui/const-eval/issue-44578.nll.stderr

-39
This file was deleted.

src/test/ui/const-eval/issue-44578.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
trait Foo {
1414
const AMT: usize;
15+
//~^ ERROR E0080
1516
}
1617

1718
enum Bar<A, B> {
@@ -20,7 +21,7 @@ enum Bar<A, B> {
2021
}
2122

2223
impl<A: Foo, B: Foo> Foo for Bar<A, B> {
23-
const AMT: usize = [A::AMT][(A::AMT > B::AMT) as usize];
24+
const AMT: usize = [A::AMT][(A::AMT > B::AMT) as usize]; //~ ERROR E0080
2425
}
2526

2627
impl Foo for u8 {
@@ -33,6 +34,4 @@ impl Foo for u16 {
3334

3435
fn main() {
3536
println!("{}", <Bar<u16, u8> as Foo>::AMT);
36-
//~^ ERROR erroneous constant used
37-
//~| ERROR E0080
3837
}

src/test/ui/const-eval/issue-44578.stderr

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
error[E0080]: referenced constant
2-
--> $DIR/issue-44578.rs:35:20
1+
error[E0080]: constant evaluation error
2+
--> $DIR/issue-44578.rs:24:24
33
|
4-
LL | const AMT: usize = [A::AMT][(A::AMT > B::AMT) as usize];
5-
| ------------------------------------ index out of bounds: the len is 1 but the index is 1
6-
...
7-
LL | println!("{}", <Bar<u16, u8> as Foo>::AMT);
8-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
4+
LL | const AMT: usize = [A::AMT][(A::AMT > B::AMT) as usize]; //~ ERROR E0080
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ index out of bounds: the len is 1 but the index is 1
96

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

1616
error: aborting due to 2 previous errors
1717

src/test/ui/const-eval/issue-50814-2.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ impl<T: C> Foo<T> for A<T> {
2323
}
2424

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

3029
impl C for () {
+9-16
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
1-
error[E0080]: referenced constant
2-
--> $DIR/issue-50814-2.rs:26:5
1+
error[E0597]: borrowed value does not live long enough
2+
--> $DIR/issue-50814-2.rs:26:6
33
|
4-
LL | const BAR: usize = [5, 6, 7][T::BOO];
5-
| ----------------- index out of bounds: the len is 3 but the index is 42
6-
...
7-
LL | &<A<T> as Foo<T>>::BAR //~ ERROR erroneous constant used
8-
| ^^^^^^^^^^^^^^^^^^^^^^
9-
10-
error[E0080]: erroneous constant used
11-
--> $DIR/issue-50814-2.rs:26:5
4+
LL | &<A<T> as Foo<T>>::BAR //~ ERROR borrowed value does not live long enough
5+
| ^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
6+
LL | }
7+
| - temporary value only lives until here
128
|
13-
LL | &<A<T> as Foo<T>>::BAR //~ ERROR erroneous constant used
14-
| ^---------------------
15-
| |
16-
| referenced constant has errors
9+
= note: borrowed value must be valid for the static lifetime...
1710

18-
error: aborting due to 2 previous errors
11+
error: aborting due to previous error
1912

20-
For more information about this error, try `rustc --explain E0080`.
13+
For more information about this error, try `rustc --explain E0597`.

src/test/ui/const-eval/issue-50814.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ impl<A: Unsigned, B: Unsigned> Unsigned for Sum<A,B> {
2424
}
2525

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

3130
fn main() {
+9-16
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
1-
error[E0080]: referenced constant
2-
--> $DIR/issue-50814.rs:27:5
1+
error[E0597]: borrowed value does not live long enough
2+
--> $DIR/issue-50814.rs:27:6
33
|
4-
LL | const MAX: u8 = A::MAX + B::MAX;
5-
| --------------- attempt to add with overflow
6-
...
7-
LL | &Sum::<U8,U8>::MAX //~ ERROR erroneous constant used
8-
| ^^^^^^^^^^^^^^^^^^
9-
10-
error[E0080]: erroneous constant used
11-
--> $DIR/issue-50814.rs:27:5
4+
LL | &Sum::<U8,U8>::MAX //~ ERROR borrowed value does not live long enough
5+
| ^^^^^^^^^^^^^^^^^ temporary value does not live long enough
6+
LL | }
7+
| - temporary value only lives until here
128
|
13-
LL | &Sum::<U8,U8>::MAX //~ ERROR erroneous constant used
14-
| ^-----------------
15-
| |
16-
| referenced constant has errors
9+
= note: borrowed value must be valid for the static lifetime...
1710

18-
error: aborting due to 2 previous errors
11+
error: aborting due to previous error
1912

20-
For more information about this error, try `rustc --explain E0080`.
13+
For more information about this error, try `rustc --explain E0597`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![allow(const_err)]
12+
13+
union Bad {
14+
usize: usize,
15+
ptr: &'static u32,
16+
}
17+
18+
const FOO: usize = unsafe {
19+
Bad { ptr: &1 }.usize
20+
};
21+
22+
23+
fn main() {
24+
let x: &'static usize = &FOO; //~ ERROR does not live long enough
25+
let y: &'static usize = &(FOO % 42); //~ ERROR does not live long enough
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0597]: borrowed value does not live long enough
2+
--> $DIR/unpromotable_constant.rs:24:30
3+
|
4+
LL | let x: &'static usize = &FOO; //~ ERROR does not live long enough
5+
| ^^^ temporary value does not live long enough
6+
LL | let y: &'static usize = &(FOO % 42); //~ ERROR does not live long enough
7+
LL | }
8+
| - temporary value only lives until here
9+
|
10+
= note: borrowed value must be valid for the static lifetime...
11+
12+
error[E0597]: borrowed value does not live long enough
13+
--> $DIR/unpromotable_constant.rs:25:30
14+
|
15+
LL | let y: &'static usize = &(FOO % 42); //~ ERROR does not live long enough
16+
| ^^^^^^^^^^ temporary value does not live long enough
17+
LL | }
18+
| - temporary value only lives until here
19+
|
20+
= note: borrowed value must be valid for the static lifetime...
21+
22+
error: aborting due to 2 previous errors
23+
24+
For more information about this error, try `rustc --explain E0597`.

0 commit comments

Comments
 (0)