Skip to content

rustc: Change byte literals to fixed-size arrays #18480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/librustc/middle/trans/common.rs
Original file line number Diff line number Diff line change
@@ -677,9 +677,8 @@ pub fn C_str_slice(cx: &CrateContext, s: InternedString) -> ValueRef {
}
}

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

let gsym = token::gensym("binary");
@@ -689,9 +688,7 @@ pub fn C_binary_slice(cx: &CrateContext, data: &[u8]) -> ValueRef {
llvm::LLVMSetInitializer(g, lldata);
llvm::LLVMSetGlobalConstant(g, True);
llvm::SetLinkage(g, llvm::InternalLinkage);

let cs = llvm::LLVMConstPointerCast(g, Type::i8p(cx).to_ref());
C_struct(cx, [cs, C_uint(cx, len)], false)
g
}
}

2 changes: 1 addition & 1 deletion src/librustc/middle/trans/consts.rs
Original file line number Diff line number Diff line change
@@ -83,7 +83,7 @@ pub fn const_lit(cx: &CrateContext, e: &ast::Expr, lit: &ast::Lit)
ast::LitBool(b) => C_bool(cx, b),
ast::LitNil => C_nil(cx),
ast::LitStr(ref s, _) => C_str_slice(cx, (*s).clone()),
ast::LitBinary(ref data) => C_binary_slice(cx, data.as_slice()),
ast::LitBinary(ref data) => C_binary_array(cx, data.as_slice()),
}
}

7 changes: 5 additions & 2 deletions src/librustc/middle/typeck/check/mod.rs
Original file line number Diff line number Diff line change
@@ -2690,8 +2690,11 @@ fn check_lit(fcx: &FnCtxt,

match lit.node {
ast::LitStr(..) => ty::mk_str_slice(tcx, ty::ReStatic, ast::MutImmutable),
ast::LitBinary(..) => {
ty::mk_slice(tcx, ty::ReStatic, ty::mt{ ty: ty::mk_u8(), mutbl: ast::MutImmutable })
ast::LitBinary(ref v) => {
ty::mk_rptr(tcx, ty::ReStatic, ty::mt {
ty: ty::mk_vec(tcx, ty::mk_u8(), Some(v.len())),
mutbl: ast::MutImmutable,
})
}
ast::LitByte(_) => ty::mk_u8(),
ast::LitChar(_) => ty::mk_char(),
14 changes: 7 additions & 7 deletions src/libstd/path/posix.rs
Original file line number Diff line number Diff line change
@@ -154,7 +154,7 @@ impl GenericPathUnsafe for Path {
unsafe fn set_filename_unchecked<T: BytesContainer>(&mut self, filename: T) {
let filename = filename.container_as_bytes();
match self.sepidx {
None if b".." == self.repr.as_slice() => {
None if self.repr.as_slice() == b".." => {
let mut v = Vec::with_capacity(3 + filename.len());
v.push_all(dot_dot_static);
v.push(SEP_BYTE);
@@ -214,7 +214,7 @@ impl GenericPath for Path {

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

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

fn pop(&mut self) -> bool {
match self.sepidx {
None if b"." == self.repr.as_slice() => false,
None if self.repr.as_slice() == b"." => false,
None => {
self.repr = vec![b'.'];
self.sepidx = None;
true
}
Some(0) if b"/" == self.repr.as_slice() => false,
Some(0) if self.repr.as_slice() == b"/" => false,
Some(idx) => {
if idx == 0 {
self.repr.truncate(idx+1);
@@ -273,7 +273,7 @@ impl GenericPath for Path {
} else {
let mut ita = self.components();
let mut itb = other.components();
if b"." == self.repr.as_slice() {
if self.repr.as_slice() == b"." {
return match itb.next() {
None => true,
Some(b) => b != b".."
2 changes: 1 addition & 1 deletion src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
@@ -801,7 +801,7 @@ impl LitIntType {
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
pub enum Lit_ {
LitStr(InternedString, StrStyle),
LitBinary(Rc<Vec<u8> >),
LitBinary(Rc<Vec<u8>>),
LitByte(u8),
LitChar(char),
LitInt(u64, LitIntType),
17 changes: 17 additions & 0 deletions src/test/run-pass/issue-18465.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

const FOO: &'static [u8, ..3] = b"foo";
const BAR: &'static [u8] = b"foo";

fn main() {
let foo: &'static [u8, ..3] = b"foo";
let bar: &'static [u8] = b"foo";
}