Skip to content

Commit 8f209d5

Browse files
committed
Auto merge of #24423 - tbelaire:include_bytes, r=alexcrichton
This is a little bit tricky, since with include_str!, we know that we are including utf-8 content, so it's safe to store the source as a String in a FileMap. We don't know that for include_bytes!, but I don't think we actually need to track the contents anyways, so I'm passing "". new_filemap does check for the zero length content, and it should be reasonable, howeven I'm not sure if it would be better to pass None instead of Some(Rc::new("")) as the src component of a FileMap. Fixes bug #24348
2 parents 798fa22 + e4b3fac commit 8f209d5

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

Diff for: src/libsyntax/ext/source_util.rs

+5
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ pub fn expand_include_bytes(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
184184
return DummyResult::expr(sp);
185185
}
186186
Ok(..) => {
187+
// Add this input file to the code map to make it available as
188+
// dependency information, but don't enter it's contents
189+
let filename = format!("{}", file.display());
190+
cx.codemap().new_filemap(filename, "".to_string());
191+
187192
base::MacEager::expr(cx.expr_lit(sp, ast::LitBinary(Rc::new(bytes))))
188193
}
189194
}

Diff for: src/test/run-make/include_bytes_deps/Makefile

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-include ../tools.mk
2+
3+
# FIXME: ignore freebsd/windows
4+
# on windows `rustc --dep-info` produces Makefile dependency with
5+
# windows native paths (e.g. `c:\path\to\libfoo.a`)
6+
# but msys make seems to fail to recognize such paths, so test fails.
7+
ifneq ($(shell uname),FreeBSD)
8+
ifndef IS_WINDOWS
9+
all:
10+
$(RUSTC) --emit dep-info main.rs
11+
grep "input.txt" $(TMPDIR)/main.d
12+
grep "input.bin" $(TMPDIR)/main.d
13+
else
14+
all:
15+
16+
endif
17+
18+
else
19+
all:
20+
21+
endif

Diff for: src/test/run-make/include_bytes_deps/input.bin

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world!

Diff for: src/test/run-make/include_bytes_deps/input.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world!

Diff for: src/test/run-make/include_bytes_deps/main.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+
pub fn main() {
12+
const INPUT_TXT: &'static str = include_str!("input.txt");
13+
const INPUT_BIN: &'static [u8] = include_bytes!("input.bin");
14+
15+
println!("{}", INPUT_TXT);
16+
println!("{:?}", INPUT_BIN);
17+
}

0 commit comments

Comments
 (0)