Skip to content

Commit 81f402f

Browse files
committed
Add stub of concat!("", b"") support while still emitting error
1 parent 764ca0d commit 81f402f

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/libsyntax_ext/concat.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ pub fn expand_syntax_ext(
3030
let mut string_pos = vec![];
3131
let mut b_accumulator: Vec<u8> = vec![];
3232
let mut b_pos: Vec<Span> = vec![];
33+
// We don't support mixing things with byte str literals, but do a best effort to fill in a
34+
// reasonable byte str output to avoid further errors down the line.
35+
let mut unified_accumulator: Vec<u8> = vec![];
3336
let mut missing_literal = vec![];
3437
for e in es {
3538
match e.node {
@@ -39,28 +42,34 @@ pub fn expand_syntax_ext(
3942
| ast::LitKind::FloatUnsuffixed(ref s) => {
4043
string_accumulator.push_str(&s.as_str());
4144
string_pos.push(e.span);
45+
unified_accumulator.extend(s.to_string().into_bytes());
4246
}
4347
ast::LitKind::Char(c) => {
4448
string_accumulator.push(c);
4549
string_pos.push(e.span);
50+
unified_accumulator.extend(c.to_string().into_bytes());
4651
}
4752
ast::LitKind::Int(i, ast::LitIntType::Unsigned(_))
4853
| ast::LitKind::Int(i, ast::LitIntType::Signed(_))
4954
| ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) => {
5055
string_accumulator.push_str(&i.to_string());
5156
string_pos.push(e.span);
57+
unified_accumulator.extend(i.to_bytes().iter());
5258
}
5359
ast::LitKind::Bool(b) => {
5460
string_accumulator.push_str(&b.to_string());
5561
string_pos.push(e.span);
62+
unified_accumulator.push(b as u8);
5663
}
5764
ast::LitKind::Byte(byte) => {
5865
b_accumulator.push(byte);
5966
b_pos.push(e.span);
67+
unified_accumulator.push(byte);
6068
}
6169
ast::LitKind::ByteStr(ref b_str) => {
6270
b_accumulator.extend(b_str.iter());
6371
b_pos.push(e.span);
72+
unified_accumulator.extend(b_str.iter());
6473
}
6574
},
6675
_ => {
@@ -73,7 +82,8 @@ pub fn expand_syntax_ext(
7382
err.note("only literals (like `\"foo\"`, `42` and `3.14`) can be passed to `concat!()`");
7483
err.emit();
7584
}
76-
// Do not allow mixing "" and b""
85+
let sp = sp.apply_mark(cx.current_expansion.mark);
86+
// Do not allow mixing "" and b"", but return the joint b"" to avoid further errors
7787
if string_accumulator.len() > 0 && b_accumulator.len() > 0 {
7888
let mut err = cx.struct_span_err(
7989
b_pos.clone(),
@@ -95,9 +105,8 @@ pub fn expand_syntax_ext(
95105
.collect(),
96106
);
97107
err.emit();
98-
}
99-
let sp = sp.apply_mark(cx.current_expansion.mark);
100-
if b_accumulator.len() > 0 {
108+
base::MacEager::expr(cx.expr_byte_str(sp, unified_accumulator))
109+
} else if b_accumulator.len() > 0 {
101110
base::MacEager::expr(cx.expr_byte_str(sp, b_accumulator))
102111
} else {
103112
base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&string_accumulator)))

src/libsyntax_ext/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
1515
html_root_url = "https://doc.rust-lang.org/nightly/")]
1616

17-
#![feature(proc_macro_internals)]
1817
#![feature(decl_macro)]
18+
#![feature(int_to_from_bytes)]
19+
#![feature(proc_macro_internals)]
1920
#![feature(str_escape)]
2021

2122
#![feature(rustc_diagnostic_macros)]

0 commit comments

Comments
 (0)