Skip to content

Commit 55b235a

Browse files
committed
Auto merge of #51570 - oli-obk:demotion, r=<try>
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
2 parents 5205ae8 + db8dea2 commit 55b235a

File tree

6 files changed

+82
-29
lines changed

6 files changed

+82
-29
lines changed

src/librustc_mir/transform/qualify_consts.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -566,12 +566,10 @@ 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+
if let Some(def) = base_ty.ty_adt_def() {
571+
if def.is_union() {
572+
this.add(Qualif::NOT_PROMOTABLE);
575573
}
576574
} else if this.qualif.intersects(Qualif::STATIC) {
577575
span_err!(this.tcx.sess, this.span, E0494,
@@ -615,6 +613,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
615613
} = constant.literal {
616614
// Don't peek inside trait associated constants.
617615
if self.tcx.trait_of_item(def_id).is_some() {
616+
self.add(Qualif::NOT_PROMOTABLE);
618617
self.add_type(ty);
619618
} else {
620619
let (bits, _) = self.tcx.at(constant.span).mir_const_qualif(def_id);

src/librustc_passes/rvalue_promotion.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ impl<'a, 'tcx> Visitor<'tcx> for CheckCrateVisitor<'a, 'tcx> {
208208

209209
self.visit_body(body);
210210

211+
if self.promotable {
212+
let body_hir_id = tcx.hir.node_to_hir_id(body_id.node_id);
213+
assert!(!self.result.insert(body_hir_id.local_id));
214+
}
215+
211216
self.in_fn = outer_in_fn;
212217
self.tables = outer_tables;
213218
self.param_env = outer_param_env;
@@ -361,17 +366,14 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, e: &hir::Expr, node
361366

362367
Def::Const(did) |
363368
Def::AssociatedConst(did) => {
364-
let promotable = if v.tcx.trait_of_item(did).is_some() {
369+
if v.tcx.trait_of_item(did).is_some() {
365370
// Don't peek inside trait associated constants.
366-
false
371+
v.promotable = false;
367372
} 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);
373+
// Just in case the type is more specific than the definition,
374+
// e.g. impl associated const with type parameters, check it.
375+
v.promotable &= v.tcx.at(e.span).const_is_rvalue_promotable_to_static(did);
376+
}
375377
}
376378

377379
_ => {

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ enum Bar<A, B> {
2020
}
2121

2222
impl<A: Foo, B: Foo> Foo for Bar<A, B> {
23-
const AMT: usize = [A::AMT][(A::AMT > B::AMT) as usize];
23+
const AMT: usize = [A::AMT][(A::AMT > B::AMT) as usize]; //~ ERROR E0080
24+
//~^ 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

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
error[E0080]: referenced constant
2-
--> $DIR/issue-44578.rs:35:20
1+
error[E0080]: constant evaluation error
2+
--> $DIR/issue-44578.rs:23: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:23:24
129
|
13-
LL | println!("{}", <Bar<u16, u8> as Foo>::AMT);
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors
10+
LL | const AMT: usize = [A::AMT][(A::AMT > B::AMT) as usize]; //~ ERROR E0080
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ index out of bounds: the len is 1 but the index is 1
12+
|
13+
note: for constant here
14+
--> $DIR/issue-44578.rs:14:5
15+
|
16+
LL | const AMT: usize;
17+
| ^^^^^^^^^^^^^^^^^
1518

1619
error: aborting due to 2 previous errors
1720

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)