Skip to content

Commit 418daa7

Browse files
committed
Fix 31267, add rpass tests
1 parent 9041b93 commit 418daa7

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

src/librustc_trans/trans/expr.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,20 @@ pub fn trans_into<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
149149
},
150150
}
151151
}
152+
153+
// If we see a const here, that's because it evaluates to a type with zero size. We
154+
// should be able to just discard it, since const expressions are guaranteed not to
155+
// have side effects. This seems to be reached through tuple struct constructors being
156+
// passed zero-size constants.
157+
if let hir::ExprPath(..) = expr.node {
158+
match bcx.def(expr.id) {
159+
Def::Const(_) | Def::AssociatedConst(_) => {
160+
return bcx;
161+
}
162+
_ => {}
163+
}
164+
}
165+
152166
// Even if we don't have a value to emit, and the expression
153167
// doesn't have any side-effects, we still have to translate the
154168
// body of any closures.
@@ -160,7 +174,7 @@ pub fn trans_into<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
160174
match expr.node {
161175
hir::ExprPath(..) => {
162176
match bcx.def(expr.id) {
163-
Def::Const(did) => {
177+
Def::Const(did) | Def::AssociatedConst(did) => {
164178
let empty_substs = bcx.tcx().mk_substs(Substs::trans_empty());
165179
let const_expr = consts::get_const_expr(bcx.ccx(), did, expr,
166180
empty_substs);
@@ -896,7 +910,7 @@ fn trans_def<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
896910
let lval = Lvalue::new("expr::trans_def");
897911
DatumBlock::new(bcx, Datum::new(val, const_ty, LvalueExpr(lval)))
898912
}
899-
Def::Const(_) => {
913+
Def::Const(_) | Def::AssociatedConst(_) => {
900914
bcx.sess().span_bug(ref_expr.span,
901915
"constant expression should not reach expr::trans_def")
902916
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2016 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+
#![feature(associated_consts)]
12+
13+
#[derive(Clone, Copy, Debug)]
14+
struct Bar;
15+
16+
const BAZ: Bar = Bar;
17+
18+
#[derive(Debug)]
19+
struct Foo([Bar; 1]);
20+
21+
struct Biz;
22+
23+
impl Biz {
24+
const BAZ: Foo = Foo([BAZ; 1]);
25+
}
26+
27+
fn main() {
28+
let foo = Biz::BAZ;
29+
println!("{:?}", foo);
30+
}

src/test/run-pass/issue-31267.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2016 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+
// Regression test for issue #31267
12+
13+
#![feature(associated_consts)]
14+
15+
struct Foo;
16+
17+
impl Foo {
18+
const FOO: [i32; 3] = [0; 3];
19+
}
20+
21+
pub fn main() {
22+
let foo = Foo::FOO;
23+
assert_eq!(foo, [0i32, 0, 0]);
24+
}

0 commit comments

Comments
 (0)