Skip to content

Commit f849344

Browse files
committed
rustc: Change byte literals to fixed-size arrays
This commit alters the type of `b"foo"` from `&'static [u8]` to `&'static [u8, ..3]` and is an implementation of RFC 339. This is a breaking change because not all operations are always compatible with fixed-size arrays currently when compared with slices. As seen in the diff, if a fixed-size array is the left hand size of an equality then the operator may not resolve. Breakage may require some shuffling or explicitly converting to a slice via `.as_slice()` or `[]`. [breaking-change] Closes #18465
1 parent 221fc1e commit f849344

File tree

6 files changed

+33
-16
lines changed

6 files changed

+33
-16
lines changed

src/librustc/middle/trans/common.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -677,9 +677,8 @@ pub fn C_str_slice(cx: &CrateContext, s: InternedString) -> ValueRef {
677677
}
678678
}
679679

680-
pub fn C_binary_slice(cx: &CrateContext, data: &[u8]) -> ValueRef {
680+
pub fn C_binary_array(cx: &CrateContext, data: &[u8]) -> ValueRef {
681681
unsafe {
682-
let len = data.len();
683682
let lldata = C_bytes(cx, data);
684683

685684
let gsym = token::gensym("binary");
@@ -689,9 +688,7 @@ pub fn C_binary_slice(cx: &CrateContext, data: &[u8]) -> ValueRef {
689688
llvm::LLVMSetInitializer(g, lldata);
690689
llvm::LLVMSetGlobalConstant(g, True);
691690
llvm::SetLinkage(g, llvm::InternalLinkage);
692-
693-
let cs = llvm::LLVMConstPointerCast(g, Type::i8p(cx).to_ref());
694-
C_struct(cx, [cs, C_uint(cx, len)], false)
691+
g
695692
}
696693
}
697694

src/librustc/middle/trans/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub fn const_lit(cx: &CrateContext, e: &ast::Expr, lit: &ast::Lit)
8383
ast::LitBool(b) => C_bool(cx, b),
8484
ast::LitNil => C_nil(cx),
8585
ast::LitStr(ref s, _) => C_str_slice(cx, (*s).clone()),
86-
ast::LitBinary(ref data) => C_binary_slice(cx, data.as_slice()),
86+
ast::LitBinary(ref data) => C_binary_array(cx, data.as_slice()),
8787
}
8888
}
8989

src/librustc/middle/typeck/check/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -2690,8 +2690,11 @@ fn check_lit(fcx: &FnCtxt,
26902690

26912691
match lit.node {
26922692
ast::LitStr(..) => ty::mk_str_slice(tcx, ty::ReStatic, ast::MutImmutable),
2693-
ast::LitBinary(..) => {
2694-
ty::mk_slice(tcx, ty::ReStatic, ty::mt{ ty: ty::mk_u8(), mutbl: ast::MutImmutable })
2693+
ast::LitBinary(ref v) => {
2694+
ty::mk_rptr(tcx, ty::ReStatic, ty::mt {
2695+
ty: ty::mk_vec(tcx, ty::mk_u8(), Some(v.len())),
2696+
mutbl: ast::MutImmutable,
2697+
})
26952698
}
26962699
ast::LitByte(_) => ty::mk_u8(),
26972700
ast::LitChar(_) => ty::mk_char(),

src/libstd/path/posix.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl GenericPathUnsafe for Path {
154154
unsafe fn set_filename_unchecked<T: BytesContainer>(&mut self, filename: T) {
155155
let filename = filename.container_as_bytes();
156156
match self.sepidx {
157-
None if b".." == self.repr.as_slice() => {
157+
None if self.repr.as_slice() == b".." => {
158158
let mut v = Vec::with_capacity(3 + filename.len());
159159
v.push_all(dot_dot_static);
160160
v.push(SEP_BYTE);
@@ -214,7 +214,7 @@ impl GenericPath for Path {
214214

215215
fn dirname<'a>(&'a self) -> &'a [u8] {
216216
match self.sepidx {
217-
None if b".." == self.repr.as_slice() => self.repr.as_slice(),
217+
None if self.repr.as_slice() == b".." => self.repr.as_slice(),
218218
None => dot_static,
219219
Some(0) => self.repr[..1],
220220
Some(idx) if self.repr[idx+1..] == b".." => self.repr.as_slice(),
@@ -224,8 +224,8 @@ impl GenericPath for Path {
224224

225225
fn filename<'a>(&'a self) -> Option<&'a [u8]> {
226226
match self.sepidx {
227-
None if b"." == self.repr.as_slice() ||
228-
b".." == self.repr.as_slice() => None,
227+
None if self.repr.as_slice() == b"." ||
228+
self.repr.as_slice() == b".." => None,
229229
None => Some(self.repr.as_slice()),
230230
Some(idx) if self.repr[idx+1..] == b".." => None,
231231
Some(0) if self.repr[1..].is_empty() => None,
@@ -235,13 +235,13 @@ impl GenericPath for Path {
235235

236236
fn pop(&mut self) -> bool {
237237
match self.sepidx {
238-
None if b"." == self.repr.as_slice() => false,
238+
None if self.repr.as_slice() == b"." => false,
239239
None => {
240240
self.repr = vec![b'.'];
241241
self.sepidx = None;
242242
true
243243
}
244-
Some(0) if b"/" == self.repr.as_slice() => false,
244+
Some(0) if self.repr.as_slice() == b"/" => false,
245245
Some(idx) => {
246246
if idx == 0 {
247247
self.repr.truncate(idx+1);
@@ -273,7 +273,7 @@ impl GenericPath for Path {
273273
} else {
274274
let mut ita = self.components();
275275
let mut itb = other.components();
276-
if b"." == self.repr.as_slice() {
276+
if self.repr.as_slice() == b"." {
277277
return match itb.next() {
278278
None => true,
279279
Some(b) => b != b".."

src/libsyntax/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ impl LitIntType {
801801
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
802802
pub enum Lit_ {
803803
LitStr(InternedString, StrStyle),
804-
LitBinary(Rc<Vec<u8> >),
804+
LitBinary(Rc<Vec<u8>>),
805805
LitByte(u8),
806806
LitChar(char),
807807
LitInt(u64, LitIntType),

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

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2014 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+
const FOO: &'static [u8, ..3] = b"foo";
12+
const BAR: &'static [u8] = b"foo";
13+
14+
fn main() {
15+
let foo: &'static [u8, ..3] = b"foo";
16+
let bar: &'static [u8] = b"foo";
17+
}

0 commit comments

Comments
 (0)