Skip to content

Commit ccd3924

Browse files
authored
Merge pull request #21573 from ChayimFriedman2/include-bytes-len
fix: Infer the expected len in `include_bytes!()`, to avoid mismatches
2 parents 1d79e8e + 948d7e8 commit ccd3924

4 files changed

Lines changed: 31 additions & 8 deletions

File tree

crates/hir-expand/src/builtin/fn_macro.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -830,15 +830,18 @@ fn include_bytes_expand(
830830
span: Span,
831831
) -> ExpandResult<tt::TopSubtree> {
832832
// FIXME: actually read the file here if the user asked for macro expansion
833-
let res = tt::TopSubtree::invisible_from_leaves(
833+
let underscore = sym::underscore;
834+
let zero = tt::Literal {
835+
text_and_suffix: sym::_0_u8,
834836
span,
835-
[tt::Leaf::Literal(tt::Literal {
836-
text_and_suffix: Symbol::empty(),
837-
span,
838-
kind: tt::LitKind::ByteStrRaw(1),
839-
suffix_len: 0,
840-
})],
841-
);
837+
kind: tt::LitKind::Integer,
838+
suffix_len: 3,
839+
};
840+
// We don't use a real length since we can't know the file length, so we use an underscore
841+
// to infer it.
842+
let res = quote! {span =>
843+
&[#zero; #underscore]
844+
};
842845
ExpandResult::ok(res)
843846
}
844847

crates/hir-ty/src/tests/simple.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4064,3 +4064,13 @@ fn foo() {
40644064
"#]],
40654065
);
40664066
}
4067+
4068+
#[test]
4069+
fn include_bytes_len_mismatch() {
4070+
check_no_mismatches(
4071+
r#"
4072+
//- minicore: include_bytes
4073+
static S: &[u8; 158] = include_bytes!("/foo/bar/baz.txt");
4074+
"#,
4075+
);
4076+
}

crates/intern/src/symbol/symbols.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ define_symbols! {
110110
win64_dash_unwind = "win64-unwind",
111111
x86_dash_interrupt = "x86-interrupt",
112112
rust_dash_preserve_dash_none = "preserve-none",
113+
_0_u8 = "0_u8",
113114

114115
@PLAIN:
115116
__ra_fixup,

crates/test-utils/src/minicore.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
//! dispatch_from_dyn: unsize, pin
4444
//! hash: sized
4545
//! include:
46+
//! include_bytes:
4647
//! index: sized
4748
//! infallible:
4849
//! int_impl: size_of, transmute
@@ -2061,6 +2062,14 @@ mod macros {
20612062
}
20622063
// endregion:include
20632064

2065+
// region:include_bytes
2066+
#[rustc_builtin_macro]
2067+
#[macro_export]
2068+
macro_rules! include_bytes {
2069+
($file:expr $(,)?) => {{ /* compiler built-in */ }};
2070+
}
2071+
// endregion:include_bytes
2072+
20642073
// region:concat
20652074
#[rustc_builtin_macro]
20662075
#[macro_export]

0 commit comments

Comments
 (0)