Skip to content

Commit 7296610

Browse files
danielsntedinski
authored andcommitted
Fix issue where array types were recorded as [x; _] and hence aliasing (rust-lang#15)
1 parent 9b69d7a commit 7296610

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

compiler/rustc_codegen_llvm/src/gotoc/typ.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use rustc_target::abi::{
2222
use std::convert::TryInto;
2323
use std::fmt::Debug;
2424
use tracing::debug;
25+
use ty::layout::HasParamEnv;
2526
/// Map the unit type to an empty struct
2627
///
2728
/// Mapping unit to `void` works for functions with no return type but not for variables with type
@@ -301,13 +302,18 @@ impl<'tcx> GotocCtx<'tcx> {
301302
}
302303
}
303304
ty::Foreign(defid) => self.codegen_foreign(ty, *defid),
304-
ty::Array(et, _) => {
305+
ty::Array(et, len) => {
306+
let array_name = format!(
307+
"[{}; {}]",
308+
self.ty_mangled_name(et),
309+
len.try_eval_usize(self.tcx, self.param_env()).unwrap()
310+
);
305311
// wrap arrays into struct so that one can take advantage of struct copy in C
306312
//
307313
// struct [T; n] {
308314
// T _0[n];
309315
// }
310-
self.ensure_struct(&self.ty_mangled_name(ty), |ctx, name| {
316+
self.ensure_struct(&array_name, |ctx, name| {
311317
if et.is_unit() {
312318
// we do not generate a struct with an array of units
313319
vec![]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
const SIZE1: usize = 1;
4+
const SIZE2: usize = 2;
5+
6+
pub static TABLE1: [(u64, u64); SIZE1] = [(0, 1)];
7+
8+
pub static TABLE2: [(u64, u64); SIZE2] = [(2, 3), (4, 5)];
9+
10+
// This is to ensure that we don't just constant propagate away the assertion
11+
fn test_equal(a: u64, b: u64) -> bool {
12+
a == b
13+
}
14+
15+
fn main() {
16+
let x = TABLE1[0];
17+
assert!(test_equal(x.1, 1));
18+
let y = TABLE2[0];
19+
assert!(test_equal(y.1, 3));
20+
let z = TABLE2[1];
21+
assert!(test_equal(z.1, 5));
22+
}

0 commit comments

Comments
 (0)